17f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda/* 27f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * Copyright (C) 2015 Cedric Hnyda ced.hnyda@gmail.com 37f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * 47f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * This program is free software; you can redistribute it and/or modify it 57f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * under the terms of version 2 of the GNU General Public License as 67f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * published by the Free Software Foundation. 77f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * 87f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * This program is distributed in the hope that it would be useful, but 97f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * WITHOUT ANY WARRANTY; without even the implied warranty of 107f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 117f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * 127f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * Further, this software is distributed without any warranty that it is 137f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * free of the rightful claim of any third person regarding infringement 147f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * or the like. Any license provided herein, whether implied or 157f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * otherwise, applies only to this software file. Patent licenses, if 167f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * any, provided herein do not apply to combinations of this program with 177f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * other software, or any other product whatsoever. 187f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * 197f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda */ 207f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda 217f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda/* 227f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * AUTHOR : Cédric Hnyda 237f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * DATE STARTED : 06/13/2015 247f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * 257f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * Calls getrandom(2), checks that the buffer is filled with random bytes 267f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * and expects success. 277f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda * 287f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda */ 297f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda 30cc634814d1881137a19287eeaa0cf6fd15ecf4daJan Stancek#include "lapi/getrandom.h" 31817d8095fbfe5ebe22b29f58b7483f3d8d5e3fccRichard Palethorpe#include "lapi/syscalls.h" 32e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis#include "tst_test.h" 337f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda 34cc634814d1881137a19287eeaa0cf6fd15ecf4daJan Stancekstatic int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK, 35cc634814d1881137a19287eeaa0cf6fd15ecf4daJan Stancek GRND_RANDOM | GRND_NONBLOCK }; 36cc634814d1881137a19287eeaa0cf6fd15ecf4daJan Stancek 37e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubisstatic int check_content(unsigned char *buf, int nb) 387f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda{ 397f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda int table[256]; 407f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda int i, index, max; 417f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda 427f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda memset(table, 0, sizeof(table)); 437f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda 44b94f958c4632aa2a9329e6c40607b8161142f0c9Jan Stancek max = 6 + nb * 0.2; 457f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda 467f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda for (i = 0; i < nb; i++) { 477f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda index = buf[i]; 487f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda table[index]++; 497f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda } 507f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda 517f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda for (i = 0; i < nb; i++) { 527f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda if (max > 0 && table[i] > max) 537f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda return 0; 547f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda } 557f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda return 1; 567f72a2c4fbfa4b4dd1c5e71f523eb4325297f97cCédric Hnyda} 57e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis 58e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubisstatic void verify_getrandom(unsigned int n) 59e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis{ 60e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis unsigned char buf[256]; 61e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis 62e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis memset(buf, 0, sizeof(buf)); 63e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis 64e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis do { 65e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis TEST(tst_syscall(__NR_getrandom, buf, sizeof(buf), modes[n])); 66e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis } while ((modes[n] & GRND_NONBLOCK) && TEST_RETURN == -1 67e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis && TEST_ERRNO == EAGAIN); 68e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis 69e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis if (!check_content(buf, TEST_RETURN)) 70e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis tst_res(TFAIL | TTERRNO, "getrandom failed"); 71e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis else 72e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis tst_res(TPASS, "getrandom returned %ld", TEST_RETURN); 73e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis} 74e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis 75e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubisstatic struct tst_test test = { 76e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis .tcnt = ARRAY_SIZE(modes), 77e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis .test = verify_getrandom, 78e0c1daf59ed138851dc91e4852b8c92d4c59af13Cyril Hrubis}; 79