• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Carlo Marcelo Arenas Belón <carenas@gmail.com>
4  */
5 /*
6  * Very basic test for the RND* ioctls.
7  *
8  * Reads the entropy available from both /proc and the ioctl and compares
9  * they are similar enough (within a configured fuzz factor)
10  *
11  */
12 
13 #include <asm/types.h>
14 #include <linux/random.h>
15 #include <stdlib.h>
16 #include "tst_test.h"
17 
18 static char *s_fuzz;
19 static int fuzz = 2;
20 static int fd;
21 
verify_ioctl(void)22 static void verify_ioctl(void)
23 {
24 	int cnt, pcnt;
25 
26 	SAFE_IOCTL(fd, RNDGETENTCNT, &cnt);
27 	SAFE_FILE_SCANF("/proc/sys/kernel/random/entropy_avail", "%d", &pcnt);
28 	tst_res(TINFO, "entropy value from ioctl: %d, proc: %d", cnt, pcnt);
29 
30 	if (abs(pcnt - cnt) <= fuzz)
31 		tst_res(TPASS, "entropy value within expected parameters");
32 	else
33 		tst_res(TFAIL, "incorrect entropy value from ioctl");
34 }
35 
setup(void)36 static void setup(void)
37 {
38 	fd = SAFE_OPEN("/dev/urandom", O_RDONLY);
39 	if (s_fuzz)
40 		fuzz = SAFE_STRTOL(s_fuzz, 0, 4096);
41 }
42 
cleanup(void)43 static void cleanup(void)
44 {
45 	if (fd > 0)
46 		SAFE_CLOSE(fd);
47 }
48 
49 static struct tst_test test = {
50 	.setup = setup,
51 	.cleanup = cleanup,
52 	.options = (struct tst_option[]) {
53 		{"f:", &s_fuzz, "Fuzz factor for valid match (default 2)"},
54 		{}
55 	},
56 	.test_all = verify_ioctl,
57 };
58