Debian Codegraph @

src/lib/session_mgr/test/SessionManagerTests.cpp

raw
1 /*
2 * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation)
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 SessionManagerTests.cpp
29
30 Contains test cases for SessionManager
31 *****************************************************************************/
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <cppunit/extensions/HelperMacros.h>
36 #include "SessionManagerTests.h"
37 #include "ObjectStore.h"
38 #include "SessionManager.h"
39 #include "Session.h"
40 #include "SlotManager.h"
41 #include "cryptoki.h"
42
43 CPPUNIT_TEST_SUITE_REGISTRATION(SessionManagerTests);
44
45 void SessionManagerTests::setUp()
46 {
47 CPPUNIT_ASSERT(!system("mkdir testdir"));
48 }
49
50 void SessionManagerTests::tearDown()
51 {
52 #ifndef _WIN32
53 CPPUNIT_ASSERT(!system("rm -rf testdir"));
54 #else
55 CPPUNIT_ASSERT(!system("rmdir /s /q testdir 2> nul"));
56 #endif
57 }
58
59 void SessionManagerTests::testOpenClose()
60 {
61 // Create an empty object store
62 #ifndef _WIN32
63 ObjectStore store("./testdir");
64 #else
65 ObjectStore store(".\\testdir");
66 #endif
67
68 // Create the managers
69 SlotManager slotManager(&store);
70 SessionManager sessionManager;
71
72 // Get a slot
73 CK_SLOT_ID slotID = 0;
74 Slot* slot = slotManager.getSlot(slotID);
75
76 // Use some bad data
77 CK_SESSION_HANDLE hSession;
78 CK_RV rv = sessionManager.openSession(NULL, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
79 CPPUNIT_ASSERT(rv == CKR_SLOT_ID_INVALID);
80 rv = sessionManager.openSession(slot, 0, NULL_PTR, NULL_PTR, &hSession);
81 CPPUNIT_ASSERT(rv == CKR_SESSION_PARALLEL_NOT_SUPPORTED);
82 rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, NULL_PTR);
83 CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);
84
85 // Try open a slot with an uninitialized token
86 rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
87 CPPUNIT_ASSERT(rv == CKR_TOKEN_NOT_RECOGNIZED);
88
89 // Initialize the token
90 ByteString soPIN((unsigned char*)"1234", 4);
91 CK_UTF8CHAR label[33] = "My test token ";
92 CPPUNIT_ASSERT(slot->initToken(soPIN, label) == CKR_OK);
93
94 // Open a session
95 bool haveSession = sessionManager.haveSession(slotID);
96 CPPUNIT_ASSERT(haveSession == false);
97 bool haveROSession = sessionManager.haveROSession(slotID);
98 CPPUNIT_ASSERT(haveROSession == false);
99 rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
100 CPPUNIT_ASSERT(rv == CKR_OK);
101 haveSession = sessionManager.haveSession(slotID);
102 CPPUNIT_ASSERT(haveSession == true);
103 haveROSession = sessionManager.haveROSession(slotID);
104 CPPUNIT_ASSERT(haveROSession == true);
105
106 // Close session
107 rv = sessionManager.closeSession(CK_INVALID_HANDLE);
108 CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID);
109 rv = sessionManager.closeSession(hSession);
110 CPPUNIT_ASSERT(rv == CKR_OK);
111 rv = sessionManager.closeSession(hSession);
112 CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID);
113 haveSession = sessionManager.haveSession(slotID);
114 CPPUNIT_ASSERT(haveSession == false);
115 haveROSession = sessionManager.haveROSession(slotID);
116 CPPUNIT_ASSERT(haveROSession == false);
117
118 // Try open a Read-Only session when in SO mode
119 rv = slot->getToken()->loginSO(soPIN);
120 CPPUNIT_ASSERT(rv == CKR_OK);
121 rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
122 CPPUNIT_ASSERT(rv == CKR_SESSION_READ_WRITE_SO_EXISTS);
123 rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession);
124 CPPUNIT_ASSERT(rv == CKR_OK);
125 haveSession = sessionManager.haveSession(slotID);
126 CPPUNIT_ASSERT(haveSession == true);
127 haveROSession = sessionManager.haveROSession(slotID);
128 CPPUNIT_ASSERT(haveROSession == false);
129
130 // Close session and check that we are logged out
131 bool isLoggedIn = slot->getToken()->isSOLoggedIn();
132 CPPUNIT_ASSERT(isLoggedIn == true);
133 rv = sessionManager.closeSession(hSession);
134 CPPUNIT_ASSERT(rv == CKR_OK);
135 isLoggedIn = slot->getToken()->isSOLoggedIn();
136 CPPUNIT_ASSERT(isLoggedIn == false);
137 haveSession = sessionManager.haveSession(slotID);
138 CPPUNIT_ASSERT(haveSession == false);
139 haveROSession = sessionManager.haveROSession(slotID);
140 CPPUNIT_ASSERT(haveROSession == false);
141
142 // Open a new logged in session
143 rv = slot->getToken()->loginSO(soPIN);
144 CPPUNIT_ASSERT(rv == CKR_OK);
145 rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession);
146 CPPUNIT_ASSERT(rv == CKR_OK);
147
148 // Close all sessions and check that we are logged out
149 isLoggedIn = slot->getToken()->isSOLoggedIn();
150 CPPUNIT_ASSERT(isLoggedIn == true);
151 rv = sessionManager.closeAllSessions(NULL);
152 CPPUNIT_ASSERT(rv == CKR_SLOT_ID_INVALID);
153 rv = sessionManager.closeAllSessions(slot);
154 CPPUNIT_ASSERT(rv == CKR_OK);
155 isLoggedIn = slot->getToken()->isSOLoggedIn();
156 CPPUNIT_ASSERT(isLoggedIn == false);
157 }
158
159 void SessionManagerTests::testSessionInfo()
160 {
161 // Create an empty object store
162 #ifndef _WIN32
163 ObjectStore store("./testdir");
164 #else
165 ObjectStore store(".\\testdir");
166 #endif
167
168 // Create the managers
169 SlotManager slotManager(&store);
170 SessionManager sessionManager;
171
172 // Get a slot
173 CK_SLOT_ID slotID = 0;
174 Slot* slot = slotManager.getSlot(slotID);
175
176 // Initialize the token
177 ByteString soPIN((unsigned char*)"1234", 4);
178 ByteString userPIN((unsigned char*)"1234", 4);
179 CK_UTF8CHAR label[33] = "My test token ";
180 CPPUNIT_ASSERT(slot->initToken(soPIN, label) == CKR_OK);
181 CPPUNIT_ASSERT(slot->getToken()->loginSO(soPIN) == CKR_OK);
182 CPPUNIT_ASSERT(slot->getToken()->initUserPIN(userPIN) == CKR_OK);
183 slot->getToken()->logout();
184
185 // Get a session
186 CK_SESSION_HANDLE hSession;
187 CK_RV rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
188 CPPUNIT_ASSERT(rv == CKR_OK);
189
190 // Get session info
191 CK_SESSION_INFO info;
192 rv = sessionManager.getSessionInfo(CK_INVALID_HANDLE, &info);
193 CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID);
194 rv = sessionManager.getSessionInfo(hSession, NULL_PTR);
195 CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);
196 rv = sessionManager.getSessionInfo(hSession, &info);
197 CPPUNIT_ASSERT(rv == CKR_OK);
198
199 // Public RO session info
200 CPPUNIT_ASSERT(info.slotID == slotID);
201 CPPUNIT_ASSERT(info.state == CKS_RO_PUBLIC_SESSION);
202 CPPUNIT_ASSERT(info.flags == CKF_SERIAL_SESSION);
203
204 rv = sessionManager.closeSession(hSession);
205 CPPUNIT_ASSERT(rv == CKR_OK);
206
207 // Public RW session info
208 rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession);
209 CPPUNIT_ASSERT(rv == CKR_OK);
210 Session* session = sessionManager.getSession(CK_INVALID_HANDLE);
211 CPPUNIT_ASSERT(session == NULL);
212 session = sessionManager.getSession(hSession);
213 CPPUNIT_ASSERT(session != NULL);
214 rv = session->getInfo(&info);
215 CPPUNIT_ASSERT(rv == CKR_OK);
216 CPPUNIT_ASSERT(info.state == CKS_RW_PUBLIC_SESSION);
217 CPPUNIT_ASSERT(info.flags == (CKF_SERIAL_SESSION | CKF_RW_SESSION));
218
219 rv = sessionManager.closeSession(hSession);
220 CPPUNIT_ASSERT(rv == CKR_OK);
221
222 // User RO session info
223 rv = slot->getToken()->loginUser(userPIN);
224 CPPUNIT_ASSERT(rv == CKR_OK);
225 rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
226 CPPUNIT_ASSERT(rv == CKR_OK);
227 rv = sessionManager.getSessionInfo(hSession, &info);
228 CPPUNIT_ASSERT(rv == CKR_OK);
229 CPPUNIT_ASSERT(info.state == CKS_RO_USER_FUNCTIONS);
230 CPPUNIT_ASSERT(info.flags == CKF_SERIAL_SESSION);
231
232 rv = sessionManager.closeSession(hSession);
233 CPPUNIT_ASSERT(rv == CKR_OK);
234
235 // User RW session info
236 rv = slot->getToken()->loginUser(userPIN);
237 CPPUNIT_ASSERT(rv == CKR_OK);
238 rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession);
239 CPPUNIT_ASSERT(rv == CKR_OK);
240 rv = sessionManager.getSessionInfo(hSession, &info);
241 CPPUNIT_ASSERT(rv == CKR_OK);
242 CPPUNIT_ASSERT(info.state == CKS_RW_USER_FUNCTIONS);
243 CPPUNIT_ASSERT(info.flags == (CKF_SERIAL_SESSION | CKF_RW_SESSION));
244
245 rv = sessionManager.closeSession(hSession);
246 CPPUNIT_ASSERT(rv == CKR_OK);
247
248 // SO RW session info
249 rv = slot->getToken()->loginSO(soPIN);
250 CPPUNIT_ASSERT(rv == CKR_OK);
251 rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession);
252 CPPUNIT_ASSERT(rv == CKR_OK);
253 rv = sessionManager.getSessionInfo(hSession, &info);
254 CPPUNIT_ASSERT(rv == CKR_OK);
255 CPPUNIT_ASSERT(info.state == CKS_RW_SO_FUNCTIONS);
256 CPPUNIT_ASSERT(info.flags == (CKF_SERIAL_SESSION | CKF_RW_SESSION));
257
258 rv = sessionManager.closeSession(hSession);
259 CPPUNIT_ASSERT(rv == CKR_OK);
260 }