xref: /titanic_41/usr/src/test/libc-tests/tests/random/getrandom.c (revision 28f17cb2cce2c4ba0dcda9d9f52a8935aa02e489)
1*28f17cb2SRobert Mustacchi /*
2*28f17cb2SRobert Mustacchi  * This file and its contents are supplied under the terms of the
3*28f17cb2SRobert Mustacchi  * Common Development and Distribution License ("CDDL"), version 1.0.
4*28f17cb2SRobert Mustacchi  * You may only use this file in accordance with the terms of version
5*28f17cb2SRobert Mustacchi  * 1.0 of the CDDL.
6*28f17cb2SRobert Mustacchi  *
7*28f17cb2SRobert Mustacchi  * A full copy of the text of the CDDL should have accompanied this
8*28f17cb2SRobert Mustacchi  * source.  A copy of the CDDL is also available via the Internet at
9*28f17cb2SRobert Mustacchi  * http://www.illumos.org/license/CDDL.
10*28f17cb2SRobert Mustacchi  */
11*28f17cb2SRobert Mustacchi 
12*28f17cb2SRobert Mustacchi /*
13*28f17cb2SRobert Mustacchi  * Copyright (c) 2015, Joyent, Inc.
14*28f17cb2SRobert Mustacchi  */
15*28f17cb2SRobert Mustacchi 
16*28f17cb2SRobert Mustacchi /*
17*28f17cb2SRobert Mustacchi  * Test getrandom(2)
18*28f17cb2SRobert Mustacchi  */
19*28f17cb2SRobert Mustacchi 
20*28f17cb2SRobert Mustacchi #include <sys/random.h>
21*28f17cb2SRobert Mustacchi #include <sys/mman.h>
22*28f17cb2SRobert Mustacchi #include <assert.h>
23*28f17cb2SRobert Mustacchi #include <errno.h>
24*28f17cb2SRobert Mustacchi 
25*28f17cb2SRobert Mustacchi int
main(void)26*28f17cb2SRobert Mustacchi main(void)
27*28f17cb2SRobert Mustacchi {
28*28f17cb2SRobert Mustacchi 	int ret;
29*28f17cb2SRobert Mustacchi 	void *addr;
30*28f17cb2SRobert Mustacchi 	uint8_t buf[32];
31*28f17cb2SRobert Mustacchi 	uint8_t bigbuf[4096];
32*28f17cb2SRobert Mustacchi 
33*28f17cb2SRobert Mustacchi 	/* Go through flags values, start with invalid */
34*28f17cb2SRobert Mustacchi 	ret = getrandom(buf, sizeof (buf), 42);
35*28f17cb2SRobert Mustacchi 	assert(ret == -1);
36*28f17cb2SRobert Mustacchi 	assert(errno == EINVAL);
37*28f17cb2SRobert Mustacchi 
38*28f17cb2SRobert Mustacchi 	ret = getrandom(buf, sizeof (buf), 0);
39*28f17cb2SRobert Mustacchi 	assert(ret >= 0);
40*28f17cb2SRobert Mustacchi 
41*28f17cb2SRobert Mustacchi 	ret = getrandom(buf, sizeof (buf), GRND_NONBLOCK);
42*28f17cb2SRobert Mustacchi 	assert(ret >= 0);
43*28f17cb2SRobert Mustacchi 
44*28f17cb2SRobert Mustacchi 	ret = getrandom(buf, sizeof (buf), GRND_RANDOM);
45*28f17cb2SRobert Mustacchi 	assert(ret >= 0);
46*28f17cb2SRobert Mustacchi 
47*28f17cb2SRobert Mustacchi 	ret = getrandom(buf, sizeof (buf), GRND_RANDOM | GRND_NONBLOCK);
48*28f17cb2SRobert Mustacchi 	assert(ret >= 0);
49*28f17cb2SRobert Mustacchi 
50*28f17cb2SRobert Mustacchi 	ret = getrandom(buf, sizeof (buf), (GRND_RANDOM | GRND_NONBLOCK) << 1);
51*28f17cb2SRobert Mustacchi 	assert(ret == -1);
52*28f17cb2SRobert Mustacchi 	assert(errno == EINVAL);
53*28f17cb2SRobert Mustacchi 
54*28f17cb2SRobert Mustacchi 	/* Bad buffer addresses, eg. EFAULT */
55*28f17cb2SRobert Mustacchi 	ret = getrandom(NULL, sizeof (buf), 0);
56*28f17cb2SRobert Mustacchi 	assert(ret == -1);
57*28f17cb2SRobert Mustacchi 	assert(errno == EFAULT);
58*28f17cb2SRobert Mustacchi 
59*28f17cb2SRobert Mustacchi 	ret = getrandom(NULL, sizeof (buf), GRND_RANDOM);
60*28f17cb2SRobert Mustacchi 	assert(ret == -1);
61*28f17cb2SRobert Mustacchi 	assert(errno == EFAULT);
62*28f17cb2SRobert Mustacchi 
63*28f17cb2SRobert Mustacchi 	/* Jump through a hoop to know we'll always have a bad address */
64*28f17cb2SRobert Mustacchi 	addr = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0);
65*28f17cb2SRobert Mustacchi 	assert(addr != MAP_FAILED);
66*28f17cb2SRobert Mustacchi 	ret = munmap(addr, 4096);
67*28f17cb2SRobert Mustacchi 	assert(ret == 0);
68*28f17cb2SRobert Mustacchi 	ret = getrandom(addr, sizeof (buf), 0);
69*28f17cb2SRobert Mustacchi 	assert(ret == -1);
70*28f17cb2SRobert Mustacchi 	assert(errno == EFAULT);
71*28f17cb2SRobert Mustacchi 	ret = getrandom(addr, sizeof (buf), GRND_RANDOM);
72*28f17cb2SRobert Mustacchi 	assert(ret == -1);
73*28f17cb2SRobert Mustacchi 	assert(errno == EFAULT);
74*28f17cb2SRobert Mustacchi 
75*28f17cb2SRobert Mustacchi 	/* Verify that we get rounded down on a getrandom of /dev/random */
76*28f17cb2SRobert Mustacchi 	ret = getrandom(bigbuf, sizeof (buf), GRND_RANDOM);
77*28f17cb2SRobert Mustacchi 	assert(ret >= 0 && ret < sizeof (bigbuf));
78*28f17cb2SRobert Mustacchi 
79*28f17cb2SRobert Mustacchi 	/* Do a few simple sets where we know we should get data */
80*28f17cb2SRobert Mustacchi 	ret = getrandom(buf, sizeof (buf), 0);
81*28f17cb2SRobert Mustacchi 	assert(ret == sizeof (buf));
82*28f17cb2SRobert Mustacchi 	ret = getrandom(buf, sizeof (buf), GRND_RANDOM);
83*28f17cb2SRobert Mustacchi 	assert(ret == sizeof (buf));
84*28f17cb2SRobert Mustacchi 
85*28f17cb2SRobert Mustacchi 	return (0);
86*28f17cb2SRobert Mustacchi }
87