1# Copyright 2014 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# Most of this code is based on login_GuestAndActualSession, which performs
6# similar ownership clearing/checking tasks.
7
8import gobject, os
9from dbus.mainloop.glib import DBusGMainLoop
10
11from autotest_lib.client.bin import test, utils
12from autotest_lib.client.common_lib import error
13from autotest_lib.client.common_lib.cros import session_manager
14from autotest_lib.client.cros import cros_ui, cryptohome, ownership
15
16class login_CryptohomeOwnerQuery(test.test):
17    """Verify that the cryptohome owner user query works properly."""
18    version = 1
19
20
21    def initialize(self):
22        super(login_CryptohomeOwnerQuery, self).initialize()
23        # Ensure a clean beginning.
24        ownership.restart_ui_to_clear_ownership_files()
25
26        bus_loop = DBusGMainLoop(set_as_default=True)
27        self._session_manager = session_manager.connect(bus_loop)
28        self._listener = session_manager.OwnershipSignalListener(
29                gobject.MainLoop())
30        self._listener.listen_for_new_key_and_policy()
31
32        self._cryptohome_proxy = cryptohome.CryptohomeProxy(bus_loop)
33
34
35    def run_once(self):
36        owner = 'first_user@nowhere.com'
37
38        if cryptohome.get_login_status()['owner_user_exists']:
39            raise error.TestFail('Owner existed before login')
40
41        self._cryptohome_proxy.ensure_clean_cryptohome_for(owner)
42        if not self._session_manager.StartSession(owner, ''):
43            raise error.TestFail('Could not start session for ' + owner)
44
45        self._listener.wait_for_signals(desc='Device ownership complete.')
46
47        if not cryptohome.get_login_status()['owner_user_exists']:
48            raise error.TestFail('Owner does not exist after login')
49
50
51    def cleanup(self):
52        self._session_manager.StopSession('')
53        cros_ui.start(allow_fail=True, wait_for_login_prompt=False)
54