1eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone# Use of this source code is governed by a BSD-style license that can be
3eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone# found in the LICENSE file.
4eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
5a2b32851e8716f188dde273c73f4c76058bbd289Chris Masoneimport logging, random, string, os
664170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masonefrom dbus.mainloop.glib import DBusGMainLoop
7eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
8d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masonefrom autotest_lib.client.bin import test, utils
9eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masonefrom autotest_lib.client.common_lib import error
105d010aa50a0694d498e8317fd8044e56474ce7edChris Masonefrom autotest_lib.client.common_lib.cros import policy, session_manager
119fef4639415a0481373e073f40b7c32ed93a99b5Chris Masonefrom autotest_lib.client.cros import cros_ui, cryptohome, ownership
12eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
13eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
14d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masoneclass login_RemoteOwnership(test.test):
154dbb67c9f8fbb9d31cffa910feda5186c4c1994aChris Masone    """Tests to ensure that the Ownership API can be used, as an
164dbb67c9f8fbb9d31cffa910feda5186c4c1994aChris Masone       enterprise might, to set device policies.
174dbb67c9f8fbb9d31cffa910feda5186c4c1994aChris Masone    """
184dbb67c9f8fbb9d31cffa910feda5186c4c1994aChris Masone
19eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone    version = 1
20eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
21eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone    def setup(self):
22eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone        os.chdir(self.srcdir)
23eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone        utils.make('OUT_DIR=.')
24eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
25eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
26d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone    def initialize(self):
27d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone        # Start with a clean slate wrt ownership
28a2b32851e8716f188dde273c73f4c76058bbd289Chris Masone        ownership.restart_ui_to_clear_ownership_files()
29d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone        super(login_RemoteOwnership, self).initialize()
30d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone
3164170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone        bus_loop = DBusGMainLoop(set_as_default=True)
3264170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone        self._cryptohome_proxy = cryptohome.CryptohomeProxy(bus_loop)
3364170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone        self._sm = session_manager.connect(bus_loop)
34d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone
35eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
3664170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone    def run_once(self):
37eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone        # Initial policy setup.
3863abfd2ad9eecdea50ca2567a6324370ede77d58Chris Masone        poldata = policy.build_policy_data(self.srcdir)
39105706efb7ebc97575dafd7a92bb815f79d8bb47Chris Masone        priv = ownership.known_privkey()
40105706efb7ebc97575dafd7a92bb815f79d8bb47Chris Masone        pub = ownership.known_pubkey()
41d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone        policy.push_policy_and_verify(
4264170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone            policy.generate_policy(self.srcdir, priv, pub, poldata), self._sm)
43eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
44eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone        # Force re-key the device
45eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone        (priv, pub) = ownership.pairgen_as_data()
46d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone        policy.push_policy_and_verify(
4764170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone            policy.generate_policy(self.srcdir, priv, pub, poldata), self._sm)
48eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
49eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone        # Rotate key gracefully.
5063abfd2ad9eecdea50ca2567a6324370ede77d58Chris Masone        self.username = (''.join(random.sample(string.ascii_lowercase,6)) +
5163abfd2ad9eecdea50ca2567a6324370ede77d58Chris Masone                         "@foo.com")
524dbb67c9f8fbb9d31cffa910feda5186c4c1994aChris Masone        password = ''.join(random.sample(string.ascii_lowercase,6))
5364170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone        self._cryptohome_proxy.remove(self.username)
5464170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone        self._cryptohome_proxy.mount(self.username, password, create=True)
55eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
56eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone        (new_priv, new_pub) = ownership.pairgen_as_data()
57eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
5864170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone        if not self._sm.StartSession(self.username, ''):
59eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone            raise error.TestFail('Could not start session for random user')
60eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
61d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone        policy.push_policy_and_verify(
62d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone            policy.generate_policy(self.srcdir,
63d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone                                   key=new_priv,
64d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone                                   pubkey=new_pub,
6563abfd2ad9eecdea50ca2567a6324370ede77d58Chris Masone                                   policy=poldata,
66d976e0ed3cb28e532f15826c5071149b8fabeaf8Chris Masone                                   old_key=priv),
6764170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone            self._sm)
68eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
6967f77a12725f81b88235a9d865bd4b8322fb83c7Chris Masone        try:
7064170f8e7053f92f4bbad501c61a352fdabf6d82Chris Masone            self._sm.StopSession('')
7167f77a12725f81b88235a9d865bd4b8322fb83c7Chris Masone        except error.TestError as e:
7267f77a12725f81b88235a9d865bd4b8322fb83c7Chris Masone            logging.error(str(e))
7367f77a12725f81b88235a9d865bd4b8322fb83c7Chris Masone            raise error.TestFail('Could not stop session for random user')
74eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
75eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone
76eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone    def cleanup(self):
779fef4639415a0481373e073f40b7c32ed93a99b5Chris Masone        # Best effort to bounce the UI, which may be up or down.
789fef4639415a0481373e073f40b7c32ed93a99b5Chris Masone        cros_ui.stop(allow_fail=True)
799fef4639415a0481373e073f40b7c32ed93a99b5Chris Masone        self._cryptohome_proxy.remove(self.username)
809fef4639415a0481373e073f40b7c32ed93a99b5Chris Masone        cros_ui.start(allow_fail=True, wait_for_login_prompt=False)
81eac4f4f0839c57d84d5c042deed64cf6d2189d26Chris Masone        super(login_RemoteOwnership, self).cleanup()
82