]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-random-util.c
tests: add test-random-util
[thirdparty/systemd.git] / src / test / test-random-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2017 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "hexdecoct.h"
21 #include "random-util.h"
22 #include "log.h"
23
24 static void test_acquire_random_bytes(bool high_quality_required) {
25 uint8_t buf[16] = {};
26 unsigned i;
27
28 log_info("/* %s */", __func__);
29
30 for (i = 1; i < sizeof buf; i++) {
31 assert_se(acquire_random_bytes(buf, i, high_quality_required) == 0);
32 if (i + 1 < sizeof buf)
33 assert_se(buf[i] == 0);
34
35 hexdump(stdout, buf, i);
36 }
37 }
38
39 static void test_pseudorandom_bytes(void) {
40 uint8_t buf[16] = {};
41 unsigned i;
42
43 log_info("/* %s */", __func__);
44
45 for (i = 1; i < sizeof buf; i++) {
46 pseudorandom_bytes(buf, i);
47 if (i + 1 < sizeof buf)
48 assert_se(buf[i] == 0);
49
50 hexdump(stdout, buf, i);
51 }
52 }
53
54 int main(int argc, char **argv) {
55 log_set_max_level(LOG_DEBUG);
56 log_parse_environment();
57 log_open();
58
59 test_acquire_random_bytes(false);
60 test_acquire_random_bytes(true);
61
62 test_pseudorandom_bytes();
63
64 return 0;
65 }