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.
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#define TEST_DIR "test_dir"
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_cistatic struct open_how *how;
14f08c3bdfSopenharmony_cistatic struct open_how_pad *phow;
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic int dir_fd = -1, fd_atcwd = AT_FDCWD;
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic struct tcase {
19f08c3bdfSopenharmony_ci	int *dfd;
20f08c3bdfSopenharmony_ci	const char *pathname;
21f08c3bdfSopenharmony_ci	uint64_t flags;
22f08c3bdfSopenharmony_ci	uint64_t mode;
23f08c3bdfSopenharmony_ci	uint64_t resolve;
24f08c3bdfSopenharmony_ci	struct open_how **how;
25f08c3bdfSopenharmony_ci	size_t size;
26f08c3bdfSopenharmony_ci} tcases[] = {
27f08c3bdfSopenharmony_ci	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, 0, &how, sizeof(*how)},
28f08c3bdfSopenharmony_ci	{&dir_fd, TEST_FILE, O_RDONLY, S_IRUSR, 0, &how, sizeof(*how)},
29f08c3bdfSopenharmony_ci	{&dir_fd, TEST_FILE, O_WRONLY, S_IWUSR, 0, &how, sizeof(*how)},
30f08c3bdfSopenharmony_ci	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_XDEV, &how, sizeof(*how)},
31f08c3bdfSopenharmony_ci	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_MAGICLINKS, &how, sizeof(*how)},
32f08c3bdfSopenharmony_ci	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_SYMLINKS, &how, sizeof(*how)},
33f08c3bdfSopenharmony_ci	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_BENEATH, &how, sizeof(*how)},
34f08c3bdfSopenharmony_ci	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_IN_ROOT, &how, sizeof(*how)},
35f08c3bdfSopenharmony_ci	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, 0, &how, sizeof(*how)},
36f08c3bdfSopenharmony_ci	{&fd_atcwd, TEST_FILE, O_RDONLY, S_IRUSR, 0, &how, sizeof(*how)},
37f08c3bdfSopenharmony_ci	{&fd_atcwd, TEST_FILE, O_WRONLY, S_IWUSR, 0, &how, sizeof(*how)},
38f08c3bdfSopenharmony_ci	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_XDEV, &how, sizeof(*how)},
39f08c3bdfSopenharmony_ci	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_MAGICLINKS, &how, sizeof(*how)},
40f08c3bdfSopenharmony_ci	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_SYMLINKS, &how, sizeof(*how)},
41f08c3bdfSopenharmony_ci	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_BENEATH, &how, sizeof(*how)},
42f08c3bdfSopenharmony_ci	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_IN_ROOT, (struct open_how **)&phow, sizeof(*how) + 8},
43f08c3bdfSopenharmony_ci};
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic void cleanup(void)
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	if (dir_fd != -1)
48f08c3bdfSopenharmony_ci		SAFE_CLOSE(dir_fd);
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	openat2_supported_by_kernel();
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	phow->pad = 0x00;
56f08c3bdfSopenharmony_ci	SAFE_MKDIR(TEST_DIR, 0700);
57f08c3bdfSopenharmony_ci	dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic void run(unsigned int n)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	int fd;
63f08c3bdfSopenharmony_ci	struct stat file_stat;
64f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
65f08c3bdfSopenharmony_ci	struct open_how *myhow = *tc->how;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	myhow->flags = tc->flags | O_CREAT;
68f08c3bdfSopenharmony_ci	myhow->mode = tc->mode;
69f08c3bdfSopenharmony_ci	myhow->resolve = tc->resolve;
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	TEST(fd = openat2(*tc->dfd, tc->pathname, myhow, tc->size));
72f08c3bdfSopenharmony_ci	if (fd < 0) {
73f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "openat2() failed (%d)", n);
74f08c3bdfSopenharmony_ci		return;
75f08c3bdfSopenharmony_ci	}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	SAFE_FSTAT(fd, &file_stat);
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci	if (file_stat.st_size == 0)
80f08c3bdfSopenharmony_ci		tst_res(TPASS, "openat2() passed (%d)", n);
81f08c3bdfSopenharmony_ci	else
82f08c3bdfSopenharmony_ci		tst_res(TFAIL, "fstat() didn't work as expected (%d)", n);
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
85f08c3bdfSopenharmony_ci}
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_cistatic struct tst_test test = {
88f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
89f08c3bdfSopenharmony_ci	.test = run,
90f08c3bdfSopenharmony_ci	.setup = setup,
91f08c3bdfSopenharmony_ci	.cleanup = cleanup,
92f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
93f08c3bdfSopenharmony_ci	.bufs = (struct tst_buffers []) {
94f08c3bdfSopenharmony_ci		{&how, .size = sizeof(*how)},
95f08c3bdfSopenharmony_ci		{&phow, .size = sizeof(*phow)},
96f08c3bdfSopenharmony_ci		{},
97f08c3bdfSopenharmony_ci	},
98f08c3bdfSopenharmony_ci};
99