From 89f03d199f820f3de967ee2a72b7d9a4cf21ea7f Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Thu, 4 May 2017 17:12:27 -0400 Subject: Regression tests for today's C_FindObjects() bugfixes. --- unit_tests.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/unit_tests.py b/unit_tests.py index 1a7470d..fe96207 100644 --- a/unit_tests.py +++ b/unit_tests.py @@ -187,14 +187,25 @@ class TestDevice(TestCase): self.assertIsInstance(random, str) self.assertEqual(len(random), n) - def test_findObjects(self): - "Test basic C_FindObjects*() behavior" + def test_findObjects_operation_state(self): + "Test C_FindObjects*() CKR_OPERATION_* behavior" session = p11.C_OpenSession(args.slot) + + with self.assertRaises(CKR_OPERATION_NOT_INITIALIZED): + for handle in p11.C_FindObjects(session): + self.assertIsInstance(handle, (int, long)) + + with self.assertRaises(CKR_OPERATION_NOT_INITIALIZED): + p11.C_FindObjectsFinal(session) + p11.C_FindObjectsInit(session, CKA_CLASS = CKO_PUBLIC_KEY) + with self.assertRaises(CKR_OPERATION_ACTIVE): p11.C_FindObjectsInit(session, CKA_CLASS = CKO_PRIVATE_KEY) + for handle in p11.C_FindObjects(session): self.assertIsInstance(handle, (int, long)) + p11.C_FindObjectsFinal(session) @@ -573,6 +584,31 @@ class TestKeys(TestCase): p11.C_VerifyUpdate(self.session, tbs) p11.C_VerifyFinal(self.session, sig) + def _find_objects(self, chunk_size = 10, template = None, **kwargs): + p11.C_FindObjectsInit(self.session, template, **kwargs) + for handle in p11.C_FindObjects(self.session, chunk_size): + self.assertIsInstance(handle, (int, long)) + p11.C_FindObjectsFinal(self.session) + + @unittest.skipUnless(pycrypto_loaded, "requires PyCrypto") + def test_findObjects(self): + self._load_rsa_keypair(rsa_1024_pem, "RSA-1024") + self._load_rsa_keypair(rsa_2048_pem, "RSA-2048") + self._load_rsa_keypair(rsa_3416_pem, "RSA-3416") + self._find_objects(chunk_size = 1, CKA_CLASS = CKO_PUBLIC_KEY) + self._find_objects(chunk_size = 1, CKA_CLASS = CKO_PRIVATE_KEY) + self._find_objects(chunk_size = 10, CKA_CLASS = CKO_PUBLIC_KEY) + self._find_objects(chunk_size = 10, CKA_CLASS = CKO_PRIVATE_KEY) + self._find_objects(chunk_size = 100, CKA_CLASS = CKO_PUBLIC_KEY) + self._find_objects(chunk_size = 100, CKA_CLASS = CKO_PRIVATE_KEY) + self._find_objects(chunk_size = 1) + self._find_objects(chunk_size = 1) + self._find_objects(chunk_size = 10) + self._find_objects(chunk_size = 10) + self._find_objects(chunk_size = 100) + self._find_objects(chunk_size = 100) + + # Keys for preload tests, here rather than inline because they're # bulky. These are in PKCS #8 format, see PyCrypto or the "pkey" and # "genpkey" commands to OpenSSL's command line tool. -- cgit v1.2.3