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) 2020 Viresh Kumar <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * Basic openat2() test to check various failures.
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
8*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/openat2.h"
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "test_file"
11*49cdfc7eSAndroid Build Coastguard Worker
12*49cdfc7eSAndroid Build Coastguard Worker static struct open_how *how;
13*49cdfc7eSAndroid Build Coastguard Worker static struct open_how_pad *phow;
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
16*49cdfc7eSAndroid Build Coastguard Worker const char *name;
17*49cdfc7eSAndroid Build Coastguard Worker int dfd;
18*49cdfc7eSAndroid Build Coastguard Worker const char *pathname;
19*49cdfc7eSAndroid Build Coastguard Worker uint64_t flags;
20*49cdfc7eSAndroid Build Coastguard Worker uint64_t mode;
21*49cdfc7eSAndroid Build Coastguard Worker uint64_t resolve;
22*49cdfc7eSAndroid Build Coastguard Worker struct open_how **how;
23*49cdfc7eSAndroid Build Coastguard Worker size_t size;
24*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
25*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
26*49cdfc7eSAndroid Build Coastguard Worker {"invalid-dfd", -1, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, 0, &how, sizeof(*how), EBADF},
27*49cdfc7eSAndroid Build Coastguard Worker {"invalid-pathname", AT_FDCWD, NULL, O_RDONLY | O_CREAT, S_IRUSR, 0, &how, sizeof(*how), EFAULT},
28*49cdfc7eSAndroid Build Coastguard Worker {"invalid-flags", AT_FDCWD, TEST_FILE, O_RDONLY, S_IWUSR, 0, &how, sizeof(*how), EINVAL},
29*49cdfc7eSAndroid Build Coastguard Worker {"invalid-mode", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, -1, 0, &how, sizeof(*how), EINVAL},
30*49cdfc7eSAndroid Build Coastguard Worker {"invalid-resolve", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, -1, &how, sizeof(*how), EINVAL},
31*49cdfc7eSAndroid Build Coastguard Worker {"invalid-size-zero", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, 0, &how, 0, EINVAL},
32*49cdfc7eSAndroid Build Coastguard Worker {"invalid-size-small", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, 0, &how, sizeof(*how) - 1, EINVAL},
33*49cdfc7eSAndroid Build Coastguard Worker {"invalid-size-big", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, 0, &how, sizeof(*how) + 1, EFAULT},
34*49cdfc7eSAndroid Build Coastguard Worker {"invalid-size-big-with-pad", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, 0, (struct open_how **)&phow, sizeof(*how) + 8, E2BIG},
35*49cdfc7eSAndroid Build Coastguard Worker };
36*49cdfc7eSAndroid Build Coastguard Worker
setup(void)37*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker openat2_supported_by_kernel();
40*49cdfc7eSAndroid Build Coastguard Worker phow->pad = 0xDEAD;
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int n)43*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
46*49cdfc7eSAndroid Build Coastguard Worker struct open_how *myhow = *tc->how;
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker myhow->flags = tc->flags;
49*49cdfc7eSAndroid Build Coastguard Worker myhow->mode = tc->mode;
50*49cdfc7eSAndroid Build Coastguard Worker myhow->resolve = tc->resolve;
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker TEST(openat2(tc->dfd, tc->pathname, myhow, tc->size));
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET >= 0) {
55*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(TST_RET);
56*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s: openat2() passed unexpectedly",
57*49cdfc7eSAndroid Build Coastguard Worker tc->name);
58*49cdfc7eSAndroid Build Coastguard Worker return;
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_errno != TST_ERR) {
62*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "%s: openat2() should fail with %s",
63*49cdfc7eSAndroid Build Coastguard Worker tc->name, tst_strerrno(tc->exp_errno));
64*49cdfc7eSAndroid Build Coastguard Worker return;
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "%s: openat2() failed as expected", tc->name);
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
71*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
72*49cdfc7eSAndroid Build Coastguard Worker .test = run,
73*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
74*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
75*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers []) {
76*49cdfc7eSAndroid Build Coastguard Worker {&how, .size = sizeof(*how)},
77*49cdfc7eSAndroid Build Coastguard Worker {&phow, .size = sizeof(*phow)},
78*49cdfc7eSAndroid Build Coastguard Worker {},
79*49cdfc7eSAndroid Build Coastguard Worker }
80*49cdfc7eSAndroid Build Coastguard Worker };
81