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 <xuyang2018.jy@fujitsu.com>
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 #include "getrandom_var.h"
20
21 static char buff_efault[64];
22 static char buff_einval[64];
23
24 static struct test_case_t {
25 char *buff;
26 size_t size;
27 unsigned int flag;
28 int expected_errno;
29 char *desc;
30 } tcases[] = {
31 {(void *)-1, sizeof(buff_efault), 0, EFAULT,
32 "buf address is outside the accessible address space"},
33 {buff_einval, sizeof(buff_einval), -1, EINVAL, "flag is invalid"},
34 };
35
setup(void)36 static void setup(void)
37 {
38 getrandom_info();
39 }
40
verify_getrandom(unsigned int i)41 static void verify_getrandom(unsigned int i)
42 {
43 struct test_case_t *tc = &tcases[i];
44
45 /* EFAULT test can segfault on recent glibc, skip it */
46 if (tst_variant == 1 && tc->expected_errno == EFAULT) {
47 tst_res(TCONF, "Skipping EFAULT test for libc getrandom()");
48 return;
49 }
50
51 TST_EXP_FAIL2(do_getrandom(tc->buff, tc->size, tc->flag),
52 tc->expected_errno, "%s", tc->desc);
53 }
54
55 static struct tst_test test = {
56 .tcnt = ARRAY_SIZE(tcases),
57 .test = verify_getrandom,
58 .test_variants = TEST_VARIANTS,
59 .setup = setup,
60 };
61