| 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 |
SlotManagerTests.cpp |
| 29 |
|
| 30 |
Contains test cases to test the object store implementation |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <cppunit/extensions/HelperMacros.h> |
| 36 |
#include "SlotManagerTests.h" |
| 37 |
#include "SlotManager.h" |
| 38 |
#include "Token.h" |
| 39 |
#include "ObjectStore.h" |
| 40 |
#include "ObjectFile.h" |
| 41 |
#include "File.h" |
| 42 |
#include "Directory.h" |
| 43 |
#include "OSAttribute.h" |
| 44 |
#include "OSAttributes.h" |
| 45 |
#include "CryptoFactory.h" |
| 46 |
#include "cryptoki.h" |
| 47 |
|
| 48 |
CPPUNIT_TEST_SUITE_REGISTRATION(SlotManagerTests); |
| 49 |
|
| 50 |
void SlotManagerTests::setUp() |
| 51 |
{ |
| 52 |
CPPUNIT_ASSERT(!system("mkdir testdir")); |
| 53 |
} |
| 54 |
|
| 55 |
void SlotManagerTests::tearDown() |
| 56 |
{ |
| 57 |
#ifndef _WIN32 |
| 58 |
CPPUNIT_ASSERT(!system("rm -rf testdir")); |
| 59 |
#else |
| 60 |
CPPUNIT_ASSERT(!system("rmdir /s /q testdir 2> nul")); |
| 61 |
#endif |
| 62 |
} |
| 63 |
|
| 64 |
void SlotManagerTests::testNoExistingTokens() |
| 65 |
{ |
| 66 |
// Create an empty object store |
| 67 |
#ifndef _WIN32 |
| 68 |
ObjectStore store("./testdir"); |
| 69 |
#else |
| 70 |
ObjectStore store(".\\testdir"); |
| 71 |
#endif |
| 72 |
|
| 73 |
// Create the slot manager |
| 74 |
SlotManager slotManager(&store); |
| 75 |
|
| 76 |
CPPUNIT_ASSERT(slotManager.getSlots().size() == 1); |
| 77 |
|
| 78 |
// Test C_GetSlotList |
| 79 |
CK_SLOT_ID testList[10]; |
| 80 |
CK_ULONG ulCount = 10; |
| 81 |
|
| 82 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_FALSE, testList, &ulCount) == CKR_OK); |
| 83 |
CPPUNIT_ASSERT(ulCount == 1); |
| 84 |
|
| 85 |
ulCount = 10; |
| 86 |
|
| 87 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, testList, &ulCount) == CKR_OK); |
| 88 |
CPPUNIT_ASSERT(ulCount == 1); |
| 89 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotID() == testList[0]); |
| 90 |
|
| 91 |
// Retrieve slot information about the first slot |
| 92 |
CK_SLOT_INFO slotInfo; |
| 93 |
|
| 94 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 95 |
|
| 96 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 97 |
|
| 98 |
// Retrieve token information about the token in the first slot |
| 99 |
CK_TOKEN_INFO tokenInfo; |
| 100 |
|
| 101 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken() != NULL); |
| 102 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 103 |
|
| 104 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED); |
| 105 |
} |
| 106 |
|
| 107 |
void SlotManagerTests::testExistingTokens() |
| 108 |
{ |
| 109 |
// Create an empty object store |
| 110 |
#ifndef _WIN32 |
| 111 |
ObjectStore store("./testdir"); |
| 112 |
#else |
| 113 |
ObjectStore store(".\\testdir"); |
| 114 |
#endif |
| 115 |
|
| 116 |
// Create two tokens |
| 117 |
ByteString label1 = "DEADBEEF"; |
| 118 |
ByteString label2 = "DEADC0FFEE"; |
| 119 |
|
| 120 |
CPPUNIT_ASSERT(store.newToken(label1) != NULL); |
| 121 |
CPPUNIT_ASSERT(store.newToken(label2) != NULL); |
| 122 |
|
| 123 |
// Now attach the slot manager |
| 124 |
SlotManager slotManager(&store); |
| 125 |
|
| 126 |
CPPUNIT_ASSERT(slotManager.getSlots().size() == 3); |
| 127 |
|
| 128 |
// Test C_GetSlotList |
| 129 |
CK_SLOT_ID testList[10]; |
| 130 |
CK_ULONG ulCount = 10; |
| 131 |
|
| 132 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_FALSE, testList, &ulCount) == CKR_OK); |
| 133 |
CPPUNIT_ASSERT(ulCount == 3); |
| 134 |
|
| 135 |
ulCount = 10; |
| 136 |
|
| 137 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, testList, &ulCount) == CKR_OK); |
| 138 |
CPPUNIT_ASSERT(ulCount == 3); |
| 139 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotID() == testList[0]); |
| 140 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getSlotID() == testList[1]); |
| 141 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[2]]->getSlotID() == testList[2]); |
| 142 |
|
| 143 |
// Retrieve slot information about the first slot |
| 144 |
CK_SLOT_INFO slotInfo; |
| 145 |
|
| 146 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 147 |
|
| 148 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 149 |
|
| 150 |
// Retrieve token information about the token in the first slot |
| 151 |
CK_TOKEN_INFO tokenInfo; |
| 152 |
|
| 153 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken() != NULL); |
| 154 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 155 |
|
| 156 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED); |
| 157 |
CPPUNIT_ASSERT(!memcmp(tokenInfo.label, &label1[0], label1.size()) || |
| 158 |
!memcmp(tokenInfo.label, &label2[0], label2.size())); |
| 159 |
|
| 160 |
// Retrieve slot information about the second slot |
| 161 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 162 |
|
| 163 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 164 |
|
| 165 |
// Retrieve token information about the token in the second slot |
| 166 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken() != NULL); |
| 167 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 168 |
|
| 169 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED); |
| 170 |
CPPUNIT_ASSERT(!memcmp(tokenInfo.label, &label1[0], label1.size()) || |
| 171 |
!memcmp(tokenInfo.label, &label2[0], label2.size())); |
| 172 |
|
| 173 |
// Retrieve slot information about the third slot |
| 174 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[2]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 175 |
|
| 176 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 177 |
|
| 178 |
// Retrieve token information about the token in the third slot |
| 179 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[2]]->getToken() != NULL); |
| 180 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[2]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 181 |
|
| 182 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED); |
| 183 |
} |
| 184 |
|
| 185 |
void SlotManagerTests::testInitialiseTokenInLastSlot() |
| 186 |
{ |
| 187 |
{ |
| 188 |
// Create an empty object store |
| 189 |
#ifndef _WIN32 |
| 190 |
ObjectStore store("./testdir"); |
| 191 |
#else |
| 192 |
ObjectStore store(".\\testdir"); |
| 193 |
#endif |
| 194 |
|
| 195 |
// Create the slot manager |
| 196 |
SlotManager slotManager(&store); |
| 197 |
|
| 198 |
CPPUNIT_ASSERT(slotManager.getSlots().size() == 1); |
| 199 |
|
| 200 |
// Test C_GetSlotList |
| 201 |
CK_SLOT_ID testList[10]; |
| 202 |
CK_ULONG ulCount = 10; |
| 203 |
|
| 204 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_FALSE, testList, &ulCount) == CKR_OK); |
| 205 |
CPPUNIT_ASSERT(ulCount == 1); |
| 206 |
|
| 207 |
ulCount = 10; |
| 208 |
|
| 209 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, testList, &ulCount) == CKR_OK); |
| 210 |
CPPUNIT_ASSERT(ulCount == 1); |
| 211 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotID() == testList[0]); |
| 212 |
|
| 213 |
// Retrieve slot information about the first slot |
| 214 |
CK_SLOT_INFO slotInfo; |
| 215 |
|
| 216 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 217 |
|
| 218 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 219 |
|
| 220 |
// Retrieve token information about the token in the first slot |
| 221 |
CK_TOKEN_INFO tokenInfo; |
| 222 |
|
| 223 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken() != NULL); |
| 224 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 225 |
|
| 226 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED); |
| 227 |
|
| 228 |
// Now initialise the token in the first slot |
| 229 |
ByteString soPIN((unsigned char*)"1234", 4); |
| 230 |
CK_UTF8CHAR label[33] = "My test token "; |
| 231 |
|
| 232 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->initToken(soPIN, label) == CKR_OK); |
| 233 |
|
| 234 |
// Retrieve slot information about the first slot |
| 235 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 236 |
|
| 237 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 238 |
|
| 239 |
// Retrieve token information about the token in the first slot |
| 240 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken() != NULL); |
| 241 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 242 |
|
| 243 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED); |
| 244 |
CPPUNIT_ASSERT(!memcmp(tokenInfo.label, label, 32)); |
| 245 |
} |
| 246 |
|
| 247 |
// Attach a fresh slot manager |
| 248 |
#ifndef _WIN32 |
| 249 |
ObjectStore store("./testdir"); |
| 250 |
#else |
| 251 |
ObjectStore store(".\\testdir"); |
| 252 |
#endif |
| 253 |
SlotManager slotManager(&store); |
| 254 |
|
| 255 |
CPPUNIT_ASSERT(slotManager.getSlots().size() == 2); |
| 256 |
|
| 257 |
// Test C_GetSlotList |
| 258 |
CK_SLOT_ID testList[10]; |
| 259 |
CK_ULONG ulCount = 10; |
| 260 |
|
| 261 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_FALSE, testList, &ulCount) == CKR_OK); |
| 262 |
CPPUNIT_ASSERT(ulCount == 2); |
| 263 |
|
| 264 |
ulCount = 10; |
| 265 |
|
| 266 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, testList, &ulCount) == CKR_OK); |
| 267 |
CPPUNIT_ASSERT(ulCount == 2); |
| 268 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotID() == testList[0]); |
| 269 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getSlotID() == testList[1]); |
| 270 |
|
| 271 |
// Retrieve slot information about the first slot |
| 272 |
CK_SLOT_INFO slotInfo; |
| 273 |
|
| 274 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 275 |
|
| 276 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 277 |
|
| 278 |
// Retrieve token information about the token in the first slot |
| 279 |
CK_TOKEN_INFO tokenInfo; |
| 280 |
|
| 281 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken() != NULL); |
| 282 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 283 |
|
| 284 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED); |
| 285 |
|
| 286 |
CK_UTF8CHAR label[33] = "My test token "; |
| 287 |
CPPUNIT_ASSERT(!memcmp(tokenInfo.label, label, 32)); |
| 288 |
|
| 289 |
// Retrieve slot information about the second slot |
| 290 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 291 |
|
| 292 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 293 |
|
| 294 |
// Retrieve token information about the token in the second slot |
| 295 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken() != NULL); |
| 296 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 297 |
|
| 298 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED); |
| 299 |
} |
| 300 |
|
| 301 |
void SlotManagerTests::testReinitialiseExistingToken() |
| 302 |
{ |
| 303 |
// Create an empty object store |
| 304 |
#ifndef _WIN32 |
| 305 |
ObjectStore store("./testdir"); |
| 306 |
#else |
| 307 |
ObjectStore store(".\\testdir"); |
| 308 |
#endif |
| 309 |
|
| 310 |
// Create two tokens |
| 311 |
ByteString label1 = "DEADBEEF"; |
| 312 |
ByteString label2 = "DEADC0FFEE"; |
| 313 |
|
| 314 |
CPPUNIT_ASSERT(store.newToken(label1) != NULL); |
| 315 |
CPPUNIT_ASSERT(store.newToken(label2) != NULL); |
| 316 |
|
| 317 |
// Now attach the slot manager |
| 318 |
SlotManager slotManager(&store); |
| 319 |
|
| 320 |
CPPUNIT_ASSERT(slotManager.getSlots().size() == 3); |
| 321 |
|
| 322 |
// Test C_GetSlotList |
| 323 |
CK_SLOT_ID testList[10]; |
| 324 |
CK_ULONG ulCount = 10; |
| 325 |
|
| 326 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_FALSE, testList, &ulCount) == CKR_OK); |
| 327 |
CPPUNIT_ASSERT(ulCount == 3); |
| 328 |
|
| 329 |
ulCount = 10; |
| 330 |
|
| 331 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, testList, &ulCount) == CKR_OK); |
| 332 |
CPPUNIT_ASSERT(ulCount == 3); |
| 333 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotID() == testList[0]); |
| 334 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getSlotID() == testList[1]); |
| 335 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[2]]->getSlotID() == testList[2]); |
| 336 |
|
| 337 |
// Retrieve slot information about the first slot |
| 338 |
CK_SLOT_INFO slotInfo; |
| 339 |
|
| 340 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 341 |
|
| 342 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 343 |
|
| 344 |
// Retrieve token information about the token in the first slot |
| 345 |
CK_TOKEN_INFO tokenInfo; |
| 346 |
|
| 347 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken() != NULL); |
| 348 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 349 |
|
| 350 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED); |
| 351 |
CPPUNIT_ASSERT(!memcmp(tokenInfo.label, &label1[0], label1.size()) || |
| 352 |
!memcmp(tokenInfo.label, &label2[0], label2.size())); |
| 353 |
|
| 354 |
// Retrieve slot information about the second slot |
| 355 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 356 |
|
| 357 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 358 |
|
| 359 |
// Retrieve token information about the token in the second slot |
| 360 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken() != NULL); |
| 361 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 362 |
|
| 363 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED); |
| 364 |
CPPUNIT_ASSERT(!memcmp(tokenInfo.label, &label1[0], label1.size()) || |
| 365 |
!memcmp(tokenInfo.label, &label2[0], label2.size())); |
| 366 |
|
| 367 |
// Retrieve slot information about the third slot |
| 368 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[2]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 369 |
|
| 370 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 371 |
|
| 372 |
// Retrieve token information about the token in the third slot |
| 373 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[2]]->getToken() != NULL); |
| 374 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[2]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 375 |
|
| 376 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED); |
| 377 |
|
| 378 |
// Now reinitialise the token in the second slot |
| 379 |
ByteString soPIN((unsigned char*)"1234", 4); |
| 380 |
CK_UTF8CHAR label[33] = "My test token "; |
| 381 |
|
| 382 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->initToken(soPIN, label) == CKR_OK); |
| 383 |
|
| 384 |
// Retrieve slot information about the first slot |
| 385 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getSlotInfo(&slotInfo) == CKR_OK); |
| 386 |
|
| 387 |
CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT); |
| 388 |
|
| 389 |
// Retrieve token information about the token in the first slot |
| 390 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken() != NULL); |
| 391 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 392 |
|
| 393 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED); |
| 394 |
CPPUNIT_ASSERT(!memcmp(tokenInfo.label, label, 32)); |
| 395 |
} |
| 396 |
|
| 397 |
void SlotManagerTests::testUninitialisedToken() |
| 398 |
{ |
| 399 |
// Create an empty object store |
| 400 |
#ifndef _WIN32 |
| 401 |
ObjectStore store("./testdir"); |
| 402 |
#else |
| 403 |
ObjectStore store(".\\testdir"); |
| 404 |
#endif |
| 405 |
|
| 406 |
// Now attach the slot manager |
| 407 |
SlotManager slotManager(&store); |
| 408 |
|
| 409 |
CPPUNIT_ASSERT(slotManager.getSlots().size() == 1); |
| 410 |
|
| 411 |
// Test C_GetSlotList |
| 412 |
CK_SLOT_ID testList[10]; |
| 413 |
CK_ULONG ulCount = 10; |
| 414 |
|
| 415 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_FALSE, testList, &ulCount) == CKR_OK); |
| 416 |
CPPUNIT_ASSERT(ulCount == 1); |
| 417 |
ulCount = 10; |
| 418 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, testList, &ulCount) == CKR_OK); |
| 419 |
CPPUNIT_ASSERT(ulCount == 1); |
| 420 |
|
| 421 |
// Initialise the token in the first slot |
| 422 |
ByteString soPIN((unsigned char*)"1234", 4); |
| 423 |
CK_UTF8CHAR label[33] = "My test token "; |
| 424 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->initToken(soPIN, label) == CKR_OK); |
| 425 |
|
| 426 |
// Check if a new slot is added |
| 427 |
ulCount = 10; |
| 428 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_FALSE, testList, &ulCount) == CKR_OK); |
| 429 |
CPPUNIT_ASSERT(ulCount == 1); |
| 430 |
ulCount = 10; |
| 431 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, testList, &ulCount) == CKR_OK); |
| 432 |
CPPUNIT_ASSERT(ulCount == 1); |
| 433 |
ulCount = 10; |
| 434 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_FALSE, NULL_PTR, &ulCount) == CKR_OK); |
| 435 |
CPPUNIT_ASSERT(ulCount == 2); |
| 436 |
ulCount = 10; |
| 437 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, NULL_PTR, &ulCount) == CKR_OK); |
| 438 |
CPPUNIT_ASSERT(ulCount == 2); |
| 439 |
|
| 440 |
// get new list |
| 441 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, testList, &ulCount) == CKR_OK); |
| 442 |
CPPUNIT_ASSERT(ulCount == 2); |
| 443 |
|
| 444 |
// Retrieve token information about the tokens |
| 445 |
CK_TOKEN_INFO tokenInfo; |
| 446 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken() != NULL); |
| 447 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 448 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED); |
| 449 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken() != NULL); |
| 450 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 451 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED); |
| 452 |
|
| 453 |
// Initialise the token in the second slot |
| 454 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->initToken(soPIN, label) == CKR_OK); |
| 455 |
|
| 456 |
// Check if a new slot is added |
| 457 |
ulCount = 10; |
| 458 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_FALSE, testList, &ulCount) == CKR_OK); |
| 459 |
CPPUNIT_ASSERT(ulCount == 2); |
| 460 |
ulCount = 10; |
| 461 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, testList, &ulCount) == CKR_OK); |
| 462 |
CPPUNIT_ASSERT(ulCount == 2); |
| 463 |
ulCount = 10; |
| 464 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_FALSE, NULL_PTR, &ulCount) == CKR_OK); |
| 465 |
CPPUNIT_ASSERT(ulCount == 3); |
| 466 |
ulCount = 10; |
| 467 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, NULL_PTR, &ulCount) == CKR_OK); |
| 468 |
CPPUNIT_ASSERT(ulCount == 3); |
| 469 |
|
| 470 |
// get new list |
| 471 |
CPPUNIT_ASSERT(slotManager.getSlotList(&store, CK_TRUE, testList, &ulCount) == CKR_OK); |
| 472 |
CPPUNIT_ASSERT(ulCount == 3); |
| 473 |
|
| 474 |
// Retrieve token information about the tokens |
| 475 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken() != NULL); |
| 476 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[0]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 477 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED); |
| 478 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken() != NULL); |
| 479 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[1]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 480 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED); |
| 481 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[2]]->getToken() != NULL); |
| 482 |
CPPUNIT_ASSERT(slotManager.getSlots()[testList[2]]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK); |
| 483 |
CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED); |
| 484 |
} |
| 485 |
|