1# Copyright (c) 2012 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 random, shutil
6from autotest_lib.client.bin import test, utils
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.cros import pkcs11
9
10class platform_Pkcs11Events(test.test):
11    version = 1
12
13    def run_once(self, num_tokens, num_events):
14        # Setup some token directories.
15        token_list = ['/tmp/chaps%d' % x for x in range(num_tokens)]
16        pkcs11.setup_p11_test_token(True)
17        shutil.rmtree('%s/database' % pkcs11.TMP_CHAPS_DIR, ignore_errors=True)
18        for token in token_list:
19            shutil.rmtree(token, ignore_errors=True)
20            pkcs11.copytree_with_ownership(pkcs11.TMP_CHAPS_DIR, token)
21
22        # Setup a key on each token.
23        for token in token_list:
24            utils.system('chaps_client --load --path=%s --auth=%s' %
25                         (token, token))
26            utils.system('p11_replay --inject')
27            utils.system('chaps_client --unload --path=%s' % token)
28
29        # Follow a login by an immediate logout.
30        for token in token_list:
31            utils.system('chaps_client --load --path=%s --auth=%s' %
32                         (token, token))
33        for token in token_list:
34            utils.system('chaps_client --unload --path=%s' % token)
35
36        # Hit the tokens with a bunch of random login / logout events.
37        for i in range(num_events):
38            token = random.choice(token_list)
39            event = random.choice(['login', 'logout'])
40            if event == 'login':
41              utils.system('chaps_client --load --path=%s --auth=%s' %
42                           (token, token))
43              # Note: This won't necessarily test the token we just loaded but
44              # we do know there should be at least one token available.
45              result = utils.system('p11_replay --replay_wifi',
46                                    ignore_status=True)
47              if result != 0:
48                  raise error.TestFail('At least one token is not functional.')
49            else:
50              utils.system('chaps_client --unload --path=%s' % token)
51
52        # See if each token is still functional.
53        for token in token_list:
54            utils.system('chaps_client --unload --path=%s' % token)
55        for token in token_list:
56            utils.system('chaps_client --load --path=%s --auth=%s' %
57                         (token, token))
58            result = utils.system('p11_replay --replay_wifi',
59                                  ignore_status=True)
60            if result != 0:
61                raise error.TestFail('Token is not functional: %s' % token)
62            utils.system('chaps_client --unload --path=%s' % token)
63            shutil.rmtree(token, ignore_errors=True)
64
65        pkcs11.cleanup_p11_test_token()
66