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, hashlib, logging, 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 chrome, session_manager
11from autotest_lib.client.cros import constants, cryptohome, ownership
12
13
14class login_OwnershipNotRetaken(test.test):
15    """Subsequent logins after the owner must not clobber the owner's key."""
16    version = 2
17
18    _TEST_USER = 'example@chromium.org'
19    _TEST_PASS = 'testme'
20    _TEST_GAIAID = '7583'
21
22
23    def initialize(self):
24        super(login_OwnershipNotRetaken, self).initialize()
25        # Start clean, wrt ownership and the desired user.
26        ownership.restart_ui_to_clear_ownership_files()
27
28        bus_loop = DBusGMainLoop(set_as_default=True)
29        self._cryptohome_proxy = cryptohome.CryptohomeProxy(bus_loop,
30                                                            self.autodir,
31                                                            self.job)
32
33
34    def run_once(self):
35        # TODO(apronin): crbug.com/618392. This test flakes on these boards.
36        boards_to_skip = ['tricky', 'peach_pit', 'falco']
37        board = utils.get_current_board()
38        if board in boards_to_skip:
39            logging.info("Skipping test run on %s.", board)
40            return
41
42        listener = session_manager.OwnershipSignalListener(gobject.MainLoop())
43        listener.listen_for_new_key_and_policy()
44        # Sign in. Sign out happens automatically when cr goes out of scope.
45        with chrome.Chrome(clear_enterprise_policy=False) as cr:
46            listener.wait_for_signals(desc='Owner settings written to disk.')
47
48        key = open(constants.OWNER_KEY_FILE, 'rb')
49        hash = hashlib.md5(key.read())
50        key.close()
51        mtime = os.stat(constants.OWNER_KEY_FILE).st_mtime
52
53        # Sign in/sign out as a second user.
54        with chrome.Chrome(clear_enterprise_policy=False,
55                           username=self._TEST_USER,
56                           password=self._TEST_PASS,
57                           gaia_id=self._TEST_GAIAID) as cr:
58            pass
59
60        # Checking mtime to see if key file was touched during second sign in.
61        if os.stat(constants.OWNER_KEY_FILE).st_mtime > mtime:
62            raise error.TestFail("Owner key was touched on second login!")
63
64        # Sanity check.
65        key2 = open(constants.OWNER_KEY_FILE, 'rb')
66        hash2 = hashlib.md5(key2.read())
67        key2.close()
68        if hash.hexdigest() != hash2.hexdigest():
69            raise error.TestFail("Owner key was touched on second login!")
70
71
72    def cleanup(self):
73        self._cryptohome_proxy.remove(self._TEST_USER)
74        super(login_OwnershipNotRetaken, self).cleanup()
75