1# Copyright (c) 2011 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 logging, os, tempfile 6from dbus.mainloop.glib import DBusGMainLoop 7 8from autotest_lib.client.bin import test, utils 9from autotest_lib.client.common_lib import autotemp, error 10from autotest_lib.client.common_lib.cros import policy, session_manager 11from autotest_lib.client.cros import cros_ui, cryptohome, ownership 12 13 14class login_OwnershipApi(test.test): 15 """Tests to ensure that the Ownership API works for a local device owner. 16 """ 17 version = 1 18 19 _tempdir = None 20 21 def setup(self): 22 os.chdir(self.srcdir) 23 utils.make('OUT_DIR=.') 24 25 26 def initialize(self): 27 super(login_OwnershipApi, self).initialize() 28 self._bus_loop = DBusGMainLoop(set_as_default=True) 29 self._cryptohome_proxy = cryptohome.CryptohomeProxy(self._bus_loop) 30 31 # Clear existing ownership and inject known keys. 32 cros_ui.stop() 33 ownership.clear_ownership_files_no_restart() 34 35 # Make device already owned by ownership.TESTUSER. 36 self._cryptohome_proxy.mount(ownership.TESTUSER, 37 ownership.TESTPASS, 38 create=True) 39 ownership.use_known_ownerkeys(ownership.TESTUSER) 40 41 self._tempdir = autotemp.tempdir(unique_id=self.__class__.__name__) 42 cros_ui.start() 43 44 45 def __generate_temp_filename(self, dir): 46 """Generate a guaranteed-unique filename in dir.""" 47 just_for_name = tempfile.NamedTemporaryFile(dir=dir, delete=True) 48 basename = just_for_name.name 49 just_for_name.close() # deletes file. 50 return basename 51 52 53 def run_once(self): 54 pkey = ownership.known_privkey() 55 pubkey = ownership.known_pubkey() 56 sm = session_manager.connect(self._bus_loop) 57 sm.StartSession(ownership.TESTUSER, '') 58 59 poldata = policy.build_policy_data(self.srcdir, 60 owner=ownership.TESTUSER, 61 guests=False, 62 new_users=True, 63 roaming=True, 64 whitelist=(ownership.TESTUSER, 65 'a@b.c')) 66 67 policy_string = policy.generate_policy(self.srcdir, 68 pkey, 69 pubkey, 70 poldata) 71 policy.push_policy_and_verify(policy_string, sm) 72 retrieved_policy = policy.get_policy(sm) 73 if retrieved_policy is None: raise error.TestFail('Policy not found') 74 policy.compare_policy_response(self.srcdir, 75 retrieved_policy, 76 owner=ownership.TESTUSER, 77 guests=False, 78 new_users=True, 79 roaming=True, 80 whitelist=(ownership.TESTUSER, 'a@b.c')) 81 try: 82 # Sanity check against an incorrect policy 83 policy.compare_policy_response(self.srcdir, 84 retrieved_policy, 85 owner=ownership.TESTUSER, 86 guests=True, 87 whitelist=(ownership.TESTUSER, 88 'a@b.c')) 89 except ownership.OwnershipError: 90 pass 91 else: 92 raise error.TestFail('Did not detect bad policy') 93 94 try: 95 sm.StopSession('') 96 except error.TestError as e: 97 logging.error(str(e)) 98 raise error.TestFail('Could not stop session for owner') 99 100 101 def cleanup(self): 102 if self._tempdir: self._tempdir.clean() 103 # Best effort to bounce the UI, which may be up or down. 104 cros_ui.stop(allow_fail=True) 105 self._cryptohome_proxy.remove(ownership.TESTUSER) 106 cros_ui.start(allow_fail=True, wait_for_login_prompt=False) 107 super(login_OwnershipApi, self).cleanup() 108