Skip to content

Commit

Permalink
test: fix session-ticket, non-blocking-io tests on 32 bit (aws#3969)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmayclin authored and dougch committed May 31, 2023
1 parent 34704b6 commit 15bce95
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,6 @@ if (BUILD_TESTING)
# these tests have 2038 time_t issues
set(CTEST_CUSTOM_TESTS_IGNORE
s2n_cert_status_extension_test
s2n_self_talk_nonblocking_test
s2n_session_ticket_test
s2n_x509_validator_test
)
message(STATUS "Detected 32-Bit system - disabling the following tests - ${CTEST_CUSTOM_TESTS_IGNORE}")
Expand Down
18 changes: 6 additions & 12 deletions tests/unit/s2n_self_talk_nonblocking_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
#include "utils/s2n_safety.h"

static const float minimum_send_percent = 5.0;
static const uint32_t max_client_run_time = 300;

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

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

time_t start_time = time(0);

/* Give the server a chance to listen */
sleep(1);

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

/* Receive 10MB of data */
uint32_t remaining = size;
while (remaining && LESS_THAN_ELAPSED_SECONDS(start_time, max_client_run_time)) {
while (remaining) {
int r = s2n_recv(client_conn, ptr, remaining, &blocked);
if (r < 0) {
return 1;
Expand All @@ -82,7 +78,7 @@ int mock_client(struct s2n_test_io_pair *io_pair, uint8_t *expected_data, uint32
int shutdown_rc = -1;
do {
shutdown_rc = s2n_shutdown(client_conn, &blocked);
} while (shutdown_rc != 0 && LESS_THAN_ELAPSED_SECONDS(start_time, max_client_run_time));
} while (shutdown_rc != 0);

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

time_t start_time = time(0);

for (i = 0; i < iov_size; i++) {
total_size += iov[i].iov_len;
}
Expand All @@ -133,7 +127,7 @@ int mock_client_iov(struct s2n_test_io_pair *io_pair, struct iovec *iov, uint32_
}

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

remaining = iov[0].iov_len;
while (remaining && LESS_THAN_ELAPSED_SECONDS(start_time, max_client_run_time)) {
while (remaining) {
int r = s2n_recv(client_conn, &buffer[buffer_offs], remaining, &blocked);
if (r < 0) {
return 1;
Expand All @@ -159,7 +153,7 @@ int mock_client_iov(struct s2n_test_io_pair *io_pair, struct iovec *iov, uint32_
int shutdown_rc = -1;
do {
shutdown_rc = s2n_shutdown(client_conn, &blocked);
} while (shutdown_rc != 0 && LESS_THAN_ELAPSED_SECONDS(start_time, max_client_run_time));
} while (shutdown_rc != 0);

for (i = 0, buffer_offs = 0; i < iov_size; i++) {
if (memcmp(iov[i].iov_base, &buffer[buffer_offs], iov[i].iov_len)) {
Expand Down
19 changes: 16 additions & 3 deletions tests/unit/s2n_session_ticket_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,27 @@

#define S2N_CLOCK_SYS CLOCK_REALTIME

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

clock_gettime(S2N_CLOCK_SYS, &current_time);

*nanoseconds = current_time.tv_sec * 1000000000;
*nanoseconds += current_time.tv_nsec;
/**
* current_time fields are represented as time_t, and time_t has a platform
* dependent size. On 32 bit platforms, attempting to convert the current
* system time to nanoseconds will overflow, causing odd failures in unit
* tests. We upcast current_time fields to uint64_t before multiplying to
* avoid this.
*/
*nanoseconds = 0;
*nanoseconds += (uint64_t) current_time.tv_sec * ONE_SEC_IN_NANOS;
*nanoseconds += (uint64_t) current_time.tv_nsec;
*nanoseconds += *(uint64_t *) data;

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

0 comments on commit 15bce95

Please sign in to comment.