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  TokenTests.cpp
29 
30  Contains test cases to C_InitToken
31  *****************************************************************************/
32 
33 #include <config.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "TokenTests.h"
37 
38 CPPUNIT_TEST_SUITE_REGISTRATION(TokenTests);
39 
testInitToken()40 void TokenTests::testInitToken()
41 {
42 	CK_RV rv;
43 	CK_UTF8CHAR label[32];
44 	CK_SESSION_HANDLE hSession;
45 	CK_TOKEN_INFO tokenInfo;
46 	CK_CHAR serialNumber[16];
47 
48 	memset(label, ' ', 32);
49 	memcpy(label, "token1", strlen("token1"));
50 
51 	// Just make sure that we finalize any previous failed tests
52 	CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
53 
54 	rv = CRYPTOKI_F_PTR( C_InitToken(m_initializedTokenSlotID, m_soPin1, m_soPin1Length, label) );
55 	CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED);
56 
57 	rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
58 	CPPUNIT_ASSERT(rv == CKR_OK);
59 
60 	rv = CRYPTOKI_F_PTR( C_InitToken(m_initializedTokenSlotID, NULL_PTR, m_soPin1Length, label) );
61 	CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);
62 
63 	rv = CRYPTOKI_F_PTR( C_InitToken(m_invalidSlotID, m_soPin1, m_soPin1Length, label) );
64 	CPPUNIT_ASSERT(rv == CKR_SLOT_ID_INVALID);
65 
66 	// Initialize
67 	rv = CRYPTOKI_F_PTR( C_InitToken(m_initializedTokenSlotID, m_soPin1, m_soPin1Length, label) );
68 	CPPUNIT_ASSERT(rv == CKR_OK);
69 
70 	// Get token serial
71 	rv = CRYPTOKI_F_PTR( C_GetTokenInfo(m_initializedTokenSlotID, &tokenInfo) );
72 	CPPUNIT_ASSERT(rv == CKR_OK);
73 	memcpy(serialNumber, tokenInfo.serialNumber, 16);
74 
75 	// Initialize with wrong password
76 	rv = CRYPTOKI_F_PTR( C_InitToken(m_initializedTokenSlotID, m_soPin1, m_soPin1Length - 1, label) );
77 	CPPUNIT_ASSERT(rv == CKR_PIN_INCORRECT);
78 
79 	rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession) );
80 	CPPUNIT_ASSERT(rv == CKR_OK);
81 
82 	rv = CRYPTOKI_F_PTR( C_InitToken(m_initializedTokenSlotID, m_soPin1, m_soPin1Length, label) );
83 	CPPUNIT_ASSERT(rv == CKR_SESSION_EXISTS);
84 
85 	rv = CRYPTOKI_F_PTR( C_CloseSession(hSession) );
86 	CPPUNIT_ASSERT(rv == CKR_OK);
87 
88 	// Re-initialize
89 	rv = CRYPTOKI_F_PTR( C_InitToken(m_initializedTokenSlotID, m_soPin1, m_soPin1Length, label) );
90 	CPPUNIT_ASSERT(rv == CKR_OK);
91 
92 	// Compare token serial
93 	rv = CRYPTOKI_F_PTR( C_GetTokenInfo(m_initializedTokenSlotID, &tokenInfo) );
94 	CPPUNIT_ASSERT(rv == CKR_OK);
95 	CPPUNIT_ASSERT(memcmp(serialNumber, tokenInfo.serialNumber, 16) == 0);
96 
97 	CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
98 }
99