Debian Codegraph @

crt/s2n/tests/unit/s2n_init_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 <pthread.h>
17
18 #include "s2n_test.h"
19
20 bool s2n_is_initialized(void);
21
22 static void *s2n_init_fail_cb(void *_unused_arg)
23 {
24 (void) _unused_arg;
25
26 EXPECT_FAILURE_WITH_ERRNO(s2n_init(), S2N_ERR_INITIALIZED);
27 return NULL;
28 }
29
30 static void *s2n_init_success_cb(void *_unused_arg)
31 {
32 (void) _unused_arg;
33
34 EXPECT_SUCCESS(s2n_init());
35 return NULL;
36 }
37
38 int main(int argc, char **argv)
39 {
40 BEGIN_TEST_NO_INIT();
41
42 /* Calling s2n_init twice in a row will cause an error */
43 EXPECT_SUCCESS(s2n_init());
44 EXPECT_FAILURE_WITH_ERRNO(s2n_init(), S2N_ERR_INITIALIZED);
45 EXPECT_SUCCESS(s2n_cleanup_final());
46
47 /* Second call to s2n_cleanup_final will fail, since the full cleanup is not idempotent. */
48 EXPECT_FAILURE_WITH_ERRNO(s2n_cleanup_final(), S2N_ERR_NOT_INITIALIZED);
49
50 /* Clean up and init multiple times */
51 for (size_t i = 0; i < 10; i++) {
52 EXPECT_SUCCESS(s2n_init());
53 EXPECT_SUCCESS(s2n_cleanup_final());
54 }
55
56 /* Calling s2n_init again after creating a process will cause an error */
57 EXPECT_SUCCESS(s2n_init());
58 int pid = fork();
59 if (pid == 0) {
60 /* Child process */
61 EXPECT_FAILURE_WITH_ERRNO(s2n_init(), S2N_ERR_INITIALIZED);
62 return 0;
63 }
64 EXPECT_SUCCESS(s2n_cleanup_final());
65
66 /* Calling s2n_init again after creating a thread will cause an error */
67 EXPECT_SUCCESS(s2n_init());
68 pthread_t init_thread = { 0 };
69 EXPECT_EQUAL(pthread_create(&init_thread, NULL, s2n_init_fail_cb, NULL), 0);
70 EXPECT_EQUAL(pthread_join(init_thread, NULL), 0);
71 EXPECT_SUCCESS(s2n_cleanup_final());
72
73 /* s2n_cleanup_final fully de-initializes the library */
74 EXPECT_SUCCESS(s2n_init());
75 EXPECT_TRUE(s2n_is_initialized());
76 EXPECT_SUCCESS(s2n_cleanup_final());
77 EXPECT_FALSE(s2n_is_initialized());
78
79 /* The following test requires atexit to be enabled. */
80 EXPECT_SUCCESS(s2n_enable_atexit());
81
82 /* Initializing s2n on a child thread without calling s2n_cleanup on that
83 * thread will not result in a memory leak. This is because we register
84 * thread-local memory to be cleaned up at thread-exit. */
85 pthread_t init_success_thread = { 0 };
86 EXPECT_EQUAL(pthread_create(&init_success_thread, NULL, s2n_init_success_cb, NULL), 0);
87 EXPECT_EQUAL(pthread_join(init_success_thread, NULL), 0);
88
89 END_TEST_NO_INIT();
90 }