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 5 import logging, os, shutil 6 7 from autotest_lib.client.bin import test, utils 8 from autotest_lib.client.common_lib import error 9 from autotest_lib.client.cros import pkcs11 10 11 class Pkcs11InitFailure(error.TestError): 12 pass 13 14 15 class platform_Pkcs11InitUnderErrors(test.test): 16 version = 1 17 18 def __chaps_init_iteration(self): 19 # Try initializing and using the temporary chaps test token. 20 pkcs11.load_p11_test_token() 21 if not pkcs11.verify_p11_test_token(): 22 return False 23 pkcs11.unload_p11_test_token() 24 pkcs11.restore_p11_test_token() 25 return True 26 27 def __test_chaps_init(self): 28 pkcs11.setup_p11_test_token(True) 29 dbpath = pkcs11.get_p11_test_token_db_path() 30 # Make sure the test token is functional. 31 if not self.__chaps_init_iteration(): 32 raise error.TestFail('Token verification failed.') 33 # Erase the chaps database directory. 34 shutil.rmtree(dbpath, ignore_errors=True) 35 if not self.__chaps_init_iteration(): 36 raise error.TestFail('Token verification failed after erasing the ' 37 'database directory.') 38 # Corrupt each file in the chaps database directory. 39 for f in os.listdir(dbpath): 40 utils.system('dd if=/dev/zero of=%s bs=1 count=1000 >/dev/null 2>&1' 41 % os.path.join(dbpath, f)) 42 if not self.__chaps_init_iteration(): 43 raise error.TestFail('Token verification failed after corrupting ' 44 'the database.') 45 pkcs11.cleanup_p11_test_token() 46 47 def run_once(self): 48 self.__test_chaps_init() 49 return 50 51