Skip to content

Commit 9b7b1f3

Browse files
authored
test: fix session-ticket, non-blocking-io tests on 32 bit (#3969)
1 parent 760c09a commit 9b7b1f3

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,6 @@ if (BUILD_TESTING)
658658
# these tests have 2038 time_t issues
659659
set(CTEST_CUSTOM_TESTS_IGNORE
660660
s2n_cert_status_extension_test
661-
s2n_self_talk_nonblocking_test
662-
s2n_session_ticket_test
663661
s2n_x509_validator_test
664662
)
665663
message(STATUS "Detected 32-Bit system - disabling the following tests - ${CTEST_CUSTOM_TESTS_IGNORE}")

tests/unit/s2n_self_talk_nonblocking_test.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@
2828
#include "utils/s2n_safety.h"
2929

3030
static const float minimum_send_percent = 5.0;
31-
static const uint32_t max_client_run_time = 300;
3231

33-
#define LESS_THAN_ELAPSED_SECONDS(start_time, max_time) ((start_time - time(0)) < max_time)
34-
#define MIN_PERCENT_COMPLETE(remaining, total) ((((total - remaining) / (total * 1.0)) * 100.0) > minimum_send_percent)
32+
#define MIN_PERCENT_COMPLETE(remaining, total) ((((total - remaining) / (total * 1.0)) * 100.0) > minimum_send_percent)
3533

3634
int mock_client(struct s2n_test_io_pair *io_pair, uint8_t *expected_data, uint32_t size)
3735
{
@@ -46,8 +44,6 @@ int mock_client(struct s2n_test_io_pair *io_pair, uint8_t *expected_data, uint32
4644
* CI/CD pipelines might never complete */
4745
int should_block = 1;
4846

49-
time_t start_time = time(0);
50-
5147
/* Give the server a chance to listen */
5248
sleep(1);
5349

@@ -66,7 +62,7 @@ int mock_client(struct s2n_test_io_pair *io_pair, uint8_t *expected_data, uint32
6662

6763
/* Receive 10MB of data */
6864
uint32_t remaining = size;
69-
while (remaining && LESS_THAN_ELAPSED_SECONDS(start_time, max_client_run_time)) {
65+
while (remaining) {
7066
int r = s2n_recv(client_conn, ptr, remaining, &blocked);
7167
if (r < 0) {
7268
return 1;
@@ -82,7 +78,7 @@ int mock_client(struct s2n_test_io_pair *io_pair, uint8_t *expected_data, uint32
8278
int shutdown_rc = -1;
8379
do {
8480
shutdown_rc = s2n_shutdown(client_conn, &blocked);
85-
} while (shutdown_rc != 0 && LESS_THAN_ELAPSED_SECONDS(start_time, max_client_run_time));
81+
} while (shutdown_rc != 0);
8682

8783
for (size_t i = 0; i < size; i++) {
8884
if (buffer[i] != expected_data[i]) {
@@ -108,8 +104,6 @@ int mock_client_iov(struct s2n_test_io_pair *io_pair, struct iovec *iov, uint32_
108104
int total_size = 0, i;
109105
int should_block = 1;
110106

111-
time_t start_time = time(0);
112-
113107
for (i = 0; i < iov_size; i++) {
114108
total_size += iov[i].iov_len;
115109
}
@@ -133,7 +127,7 @@ int mock_client_iov(struct s2n_test_io_pair *io_pair, struct iovec *iov, uint32_
133127
}
134128

135129
uint32_t remaining = total_size;
136-
while (remaining && LESS_THAN_ELAPSED_SECONDS(start_time, max_client_run_time)) {
130+
while (remaining) {
137131
int r = s2n_recv(client_conn, &buffer[buffer_offs], remaining, &blocked);
138132
if (r < 0) {
139133
return 1;
@@ -147,7 +141,7 @@ int mock_client_iov(struct s2n_test_io_pair *io_pair, struct iovec *iov, uint32_
147141
}
148142

149143
remaining = iov[0].iov_len;
150-
while (remaining && LESS_THAN_ELAPSED_SECONDS(start_time, max_client_run_time)) {
144+
while (remaining) {
151145
int r = s2n_recv(client_conn, &buffer[buffer_offs], remaining, &blocked);
152146
if (r < 0) {
153147
return 1;
@@ -159,7 +153,7 @@ int mock_client_iov(struct s2n_test_io_pair *io_pair, struct iovec *iov, uint32_
159153
int shutdown_rc = -1;
160154
do {
161155
shutdown_rc = s2n_shutdown(client_conn, &blocked);
162-
} while (shutdown_rc != 0 && LESS_THAN_ELAPSED_SECONDS(start_time, max_client_run_time));
156+
} while (shutdown_rc != 0);
163157

164158
for (i = 0, buffer_offs = 0; i < iov_size; i++) {
165159
if (memcmp(iov[i].iov_base, &buffer[buffer_offs], iov[i].iov_len)) {

tests/unit/s2n_session_ticket_test.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,27 @@
2929

3030
#define S2N_CLOCK_SYS CLOCK_REALTIME
3131

32+
/**
33+
* This function is used to "skip" time in unit tests. It will mock the system
34+
* time to be current_time (ns) + data (ns). The "data" parameter is a uint64_t
35+
* passed in as a void*.
36+
*/
3237
int mock_nanoseconds_since_epoch(void *data, uint64_t *nanoseconds)
3338
{
3439
struct timespec current_time;
3540

3641
clock_gettime(S2N_CLOCK_SYS, &current_time);
3742

38-
*nanoseconds = current_time.tv_sec * 1000000000;
39-
*nanoseconds += current_time.tv_nsec;
43+
/**
44+
* current_time fields are represented as time_t, and time_t has a platform
45+
* dependent size. On 32 bit platforms, attempting to convert the current
46+
* system time to nanoseconds will overflow, causing odd failures in unit
47+
* tests. We upcast current_time fields to uint64_t before multiplying to
48+
* avoid this.
49+
*/
50+
*nanoseconds = 0;
51+
*nanoseconds += (uint64_t) current_time.tv_sec * ONE_SEC_IN_NANOS;
52+
*nanoseconds += (uint64_t) current_time.tv_nsec;
4053
*nanoseconds += *(uint64_t *) data;
4154

4255
return 0;
@@ -822,7 +835,7 @@ int main(int argc, char **argv)
822835
* selection function uses a weighted random selection algorithm. Here we retry the test once if the key chosen
823836
* is not the expected key.
824837
*
825-
* The wrong key will be chosen 0.02% of the time. This value is drawn from the weight of the expected key,
838+
* The wrong key will be chosen 0.02% of the time. This value is drawn from the weight of the expected key,
826839
* which does not change per test run. Therefore, the probability that the test chooses the wrong key
827840
* more than allowed_failures times is 0.0002 ^ 2 = 0.00000004, which is extremely unlikely to occur. If
828841
* the logic changes to chose the wrong key at a higher rate, say 50% of the time, this test would fail at a

0 commit comments

Comments
 (0)