chiark / gitweb /
Drop my copyright headers
[elogind.git] / src / test / test-random-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "hexdecoct.h"
4 #include "random-util.h"
5 #include "log.h"
6
7 static void test_acquire_random_bytes(bool high_quality_required) {
8         uint8_t buf[16] = {};
9         unsigned i;
10
11         log_info("/* %s */", __func__);
12
13         for (i = 1; i < sizeof buf; i++) {
14                 assert_se(acquire_random_bytes(buf, i, high_quality_required) == 0);
15                 if (i + 1 < sizeof buf)
16                         assert_se(buf[i] == 0);
17
18                 hexdump(stdout, buf, i);
19         }
20 }
21
22 static void test_pseudorandom_bytes(void) {
23         uint8_t buf[16] = {};
24         unsigned i;
25
26         log_info("/* %s */", __func__);
27
28         for (i = 1; i < sizeof buf; i++) {
29                 pseudorandom_bytes(buf, i);
30                 if (i + 1 < sizeof buf)
31                         assert_se(buf[i] == 0);
32
33                 hexdump(stdout, buf, i);
34         }
35 }
36
37 int main(int argc, char **argv) {
38         log_set_max_level(LOG_DEBUG);
39         log_parse_environment();
40         log_open();
41
42         test_acquire_random_bytes(false);
43         test_acquire_random_bytes(true);
44
45         test_pseudorandom_bytes();
46
47         return 0;
48 }