Debian Codegraph @

src/lib/test/ForkTests.cpp

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Copyright (c) 2020 by Red Hat, Inc.
3 *
4 * Author: Anderson Toshiyuki Sasaki <[email protected]>
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*****************************************************************************
31 ForkTests.cpp
32
33 Contains test cases for forking scenarios
34 *****************************************************************************/
35 #include <stdlib.h>
36 #include <string.h>
37 #include <cppunit/extensions/HelperMacros.h>
38 #include "ForkTests.h"
39 #include "cryptoki.h"
40 #include "osmutex.h"
41
42 #include <sys/types.h>
43 #include <unistd.h>
44
45 CPPUNIT_TEST_SUITE_REGISTRATION(ForkTests);
46
47 void ForkTests::setUp()
48 {
49
50 #ifndef _WIN32
51 setenv("SOFTHSM2_CONF", "./softhsm2.conf", 1);
52 #else
53 setenv("SOFTHSM2_CONF", ".\\softhsm2.conf", 1);
54 #endif
55 }
56
57 void ForkTests::tearDown()
58 {
59 // Just make sure that we finalize any previous failed tests
60 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
61 }
62
63 void ForkTests::testFork()
64 {
65 CK_RV rv;
66 pid_t pid;
67
68 // Just make sure that we finalize any previous failed tests
69 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
70
71 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
72 CPPUNIT_ASSERT(rv == CKR_OK);
73
74 pid = fork();
75
76 switch(pid) {
77 case -1:
78 CPPUNIT_FAIL("Fork failed");
79 break;
80 case 0:
81 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
82 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
•83 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
84 CPPUNIT_ASSERT(rv == CKR_OK);
85 _exit(0);
86 break;
87 default:
88 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
89 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
•90 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
91 CPPUNIT_ASSERT(rv == CKR_OK);
92 break;
93 }
94 }
95
96 void ForkTests::testResetOnFork()
97 {
98 CK_RV rv;
99 CK_SLOT_INFO slotInfo;
100 pid_t pid;
101
102 // Just make sure that we finalize any previous failed tests
103 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
104
105 #ifndef _WIN32
106 setenv("SOFTHSM2_CONF", "./softhsm2-reset-on-fork.conf", 1);
107 #else
108 setenv("SOFTHSM2_CONF", ".\\softhsm2-reset-on-fork.conf", 1);
109 #endif
110
111 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
112 CPPUNIT_ASSERT(rv == CKR_OK);
113
114 pid = fork();
115
116 switch(pid) {
117 case -1:
118 CPPUNIT_FAIL("Fork failed");
119 break;
120 case 0:
121 /* For the child, the token is expected to be reset on fork */
122 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
123 CPPUNIT_ASSERT(rv == CKR_OK);
•124 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
125 CPPUNIT_ASSERT(rv == CKR_OK);
126 _exit(0);
127 break;
128 default:
129 /* For the parent, the token is expected to be still initialized */
130 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
131 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
•132 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
133 CPPUNIT_ASSERT(rv == CKR_OK);
134 break;
135 }
136
137
138 #ifndef _WIN32
139 setenv("SOFTHSM2_CONF", "./softhsm2.conf", 1);
140 #else
141 setenv("SOFTHSM2_CONF", ".\\softhsm2.conf", 1);
142 #endif
143 }