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