aboutsummaryrefslogtreecommitdiffstats
path: root/SoftHSMv2/src/lib/handle_mgr/test/HandleManagerTests.cpp
blob: fbf040363cc31a81e3134d4e30db1deed8460188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (c) 2012 SURFnet bv
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*****************************************************************************
 HandleManagerTests.cpp

 Contains test cases to test the handle manager implementation
 *****************************************************************************/

#include <stdlib.h>
#include <string.h>
#include <cppunit/extensions/HelperMacros.h>
#include "HandleManagerTests.h"

CPPUNIT_TEST_SUITE_REGISTRATION(HandleManagerTests);

void HandleManagerTests::setUp()
{
	handleManager = new HandleManager();
}

void HandleManagerTests::tearDown()
{
	delete handleManager;
}

void HandleManagerTests::testHandleManager()
{
	CPPUNIT_ASSERT(handleManager != NULL);

	CK_SLOT_ID slotID = 1234; // we need a unique value
	CK_SESSION_HANDLE hSession;
	CK_VOID_PTR session = &hSession; // we need a unique value
	CK_SESSION_HANDLE hSession2;
	CK_VOID_PTR session2 = &hSession2; // we need a unique value
	CK_OBJECT_HANDLE hObject;
	CK_VOID_PTR object = &hObject; // we need a unique value
	CK_OBJECT_HANDLE hObject2;
	CK_VOID_PTR object2 = &hObject2; // we need a unique value
	CK_OBJECT_HANDLE hObject3;
	CK_VOID_PTR object3 = &hObject3; // we need a unique value
	CK_OBJECT_HANDLE hObject4;
	CK_VOID_PTR object4 = &hObject4; // we need a unique value
	CK_OBJECT_HANDLE hObject5;
	CK_VOID_PTR object5 = &hObject5; // we need a unique value

	// Check session object management.
	hSession = handleManager->addSession(slotID, session);
	CPPUNIT_ASSERT(hSession != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(session == handleManager->getSession(hSession));
	CPPUNIT_ASSERT_NO_THROW(handleManager->sessionClosed(123124));
	handleManager->sessionClosed(hSession);
	CPPUNIT_ASSERT(NULL == handleManager->getSession(hSession));

	// Add an object, hSession doesn't have to exists
	hObject = handleManager->addSessionObject(slotID, 4412412, true, object);
	CPPUNIT_ASSERT(hObject != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(object == handleManager->getObject(hObject));
	handleManager->sessionClosed(4412412);
	// Object still exists as the hSession was invalid
	CPPUNIT_ASSERT(object == handleManager->getObject(hObject));
	handleManager->allSessionsClosed(slotID);
	// Object is now gone as all sessions for the given slotID have been removed.
	CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject));

	// Add an object and then destroy it.
	hObject = handleManager->addSessionObject(slotID, 4412412, true, object);
	CPPUNIT_ASSERT(hObject != CK_INVALID_HANDLE);
	handleManager->destroyObject(hObject);
	CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject));

	hObject = handleManager->addTokenObject(slotID, false, object);
	CPPUNIT_ASSERT(hObject != CK_INVALID_HANDLE);
	handleManager->destroyObject(hObject);
	CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject));

	// Create a valid session again
	hSession = handleManager->addSession(slotID, session);
	CPPUNIT_ASSERT(hSession != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(session == handleManager->getSession(hSession));

	// Now some magic with a couple of objects
	// First add a public object
	hObject = handleManager->addTokenObject(slotID, false, object);
	CPPUNIT_ASSERT(hObject != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(object == handleManager->getObject(hObject));

	// Now add a private object
	hObject2 = handleManager->addTokenObject(slotID, true, object2);
	CPPUNIT_ASSERT(hObject2 != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(object2 == handleManager->getObject(hObject2));

	// Now add another private object
	hObject3 = handleManager->addTokenObject(slotID, true, object3);
	CPPUNIT_ASSERT(hObject3 != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(object3 == handleManager->getObject(hObject3));

	// Adding the same object will return the same handle whether the object is marked private or public.
	CPPUNIT_ASSERT(hObject2 == handleManager->addTokenObject(slotID, true, object2));
	// Because the private state of an object cannot be changed it won't be marked as public, it remains private
	CPPUNIT_ASSERT(hObject2 == handleManager->addTokenObject(slotID, false, object2));

	// It is not allowed to migrate an object from one slot to another, so here we return an invalid handle.
	CPPUNIT_ASSERT(CK_INVALID_HANDLE == handleManager->addTokenObject(124121, false, object2));

	// Now add another private session object
	hObject4 = handleManager->addSessionObject(slotID, hSession, true, object4);
	CPPUNIT_ASSERT(hObject4 != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(object4 == handleManager->getObject(hObject4));

	// Now add another public session object
	hObject5 = handleManager->addSessionObject(slotID, hSession, false, object5);
	CPPUNIT_ASSERT(hObject5 != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(object5 == handleManager->getObject(hObject5));

	// Logout, now private objects should be gone.
	handleManager->tokenLoggedOut(slotID);
	CPPUNIT_ASSERT(object == handleManager->getObject(hObject));
	CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject2)); // should still be private and removed.
	CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject3));
	CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject4));
	CPPUNIT_ASSERT(object5 == handleManager->getObject(hObject5));

	// Create another valid session for the slot
	hSession2 = handleManager->addSession(slotID, session2);
	CPPUNIT_ASSERT(hSession2 != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(session2 == handleManager->getSession(hSession2));

	handleManager->sessionClosed(hSession);
	CPPUNIT_ASSERT(object == handleManager->getObject(hObject)); // token object should still be there.
	CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject5)); // session object should be gone.

	// Removing the last remaining session should kill the remaining handle.
	handleManager->sessionClosed(hSession2);
	CPPUNIT_ASSERT(NULL == handleManager->getObject(hObject)); // should be gone now.

	CPPUNIT_ASSERT(NULL == handleManager->getSession(hSession));
	CPPUNIT_ASSERT(NULL == handleManager->getSession(hSession2));


	// Create a valid session again
	hSession = handleManager->addSession(slotID, session);
	CPPUNIT_ASSERT(hSession != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(session == handleManager->getSession(hSession));

	// Create another valid session for the slot
	hSession2 = handleManager->addSession(slotID, session2);
	CPPUNIT_ASSERT(hSession2 != CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(session2 == handleManager->getSession(hSession2));

	handleManager->allSessionsClosed(slotID);

	CPPUNIT_ASSERT(NULL == handleManager->getSession(hSession));
	CPPUNIT_ASSERT(NULL == handleManager->getSession(hSession2));
}