2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright (c) 2015, Joyent, Inc.
20 #include <sys/random.h>
33 /* Go through flags values, start with invalid */
34 ret
= getrandom(buf
, sizeof (buf
), 42);
36 assert(errno
== EINVAL
);
38 ret
= getrandom(buf
, sizeof (buf
), 0);
41 ret
= getrandom(buf
, sizeof (buf
), GRND_NONBLOCK
);
44 ret
= getrandom(buf
, sizeof (buf
), GRND_RANDOM
);
47 ret
= getrandom(buf
, sizeof (buf
), GRND_RANDOM
| GRND_NONBLOCK
);
50 ret
= getrandom(buf
, sizeof (buf
), (GRND_RANDOM
| GRND_NONBLOCK
) << 1);
52 assert(errno
== EINVAL
);
54 /* Bad buffer addresses, eg. EFAULT */
55 ret
= getrandom(NULL
, sizeof (buf
), 0);
57 assert(errno
== EFAULT
);
59 ret
= getrandom(NULL
, sizeof (buf
), GRND_RANDOM
);
61 assert(errno
== EFAULT
);
63 /* Jump through a hoop to know we'll always have a bad address */
64 addr
= mmap(NULL
, 4096, PROT_READ
, MAP_PRIVATE
| MAP_ANON
, -1, 0);
65 assert(addr
!= MAP_FAILED
);
66 ret
= munmap(addr
, 4096);
68 ret
= getrandom(addr
, sizeof (buf
), 0);
70 assert(errno
== EFAULT
);
71 ret
= getrandom(addr
, sizeof (buf
), GRND_RANDOM
);
73 assert(errno
== EFAULT
);
75 /* Verify that we get rounded down on a getrandom of /dev/random */
76 ret
= getrandom(bigbuf
, sizeof (buf
), GRND_RANDOM
);
77 assert(ret
>= 0 && ret
< sizeof (bigbuf
));
79 /* Do a few simple sets where we know we should get data */
80 ret
= getrandom(buf
, sizeof (buf
), 0);
81 assert(ret
== sizeof (buf
));
82 ret
= getrandom(buf
, sizeof (buf
), GRND_RANDOM
);
83 assert(ret
== sizeof (buf
));