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
5from dbus.mainloop.glib import DBusGMainLoop
6
7from autotest_lib.client.common_lib.cros import session_manager
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.bin import test, utils
10from autotest_lib.client.cros import cros_ui
11
12class platform_SessionManagerStateKeyGeneration(test.test):
13    '''Verifies that session_manager's GetServerBackedStateKeys DBus method
14    returns valid state keys.'''
15    version = 1
16
17    def initialize(self):
18        super(platform_SessionManagerStateKeyGeneration, self).initialize()
19        cros_ui.stop(allow_fail=True)
20        cros_ui.start()
21        self._bus_loop = DBusGMainLoop(set_as_default=True)
22
23    def run_once(self):
24        try:
25            if utils.system_output('crossystem mainfw_type') == 'nonchrome':
26                raise error.TestNAError(
27                    'State key generation only works on Chrome OS hardware')
28        except error.CmdError, e:
29            raise error.TestError('Failed to run crossystem: %s' % e)
30
31        # Retrieve state keys.
32        session_manager_proxy = session_manager.connect(self._bus_loop)
33        state_keys = session_manager_proxy.GetServerBackedStateKeys(
34            byte_arrays=True)
35
36        # Sanity-check the state keys.
37        if len(state_keys) < 3:
38            raise error.TestFail("Not enough state keys")
39        if len(state_keys) != len(set(state_keys)):
40            raise error.TestFail("Duplicate state keys")
41        for state_key in state_keys:
42            if len(state_key) != 32:
43                raise error.TestFail("Bad state key size")
44