|
1 | | -""" |
2 | | -PKCS#11 Tests |
3 | | -
|
4 | | -The following environment variables will influence the behaviour of test cases: |
5 | | - - PKCS11_MODULE, mandatory, points to the library/DLL to use for testing |
6 | | - - PKCS11_TOKEN_LABEL, mandatory, contains the token label |
7 | | - - PKCS11_TOKEN_PIN, optional (default is None), contains the PIN/passphrase of the token |
8 | | - - PKCS11_TOKEN_SO_PIN, optional (default is same as PKCS11_TOKEN_PIN), security officer PIN |
9 | | - - OPENSSL_PATH, optional, path to openssl executable (i.e. the folder that contains it) |
10 | | -
|
11 | | -""" |
12 | | - |
13 | | -import os |
14 | | -import shutil |
15 | | -import unittest |
16 | | -from functools import wraps |
17 | | -from warnings import warn |
18 | | - |
19 | | -import pkcs11 |
20 | | - |
21 | | -try: |
22 | | - LIB = os.environ["PKCS11_MODULE"] |
23 | | -except KeyError as ex: |
24 | | - raise RuntimeError("Must define `PKCS11_MODULE' to run tests.") from ex |
25 | | - |
26 | | - |
27 | | -try: |
28 | | - TOKEN = os.environ["PKCS11_TOKEN_LABEL"] |
29 | | -except KeyError as ex: |
30 | | - raise RuntimeError("Must define `PKCS11_TOKEN_LABEL' to run tests.") from ex |
31 | | - |
32 | | -TOKEN_PIN = os.environ.get("PKCS11_TOKEN_PIN") # Can be None |
33 | | -if TOKEN_PIN is None: |
34 | | - warn("`PKCS11_TOKEN_PIN' env variable is unset.", stacklevel=2) |
35 | | - |
36 | | -TOKEN_SO_PIN = os.environ.get("PKCS11_TOKEN_SO_PIN") |
37 | | -if TOKEN_SO_PIN is None: |
38 | | - TOKEN_SO_PIN = TOKEN_PIN |
39 | | - warn( |
40 | | - "`PKCS11_TOKEN_SO_PIN' env variable is unset. Using value from `PKCS11_TOKEN_PIN'", |
41 | | - stacklevel=2, |
42 | | - ) |
43 | | - |
44 | | -OPENSSL = shutil.which("openssl", path=os.environ.get("OPENSSL_PATH")) |
45 | | -if OPENSSL is None: |
46 | | - warn("Path to OpenSSL not found. Please adjust `PATH' or define `OPENSSL_PATH'", stacklevel=2) |
47 | | - |
48 | | - |
49 | | -class TestCase(unittest.TestCase): |
50 | | - """Base test case, optionally creates a token and a session.""" |
51 | | - |
52 | | - with_token = True |
53 | | - """Creates a token for this test case.""" |
54 | | - with_session = True |
55 | | - """Creates a session for this test case.""" |
56 | | - |
57 | | - @classmethod |
58 | | - def setUpClass(cls): |
59 | | - super().setUpClass() |
60 | | - cls.lib = lib = pkcs11.lib(LIB) |
61 | | - |
62 | | - if cls.with_token or cls.with_session: |
63 | | - cls.token = lib.get_token(token_label=TOKEN) |
64 | | - |
65 | | - def setUp(self): |
66 | | - super().setUp() |
67 | | - |
68 | | - if self.with_session: |
69 | | - self.session = self.token.open(user_pin=TOKEN_PIN) |
70 | | - |
71 | | - def tearDown(self): |
72 | | - if self.with_session: |
73 | | - self.session.close() |
74 | | - |
75 | | - super().tearDown() |
76 | | - |
77 | | - |
78 | | -def requires(*mechanisms): |
79 | | - """ |
80 | | - Decorates a function or class as requiring mechanisms, else they are |
81 | | - skipped. |
82 | | - """ |
83 | | - |
84 | | - def check_requirements(self): |
85 | | - """Determine what, if any, required mechanisms are unavailable.""" |
86 | | - unavailable = set(mechanisms) - self.token.slot.get_mechanisms() |
87 | | - |
88 | | - if unavailable: |
89 | | - raise unittest.SkipTest("Requires %s" % ", ".join(map(str, unavailable))) |
90 | | - |
91 | | - def inner(func): |
92 | | - @wraps(func) |
93 | | - def wrapper(self, *args, **kwargs): |
94 | | - check_requirements(self) |
95 | | - |
96 | | - return func(self, *args, **kwargs) |
97 | | - |
98 | | - return wrapper |
99 | | - |
100 | | - return inner |
101 | | - |
102 | | - |
103 | | -def xfail(condition): |
104 | | - """Mark a test that's expected to fail for a given condition.""" |
105 | | - |
106 | | - def inner(func): |
107 | | - if condition: |
108 | | - return unittest.expectedFailure(func) |
109 | | - |
110 | | - else: |
111 | | - return func |
112 | | - |
113 | | - return inner |
114 | | - |
115 | | - |
116 | | -class Is: |
117 | | - """ |
118 | | - Test what device we're using. |
119 | | - """ |
120 | | - |
121 | | - # trick: str.endswith() can accept tuples, |
122 | | - # see https://stackoverflow.com/questions/18351951/check-if-string-ends-with-one-of-the-strings-from-a-list |
123 | | - softhsm2 = LIB.lower().endswith( |
124 | | - ("libsofthsm2.so", "libsofthsm2.dylib", "softhsm2.dll", "softhsm2-x64.dll") |
125 | | - ) |
126 | | - nfast = LIB.lower().endswith(("libcknfast.so", "cknfast.dll")) |
127 | | - opencryptoki = LIB.endswith("libopencryptoki.so") |
128 | | - travis = os.environ.get("TRAVIS") == "true" |
129 | | - |
130 | | - |
131 | | -class Avail: |
132 | | - """ |
133 | | - Test if a resource is available |
134 | | - """ |
135 | | - |
136 | | - # openssl is searched across the exec path. Optionally, OPENSSL_PATH env variable can be defined |
137 | | - # in case there is no direct path to it (i.e. PATH does not point to it) |
138 | | - openssl = OPENSSL is not None |
139 | | - |
140 | | - |
141 | | -class Only: |
142 | | - """ |
143 | | - Limit tests to given conditions |
144 | | - """ |
145 | | - |
146 | | - softhsm2 = unittest.skipUnless(Is.softhsm2, "SoftHSMv2 only") |
147 | | - openssl = unittest.skipUnless(Avail.openssl, "openssl not found in the path") |
148 | | - |
149 | | - |
150 | | -class Not: |
151 | | - """ |
152 | | - Ignore tests for given devices |
153 | | - """ |
154 | | - |
155 | | - softhsm2 = unittest.skipIf(Is.softhsm2, "Not supported by SoftHSMv2") |
156 | | - nfast = unittest.skipIf(Is.nfast, "Not supported by nFast") |
157 | | - opencryptoki = unittest.skipIf(Is.opencryptoki, "Not supported by OpenCryptoki") |
158 | | - |
159 | | - |
160 | | -class FIXME: |
161 | | - """ |
162 | | - Tests is broken on this platform. |
163 | | - """ |
164 | | - |
165 | | - softhsm2 = xfail(Is.softhsm2) |
166 | | - nfast = xfail(Is.nfast) |
167 | | - opencryptoki = xfail(Is.opencryptoki) |
168 | | - travis = xfail(Is.travis) |
0 commit comments