[autotest] Update login_OwnershipApi to use protobufs

BUG=13652
TEST=run this test

Change-Id: Ib87848224dfd97d10e0a5bd52aaaef8da701d409

R=wad@chromium.org

Review URL: http://codereview.chromium.org/6759063
diff --git a/client/cros/constants.py b/client/cros/constants.py
index da07c93..ecdba44 100644
--- a/client/cros/constants.py
+++ b/client/cros/constants.py
@@ -57,7 +57,8 @@
 
 SESSION_MANAGER = 'session_manager'
 SESSION_MANAGER_LOG = '/var/log/session_manager'
-SIGNED_PREFERENCES_FILE = WHITELIST_DIR+'/preferences'
+SIGNED_PREFERENCES_FILE = WHITELIST_DIR + '/preferences'
+SIGNED_POLICY_FILE = WHITELIST_DIR + '/policy'
 SPECIAL_CASE_DOMAIN = 'gmail.com'
 
 TOKEN_AUTH_URL = '/accounts/TokenAuth'
diff --git a/client/cros/ownership.py b/client/cros/ownership.py
index 0acd767..462c247 100644
--- a/client/cros/ownership.py
+++ b/client/cros/ownership.py
@@ -65,8 +65,8 @@
 
     The caller is responsible for cleaning up these files.
     """
-    keyfile = scoped_tempfile.tempdir.name + 'private.key'
-    certfile = scoped_tempfile.tempdir.name + 'cert.pem'
+    keyfile = scoped_tempfile.tempdir.name + '/private.key'
+    certfile = scoped_tempfile.tempdir.name + '/cert.pem'
     cmd = '%s -x509 -subj %s -newkey rsa:2048 -nodes -keyout %s -out %s' % (
         OPENSSLREQ, '/CN=me', keyfile, certfile)
     system_output_on_fail(cmd)
@@ -164,4 +164,3 @@
     if not sig_data:
         raise error.TestFail('Empty signature!')
     return sig_data
-
diff --git a/client/site_tests/login_OwnershipApi/login_OwnershipApi.py b/client/site_tests/login_OwnershipApi/login_OwnershipApi.py
index feda351..044fb90 100644
--- a/client/site_tests/login_OwnershipApi/login_OwnershipApi.py
+++ b/client/site_tests/login_OwnershipApi/login_OwnershipApi.py
@@ -7,9 +7,10 @@
 import gobject
 import logging
 import os
+import sys
 import tempfile
 
-from autotest_lib.client.bin import test
+from autotest_lib.client.bin import test, utils
 from autotest_lib.client.common_lib import autotemp, error
 from autotest_lib.client.cros import constants, cros_ui, cryptohome, login
 from autotest_lib.client.cros import ownership
@@ -20,16 +21,25 @@
 
     _testuser = 'cryptohometest@chromium.org'
     _testpass = 'testme'
-    _testpolicydata = 'hooberbloob'
+    _poldata = 'hooberbloob'
 
     _tempdir = None
 
-    def initialize(self):
+    def setup(self):
+        os.chdir(self.srcdir)
+        utils.make('OUT_DIR=.')
+
+
+    def __unlink(self, filename):
         try:
-            os.unlink(constants.OWNER_KEY_FILE)
-            os.unlink(constants.SIGNED_PREFERENCES_FILE)
+            os.unlink(filename)
         except (IOError, OSError) as error:
             logging.info(error)
+
+    def initialize(self):
+        self.__unlink(constants.OWNER_KEY_FILE)
+        self.__unlink(constants.SIGNED_PREFERENCES_FILE)
+        self.__unlink(constants.SIGNED_POLICY_FILE)
         login.refresh_login_screen()
         cryptohome.remove_vault(self._testuser)
         cryptohome.mount_vault(self._testuser, self._testpass, create=True)
@@ -63,6 +73,9 @@
     def run_once(self):
         keyfile = ownership.generate_and_register_owner_keypair(self._testuser,
                                                                 self._testpass)
+        # Pull in protobuf definitions.
+        sys.path.append(self.srcdir)
+        from device_management_backend_pb2 import PolicyFetchResponse
 
         # open DBus connection to session_manager
         bus = dbus.SystemBus()
@@ -70,18 +83,21 @@
                                '/org/chromium/SessionManager')
         sm = dbus.Interface(proxy, 'org.chromium.SessionManagerInterface')
 
-        sig = ownership.sign(keyfile, self._testuser)
-        sm.Whitelist(self._testuser, dbus.ByteArray(sig))
-        wl_sig = sm.CheckWhitelist(self._testuser, byte_arrays=True)
-        if sig != wl_sig:
-            raise error.TestFail("CheckWhitelist signature mismatch")
+        policy_proto = PolicyFetchResponse()
+        policy_proto.policy_data = self._poldata
+        policy_proto.policy_data_signature = ownership.sign(keyfile,
+                                                            self._poldata)
+        sm.StorePolicy(dbus.ByteArray(policy_proto.SerializeToString()),
+                       byte_arrays=True,
+                       reply_handler=self.__log_and_stop,
+                       error_handler=self.__log_err_and_stop)
 
-        sm.Unwhitelist(self._testuser, dbus.ByteArray(sig))
-        try:
-            sm.CheckWhitelist(self._testuser)
-            raise error.TestFail("Should not have found user in whitelist!")
-        except dbus.DBusException as e:
-            logging.debug(e)
+        self._loop = gobject.MainLoop()
+        self._loop.run()
+
+        retrieved_policy = sm.RetrievePolicy(byte_arrays=True)
+        if retrieved_policy != policy_proto.SerializeToString():
+            raise error.TestFail('Policy should not be %s' % retrieved_policy)
 
 
     def cleanup(self):
diff --git a/client/site_tests/login_OwnershipApi/src/Makefile b/client/site_tests/login_OwnershipApi/src/Makefile
new file mode 100644
index 0000000..c732915
--- /dev/null
+++ b/client/site_tests/login_OwnershipApi/src/Makefile
@@ -0,0 +1,20 @@
+# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+ifndef SYSROOT
+  $(error Define SYSROOT)
+endif
+
+OUT_DIR ?= .
+PROTO_PATH = $(SYSROOT)/usr/include/proto
+PROTO_DEFS = $(PROTO_PATH)/device_management_backend.proto
+PROTO_BINDINGS = $(OUT_DIR)/device_management_backend_pb2.py
+
+all: $(PROTO_BINDINGS)
+
+$(PROTO_BINDINGS): $(PROTO_DEFS)
+	protoc --proto_path=$(PROTO_PATH) --python_out=$(OUT_DIR) $(PROTO_DEFS)
+
+clean:
+	rm -f $(PROTO_BINDINGS)