Debian Codegraph @

src/lib/object_store/test/OSTokenTests.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
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 OSTokenTests.cpp
29
30 Contains test cases to test the object file implementation
31 *****************************************************************************/
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <cppunit/extensions/HelperMacros.h>
36 #include "OSTokenTests.h"
37 #include "OSToken.h"
38 #include "ObjectFile.h"
39 #include "File.h"
40 #include "Directory.h"
41 #include "OSAttribute.h"
42 #include "OSAttributes.h"
43 #include "cryptoki.h"
44
45 CPPUNIT_TEST_SUITE_REGISTRATION(OSTokenTests);
46
47 // FIXME: all pathnames in this file are *NIX/BSD specific
48
49 void OSTokenTests::setUp()
50 {
51 CPPUNIT_ASSERT(!system("mkdir testdir"));
52 }
53
54 void OSTokenTests::tearDown()
55 {
56 #ifndef _WIN32
57 CPPUNIT_ASSERT(!system("rm -rf testdir"));
58 #else
59 CPPUNIT_ASSERT(!system("rmdir /s /q testdir 2> nul"));
60 #endif
61 }
62
63 void OSTokenTests::testNewToken()
64 {
65 // Create a new token
66 ByteString label = "40414243"; // ABCD
67 ByteString serial = "0102030405060708";
68
69 #ifndef _WIN32
70 OSToken* newToken = OSToken::createToken("./testdir", "newToken", label, serial);
71 #else
72 OSToken* newToken = OSToken::createToken(".\\testdir", "newToken", label, serial);
73 #endif
74
75 CPPUNIT_ASSERT(newToken != NULL);
76
77 // Check the flags
78 CK_ULONG flags;
79 CPPUNIT_ASSERT(newToken->getTokenFlags(flags));
80 CPPUNIT_ASSERT(flags == (CKF_RNG | CKF_LOGIN_REQUIRED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_TOKEN_INITIALIZED | CKF_SO_PIN_LOCKED | CKF_SO_PIN_TO_BE_CHANGED));
81
82 // Set the SO PIN
83 ByteString soPIN = "3132333435363738"; // 12345678
84
85 CPPUNIT_ASSERT(newToken->setSOPIN(soPIN));
86
87 // Set the user PIN
88 ByteString userPIN = "31323334"; // 1234
89
90 CPPUNIT_ASSERT(newToken->setUserPIN(userPIN));
91
92 CPPUNIT_ASSERT(newToken->getTokenFlags(flags));
93 CPPUNIT_ASSERT(flags == (CKF_RNG | CKF_LOGIN_REQUIRED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_TOKEN_INITIALIZED | CKF_USER_PIN_INITIALIZED));
94
95 delete newToken;
96
97 // Now reopen the newly created token
98 #ifndef _WIN32
99 OSToken reopenedToken("./testdir/newToken");
100 #else
101 OSToken reopenedToken(".\\testdir\\newToken");
102 #endif
103
104 CPPUNIT_ASSERT(reopenedToken.isValid());
105
106 // Retrieve the flags, user PIN and so PIN
107 ByteString retrievedSOPIN, retrievedUserPIN;
108
109 CPPUNIT_ASSERT(reopenedToken.getSOPIN(retrievedSOPIN));
110 CPPUNIT_ASSERT(reopenedToken.getUserPIN(retrievedUserPIN));
111 CPPUNIT_ASSERT(reopenedToken.getTokenFlags(flags));
112
113 CPPUNIT_ASSERT(retrievedSOPIN == soPIN);
114 CPPUNIT_ASSERT(retrievedUserPIN == userPIN);
115 CPPUNIT_ASSERT(flags == (CKF_RNG | CKF_LOGIN_REQUIRED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_TOKEN_INITIALIZED | CKF_USER_PIN_INITIALIZED));
116 }
117
118 void OSTokenTests::testExistingToken()
119 {
120 ByteString label = "40414243"; // ABCD
121 ByteString serial = "0102030405060708";
122 ByteString soPIN = "31323334"; // 1234
123 ByteString userPIN = "30303030"; // 0000
124 ByteString id1 = "ABCDEF";
125 ByteString id2 = "FEDCBA";
126 ByteString id3 = "AABBCC";
127
128 {
129 // Create the token dir
130 #ifndef _WIN32
131 CPPUNIT_ASSERT(!system("mkdir testdir/existingToken"));
132 #else
133 CPPUNIT_ASSERT(!system("mkdir testdir\\existingToken"));
134 #endif
135
136 // Create the token object
137 #ifndef _WIN32
138 ObjectFile tokenObject(NULL, "./testdir/existingToken/token.object", "./testdir/existingToken/token.lock", true);
139 #else
140 ObjectFile tokenObject(NULL, ".\\testdir\\existingToken\\token.object", ".\\testdir\\existingToken\\token.lock", true);
141 #endif
142
143 OSAttribute labelAtt(label);
144 CPPUNIT_ASSERT(tokenObject.setAttribute(CKA_OS_TOKENLABEL, labelAtt));
145 OSAttribute serialAtt(serial);
146 CPPUNIT_ASSERT(tokenObject.setAttribute(CKA_OS_TOKENSERIAL, serialAtt));
147 OSAttribute soPINAtt(soPIN);
148 CPPUNIT_ASSERT(tokenObject.setAttribute(CKA_OS_SOPIN, soPINAtt));
149 OSAttribute userPINAtt(userPIN);
150 CPPUNIT_ASSERT(tokenObject.setAttribute(CKA_OS_USERPIN, userPINAtt));
151 CK_ULONG flags = CKF_RNG | CKF_LOGIN_REQUIRED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_TOKEN_INITIALIZED;
152 OSAttribute flagsAtt(flags);
153 CPPUNIT_ASSERT(tokenObject.setAttribute(CKA_OS_TOKENFLAGS, flagsAtt));
154
155 // Create 3 objects
156 #ifndef _WIN32
157 ObjectFile obj1(NULL, "./testdir/existingToken/1.object", "./testdir/existingToken/1.lock", true);
158 ObjectFile obj2(NULL, "./testdir/existingToken/2.object", "./testdir/existingToken/2.lock", true);
159 ObjectFile obj3(NULL, "./testdir/existingToken/3.object", "./testdir/existingToken/3.lock", true);
160 #else
161 ObjectFile obj1(NULL, ".\\testdir\\existingToken\\1.object", ".\\testdir\\existingToken\\1.lock", true);
162 ObjectFile obj2(NULL, ".\\testdir\\existingToken\\2.object", ".\\testdir\\existingToken\\2.lock", true);
163 ObjectFile obj3(NULL, ".\\testdir\\existingToken\\3.object", ".\\testdir\\existingToken\\3.lock", true);
164 #endif
165
166 OSAttribute id1Att(id1);
167 OSAttribute id2Att(id2);
168 OSAttribute id3Att(id3);
169
170 CPPUNIT_ASSERT(obj1.setAttribute(CKA_ID, id1));
171 CPPUNIT_ASSERT(obj2.setAttribute(CKA_ID, id2));
172 CPPUNIT_ASSERT(obj3.setAttribute(CKA_ID, id3));
173 }
174
175 // Now open the token
176 #ifndef _WIN32
177 OSToken existingToken("./testdir/existingToken");
178 #else
179 OSToken existingToken(".\\testdir\\existingToken");
180 #endif
181
182
183 CPPUNIT_ASSERT(existingToken.isValid());
184
185 // Retrieve SO PIN, user PIN, label, serial number and flags
186 ByteString retrievedSOPIN, retrievedUserPIN, retrievedLabel, retrievedSerial;
187 CK_ULONG flags;
188
189 CPPUNIT_ASSERT(existingToken.getSOPIN(retrievedSOPIN));
190 CPPUNIT_ASSERT(existingToken.getUserPIN(retrievedUserPIN));
191 CPPUNIT_ASSERT(existingToken.getTokenLabel(retrievedLabel));
192 CPPUNIT_ASSERT(existingToken.getTokenSerial(retrievedSerial));
193 CPPUNIT_ASSERT(existingToken.getTokenFlags(flags));
194
195 CPPUNIT_ASSERT(retrievedSOPIN == soPIN);
196 CPPUNIT_ASSERT(retrievedUserPIN == userPIN);
197 CPPUNIT_ASSERT(retrievedLabel == label);
198 CPPUNIT_ASSERT(retrievedSerial == serial);
199 CPPUNIT_ASSERT(flags == (CKF_RNG | CKF_LOGIN_REQUIRED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_TOKEN_INITIALIZED | CKF_USER_PIN_INITIALIZED));
200
201 // Check that the token contains 3 objects
202 CPPUNIT_ASSERT(existingToken.getObjects().size() == 3);
203
204 // Check that all the tokens are presented
205 bool present[3] = { false, false, false };
206 std::set<OSObject*> objects = existingToken.getObjects();
207
208 for (std::set<OSObject*>::iterator i = objects.begin(); i != objects.end(); i++)
209 {
210 ByteString retrievedId;
211
212 CPPUNIT_ASSERT((*i)->isValid());
213 CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID));
214
215 CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute());
216
217 if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id1)
218 {
219 present[0] = true;
220 }
221 else if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id2)
222 {
223 present[1] = true;
224 }
225 else if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id3)
226 {
227 present[2] = true;
228 }
229 }
230
231 CPPUNIT_ASSERT(present[0] == true);
232 CPPUNIT_ASSERT(present[1] == true);
233 CPPUNIT_ASSERT(present[2] == true);
234 }
235
236 void OSTokenTests::testNonExistentToken()
237 {
238 #ifndef _WIN32
239 OSToken doesntExist("./testdir/doesntExist");
240 #else
241 OSToken doesntExist(".\\testdir\\doesntExist");
242 #endif
243
244 CPPUNIT_ASSERT(!doesntExist.isValid());
245 }
246
247 void OSTokenTests::testCreateDeleteObjects()
248 {
249 // Test IDs
250 ByteString id[5] = { "112233445566", "AABBCCDDEEFF", "ABABABABABAB", "557788991122", "005500550055" };
251 OSAttribute idAtt[5] = { id[0], id[1], id[2], id[3], id[4] };
252 ByteString label = "AABBCCDDEEFF";
253 ByteString serial = "1234567890";
254
255 // Instantiate a new token
256 #ifndef _WIN32
257 OSToken* testToken = OSToken::createToken("./testdir", "testToken", label, serial);
258 #else
259 OSToken* testToken = OSToken::createToken(".\\testdir", "testToken", label, serial);
260 #endif
261
262 CPPUNIT_ASSERT(testToken != NULL);
263 CPPUNIT_ASSERT(testToken->isValid());
264
265 // Open the same token
266 #ifndef _WIN32
267 OSToken sameToken("./testdir/testToken");
268 #else
269 OSToken sameToken(".\\testdir\\testToken");
270 #endif
271
272 CPPUNIT_ASSERT(sameToken.isValid());
273
274 // Create 3 objects on the token
275 OSObject* obj1 = testToken->createObject();
276 CPPUNIT_ASSERT(obj1 != NULL);
277 OSObject* obj2 = testToken->createObject();
278 CPPUNIT_ASSERT(obj2 != NULL);
279 OSObject* obj3 = testToken->createObject();
280 CPPUNIT_ASSERT(obj3 != NULL);
281
282 // Now set the IDs of the 3 objects
283 obj1->setAttribute(CKA_ID, idAtt[0]);
284 obj2->setAttribute(CKA_ID, idAtt[1]);
285 obj3->setAttribute(CKA_ID, idAtt[2]);
286
287 // Check that the token contains 3 objects
288 CPPUNIT_ASSERT(testToken->getObjects().size() == 3);
289
290 // Check that all three objects are distinct and present
291 std::set<OSObject*> objects = testToken->getObjects();
292 bool present1[3] = { false, false, false };
293
294 for (std::set<OSObject*>::iterator i = objects.begin(); i != objects.end(); i++)
295 {
296 ByteString retrievedId;
297
298 CPPUNIT_ASSERT((*i)->isValid());
299 CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID));
300
301 CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute());
302
303 for (int j = 0; j < 3; j++)
304 {
305 if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[j])
306 {
307 present1[j] = true;
308 }
309 }
310 }
311
312 for (int j = 0; j < 3; j++)
313 {
314 CPPUNIT_ASSERT(present1[j] == true);
315 }
316
317 // Now check that the same objects are present in the other instance of the same token
318 std::set<OSObject*> otherObjects = sameToken.getObjects();
319 CPPUNIT_ASSERT(otherObjects.size() == 3);
320
321 bool present2[3] = { false, false, false };
322
323 for (std::set<OSObject*>::iterator i = otherObjects.begin(); i != otherObjects.end(); i++)
324 {
325 ByteString retrievedId;
326
327 CPPUNIT_ASSERT((*i)->isValid());
328 CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID));
329
330 CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute());
331
332 for (int j = 0; j < 3; j++)
333 {
334 if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[j])
335 {
336 present2[j] = true;
337 }
338 }
339 }
340
341 for (int j = 0; j < 3; j++)
342 {
343 CPPUNIT_ASSERT(present2[j] == true);
344 }
345
346 // Now delete the second object
347 for (std::set<OSObject*>::iterator i = objects.begin(); i != objects.end(); i++)
348 {
349 ByteString retrievedId;
350
351 CPPUNIT_ASSERT((*i)->isValid());
352 CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID));
353
354 CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute());
355
356 if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[1])
357 {
358 CPPUNIT_ASSERT(testToken->deleteObject(*i));
359 break;
360 }
361 }
362
363 // Verify that it was indeed removed
364 CPPUNIT_ASSERT(testToken->getObjects().size() == 2);
365
366 objects = testToken->getObjects();
367 bool present3[2] = { false, false };
368
369 for (std::set<OSObject*>::iterator i = objects.begin(); i != objects.end(); i++)
370 {
371 ByteString retrievedId;
372
373 CPPUNIT_ASSERT((*i)->isValid());
374 CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID));
375
376 CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute());
377
378 if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[0])
379 {
380 present3[0] = true;
381 }
382 if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[2])
383 {
384 present3[1] = true;
385 }
386 }
387
388 for (int j = 0; j < 2; j++)
389 {
390 CPPUNIT_ASSERT(present3[j] == true);
391 }
392
393 // Now check the other instance
394 CPPUNIT_ASSERT(sameToken.getObjects().size() == 2);
395
396 otherObjects = sameToken.getObjects();
397 bool present4[2] = { false, false };
398
399 for (std::set<OSObject*>::iterator i = otherObjects.begin(); i != otherObjects.end(); i++)
400 {
401 ByteString retrievedId;
402
403 CPPUNIT_ASSERT((*i)->isValid());
404 CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID));
405
406 CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute());
407
408 if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[0])
409 {
410 present4[0] = true;
411 }
412 if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[2])
413 {
414 present4[1] = true;
415 }
416 }
417
418 for (int j = 0; j < 2; j++)
419 {
420 CPPUNIT_ASSERT(present4[j] == true);
421 }
422
423
424 // Release the test token
425 delete testToken;
426 }
427
428 void OSTokenTests::testClearToken()
429 {
430 // Create a new token
431 ByteString label = "40414243"; // ABCD
432 ByteString serial = "0102030405060708";
433
434 #ifndef _WIN32
435 OSToken* newToken = OSToken::createToken("./testdir", "newToken", label, serial);
436 #else
437 OSToken* newToken = OSToken::createToken(".\\testdir", "newToken", label, serial);
438 #endif
439
440 CPPUNIT_ASSERT(newToken != NULL);
441
442 // Check the flags
443 CK_ULONG flags;
444 CPPUNIT_ASSERT(newToken->getTokenFlags(flags));
445 CPPUNIT_ASSERT(flags == (CKF_RNG | CKF_LOGIN_REQUIRED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_TOKEN_INITIALIZED | CKF_SO_PIN_LOCKED | CKF_SO_PIN_TO_BE_CHANGED));
446
447 // Set the SO PIN
448 ByteString soPIN = "3132333435363738"; // 12345678
449
450 CPPUNIT_ASSERT(newToken->setSOPIN(soPIN));
451
452 // Set the user PIN
453 ByteString userPIN = "31323334"; // 1234
454
455 CPPUNIT_ASSERT(newToken->setUserPIN(userPIN));
456
457 CPPUNIT_ASSERT(newToken->getTokenFlags(flags));
458 CPPUNIT_ASSERT(flags == (CKF_RNG | CKF_LOGIN_REQUIRED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_TOKEN_INITIALIZED | CKF_USER_PIN_INITIALIZED));
459
460 delete newToken;
461
462 // Now reopen the newly created token
463 #ifndef _WIN32
464 OSToken reopenedToken("./testdir/newToken");
465 #else
466 OSToken reopenedToken(".\\testdir\\newToken");
467 #endif
468
469 CPPUNIT_ASSERT(reopenedToken.isValid());
470
471 // Retrieve the flags, user PIN and so PIN
472 ByteString retrievedSOPIN, retrievedUserPIN;
473
474 CPPUNIT_ASSERT(reopenedToken.getSOPIN(retrievedSOPIN));
475 CPPUNIT_ASSERT(reopenedToken.getUserPIN(retrievedUserPIN));
476 CPPUNIT_ASSERT(reopenedToken.getTokenFlags(flags));
477
478 CPPUNIT_ASSERT(retrievedSOPIN == soPIN);
479 CPPUNIT_ASSERT(retrievedUserPIN == userPIN);
480 CPPUNIT_ASSERT(flags == (CKF_RNG | CKF_LOGIN_REQUIRED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_TOKEN_INITIALIZED | CKF_USER_PIN_INITIALIZED));
481
482 // Now reset the token
483 CPPUNIT_ASSERT(reopenedToken.resetToken(label));
484 CPPUNIT_ASSERT(reopenedToken.getSOPIN(retrievedSOPIN));
485 CPPUNIT_ASSERT(!reopenedToken.getUserPIN(retrievedUserPIN));
486 CPPUNIT_ASSERT(reopenedToken.getTokenFlags(flags));
487 CPPUNIT_ASSERT(retrievedSOPIN == soPIN);
488 CPPUNIT_ASSERT(flags == (CKF_RNG | CKF_LOGIN_REQUIRED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_TOKEN_INITIALIZED));
489 CPPUNIT_ASSERT(reopenedToken.isValid());
490
491 // Now clear the token
492 CPPUNIT_ASSERT(reopenedToken.clearToken());
493 CPPUNIT_ASSERT(!reopenedToken.isValid());
494
495 // Try to open it once more
496 #ifndef _WIN32
497 OSToken clearedToken("./testdir/newToken");
498 #else
499 OSToken clearedToken(".\\testdir\\newToken");
500 #endif
501
502 CPPUNIT_ASSERT(!clearedToken.isValid());
503 }
504