1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import gobject, os 6from dbus.mainloop.glib import DBusGMainLoop 7 8from autotest_lib.client.bin import test 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.common_lib.cros import chrome 11from autotest_lib.client.common_lib.cros import policy 12from autotest_lib.client.common_lib.cros import session_manager 13from autotest_lib.client.cros import constants, ownership 14 15 16class login_OwnershipTaken(test.test): 17 """Sign in and ensure that ownership of the device is taken.""" 18 version = 1 19 20 def initialize(self): 21 super(login_OwnershipTaken, self).initialize() 22 policy.install_protobufs(self.autodir, self.job) 23 ownership.restart_ui_to_clear_ownership_files() 24 if (os.access(constants.OWNER_KEY_FILE, os.F_OK) or 25 os.access(constants.SIGNED_POLICY_FILE, os.F_OK)): 26 raise error.TestError('Ownership already taken!') 27 28 def _validate_policy(self, retrieved_policy, username): 29 import chrome_device_policy_pb2 30 import device_management_backend_pb2 31 32 response_proto = device_management_backend_pb2.PolicyFetchResponse() 33 response_proto.ParseFromString(retrieved_policy) 34 ownership.assert_has_policy_data(response_proto) 35 36 poldata = device_management_backend_pb2.PolicyData() 37 poldata.ParseFromString(response_proto.policy_data) 38 ownership.assert_has_device_settings(poldata) 39 ownership.assert_username(poldata, username) 40 41 polval = chrome_device_policy_pb2.ChromeDeviceSettingsProto() 42 polval.ParseFromString(poldata.policy_value) 43 ownership.assert_new_users(polval, True) 44 ownership.assert_users_on_whitelist(polval, (username,)) 45 46 def run_once(self): 47 bus_loop = DBusGMainLoop(set_as_default=True) 48 listener = session_manager.OwnershipSignalListener(gobject.MainLoop()) 49 listener.listen_for_new_key_and_policy() 50 with chrome.Chrome() as cr: 51 listener.wait_for_signals(desc='Owner settings written to disk.') 52 53 sm = session_manager.connect(bus_loop) 54 retrieved_policy = sm.RetrievePolicyEx( 55 session_manager.make_device_policy_descriptor(), 56 byte_arrays=True) 57 if retrieved_policy is None: 58 raise error.TestFail('Policy not found.') 59 self._validate_policy(retrieved_policy, cr.username) 60