xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getrandom/getrandom03.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) 2015 Cedric Hnyda <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Calls getrandom(2), check that the return value is equal to the
6*49cdfc7eSAndroid Build Coastguard Worker  * number of bytes required and expects success.
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
10*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/getrandom.h"
11*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #define MAX_SIZE 256
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker static unsigned int sizes[] = {
16*49cdfc7eSAndroid Build Coastguard Worker 	1,
17*49cdfc7eSAndroid Build Coastguard Worker 	2,
18*49cdfc7eSAndroid Build Coastguard Worker 	3,
19*49cdfc7eSAndroid Build Coastguard Worker 	7,
20*49cdfc7eSAndroid Build Coastguard Worker 	8,
21*49cdfc7eSAndroid Build Coastguard Worker 	15,
22*49cdfc7eSAndroid Build Coastguard Worker 	22,
23*49cdfc7eSAndroid Build Coastguard Worker 	64,
24*49cdfc7eSAndroid Build Coastguard Worker 	127,
25*49cdfc7eSAndroid Build Coastguard Worker };
26*49cdfc7eSAndroid Build Coastguard Worker 
verify_getrandom(unsigned int n)27*49cdfc7eSAndroid Build Coastguard Worker static void verify_getrandom(unsigned int n)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	char buf[MAX_SIZE];
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_getrandom, buf, sizes[n], 0));
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != sizes[n]) {
34*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "getrandom returned %li, expected %u",
35*49cdfc7eSAndroid Build Coastguard Worker 			TST_RET, sizes[n]);
36*49cdfc7eSAndroid Build Coastguard Worker 	} else {
37*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "getrandom returned %ld", TST_RET);
38*49cdfc7eSAndroid Build Coastguard Worker 	}
39*49cdfc7eSAndroid Build Coastguard Worker }
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
42*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(sizes),
43*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_getrandom,
44*49cdfc7eSAndroid Build Coastguard Worker };
45