Debian Codegraph @

src/lib/test/ForkTests.cpp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 break;
84 default:
85 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
86 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
87 break;
88 }
89
90 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
91 CPPUNIT_ASSERT(rv == CKR_OK);
92 }
93
94 void ForkTests::testResetOnFork()
95 {
96 CK_RV rv;
97 CK_SLOT_INFO slotInfo;
98 pid_t pid;
99
100 // Just make sure that we finalize any previous failed tests
101 CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
102
103 #ifndef _WIN32
104 setenv("SOFTHSM2_CONF", "./softhsm2-reset-on-fork.conf", 1);
105 #else
106 setenv("SOFTHSM2_CONF", ".\\softhsm2-reset-on-fork.conf", 1);
107 #endif
108
109 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
110 CPPUNIT_ASSERT(rv == CKR_OK);
111
112 pid = fork();
113
114 switch(pid) {
115 case -1:
116 CPPUNIT_FAIL("Fork failed");
117 break;
118 case 0:
119 /* For the child, the token is expected to be reset on fork */
120 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
121 CPPUNIT_ASSERT(rv == CKR_OK);
122 break;
123 default:
124 /* For the parent, the token is expected to be still initialized */
125 rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
126 CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
127 break;
128 }
129
130 rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
131 CPPUNIT_ASSERT(rv == CKR_OK);
132
133 #ifndef _WIN32
134 setenv("SOFTHSM2_CONF", "./softhsm2.conf", 1);
135 #else
136 setenv("SOFTHSM2_CONF", ".\\softhsm2.conf", 1);
137 #endif
138 }