| 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 |
SessionObjectStoreTests.cpp |
| 29 |
|
| 30 |
Contains test cases to test the session object store implementation |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <cppunit/extensions/HelperMacros.h> |
| 36 |
#include <memory> |
| 37 |
#include "SessionObjectStoreTests.h" |
| 38 |
#include "SessionObjectStore.h" |
| 39 |
#include "SessionObject.h" |
| 40 |
#include "OSAttribute.h" |
| 41 |
#include "OSAttributes.h" |
| 42 |
#include "cryptoki.h" |
| 43 |
|
| 44 |
CPPUNIT_TEST_SUITE_REGISTRATION(SessionObjectStoreTests); |
| 45 |
|
| 46 |
void SessionObjectStoreTests::setUp() |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
void SessionObjectStoreTests::tearDown() |
| 51 |
{ |
| 52 |
} |
| 53 |
|
| 54 |
void SessionObjectStoreTests::testCreateDeleteObjects() |
| 55 |
{ |
| 56 |
// Test IDs |
| 57 |
ByteString id[5] = { "112233445566", "AABBCCDDEEFF", "ABABABABABAB", "557788991122", "005500550055" }; |
| 58 |
OSAttribute idAtt[5] = { id[0], id[1], id[2], id[3], id[4] }; |
| 59 |
ByteString label = "AABBCCDDEEFF"; |
| 60 |
ByteString serial = "1234567890"; |
| 61 |
|
| 62 |
// Get access to the session object store |
| 63 |
SessionObjectStore* testStore = new SessionObjectStore(); |
| 64 |
|
| 65 |
// Create 3 objects in the store |
| 66 |
SessionObject* obj1 = testStore->createObject(1, 1); |
| 67 |
CPPUNIT_ASSERT(obj1 != NULL); |
| 68 |
SessionObject* obj2 = testStore->createObject(1, 1); |
| 69 |
CPPUNIT_ASSERT(obj2 != NULL); |
| 70 |
SessionObject* obj3 = testStore->createObject(1, 1); |
| 71 |
CPPUNIT_ASSERT(obj3 != NULL); |
| 72 |
|
| 73 |
// Now set the IDs of the 3 objects |
| 74 |
obj1->setAttribute(CKA_ID, idAtt[0]); |
| 75 |
obj2->setAttribute(CKA_ID, idAtt[1]); |
| 76 |
obj3->setAttribute(CKA_ID, idAtt[2]); |
| 77 |
|
| 78 |
// Check that the store contains 3 objects |
| 79 |
CPPUNIT_ASSERT(testStore->getObjectCount() == 3); |
| 80 |
|
| 81 |
// Check that all three objects are distinct and present |
| 82 |
std::set<OSObject*> objects; |
| 83 |
testStore->getObjects(objects); |
| 84 |
bool present1[3] = { false, false, false }; |
| 85 |
|
| 86 |
for (std::set<OSObject*>::iterator i = objects.begin(); i != objects.end(); i++) |
| 87 |
{ |
| 88 |
ByteString retrievedId; |
| 89 |
|
| 90 |
CPPUNIT_ASSERT((*i)->isValid()); |
| 91 |
CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID)); |
| 92 |
|
| 93 |
CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute()); |
| 94 |
|
| 95 |
for (int j = 0; j < 3; j++) |
| 96 |
{ |
| 97 |
if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[j]) |
| 98 |
{ |
| 99 |
present1[j] = true; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
for (int j = 0; j < 3; j++) |
| 105 |
{ |
| 106 |
CPPUNIT_ASSERT(present1[j] == true); |
| 107 |
} |
| 108 |
|
| 109 |
// Now delete the second object |
| 110 |
for (std::set<OSObject*>::iterator i = objects.begin(); i != objects.end(); i++) |
| 111 |
{ |
| 112 |
CPPUNIT_ASSERT((*i)->isValid()); |
| 113 |
CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID)); |
| 114 |
|
| 115 |
CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute()); |
| 116 |
|
| 117 |
if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[1]) |
| 118 |
{ |
| 119 |
CPPUNIT_ASSERT(testStore->deleteObject(static_cast<SessionObject*>(*i))); |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// Verify that it was indeed removed |
| 125 |
CPPUNIT_ASSERT(testStore->getObjectCount() == 2); |
| 126 |
|
| 127 |
objects.clear(); |
| 128 |
testStore->getObjects(objects); |
| 129 |
bool present3[2] = { false, false }; |
| 130 |
|
| 131 |
for (std::set<OSObject*>::iterator i = objects.begin(); i != objects.end(); i++) |
| 132 |
{ |
| 133 |
CPPUNIT_ASSERT((*i)->isValid()); |
| 134 |
CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID)); |
| 135 |
|
| 136 |
CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute()); |
| 137 |
|
| 138 |
if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[0]) |
| 139 |
{ |
| 140 |
present3[0] = true; |
| 141 |
} |
| 142 |
if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[2]) |
| 143 |
{ |
| 144 |
present3[1] = true; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
for (int j = 0; j < 2; j++) |
| 149 |
{ |
| 150 |
CPPUNIT_ASSERT(present3[j] == true); |
| 151 |
} |
| 152 |
|
| 153 |
delete testStore; |
| 154 |
} |
| 155 |
|
| 156 |
void SessionObjectStoreTests::testMultiSession() |
| 157 |
{ |
| 158 |
// Get access to the store |
| 159 |
SessionObjectStore* store = new SessionObjectStore(); |
| 160 |
|
| 161 |
// Check that the store is empty |
| 162 |
CPPUNIT_ASSERT(store->getObjectCount() == 0); |
| 163 |
|
| 164 |
// Test IDs |
| 165 |
ByteString id[5] = { "112233445566", "AABBCCDDEEFF", "ABABABABABAB", "557788991122", "005500550055" }; |
| 166 |
OSAttribute idAtt[5] = { id[0], id[1], id[2], id[3], id[4] }; |
| 167 |
|
| 168 |
// Create 3 objects in the store for three different sessions |
| 169 |
SessionObject* obj1 = store->createObject(1, 1); |
| 170 |
CPPUNIT_ASSERT(obj1 != NULL); |
| 171 |
SessionObject* obj2 = store->createObject(1, 2); |
| 172 |
CPPUNIT_ASSERT(obj2 != NULL); |
| 173 |
SessionObject* obj3 = store->createObject(1, 3); |
| 174 |
CPPUNIT_ASSERT(obj3 != NULL); |
| 175 |
|
| 176 |
// Now set the IDs of the 3 objects |
| 177 |
obj1->setAttribute(CKA_ID, idAtt[0]); |
| 178 |
obj2->setAttribute(CKA_ID, idAtt[1]); |
| 179 |
obj3->setAttribute(CKA_ID, idAtt[2]); |
| 180 |
|
| 181 |
// Check that the store contains 3 objects |
| 182 |
CPPUNIT_ASSERT(store->getObjectCount() == 3); |
| 183 |
|
| 184 |
// Check that all three objects are distinct and present |
| 185 |
std::set<OSObject*> objects; |
| 186 |
store->getObjects(objects); |
| 187 |
bool present1[3] = { false, false, false }; |
| 188 |
|
| 189 |
for (std::set<OSObject*>::iterator i = objects.begin(); i != objects.end(); i++) |
| 190 |
{ |
| 191 |
ByteString retrievedId; |
| 192 |
|
| 193 |
CPPUNIT_ASSERT((*i)->isValid()); |
| 194 |
CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID)); |
| 195 |
|
| 196 |
CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute()); |
| 197 |
|
| 198 |
for (int j = 0; j < 3; j++) |
| 199 |
{ |
| 200 |
if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[j]) |
| 201 |
{ |
| 202 |
present1[j] = true; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
for (int j = 0; j < 3; j++) |
| 208 |
{ |
| 209 |
CPPUNIT_ASSERT(present1[j] == true); |
| 210 |
} |
| 211 |
|
| 212 |
// Now indicate that the second session has been closed |
| 213 |
store->sessionClosed(2); |
| 214 |
|
| 215 |
// Verify that it was indeed removed |
| 216 |
CPPUNIT_ASSERT(store->getObjectCount() == 2); |
| 217 |
|
| 218 |
objects.clear(); |
| 219 |
store->getObjects(objects); |
| 220 |
bool present3[2] = { false, false }; |
| 221 |
|
| 222 |
for (std::set<OSObject*>::iterator i = objects.begin(); i != objects.end(); i++) |
| 223 |
{ |
| 224 |
ByteString retrievedId; |
| 225 |
|
| 226 |
CPPUNIT_ASSERT((*i)->isValid()); |
| 227 |
CPPUNIT_ASSERT((*i)->attributeExists(CKA_ID)); |
| 228 |
|
| 229 |
CPPUNIT_ASSERT((*i)->getAttribute(CKA_ID).isByteStringAttribute()); |
| 230 |
|
| 231 |
if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[0]) |
| 232 |
{ |
| 233 |
present3[0] = true; |
| 234 |
} |
| 235 |
if ((*i)->getAttribute(CKA_ID).getByteStringValue() == id[2]) |
| 236 |
{ |
| 237 |
present3[1] = true; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
for (int j = 0; j < 2; j++) |
| 242 |
{ |
| 243 |
CPPUNIT_ASSERT(present3[j] == true); |
| 244 |
} |
| 245 |
|
| 246 |
// Create two more objects for session 7 |
| 247 |
SessionObject* obj4 = store->createObject(1, 7); |
| 248 |
CPPUNIT_ASSERT(obj4 != NULL); |
| 249 |
SessionObject* obj5 = store->createObject(1, 7); |
| 250 |
CPPUNIT_ASSERT(obj5 != NULL); |
| 251 |
|
| 252 |
CPPUNIT_ASSERT(store->getObjectCount() == 4); |
| 253 |
|
| 254 |
// Close session 1 |
| 255 |
store->sessionClosed(1); |
| 256 |
|
| 257 |
CPPUNIT_ASSERT(store->getObjectCount() == 3); |
| 258 |
|
| 259 |
objects.clear(); |
| 260 |
store->getObjects(objects); |
| 261 |
|
| 262 |
CPPUNIT_ASSERT(objects.find(obj1) == objects.end()); |
| 263 |
CPPUNIT_ASSERT(objects.find(obj2) == objects.end()); |
| 264 |
CPPUNIT_ASSERT(objects.find(obj3) != objects.end()); |
| 265 |
CPPUNIT_ASSERT(objects.find(obj4) != objects.end()); |
| 266 |
CPPUNIT_ASSERT(objects.find(obj5) != objects.end()); |
| 267 |
|
| 268 |
CPPUNIT_ASSERT(!obj1->isValid()); |
| 269 |
CPPUNIT_ASSERT(!obj2->isValid()); |
| 270 |
CPPUNIT_ASSERT(obj3->isValid()); |
| 271 |
CPPUNIT_ASSERT(obj4->isValid()); |
| 272 |
CPPUNIT_ASSERT(obj5->isValid()); |
| 273 |
|
| 274 |
// Close session 7 |
| 275 |
store->sessionClosed(7); |
| 276 |
|
| 277 |
CPPUNIT_ASSERT(store->getObjectCount() == 1); |
| 278 |
|
| 279 |
objects.clear(); |
| 280 |
store->getObjects(objects); |
| 281 |
|
| 282 |
CPPUNIT_ASSERT(objects.find(obj1) == objects.end()); |
| 283 |
CPPUNIT_ASSERT(objects.find(obj2) == objects.end()); |
| 284 |
CPPUNIT_ASSERT(objects.find(obj3) != objects.end()); |
| 285 |
CPPUNIT_ASSERT(objects.find(obj4) == objects.end()); |
| 286 |
CPPUNIT_ASSERT(objects.find(obj5) == objects.end()); |
| 287 |
|
| 288 |
CPPUNIT_ASSERT(!obj1->isValid()); |
| 289 |
CPPUNIT_ASSERT(!obj2->isValid()); |
| 290 |
CPPUNIT_ASSERT(obj3->isValid()); |
| 291 |
CPPUNIT_ASSERT(!obj4->isValid()); |
| 292 |
CPPUNIT_ASSERT(!obj5->isValid()); |
| 293 |
|
| 294 |
delete store; |
| 295 |
} |
| 296 |
|
| 297 |
void SessionObjectStoreTests::testWipeStore() |
| 298 |
{ |
| 299 |
// Get access to the store |
| 300 |
SessionObjectStore* store = new SessionObjectStore(); |
| 301 |
|
| 302 |
// Check that the store is empty |
| 303 |
CPPUNIT_ASSERT(store->getObjectCount() == 0); |
| 304 |
|
| 305 |
// Create 3 objects in the store for three different sessions |
| 306 |
SessionObject* obj1 = store->createObject(1, 1); |
| 307 |
CPPUNIT_ASSERT(obj1 != NULL); |
| 308 |
SessionObject* obj2 = store->createObject(1, 2); |
| 309 |
CPPUNIT_ASSERT(obj2 != NULL); |
| 310 |
SessionObject* obj3 = store->createObject(1, 3); |
| 311 |
CPPUNIT_ASSERT(obj3 != NULL); |
| 312 |
|
| 313 |
// Wipe the store |
| 314 |
store->clearStore(); |
| 315 |
|
| 316 |
// Check that the store is empty |
| 317 |
CPPUNIT_ASSERT(store->getObjectCount() == 0); |
| 318 |
|
| 319 |
delete store; |
| 320 |
} |
| 321 |
|