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 gobject, os
6from dbus.mainloop.glib import DBusGMainLoop
7
8from autotest_lib.client.bin import test, utils
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.common_lib.cros import policy, session_manager
11from autotest_lib.client.cros import constants, cros_ui, cryptohome, ownership
12
13
14class login_OwnershipRetaken(test.test):
15    """"Ensure that ownership is re-taken upon loss of owner's cryptohome."""
16    version = 1
17
18    _tempdir = None
19    _got_new_key = False
20    _got_new_policy = False
21
22    def setup(self):
23        os.chdir(self.srcdir)
24        utils.make('OUT_DIR=.')
25
26
27    def initialize(self):
28        super(login_OwnershipRetaken, self).initialize()
29        # Start clean, wrt ownership and the desired user.
30        ownership.restart_ui_to_clear_ownership_files()
31
32        bus_loop = DBusGMainLoop(set_as_default=True)
33        self._cryptohome_proxy = cryptohome.CryptohomeProxy(bus_loop)
34        self._cryptohome_proxy.remove(ownership.TESTUSER)
35
36        self._sm = session_manager.connect(bus_loop)
37
38
39    def run_once(self):
40        pkey = ownership.known_privkey()
41        pubkey = ownership.known_pubkey()
42
43        # Pre-configure some owner settings, including initial key.
44        poldata = policy.build_policy_data(self.srcdir,
45                                           owner=ownership.TESTUSER,
46                                           guests=False,
47                                           new_users=True,
48                                           roaming=True,
49                                           whitelist=(ownership.TESTUSER,
50                                                      'a@b.c'))
51        policy_string = policy.generate_policy(self.srcdir,
52                                               pkey,
53                                               pubkey,
54                                               poldata)
55        policy.push_policy_and_verify(policy_string, self._sm)
56
57        # grab key, ensure that it's the same as the known key.
58        if (utils.read_file(constants.OWNER_KEY_FILE) != pubkey):
59            raise error.TestFail('Owner key should not have changed!')
60
61        # Start a new session, which will trigger the re-taking of ownership.
62        listener = session_manager.OwnershipSignalListener(gobject.MainLoop())
63        listener.listen_for_new_key_and_policy()
64        self._cryptohome_proxy.mount(ownership.TESTUSER,
65                                     ownership.TESTPASS,
66                                     create=True)
67        self._sm.StartSession(ownership.TESTUSER, '')
68
69        listener.wait_for_signals(desc='Re-taking of ownership complete.')
70
71        # grab key, ensure that it's different than known key
72        if (utils.read_file(constants.OWNER_KEY_FILE) == pubkey):
73            raise error.TestFail('Owner key should have changed!')
74
75        # RetrievePolicy, check sig against new key, check properties
76        retrieved_policy = self._sm.RetrievePolicy(byte_arrays=True)
77        if retrieved_policy is None:
78            raise error.TestError('Policy not found')
79        policy.compare_policy_response(self.srcdir,
80                                       retrieved_policy,
81                                       owner=ownership.TESTUSER,
82                                       guests=False,
83                                       new_users=True,
84                                       roaming=True,
85                                       whitelist=(ownership.TESTUSER, 'a@b.c'))
86
87
88    def cleanup(self):
89        if self._tempdir: self._tempdir.clean()
90        cros_ui.restart()
91        self._cryptohome_proxy.remove(ownership.TESTUSER)
92        super(login_OwnershipRetaken, self).cleanup()
93