xref: /titanic_44/usr/src/test/libc-tests/tests/random/getentropy.c (revision a60349c89adffc0902b2353230891d8e7f2b24d9)
1 /*
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
5  * 1.0 of the CDDL.
6  *
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.
10  */
11 
12 /*
13  * Copyright (c) 2015, Joyent, Inc.
14  */
15 
16 /*
17  * Test getentropy(3C)
18  */
19 
20 #include <unistd.h>
21 #include <sys/mman.h>
22 #include <assert.h>
23 #include <errno.h>
24 
25 int
26 main(void)
27 {
28 	int ret;
29 	void *addr;
30 	uint8_t errbuf[512];
31 	uint8_t buf[128];
32 
33 	ret = getentropy(buf, sizeof (buf));
34 	assert(ret == 0);
35 
36 	/* Max buffer is 256 bytes, verify if we go larger, we error */
37 	ret = getentropy(errbuf, sizeof (errbuf));
38 	assert(ret == -1);
39 	assert(errno == EIO);
40 
41 	ret = getentropy(errbuf, 257);
42 	assert(ret == -1);
43 	assert(errno == EIO);
44 
45 	ret = getentropy(errbuf, 256);
46 	assert(ret == 0);
47 
48 	ret = getentropy(errbuf, 0);
49 	assert(ret == 0);
50 
51 	/* Bad buffers */
52 	ret = getentropy(NULL, sizeof (buf));
53 	assert(ret == -1);
54 	assert(errno == EFAULT);
55 
56 	/* Jump through a hoop to know we'll always have a bad address */
57 	addr = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0);
58 	assert(addr != MAP_FAILED);
59 	ret = munmap(addr, 4096);
60 	assert(ret == 0);
61 	ret = getentropy(addr, sizeof (buf));
62 	assert(ret == -1);
63 	assert(errno == EFAULT);
64 
65 	return (0);
66 }
67