Debian Codegraph @

crt/s2n/tests/unit/s2n_locking_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "crypto/s2n_locking.h"
17
18 #include <pthread.h>
19
20 #include "crypto/s2n_openssl.h"
21 #include "s2n_test.h"
22
23 #define LOCK_N 1
24
25 #if !(S2N_OPENSSL_VERSION_AT_LEAST(1, 1, 0))
26
27 static void *s2n_test_thread(void *arg)
28 {
29 bool *lock_was_acquired = (bool *) arg;
30 CRYPTO_lock(CRYPTO_LOCK, LOCK_N, NULL, 0);
31 *lock_was_acquired = true;
32 CRYPTO_lock(CRYPTO_UNLOCK, LOCK_N, NULL, 0);
33 return NULL;
34 }
35
36 static void s2n_test_locking_cb(int mode, int n, char *file, int line)
37 {
38 return;
39 }
40
41 int main(int argc, char **argv)
42 {
43 BEGIN_TEST();
44
45 /* Test: libcrypto locks should actually lock.
46 *
47 * If libcrypto locking is not configured properly, then
48 * libcrypto locks will be no-ops and will not block threads.
49 */
50 {
51 /* Lock one of the libcrypto locks */
52 CRYPTO_lock(CRYPTO_LOCK, LOCK_N, NULL, 0);
53
54 /* Create a new thread which will try to take the lock held by this thread
55 * in order to set a flag.
56 */
57 bool lock_was_acquired = false;
58 pthread_t thread = { 0 };
59 EXPECT_EQUAL(pthread_create(&thread, NULL, s2n_test_thread, &lock_was_acquired), 0);
60
61 /* Expect the flag NOT to be set because we still hold the lock */
62 sleep(1);
63 EXPECT_FALSE(lock_was_acquired);
64
65 /* Release the libcrypto lock */
66 CRYPTO_lock(CRYPTO_UNLOCK, LOCK_N, NULL, 0);
67
68 /* Expect the flag to be set because the new thread took the lock */
69 void *retval = NULL;
70 EXPECT_EQUAL(pthread_join(thread, &retval), 0);
71 EXPECT_TRUE(lock_was_acquired);
72 };
73
74 /* Test: basic lifecycle */
75 {
76 EXPECT_OK(s2n_locking_cleanup());
77 EXPECT_OK(s2n_locking_init());
78 };
79
80 /* Test: s2n-tls should not override locking configured by the application */
81 {
82 /* The callback should have already been set by BEGIN_TEST()
83 * or a later call to s2n_locking_init().
84 */
85 EXPECT_NOT_EQUAL(CRYPTO_get_locking_callback(), NULL);
86
87 /* Manually override the existing callback.
88 * Applications might set their callback after calling s2n_init.
89 */
90 CRYPTO_set_locking_callback((void (*)()) s2n_test_locking_cb);
91 EXPECT_EQUAL(CRYPTO_get_locking_callback(), (void (*)()) s2n_test_locking_cb);
92
93 /* Cleaning up does not affect the application-set callback */
94 EXPECT_OK(s2n_locking_cleanup());
95 EXPECT_EQUAL(CRYPTO_get_locking_callback(), (void (*)()) s2n_test_locking_cb);
96
97 /* Initializing again doesn't override the application-set callback.
98 * Applications might set their callback before calling s2n_init.
99 */
100 EXPECT_OK(s2n_locking_init());
101 EXPECT_EQUAL(CRYPTO_get_locking_callback(), (void (*)()) s2n_test_locking_cb);
102 EXPECT_OK(s2n_locking_cleanup());
103 EXPECT_EQUAL(CRYPTO_get_locking_callback(), (void (*)()) s2n_test_locking_cb);
104
105 /* Reset the callback */
106 CRYPTO_set_locking_callback(NULL);
107 EXPECT_EQUAL(CRYPTO_get_locking_callback(), NULL);
108
109 /* Initializing now sets the missing callback */
110 EXPECT_OK(s2n_locking_init());
111 EXPECT_NOT_EQUAL(CRYPTO_get_locking_callback(), NULL);
112 };
113
114 END_TEST();
115 }
116
117 #else
118
119 int main(int argc, char **argv)
120 {
121 BEGIN_TEST();
122 END_TEST();
123 }
124
125 #endif