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
8
9from autotest_lib.client.bin import test, utils
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.common_lib.cros import policy, session_manager
12from autotest_lib.client.cros import constants, cros_ui, cryptohome, ownership
13
14
15class login_GuestAndActualSession(test.test):
16    """Ensure that the session_manager correctly handles ownership when a guest
17       signs in before a real user.
18    """
19    version = 1
20
21    def setup(self):
22        os.chdir(self.srcdir)
23        utils.make('OUT_DIR=.')
24
25
26    def initialize(self):
27        super(login_GuestAndActualSession, self).initialize()
28        # Ensure a clean beginning.
29        ownership.restart_ui_to_clear_ownership_files()
30
31        bus_loop = DBusGMainLoop(set_as_default=True)
32        self._session_manager = session_manager.connect(bus_loop)
33        self._listener = session_manager.OwnershipSignalListener(
34                gobject.MainLoop())
35        self._listener.listen_for_new_key_and_policy()
36
37        self._cryptohome_proxy = cryptohome.CryptohomeProxy(bus_loop)
38
39
40    def run_once(self):
41        owner = 'first_user@nowhere.com'
42
43        # TODO(cmasone): enable CryptohomeProxy to do a guest mount, then use.
44        cryptohome.mount_guest()
45        if not self._session_manager.StartSession(constants.GUEST_USER, ''):
46            raise error.TestFail('Could not start session for guest')
47
48        self._cryptohome_proxy.ensure_clean_cryptohome_for(owner)
49        if not self._session_manager.StartSession(owner, ''):
50            raise error.TestFail('Could not start session for ' + owner)
51
52        self._listener.wait_for_signals(desc='Device ownership complete.')
53
54        # Ensure that the first real user got to be the owner.
55        retrieved_policy = policy.get_policy(self._session_manager)
56        if retrieved_policy is None: raise error.TestFail('Policy not found')
57        policy.compare_policy_response(self.srcdir, retrieved_policy,
58                                       owner=owner)
59
60
61    def cleanup(self):
62        self._session_manager.StopSession('')
63        cros_ui.start(allow_fail=True, wait_for_login_prompt=False)
64