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, 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 cros_ui, cryptohome, ownership
12
13
14class login_MultipleSessions(test.test):
15    """Ensure that the session_manager can handle multiple calls to StartSession
16       correctly.
17    """
18    version = 1
19
20    def setup(self):
21        os.chdir(self.srcdir)
22        utils.make('OUT_DIR=.')
23
24
25    def initialize(self):
26        super(login_MultipleSessions, self).initialize()
27        # Ensure a clean beginning.
28        ownership.restart_ui_to_clear_ownership_files()
29
30        self._bus_loop = DBusGMainLoop(set_as_default=True)
31        self._session_manager = session_manager.connect(self._bus_loop)
32        self._listener = session_manager.OwnershipSignalListener(
33                gobject.MainLoop())
34        self._listener.listen_for_new_key_and_policy()
35
36        self._cryptohome_proxy = cryptohome.CryptohomeProxy(self._bus_loop)
37
38
39    def run_once(self):
40        expected_owner = 'first_user@nowhere.com'
41        other_user = 'second_user@nowhere.com'
42        self.__start_session_for(expected_owner)
43        self.__start_session_for(other_user)
44        self._listener.wait_for_signals(desc='Initial policy push complete.')
45
46        # Ensure that the first user got to be the owner.
47        retrieved_policy = policy.get_policy(self._session_manager)
48        if retrieved_policy is None: raise error.TestFail('Policy not found')
49        policy.compare_policy_response(self.srcdir, retrieved_policy,
50                                       owner=expected_owner)
51        # bounce the session manager and wait for it to come back up before
52        # reconnecting.
53        cros_ui.restart()
54        self._session_manager = session_manager.connect(self._bus_loop)
55
56        # Destroy the owner's cryptohome and start sessions again in a
57        # different order
58        self.__start_session_for(other_user)
59        self.__start_session_for(expected_owner)
60
61        self._listener.wait_for_signals(desc='Re-taking of ownership complete.')
62
63        # Ensure that the first user still gets to be the owner.
64        retrieved_policy = policy.get_policy(self._session_manager)
65        if retrieved_policy is None: raise error.TestFail('Policy not found')
66        policy.compare_policy_response(self.srcdir, retrieved_policy,
67                                       owner=expected_owner)
68
69
70    def __start_session_for(self, user):
71        """Call StartSession() for user, ensure he has clean on-device state
72
73        Make a fresh cryptohome for user, and then start a session for him
74        with the session manager.
75
76        @param user: the user to start a session for.
77
78        @raises error.TestFail: if the session cannot be started.
79        """
80        self._cryptohome_proxy.ensure_clean_cryptohome_for(user)
81        if not self._session_manager.StartSession(user, ''):
82            raise error.TestFail('Could not start session for ' + user)
83
84
85    def cleanup(self):
86        # Bounce UI, without waiting for the browser to come back. Best effort.
87        cros_ui.stop(allow_fail=True)
88        cros_ui.start(allow_fail=True, wait_for_login_prompt=False)
89