1 /* Tests for the getentropy, getrandom functions.
2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
23 #include <sys/random.h>
25 /* Set to true if any errors are encountered. */
28 /* Test getrandom with a single buffer length. NB: The passed-in
29 buffer must have room for four extra bytes after the specified
30 length, which are used to test that getrandom leaves those bytes
33 test_length (char *buffer
, size_t length
, unsigned int flags
)
35 memset (buffer
, 0, length
);
36 strcpy (buffer
+ length
, "123");
37 ssize_t ret
= getrandom (buffer
, length
, flags
);
40 /* EAGAIN is an expected error with GRND_RANDOM and
42 if ((flags
& GRND_RANDOM
)
43 && (flags
& GRND_NONBLOCK
)
46 printf ("error: getrandom (%zu, 0x%x): %m\n", length
, flags
);
52 if (flags
& GRND_RANDOM
)
54 if (ret
== 0 || ret
> length
)
56 printf ("error: getrandom (%zu, 0x%x) returned %zd\n",
63 printf ("error: getrandom (%zu, 0x%x) returned %zd\n",
70 /* One spurious test failure in 2**56 is sufficiently
73 for (int i
= 0; i
< length
; ++i
)
74 non_null
+= buffer
[i
] != 0;
77 printf ("error: getrandom (%zu, 0x%x) returned all-zero bytes\n",
82 if (memcmp (buffer
+ length
, "123", 4) != 0)
84 printf ("error: getrandom (%zu, 0x%x) wrote spurious bytes\n",
90 /* Call getrandom repeatedly to fill the buffer. */
92 getrandom_full (char *buffer
, size_t length
, unsigned int flags
)
94 char *end
= buffer
+ length
;
97 ssize_t ret
= getrandom (buffer
, end
- buffer
, flags
);
100 printf ("error: getrandom (%zu, 0x%x): %m\n", length
, flags
);
111 test_flags (unsigned int flags
)
113 /* Test various lengths, but only for !GRND_RANDOM, to conserve
116 enum { max_length
= 300 };
117 char buffer
[max_length
+ 4];
118 if (flags
& GRND_RANDOM
)
119 test_length (buffer
, 0, flags
);
122 for (int length
= 0; length
<= 9; ++length
)
123 test_length (buffer
, length
, flags
);
124 test_length (buffer
, 16, flags
);
125 test_length (buffer
, max_length
, flags
);
129 /* Test that getrandom returns different data. */
130 if (!(flags
& GRND_NONBLOCK
))
133 memset (buffer1
, 0, sizeof (buffer1
));
136 memset (buffer2
, 0, sizeof (buffer2
));
138 if (getrandom_full (buffer1
, sizeof (buffer1
), flags
)
139 && getrandom_full (buffer2
, sizeof (buffer2
), flags
))
141 /* The probability that these two 8-byte buffers are equal
142 is very small (assuming that two subsequent calls to
143 getrandom result are independent, uniformly distributed
144 random variables). */
145 if (memcmp (buffer1
, buffer2
, sizeof (buffer1
)) == 0)
147 printf ("error: getrandom returns constant value\n");
155 test_getentropy (void)
158 memset (buf
, '@', sizeof (buf
));
159 if (getentropy (buf
, 0) != 0)
161 printf ("error: getentropy zero length: %m\n");
165 for (size_t i
= 0; i
< sizeof (buf
); ++i
)
168 printf ("error: getentropy modified zero-length buffer\n");
173 if (getentropy (buf
, sizeof (buf
)) != 0)
175 printf ("error: getentropy buf: %m\n");
181 _Static_assert (sizeof (buf
) < sizeof (buf2
), "buf and buf2 compatible");
182 memset (buf2
, '@', sizeof (buf2
));
183 if (getentropy (buf2
, sizeof (buf
)) != 0)
185 printf ("error: getentropy buf2: %m\n");
190 /* The probability that these two buffers are equal is very
192 if (memcmp (buf
, buf2
, sizeof (buf
)) == 0)
194 printf ("error: getentropy appears to return constant bytes\n");
199 for (size_t i
= sizeof (buf
); i
< sizeof (buf2
); ++i
)
202 printf ("error: getentropy wrote beyond the end of the buffer\n");
208 if (getentropy (buf3
, sizeof (buf3
)) == 0)
210 printf ("error: getentropy successful for 257 byte buffer\n");
216 printf ("error: getentropy wrong error for 257 byte buffer: %m\n");
225 /* Check if getrandom is not supported by this system. */
226 if (getrandom (NULL
, 0, 0) == -1 && errno
== ENOSYS
)
229 for (int use_random
= 0; use_random
< 2; ++use_random
)
230 for (int use_nonblock
= 0; use_nonblock
< 2; ++use_nonblock
)
232 unsigned int flags
= 0;
234 flags
|= GRND_RANDOM
;
236 flags
|= GRND_NONBLOCK
;
245 #include <support/test-driver.c>