Debian Codegraph @

src/lib/test/InitTests.cpp

raw
1 /*
2 * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 InitTests.cpp
29
30 Contains test cases to C_Initialize and C_Finalize
31 *****************************************************************************/
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <cppunit/extensions/HelperMacros.h>
36 #include "InitTests.h"
37 #include "cryptoki.h"
38 #include "osmutex.h"
39
40 CPPUNIT_TEST_SUITE_REGISTRATION(InitTests);
41
42 void InitTests::setUp()
43 {
44 // printf("\nInitTests\n");
45
46 #ifndef _WIN32
47 setenv("SOFTHSM2_CONF", "./softhsm2.conf", 1);
48 #else
49 setenv("SOFTHSM2_CONF", ".\\softhsm2.conf", 1);
50 #endif
51 }
52
53 void InitTests::tearDown()
54 {
55 // Just make sure that we finalize any previous failed tests
56 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
57 }
58
59 void InitTests::testInit1()
60 {
61 CK_RV rv;
62
63 // Just make sure that we finalize any previous failed tests
64 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
65
66 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
67 CPPUNIT_ASSERT(rv == CKR_OK);
68
69 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
70 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
71
72 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
73 CPPUNIT_ASSERT(rv == CKR_OK);
74 }
75
76 void InitTests::testInit2()
77 {
78 CK_C_INITIALIZE_ARGS InitArgs;
79 CK_RV rv;
80
81 #ifdef CreateMutex
82 #undef CreateMutex
83 #endif
84
85 InitArgs.CreateMutex = NULL_PTR;
86 InitArgs.DestroyMutex = NULL_PTR;
87 InitArgs.LockMutex = NULL_PTR;
88 InitArgs.UnlockMutex = NULL_PTR;
89 InitArgs.flags = 0;
90 InitArgs.pReserved = (CK_VOID_PTR)1;
91
92 // Just make sure that we finalize any previous failed tests
93 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
94
95 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
96 CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);
97
98 InitArgs.pReserved = NULL_PTR;
99 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
100 CPPUNIT_ASSERT(rv == CKR_OK);
101
102 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
103 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
104
105 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
106 CPPUNIT_ASSERT(rv == CKR_OK);
107 }
108
109 void InitTests::testInit3()
110 {
111 CK_C_INITIALIZE_ARGS InitArgs;
112 CK_RV rv;
113
114 InitArgs.CreateMutex = NULL_PTR;
115 InitArgs.DestroyMutex = NULL_PTR;
116 InitArgs.LockMutex = NULL_PTR;
117 InitArgs.UnlockMutex = (CK_UNLOCKMUTEX)1;
118 InitArgs.flags = 0;
119 InitArgs.pReserved = NULL_PTR;
120
121 // Just make sure that we finalize any previous failed tests
122 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
123
124 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
125 CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);
126
127 InitArgs.UnlockMutex = NULL_PTR;
128 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
129 CPPUNIT_ASSERT(rv == CKR_OK);
130
131 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
132 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
133
134 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
135 CPPUNIT_ASSERT(rv == CKR_OK);
136 }
137
138 void InitTests::testInit4()
139 {
140 CK_C_INITIALIZE_ARGS InitArgs;
141 CK_RV rv;
142
143 InitArgs.CreateMutex = NULL_PTR;
144 InitArgs.DestroyMutex = NULL_PTR;
145 InitArgs.LockMutex = NULL_PTR;
146 InitArgs.UnlockMutex = (CK_UNLOCKMUTEX)1;
147 InitArgs.flags = CKF_OS_LOCKING_OK;
148 InitArgs.pReserved = NULL_PTR;
149
150 // Just make sure that we finalize any previous failed tests
151 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
152
153 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
154 CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);
155
156 InitArgs.UnlockMutex = NULL_PTR;
157 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
158 // If rv == CKR_CANT_LOCK then we cannot use multiple threads
159 CPPUNIT_ASSERT(rv == CKR_OK);
160
161 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
162 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
163
164 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
165 CPPUNIT_ASSERT(rv == CKR_OK);
166 }
167
168 void InitTests::testInit5()
169 {
170 CK_C_INITIALIZE_ARGS InitArgs;
171 CK_RV rv;
172
173 InitArgs.CreateMutex = OSCreateMutex;
174 InitArgs.DestroyMutex = OSDestroyMutex;
175 InitArgs.LockMutex = OSLockMutex;
176 InitArgs.UnlockMutex = NULL_PTR;
177 InitArgs.flags = 0;
178 InitArgs.pReserved = NULL_PTR;
179
180 // Just make sure that we finalize any previous failed tests
181 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
182
183 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
184 CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);
185
186 InitArgs.UnlockMutex = OSUnlockMutex;
187 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
188 // If rv == CKR_CANT_LOCK then we cannot use multiple threads
189 CPPUNIT_ASSERT(rv == CKR_OK);
190
191 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
192 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
193
194 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
195 CPPUNIT_ASSERT(rv == CKR_OK);
196 }
197
198 void InitTests::testInit6()
199 {
200 CK_C_INITIALIZE_ARGS InitArgs;
201 CK_RV rv;
202
203 InitArgs.CreateMutex = OSCreateMutex;
204 InitArgs.DestroyMutex = OSDestroyMutex;
205 InitArgs.LockMutex = OSLockMutex;
206 InitArgs.UnlockMutex = NULL_PTR;
207 InitArgs.flags = CKF_OS_LOCKING_OK;
208 InitArgs.pReserved = NULL_PTR;
209
210 // Just make sure that we finalize any previous failed tests
211 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
212
213 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
214 CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);
215
216 InitArgs.UnlockMutex = OSUnlockMutex;
217 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
218 // If rv == CKR_CANT_LOCK then we cannot use multiple threads
219 CPPUNIT_ASSERT(rv == CKR_OK);
220
221 rv = CRYPTOKI_F_PTR( C_Initialize((CK_VOID_PTR)&InitArgs) );
222 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
223
224 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
225 CPPUNIT_ASSERT(rv == CKR_OK);
226 }
227
228 void InitTests::testFinal()
229 {
230 CK_RV rv;
231
232 // Just make sure that we finalize any previous failed tests
233 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
234
235 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
236 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED);
237
238 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
239 CPPUNIT_ASSERT(rv == CKR_OK);
240
241 // pReserved is reserved for future versions
242 rv = CRYPTOKI_F_PTR( C_Finalize((CK_VOID_PTR)1) );
243 CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);
244
245 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
246 CPPUNIT_ASSERT(rv == CKR_OK);
247 }