From 10b3cc8dd2bcb3dea2f9a5199cdeeac0dbac69c0 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 6 Jun 2025 10:31:06 -0400 Subject: [PATCH] Add fork test for RAND_poll() --- tests/api.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/api.c b/tests/api.c index adc826ed7..f12ab0bba 100644 --- a/tests/api.c +++ b/tests/api.c @@ -51,6 +51,11 @@ #endif #include + +#ifdef __linux__ +#include +#endif + #include /* compatibility layer */ #include @@ -33142,6 +33147,56 @@ static int test_wolfSSL_RAND(void) } +static int test_wolfSSL_RAND_poll(void) +{ + EXPECT_DECLS; + +#if defined(OPENSSL_EXTRA) && defined(__linux__) + byte seed[16] = {0}; + byte randbuf[8] = {0}; + int pipefds[2] = {0}; + pid_t pid = 0; + + XMEMSET(seed, 0, sizeof(seed)); + + /* No global methods set. */ + ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1); + + ExpectIntEQ(pipe(pipefds), 0); + pid = fork(); + if (pid == 0) + { + ssize_t n_written = 0; + + /* Child process. */ + close(pipefds[0]); + RAND_poll(); + RAND_bytes(randbuf, sizeof(randbuf)); + n_written = write(pipefds[1], randbuf, sizeof(randbuf)); + close(pipefds[1]); + exit(n_written == sizeof(randbuf) ? 0 : 1); + } + else + { + /* Parent process. */ + word64 childrand64 = 0; + + close(pipefds[1]); + ExpectIntEQ(RAND_poll(), 1); + ExpectIntEQ(RAND_bytes(randbuf, sizeof(randbuf)), 1); + ExpectIntEQ(read(pipefds[0], &childrand64, sizeof(childrand64)), sizeof(childrand64)); + ExpectBufNE(randbuf, &childrand64, sizeof(randbuf)); + close(pipefds[0]); + } + RAND_cleanup(); + + ExpectIntEQ(RAND_egd(NULL), -1); +#endif + + return EXPECT_RESULT(); +} + + static int test_wolfSSL_PKCS8_Compat(void) { EXPECT_DECLS; @@ -67671,6 +67726,7 @@ TEST_CASE testCases[] = { TEST_DECL(test_wolfSSL_RAND_set_rand_method), TEST_DECL(test_wolfSSL_RAND_bytes), TEST_DECL(test_wolfSSL_RAND), + TEST_DECL(test_wolfSSL_RAND_poll), /* BN compatibility API */ TEST_DECL(test_wolfSSL_BN_CTX),