summaryrefslogtreecommitdiff
path: root/tests/random.c
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-04-05 16:07:39 -0300
committerEuAndreh <eu@euandre.org>2024-04-05 17:08:08 -0300
commitea60b1b08015060a08b1ead38e4e81c44a88017f (patch)
tree9be302cafc856410383e1b1ab315f8b1a371153d /tests/random.c
parentgit mv meta.capim meta.weave (diff)
downloadpindaiba-ea60b1b08015060a08b1ead38e4e81c44a88017f.tar.gz
pindaiba-ea60b1b08015060a08b1ead38e4e81c44a88017f.tar.xz
Move unit tests out of src/*.c into tests/
Diffstat (limited to 'tests/random.c')
-rw-r--r--tests/random.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/random.c b/tests/random.c
new file mode 100644
index 0000000..eec83c5
--- /dev/null
+++ b/tests/random.c
@@ -0,0 +1,73 @@
+#include "../src/random.c"
+#include "../src/testing.h"
+
+
+static int
+test_urandom_bytes(void) {
+ int rc = 0;
+
+ test_start("urandom_bytes()");
+
+ {
+ testing("we get to pick the size that comes out");
+
+ const size_t LEN = 256;
+ uint8_t arr[256 /* LEN */] = { 0 };
+
+ for (size_t n = 0; n < LEN; n++) {
+ if (urandom_bytes(n, &arr)) {
+ logerr("urandom_bytes(n, &arr);\n");
+ rc = -1;
+ goto out;
+ }
+ for (size_t i = n; i < LEN; i++) {
+ assert(arr[i] == 0);
+ }
+ }
+
+ test_ok();
+ }
+
+ {
+ testing("we always get a new value as a result");
+
+ const size_t LEN = 64;
+ uint8_t arr1[64 /* LEN */] = { 0 };
+ uint8_t arr2[64 /* LEN */] = { 0 };
+
+ if (urandom_bytes(LEN, &arr1)) {
+ logerr("urandom_bytes(LEN, &arr1);\n");
+ rc = -1;
+ goto out;
+ }
+
+ const size_t attempts = 10;
+ for (size_t n = 0; n < attempts; n++) {
+ if (urandom_bytes(LEN, &arr2)) {
+ logerr("urandom_bytes(LEN, &arr2);\n");
+ rc = -1;
+ goto out;
+ }
+ assert(memcmp(arr1, arr2, LEN) != 0);
+ }
+
+ test_ok();
+ }
+
+out:
+ return rc;
+}
+
+int
+main(void) {
+ int rc = 0;
+
+ if (test_urandom_bytes()) {
+ logerr("test_urandom_bytes();\n");
+ rc = -1;
+ goto out;
+ }
+
+out:
+ return !!rc;
+}