Commit 84ca5428 authored by Dave Watson's avatar Dave Watson Committed by Facebook Github Bot

Fix RandomFork test

Summary:
Fork test tries to test that we don't reuse the SecureRandom buffer on fork, but it only tests one byte.

This gives us probability one in 256*256 (or something like that) that we spuriously fail.

Check the whole buffer instead before failing.

Reviewed By: fredemmott

Differential Revision: D9242788

fbshipit-source-id: b9fb809518797c439d7fe16084a04f9964a1f597
parent 0761e428
......@@ -140,22 +140,33 @@ TEST(Random, sanity) {
}
TEST(Random, SecureFork) {
unsigned char buffer = 0;
// Init random buffer
folly::Random::secureRandom(&buffer, 1);
auto pid = fork();
EXPECT_NE(pid, -1);
if (pid) {
// parent
int status = 0;
// Random buffer size is 128, must be less than that.
int retries = 100;
while (true) {
unsigned char buffer = 0;
// Init random buffer
folly::Random::secureRandom(&buffer, 1);
auto pid2 = wait(&status);
EXPECT_NE(WEXITSTATUS(status), buffer);
EXPECT_EQ(pid, pid2);
} else {
// child
folly::Random::secureRandom(&buffer, 1);
exit(buffer); // Do not print gtest results
auto pid = fork();
EXPECT_NE(pid, -1);
if (pid) {
// parent
int status = 0;
folly::Random::secureRandom(&buffer, 1);
auto pid2 = wait(&status);
EXPECT_EQ(pid, pid2);
// Since this *is* random data, we could randomly end up with
// the same byte. Try again a few times if so before assuming
// real failure.
if (WEXITSTATUS(status) == buffer && retries-- > 0) {
continue;
}
EXPECT_NE(WEXITSTATUS(status), buffer);
return;
} else {
// child
folly::Random::secureRandom(&buffer, 1);
exit(buffer); // Do not print gtest results
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment