1 /* 2 * Copyright (C) 2015 Cedric Hnyda ced.hnyda (at) gmail.com 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it is 13 * free of the rightful claim of any third person regarding infringement 14 * or the like. Any license provided herein, whether implied or 15 * otherwise, applies only to this software file. Patent licenses, if 16 * any, provided herein do not apply to combinations of this program with 17 * other software, or any other product whatsoever. 18 * 19 */ 20 21 /* 22 * AUTHOR : Cdric Hnyda 23 * DATE STARTED : 06/13/2015 24 * 25 * Calls getrandom(2), checks that the buffer is filled with random bytes 26 * and expects success. 27 * 28 */ 29 30 #include "lapi/getrandom.h" 31 #include "linux_syscall_numbers.h" 32 #include "test.h" 33 34 char *TCID = "getrandom02"; 35 static int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK, 36 GRND_RANDOM | GRND_NONBLOCK }; 37 38 int TST_TOTAL = ARRAY_SIZE(modes); 39 40 static unsigned char buf[256]; 41 static size_t size = 256; 42 43 static void fill(void); 44 static int check_content(int nb); 45 46 int main(int ac, char **av) 47 { 48 int lc, i; 49 50 tst_parse_opts(ac, av, NULL, NULL); 51 52 for (lc = 0; TEST_LOOPING(lc); lc++) { 53 tst_count = 0; 54 55 for (i = 0; i < TST_TOTAL; i++) { 56 fill(); 57 58 do { 59 TEST(ltp_syscall(__NR_getrandom, buf, size, 60 modes[i])); 61 } while ((modes[i] & GRND_NONBLOCK) && TEST_RETURN == -1 62 && TEST_ERRNO == EAGAIN); 63 64 if (!check_content(TEST_RETURN)) 65 tst_resm(TFAIL | TTERRNO, "getrandom failed"); 66 else 67 tst_resm(TPASS, "getrandom returned %ld", 68 TEST_RETURN); 69 } 70 } 71 tst_exit(); 72 } 73 74 static void fill(void) 75 { 76 memset(buf, '\0', sizeof(buf)); 77 } 78 79 static int check_content(int nb) 80 { 81 int table[256]; 82 int i, index, max; 83 84 memset(table, 0, sizeof(table)); 85 86 max = nb * 0.10; 87 88 for (i = 0; i < nb; i++) { 89 index = buf[i]; 90 table[index]++; 91 } 92 93 for (i = 0; i < nb; i++) { 94 if (max > 0 && table[i] > max) 95 return 0; 96 } 97 return 1; 98 } 99