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