Home | History | Annotate | Download | only in ioctl
      1 /*
      2  * Copyright (c) 2017 Carlo Marcelo Arenas Beln <carenas (at) gmail.com>
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 /*
     18  * Very basic test for the RND* ioctls.
     19  *
     20  * Reads the entropy available from both /proc and the ioctl and compares
     21  * they are similar enough (within a configured fuzz factor)
     22  *
     23  */
     24 
     25 #include <asm/types.h>
     26 #include <linux/random.h>
     27 #include <stdlib.h>
     28 #include "tst_test.h"
     29 
     30 static char *s_fuzz;
     31 static int fuzz = 2;
     32 static struct tst_option options[] = {
     33 	{"f:", &s_fuzz, "-f c     Fuzz factor for valid match (default 2)"},
     34 	{NULL, NULL, NULL}
     35 };
     36 static int fd;
     37 
     38 static void verify_ioctl(void)
     39 {
     40 	int cnt, pcnt;
     41 
     42 	SAFE_IOCTL(fd, RNDGETENTCNT, &cnt);
     43 	SAFE_FILE_SCANF("/proc/sys/kernel/random/entropy_avail", "%d", &pcnt);
     44 	tst_res(TINFO, "entropy value from ioctl: %d, proc: %d", cnt, pcnt);
     45 
     46 	if (abs(pcnt - cnt) <= fuzz)
     47 		tst_res(TPASS, "entropy value within expected parameters");
     48 	else
     49 		tst_res(TFAIL, "incorrect entropy value from ioctl");
     50 }
     51 
     52 static void setup(void)
     53 {
     54 	fd = SAFE_OPEN("/dev/urandom", O_RDONLY);
     55 	if (s_fuzz)
     56 		fuzz = SAFE_STRTOL(s_fuzz, 0, 4096);
     57 }
     58 
     59 static void cleanup(void)
     60 {
     61 	if (fd > 0)
     62 		SAFE_CLOSE(fd);
     63 }
     64 
     65 static struct tst_test test = {
     66 	.setup = setup,
     67 	.cleanup = cleanup,
     68 	.options = options,
     69 	.test_all = verify_ioctl,
     70 };
     71