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 time
6
7from autotest_lib.client.bin import test
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib.cros import chrome
10from autotest_lib.client.cros import pkcs11
11
12class platform_Pkcs11InitOnLogin(test.test):
13    """This test logs in and verifies that the TPM token is working."""
14    version = 1
15
16    def run_once(self):
17        start_time = time.time()
18        with chrome.Chrome() as cr:
19            if not pkcs11.wait_for_pkcs11_token():
20                raise error.TestFail('The PKCS #11 token is not available.')
21            end_time = time.time()
22            self.write_perf_keyval(
23                { 'seconds_pkcs11_onlogin_init': end_time - start_time } )
24            if not pkcs11.verify_pkcs11_initialized():
25                raise error.TestFail('Initialized token failed checks!')
26            if not pkcs11.inject_and_test_key():
27                raise error.TestFail('Failed to inject a key.')
28        # Login again with the same account.
29        with chrome.Chrome(dont_override_profile=True) as cr:
30            if not pkcs11.wait_for_pkcs11_token():
31                raise error.TestFail(
32                    'The PKCS #11 token is no longer available.')
33            if not pkcs11.test_and_cleanup_key():
34                raise error.TestFail('The PKCS #11 key is no longer valid.')
35
36