Debian Codegraph @

src/lib/object_store/test/DBTokenTests.cpp

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