Debian Codegraph @

crt/s2n/tests/unit/s2n_io_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 "utils/s2n_io.h"
17
18 #include <sys/wait.h>
19
20 #include "s2n_test.h"
21 #include "tests/testlib/s2n_testlib.h"
22
23 #define S2N_TEST_SIGNAL SIGUSR1
24 #define S2N_TEST_SUCCESS 123
25
26 static int s2n_test_mocked_interrupt(uint8_t n_times, uint8_t *counter)
27 {
28 if (*counter < n_times) {
29 (*counter)++;
30
31 errno = EINTR;
32 return -1;
33 }
34 return S2N_TEST_SUCCESS;
35 }
36
37 static int s2n_test_real_interrupt(int fd, uint8_t n_times, uint8_t *counter)
38 {
39 if (*counter < n_times) {
40 (*counter)++;
41
42 /* There is no data being written to fd,
43 * so this will block indefinitely unless interrupted.
44 */
45 uint8_t buffer = 0;
46 int r = read(fd, &buffer, 1);
47 EXPECT_TRUE(r < 0);
48 EXPECT_EQUAL(errno, EINTR);
49 return r;
50 }
51 return S2N_TEST_SUCCESS;
52 }
53
54 static void s2n_test_sig_handler(int signum)
55 {
56 EXPECT_EQUAL(signum, S2N_TEST_SIGNAL);
57 }
58
59 static int s2n_fail_without_errno(uint8_t *counter)
60 {
61 (*counter)++;
62 if (*counter > 10) {
63 /* To avoid an infinite loop on test case failure, eventually return success */
64 return 0;
65 }
66 return -1;
67 }
68
69 int main(int argc, char **argv)
70 {
71 BEGIN_TEST();
72
73 /* Test S2N_IO_RETRY_EINTR */
74 {
75 const uint8_t n_times = 5;
76
77 /* Retries on errno == EINTR */
78 {
79 uint8_t counter = 0;
80 int result = 0;
81 S2N_IO_RETRY_EINTR(result, s2n_test_mocked_interrupt(n_times, &counter));
82 EXPECT_EQUAL(result, S2N_TEST_SUCCESS);
83 EXPECT_EQUAL(counter, n_times);
84 };
85
86 /* Retries on real interrupt */
87 {
88 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
89 EXPECT_SUCCESS(s2n_io_pair_init(&io_pair));
90
91 struct sigaction action = {
92 .sa_handler = s2n_test_sig_handler,
93 };
94 sigaction(SIGUSR1, &action, NULL);
95
96 fflush(stdout);
97 pid_t pid = fork();
98 if (pid == 0) {
99 uint8_t counter = 0;
100 int result = 0;
101 S2N_IO_RETRY_EINTR(result,
102 s2n_test_real_interrupt(io_pair.client, n_times, &counter));
103 EXPECT_EQUAL(result, S2N_TEST_SUCCESS);
104 EXPECT_EQUAL(counter, n_times);
105 /* Child process exit doesn't close io_pair properly, so manually close it*/
106 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
107 exit(0);
108 }
109
110 /* Keep sending the signal until the process exits */
111 int status = 0;
112 while (waitpid(pid, &status, WNOHANG) == 0) {
113 EXPECT_EQUAL(kill(pid, S2N_TEST_SIGNAL), 0);
114 }
115 EXPECT_EQUAL(status, EXIT_SUCCESS);
116 };
117
118 /* Handles IO methods that don't properly set errno */
119 {
120 /* Set errno to EINTR to try to trigger retry */
121 errno = EINTR;
122
123 uint8_t counter = 0;
124 int result = 0;
125 S2N_IO_RETRY_EINTR(result, s2n_fail_without_errno(&counter));
126 EXPECT_EQUAL(result, -1);
127 EXPECT_EQUAL(counter, 1);
128 EXPECT_EQUAL(errno, 0);
129 };
130 };
131
132 END_TEST();
133 }