| 1 |
/* |
| 2 |
* Copyright (c) 2012 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 |
HandleManagerTests.cpp |
| 29 |
|
| 30 |
Contains test cases to test the handle manager implementation |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <cppunit/extensions/HelperMacros.h> |
| 36 |
#include "HandleManagerTests.h" |
| 37 |
|
| 38 |
CPPUNIT_TEST_SUITE_REGISTRATION(HandleManagerTests); |
| 39 |
|
| 40 |
void HandleManagerTests::setUp() |
| 41 |
{ |
| 42 |
handleManager = new HandleManager(); |
| 43 |
} |
| 44 |
|
| 45 |
void HandleManagerTests::tearDown() |
| 46 |
{ |
| 47 |
delete handleManager; |
| 48 |
} |
| 49 |
|
| 50 |
void HandleManagerTests::testHandleManager() |
| 51 |
{ |
| 52 |
CPPUNIT_ASSERT(handleManager != NULL); |
| 53 |
|
| 54 |
CK_SLOT_ID slotID = 1234; // we need a unique value |
| 55 |
CK_SESSION_HANDLE hSession; |
| 56 |
CK_VOID_PTR session = &hSession; // we need a unique value |
| 57 |
CK_SESSION_HANDLE hSession2; |
| 58 |
CK_VOID_PTR session2 = &hSession2; // we need a unique value |
| 59 |
CK_OBJECT_HANDLE hObject; |
| 60 |
CK_VOID_PTR object = &hObject; // we need a unique value |
| 61 |
CK_OBJECT_HANDLE hObject2; |
| 62 |
CK_VOID_PTR object2 = &hObject2; // we need a unique value |
| 63 |
CK_OBJECT_HANDLE hObject3; |
| 64 |
CK_VOID_PTR object3 = &hObject3; // we need a unique value |
| 65 |
CK_OBJECT_HANDLE hObject4; |
| 66 |
CK_VOID_PTR object4 = &hObject4; // we need a unique value |
| 67 |
CK_OBJECT_HANDLE hObject5; |
| 68 |
CK_VOID_PTR object5 = &hObject5; // we need a unique value |
| 69 |
|
| 70 |
// Check session object management. |
| 71 |
hSession = handleManager->addSession(slotID, session); |
| 72 |
CPPUNIT_ASSERT(hSession != CK_INVALID_HANDLE); |
| 73 |
CPPUNIT_ASSERT(session == handleManager->getSession(hSession)); |
| 74 |
CPPUNIT_ASSERT_NO_THROW(handleManager->sessionClosed(123124)); |
| 75 |
handleManager->sessionClosed(hSession); |
| 76 |
CPPUNIT_ASSERT(NULL == handleManager->getSession(hSession)); |
| 77 |
|
| 78 |
// Add an object, hSession doesn't have to exists |
| 79 |
hObject = handleManager->addSessionObject(slotID, 4412412, true, object); |
| 80 |
CPPUNIT_ASSERT(hObject != CK_INVALID_HANDLE); |
| 81 |
CPPUNIT_ASSERT(object == handleManager->getObject(hObject)); |
| 82 |
handleManager->sessionClosed(4412412); |
| 83 |
// Object still exists as the hSession was invalid |
| 84 |
CPPUNIT_ASSERT(object == handleManager->getObject(hObject)); |
| 85 |
handleManager->allSessionsClosed(slotID); |
| 86 |
// Object is now gone as all sessions for the given slotID have been removed. |
| 87 |
CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject)); |
| 88 |
|
| 89 |
// Add an object and then destroy it. |
| 90 |
hObject = handleManager->addSessionObject(slotID, 4412412, true, object); |
| 91 |
CPPUNIT_ASSERT(hObject != CK_INVALID_HANDLE); |
| 92 |
handleManager->destroyObject(hObject); |
| 93 |
CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject)); |
| 94 |
|
| 95 |
hObject = handleManager->addTokenObject(slotID, false, object); |
| 96 |
CPPUNIT_ASSERT(hObject != CK_INVALID_HANDLE); |
| 97 |
handleManager->destroyObject(hObject); |
| 98 |
CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject)); |
| 99 |
|
| 100 |
// Create a valid session again |
| 101 |
hSession = handleManager->addSession(slotID, session); |
| 102 |
CPPUNIT_ASSERT(hSession != CK_INVALID_HANDLE); |
| 103 |
CPPUNIT_ASSERT(session == handleManager->getSession(hSession)); |
| 104 |
|
| 105 |
// Now some magic with a couple of objects |
| 106 |
// First add a public object |
| 107 |
hObject = handleManager->addTokenObject(slotID, false, object); |
| 108 |
CPPUNIT_ASSERT(hObject != CK_INVALID_HANDLE); |
| 109 |
CPPUNIT_ASSERT(object == handleManager->getObject(hObject)); |
| 110 |
|
| 111 |
// Now add a private object |
| 112 |
hObject2 = handleManager->addTokenObject(slotID, true, object2); |
| 113 |
CPPUNIT_ASSERT(hObject2 != CK_INVALID_HANDLE); |
| 114 |
CPPUNIT_ASSERT(object2 == handleManager->getObject(hObject2)); |
| 115 |
|
| 116 |
// Now add another private object |
| 117 |
hObject3 = handleManager->addTokenObject(slotID, true, object3); |
| 118 |
CPPUNIT_ASSERT(hObject3 != CK_INVALID_HANDLE); |
| 119 |
CPPUNIT_ASSERT(object3 == handleManager->getObject(hObject3)); |
| 120 |
|
| 121 |
// Adding the same object will return the same handle whether the object is marked private or public. |
| 122 |
CPPUNIT_ASSERT(hObject2 == handleManager->addTokenObject(slotID, true, object2)); |
| 123 |
// Because the private state of an object cannot be changed it won't be marked as public, it remains private |
| 124 |
CPPUNIT_ASSERT(hObject2 == handleManager->addTokenObject(slotID, false, object2)); |
| 125 |
|
| 126 |
// It is not allowed to migrate an object from one slot to another, so here we return an invalid handle. |
| 127 |
CPPUNIT_ASSERT(CK_INVALID_HANDLE == handleManager->addTokenObject(124121, false, object2)); |
| 128 |
|
| 129 |
// Now add another private session object |
| 130 |
hObject4 = handleManager->addSessionObject(slotID, hSession, true, object4); |
| 131 |
CPPUNIT_ASSERT(hObject4 != CK_INVALID_HANDLE); |
| 132 |
CPPUNIT_ASSERT(object4 == handleManager->getObject(hObject4)); |
| 133 |
|
| 134 |
// Now add another public session object |
| 135 |
hObject5 = handleManager->addSessionObject(slotID, hSession, false, object5); |
| 136 |
CPPUNIT_ASSERT(hObject5 != CK_INVALID_HANDLE); |
| 137 |
CPPUNIT_ASSERT(object5 == handleManager->getObject(hObject5)); |
| 138 |
|
| 139 |
// Logout, now private objects should be gone. |
| 140 |
handleManager->tokenLoggedOut(slotID); |
| 141 |
CPPUNIT_ASSERT(object == handleManager->getObject(hObject)); |
| 142 |
CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject2)); // should still be private and removed. |
| 143 |
CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject3)); |
| 144 |
CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject4)); |
| 145 |
CPPUNIT_ASSERT(object5 == handleManager->getObject(hObject5)); |
| 146 |
|
| 147 |
// Create another valid session for the slot |
| 148 |
hSession2 = handleManager->addSession(slotID, session2); |
| 149 |
CPPUNIT_ASSERT(hSession2 != CK_INVALID_HANDLE); |
| 150 |
CPPUNIT_ASSERT(session2 == handleManager->getSession(hSession2)); |
| 151 |
|
| 152 |
handleManager->sessionClosed(hSession); |
| 153 |
CPPUNIT_ASSERT(object == handleManager->getObject(hObject)); // token object should still be there. |
| 154 |
CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject5)); // session object should be gone. |
| 155 |
|
| 156 |
// Removing the last remaining session should kill the remaining handle. |
| 157 |
handleManager->sessionClosed(hSession2); |
| 158 |
CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject)); // should be gone now. |
| 159 |
|
| 160 |
CPPUNIT_ASSERT(NULL == handleManager->getSession(hSession)); |
| 161 |
CPPUNIT_ASSERT(NULL == handleManager->getSession(hSession2)); |
| 162 |
|
| 163 |
|
| 164 |
// Create a valid session again |
| 165 |
hSession = handleManager->addSession(slotID, session); |
| 166 |
CPPUNIT_ASSERT(hSession != CK_INVALID_HANDLE); |
| 167 |
CPPUNIT_ASSERT(session == handleManager->getSession(hSession)); |
| 168 |
|
| 169 |
// Create another valid session for the slot |
| 170 |
hSession2 = handleManager->addSession(slotID, session2); |
| 171 |
CPPUNIT_ASSERT(hSession2 != CK_INVALID_HANDLE); |
| 172 |
CPPUNIT_ASSERT(session2 == handleManager->getSession(hSession2)); |
| 173 |
|
| 174 |
handleManager->allSessionsClosed(slotID); |
| 175 |
|
| 176 |
CPPUNIT_ASSERT(NULL == handleManager->getSession(hSession)); |
| 177 |
CPPUNIT_ASSERT(NULL == handleManager->getSession(hSession2)); |
| 178 |
} |