| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 43 |
|
| 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 |
|
| 64 |
return 0; |
| 65 |
} |
| 66 |
return -1; |
| 67 |
} |
| 68 |
|
| 69 |
int main(int argc, char **argv) |
| 70 |
{ |
| 71 |
BEGIN_TEST(); |
| 72 |
|
| 73 |
|
| 74 |
{ |
| 75 |
const uint8_t n_times = 5; |
| 76 |
|
| 77 |
|
| 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 |
|
| 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 |
|
| 106 |
EXPECT_SUCCESS(s2n_io_pair_close(&io_pair)); |
| 107 |
exit(0); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 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 |
|
| 119 |
{ |
| 120 |
|
| 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 |
} |