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