• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2011 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 logging, os, tempfile
6from dbus.mainloop.glib import DBusGMainLoop
7
8from autotest_lib.client.bin import test, utils
9from autotest_lib.client.common_lib import autotemp, 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_OwnershipApi(test.test):
15    """Tests to ensure that the Ownership API works for a local device owner.
16    """
17    version = 1
18
19    _tempdir = None
20
21    def setup(self):
22        os.chdir(self.srcdir)
23        utils.make('OUT_DIR=.')
24
25
26    def initialize(self):
27        super(login_OwnershipApi, self).initialize()
28        self._bus_loop = DBusGMainLoop(set_as_default=True)
29        self._cryptohome_proxy = cryptohome.CryptohomeProxy(self._bus_loop)
30
31        # Clear existing ownership and inject known keys.
32        cros_ui.stop()
33        ownership.clear_ownership_files_no_restart()
34
35        # Make device already owned by ownership.TESTUSER.
36        self._cryptohome_proxy.mount(ownership.TESTUSER,
37                                     ownership.TESTPASS,
38                                     create=True)
39        ownership.use_known_ownerkeys(ownership.TESTUSER)
40
41        self._tempdir = autotemp.tempdir(unique_id=self.__class__.__name__)
42        cros_ui.start()
43
44
45    def __generate_temp_filename(self, dir):
46        """Generate a guaranteed-unique filename in dir."""
47        just_for_name = tempfile.NamedTemporaryFile(dir=dir, delete=True)
48        basename = just_for_name.name
49        just_for_name.close()  # deletes file.
50        return basename
51
52
53    def run_once(self):
54        pkey = ownership.known_privkey()
55        pubkey = ownership.known_pubkey()
56        sm = session_manager.connect(self._bus_loop)
57        if not sm.StartSession(ownership.TESTUSER, ''):
58            raise error.TestFail('Could not start session for owner')
59
60        poldata = policy.build_policy_data(self.srcdir,
61                                           owner=ownership.TESTUSER,
62                                           guests=False,
63                                           new_users=True,
64                                           roaming=True,
65                                           whitelist=(ownership.TESTUSER,
66                                                      'a@b.c'))
67
68        policy_string = policy.generate_policy(self.srcdir,
69                                               pkey,
70                                               pubkey,
71                                               poldata)
72        policy.push_policy_and_verify(policy_string, sm)
73        retrieved_policy = policy.get_policy(sm)
74        if retrieved_policy is None: raise error.TestFail('Policy not found')
75        policy.compare_policy_response(self.srcdir,
76                                       retrieved_policy,
77                                       owner=ownership.TESTUSER,
78                                       guests=False,
79                                       new_users=True,
80                                       roaming=True,
81                                       whitelist=(ownership.TESTUSER, 'a@b.c'))
82        try:
83            # Sanity check against an incorrect policy
84            policy.compare_policy_response(self.srcdir,
85                                           retrieved_policy,
86                                           owner=ownership.TESTUSER,
87                                           guests=True,
88                                           whitelist=(ownership.TESTUSER,
89                                                      'a@b.c'))
90        except ownership.OwnershipError:
91            pass
92        else:
93            raise error.TestFail('Did not detect bad policy')
94
95        try:
96            sm.StopSession('')
97        except error.TestError as e:
98            logging.error(str(e))
99            raise error.TestFail('Could not stop session for owner')
100
101
102    def cleanup(self):
103        if self._tempdir: self._tempdir.clean()
104        # Best effort to bounce the UI, which may be up or down.
105        cros_ui.stop(allow_fail=True)
106        self._cryptohome_proxy.remove(ownership.TESTUSER)
107        cros_ui.start(allow_fail=True, wait_for_login_prompt=False)
108        super(login_OwnershipApi, self).cleanup()
109