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