Skip to content

Commit

Permalink
Fix a flaky test failure in getentropy.c (#398)
Browse files Browse the repository at this point in the history
This test looks to be asserting that `getrandom` never returns 256
consecutive zeros, but the way it's asserting that is summing up the
bytes and asserting the sum is nonzero. Due to this being a signed
addition, however, it's possible for the bytes to be nonzero and still
trigger the assert. Locally running this test in a loop took 30 or so
seconds before it triggered a failure.

I've updated the test to instead hunt for any entry which is not equal
to zero and then assert that something is not zero.
  • Loading branch information
alexcrichton authored Mar 9, 2024
1 parent 1f63274 commit 9389ea5
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions tests/general/getentropy.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#include <assert.h>
#include <unistd.h>
#include <stdbool.h>

int main() {
char buf[256] = {0};
int ret = getentropy(buf, 256);
assert(ret == 0);

int sum = 0;
bool something_nonzero = false;
for (int i = 0; i < 256; i++) {
sum += buf[i];
if (buf[i] != 0)
something_nonzero = true;
}

assert(sum != 0);
assert(something_nonzero);

return 0;
}

0 comments on commit 9389ea5

Please sign in to comment.