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 5from dbus.mainloop.glib import DBusGMainLoop 6 7from autotest_lib.client.bin import test 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.common_lib.cros import session_manager 10from autotest_lib.client.cros import cros_ui, cryptohome 11 12 13class login_RetrieveActiveSessions(test.test): 14 """Ensure that the session_manager correctly tracks active sessions. 15 """ 16 version = 1 17 18 19 def initialize(self): 20 super(login_RetrieveActiveSessions, self).initialize() 21 cros_ui.restart() 22 23 24 def run_once(self): 25 bus_loop = DBusGMainLoop(set_as_default=True) 26 sm = session_manager.connect(bus_loop) 27 28 cryptohome_proxy = cryptohome.CryptohomeProxy( 29 bus_loop, self.autodir, self.job) 30 users = ['first_user@nowhere.com', 'second_user@nowhere.com'] 31 for user in users: 32 cryptohome_proxy.ensure_clean_cryptohome_for(user) 33 34 sm.StartSession(users[0], '') 35 self.__check_for_users_in_actives(users[:1], 36 sm.RetrieveActiveSessions()) 37 38 sm.StartSession(users[1], '') 39 self.__check_for_users_in_actives(users, sm.RetrieveActiveSessions()) 40 41 42 def __check_for_users_in_actives(self, users, actives): 43 """Checks that only members of users are in actives. 44 45 If there are too many (or too few) entries in actives, this method 46 raises. Also, if each member of users is not present in the keys of 47 actives, then the method also raises. 48 49 @param users: iterable of user names to be checked for. 50 @param actives: a dictionary of {user: userhash}, the keys of which 51 are expected to match users. 52 53 @raises error.TestFail: if one of the above criteria is not met. 54 """ 55 expected_sessions = len(users) 56 if len(actives) != expected_sessions: 57 raise error.TestFail("%d session(s) should be active, not: %s" % 58 (expected_sessions, str(actives))) 59 60 if set(users) != set(actives.keys()): 61 raise error.TestFail("Expected sessions for %s, got %s" % 62 (users, actives)) 63 64 65 def cleanup(self): 66 # Bounce UI, without waiting for the browser to come back. Best effort. 67 cros_ui.stop(allow_fail=True) 68 cros_ui.start(allow_fail=True, wait_for_login_prompt=False) 69