xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getrandom/getrandom05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
4  * Copyright (c) Linux Test Project, 2024
5  * Author: Yang Xu <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that getrandom(2) fails with
12  *
13  * - EFAULT when buf address is outside the accessible address space
14  * - EINVAL when flag is invalid
15  */
16 
17 #include "tst_test.h"
18 #include "lapi/getrandom.h"
19 
20 static char buff_efault[64];
21 static char buff_einval[64];
22 
23 static struct test_case_t {
24 	char *buff;
25 	size_t size;
26 	unsigned int flag;
27 	int expected_errno;
28 	char *desc;
29 } tcases[] = {
30 	{(void *)-1, sizeof(buff_efault), 0, EFAULT,
31 		"buf address is outside the accessible address space"},
32 	{buff_einval, sizeof(buff_einval), -1, EINVAL, "flag is invalid"},
33 };
34 
verify_getrandom(unsigned int i)35 static void verify_getrandom(unsigned int i)
36 {
37 	struct test_case_t *tc = &tcases[i];
38 
39 	TST_EXP_FAIL2(getrandom(tc->buff, tc->size, tc->flag),
40 		tc->expected_errno, "%s", tc->desc);
41 }
42 
43 static struct tst_test test = {
44 	.tcnt = ARRAY_SIZE(tcases),
45 	.test = verify_getrandom,
46 };
47