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, tempfile
6from dbus.mainloop.glib import DBusGMainLoop
7
8from autotest_lib.client.bin import test
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
22    def initialize(self):
23        super(login_OwnershipApi, self).initialize()
24        policy.install_protobufs(self.autodir, self.job)
25        self._bus_loop = DBusGMainLoop(set_as_default=True)
26
27        # Clear existing ownership and inject known keys.
28        cros_ui.stop()
29        ownership.clear_ownership_files_no_restart()
30
31        # Make device already owned by ownership.TESTUSER.
32        cryptohome.mount_vault(ownership.TESTUSER,
33                               ownership.TESTPASS,
34                               create=True)
35        ownership.use_known_ownerkeys(ownership.TESTUSER)
36
37        self._tempdir = autotemp.tempdir(unique_id=self.__class__.__name__)
38        cros_ui.start()
39
40
41    def __generate_temp_filename(self, dir):
42        """Generate a guaranteed-unique filename in dir."""
43        just_for_name = tempfile.NamedTemporaryFile(dir=dir, delete=True)
44        basename = just_for_name.name
45        just_for_name.close()  # deletes file.
46        return basename
47
48
49    def run_once(self):
50        pkey = ownership.known_privkey()
51        pubkey = ownership.known_pubkey()
52        sm = session_manager.connect(self._bus_loop)
53        sm.StartSession(ownership.TESTUSER, '')
54
55        poldata = policy.build_policy_data(owner=ownership.TESTUSER,
56                                           guests=False,
57                                           new_users=True,
58                                           roaming=True,
59                                           whitelist=(ownership.TESTUSER,
60                                                      'a@b.c'))
61
62        policy_string = policy.generate_policy(pkey, pubkey, poldata)
63        policy.push_policy_and_verify(policy_string, sm)
64        retrieved_policy = policy.get_policy(sm)
65        if retrieved_policy is None: raise error.TestFail('Policy not found')
66        policy.compare_policy_response(retrieved_policy,
67                                       owner=ownership.TESTUSER,
68                                       guests=False,
69                                       new_users=True,
70                                       roaming=True,
71                                       whitelist=(ownership.TESTUSER, 'a@b.c'))
72        try:
73            # Sanity check against an incorrect policy
74            policy.compare_policy_response(retrieved_policy,
75                                           owner=ownership.TESTUSER,
76                                           guests=True,
77                                           whitelist=(ownership.TESTUSER,
78                                                      'a@b.c'))
79        except ownership.OwnershipError:
80            pass
81        else:
82            raise error.TestFail('Did not detect bad policy')
83
84
85    def cleanup(self):
86        if self._tempdir: self._tempdir.clean()
87        # Best effort to bounce the UI, which may be up or down.
88        cros_ui.stop(allow_fail=True)
89        cryptohome.unmount_vault(ownership.TESTUSER)
90        cryptohome.remove_vault(ownership.TESTUSER)
91        cros_ui.start(allow_fail=True, wait_for_login_prompt=False)
92        super(login_OwnershipApi, self).cleanup()
93