Skip to content

Commit

Permalink
remove .c include
Browse files Browse the repository at this point in the history
  • Loading branch information
goatgoose committed Jan 19, 2024
1 parent 0ee6a0d commit a711295
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
30 changes: 18 additions & 12 deletions tests/unit/s2n_random_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
#define _GNU_SOURCE
#endif

/* The .c file is included in order to access global fields for testing purposes. */
#include "utils/s2n_random.c"
#include "utils/s2n_random.h"

#include <openssl/rand.h>
#include <pthread.h>
Expand All @@ -49,6 +48,10 @@
#define NUMBER_OF_RANGE_FUNCTION_CALLS 200
#define MAX_REPEATED_OUTPUT 4

S2N_RESULT s2n_rand_device_validate(struct s2n_rand_device *device);
S2N_RESULT s2n_rand_get_urandom_for_test(struct s2n_rand_device **device);
S2N_RESULT s2n_rand_set_urandom_for_test();

struct random_test_case {
const char *test_case_label;
int (*test_case_cb)(struct random_test_case *test_case);
Expand Down Expand Up @@ -793,32 +796,35 @@ static int s2n_random_invalid_urandom_fd_cb(struct random_test_case *test_case)
{
EXPECT_SUCCESS(s2n_disable_atexit());

struct s2n_rand_device *dev_urandom = NULL;
EXPECT_OK(s2n_rand_get_urandom_for_test(&dev_urandom));
EXPECT_NOT_NULL(dev_urandom);

for (size_t test = 0; test <= 1; test++) {
EXPECT_EQUAL(s2n_dev_urandom.fd, -1);
EXPECT_EQUAL(dev_urandom->fd, -1);

/* Validation should fail before initialization. */
EXPECT_ERROR(s2n_rand_device_validate(&s2n_dev_urandom));
EXPECT_ERROR(s2n_rand_device_validate(dev_urandom));

EXPECT_SUCCESS(s2n_init());

/* Validation should succeed after initialization. */
EXPECT_OK(s2n_rand_device_validate(&s2n_dev_urandom));
EXPECT_OK(s2n_rand_device_validate(dev_urandom));

/* Override the mix callback with urandom, in case support for rdrand is detected and enabled. */
EXPECT_SUCCESS(s2n_rand_set_callbacks(s2n_rand_init_cb_impl, s2n_rand_cleanup_cb_impl,
s2n_rand_get_entropy_from_urandom, s2n_rand_get_entropy_from_urandom));
EXPECT_OK(s2n_rand_set_urandom_for_test());

EXPECT_TRUE(s2n_dev_urandom.fd > STDERR_FILENO);
EXPECT_TRUE(dev_urandom->fd > STDERR_FILENO);
if (test == 0) {
/* Close the file descriptor. */
EXPECT_EQUAL(close(s2n_dev_urandom.fd), 0);
EXPECT_EQUAL(close(dev_urandom->fd), 0);
} else {
/* Make the file descriptor invalid by pointing it to STDERR. */
s2n_dev_urandom.fd = STDERR_FILENO;
dev_urandom->fd = STDERR_FILENO;
}

/* Validation should fail when the file descriptor is invalid. */
EXPECT_ERROR(s2n_rand_device_validate(&s2n_dev_urandom));
EXPECT_ERROR(s2n_rand_device_validate(dev_urandom));

s2n_stack_blob(rand_data, 16, 16);
EXPECT_OK(s2n_get_public_random_data(&rand_data));
Expand All @@ -833,7 +839,7 @@ static int s2n_random_invalid_urandom_fd_cb(struct random_test_case *test_case)
/* When the urandom implementation is used, the file descriptor is re-opened and
* validation should succeed.
*/
EXPECT_OK(s2n_rand_device_validate(&s2n_dev_urandom));
EXPECT_OK(s2n_rand_device_validate(dev_urandom));
EXPECT_TRUE(public_bytes_used > 0);
}

Expand Down
17 changes: 16 additions & 1 deletion utils/s2n_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ S2N_RESULT s2n_get_private_random_bytes_used(uint64_t *bytes_used)
return S2N_RESULT_OK;
}

S2N_RESULT s2n_rand_get_urandom_for_test(struct s2n_rand_device **device)
{
RESULT_ENSURE_REF(device);
RESULT_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST);
*device = &s2n_dev_urandom;
return S2N_RESULT_OK;
}

static S2N_RESULT s2n_rand_device_open(struct s2n_rand_device *device)
{
RESULT_ENSURE_REF(device);
Expand All @@ -366,7 +374,7 @@ static S2N_RESULT s2n_rand_device_open(struct s2n_rand_device *device)
return S2N_RESULT_OK;
}

static S2N_RESULT s2n_rand_device_validate(struct s2n_rand_device *device)
S2N_RESULT s2n_rand_device_validate(struct s2n_rand_device *device)
{
RESULT_ENSURE_REF(device);
RESULT_ENSURE_NE(device->fd, UNINITIALIZED_ENTROPY_FD);
Expand Down Expand Up @@ -639,6 +647,13 @@ S2N_RESULT s2n_set_private_drbg_for_test(struct s2n_drbg drbg)
return S2N_RESULT_OK;
}

S2N_RESULT s2n_rand_set_urandom_for_test()
{
RESULT_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST);
s2n_rand_mix_cb = s2n_rand_get_entropy_from_urandom;
return S2N_RESULT_OK;
}

/*
* volatile is important to prevent the compiler from
* re-ordering or optimizing the use of RDRAND.
Expand Down

0 comments on commit a711295

Please sign in to comment.