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