1 /* Test of getting random bytes.
2 Copyright (C) 2020-2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible. */
21 #include <sys/random.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (getrandom, ssize_t, (void *, size_t, unsigned int));
36 char large_buf[100000];
39 /* Check that different calls produce different results (with a high
41 ret = getrandom (buf1, sizeof (buf1), 0);
43 ASSERT (errno == ENOSYS);
46 ret = getrandom (buf2, sizeof (buf2), 0);
48 ASSERT (errno == ENOSYS);
51 /* It is very unlikely that two calls to getrandom produce the
53 ASSERT (memcmp (buf1, buf2, sizeof (buf1)) != 0);
57 /* Likewise with the "truly random" number generator. */
58 ret = getrandom (buf1, sizeof (buf1), GRND_RANDOM);
60 ASSERT (errno == ENOSYS);
63 ret = getrandom (buf2, sizeof (buf2), GRND_RANDOM);
65 ASSERT (errno == ENOSYS);
68 /* It is very unlikely that two calls to getrandom produce the
70 ASSERT (memcmp (buf1, buf2, sizeof (buf1)) != 0);
74 /* Check that GRND_NONBLOCK works. */
75 ret = getrandom (large_buf, sizeof (large_buf), GRND_RANDOM | GRND_NONBLOCK);
76 ASSERT (ret <= (ssize_t) sizeof (large_buf));
77 /* It is very unlikely that so many truly random bytes were ready. */
79 ASSERT (errno == ENOSYS || errno == EAGAIN
80 || errno == EINVAL /* Solaris */);
84 if (getrandom (buf1, 1, 0) < 1)
85 if (getrandom (buf1, 1, GRND_RANDOM) < 1)
87 fputs ("Skipping test: getrandom is ineffective\n", stderr);