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 dbus
6from dbus.mainloop.glib import DBusGMainLoop
7
8from autotest_lib.client.bin import test
9from autotest_lib.client.common_lib.cros import session_manager
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.cros import cros_ui, cryptohome
12
13
14class login_SameSessionTwice(test.test):
15    """Ensure that the session_manager won't start the same session twice.
16    """
17    version = 1
18
19
20    def initialize(self):
21        super(login_SameSessionTwice, self).initialize()
22        cros_ui.restart()
23
24
25    def run_once(self):
26        bus_loop = DBusGMainLoop(set_as_default=True)
27        sm = session_manager.connect(bus_loop)
28
29        user = 'first_user@nowhere.com'
30        cryptohome.CryptohomeProxy(bus_loop).ensure_clean_cryptohome_for(user)
31
32        if not sm.StartSession(user, ''):
33            raise error.TestFail('Could not start session for ' + user)
34
35        try:
36            if sm.StartSession(user, ''):
37                raise error.TestFail('Started second session for ' + user)
38        except dbus.DBusException as d:
39            # If I knew how to get our custom dbus errors mapped into real
40            # exceptions in PyDBus, I'd use that here :-/
41            if 'already started a session' not in d.message:
42                raise error.TestFail(d)
43
44
45    def cleanup(self):
46        # Bounce UI, without waiting for the browser to come back. Best effort.
47        cros_ui.stop(allow_fail=True)
48        cros_ui.start(allow_fail=True, wait_for_login_prompt=False)
49