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