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