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