]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-random-util.c
test-condition: make function return void
[thirdparty/systemd.git] / src / test / test-random-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2416f73b
ZJS
2
3#include "hexdecoct.h"
4#include "random-util.h"
5#include "log.h"
6
7static 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
22static 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
97fa202a
LP
37static void test_rdrand64(void) {
38 int r, i;
39
40 for (i = 0; i < 10; i++) {
41 uint64_t x = 0;
42
43 r = rdrand64(&x);
44 if (r < 0) {
45 log_error_errno(r, "RDRAND failed: %m");
46 return;
47 }
48
49 printf("%" PRIx64 "\n", x);
50 }
51}
52
2416f73b
ZJS
53int main(int argc, char **argv) {
54 log_set_max_level(LOG_DEBUG);
55 log_parse_environment();
56 log_open();
57
58 test_acquire_random_bytes(false);
59 test_acquire_random_bytes(true);
60
61 test_pseudorandom_bytes();
62
97fa202a
LP
63 test_rdrand64();
64
2416f73b
ZJS
65 return 0;
66}