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 move_mount() failure tests.
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci#include "tst_test.h"
8f08c3bdfSopenharmony_ci#include "lapi/fsmount.h"
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci#define MNTPOINT	"mntpoint"
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ciint invalid_fd = -1, fsmfd;
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_cistatic struct tcase {
15f08c3bdfSopenharmony_ci	char *name;
16f08c3bdfSopenharmony_ci	int *from_dirfd;
17f08c3bdfSopenharmony_ci	const char *from_pathname;
18f08c3bdfSopenharmony_ci	int to_dirfd;
19f08c3bdfSopenharmony_ci	const char *to_pathname;
20f08c3bdfSopenharmony_ci	unsigned int flags;
21f08c3bdfSopenharmony_ci	int exp_errno;
22f08c3bdfSopenharmony_ci} tcases[] = {
23f08c3bdfSopenharmony_ci	{"invalid-from-fd", &invalid_fd, "", AT_FDCWD, MNTPOINT, MOVE_MOUNT_F_EMPTY_PATH, EBADF},
24f08c3bdfSopenharmony_ci	{"invalid-from-path", &fsmfd, "invalid", AT_FDCWD, MNTPOINT, MOVE_MOUNT_F_EMPTY_PATH, ENOENT},
25f08c3bdfSopenharmony_ci	{"invalid-to-fd", &fsmfd, "", -1, MNTPOINT, MOVE_MOUNT_F_EMPTY_PATH, EBADF},
26f08c3bdfSopenharmony_ci	{"invalid-to-path", &fsmfd, "", AT_FDCWD, "invalid", MOVE_MOUNT_F_EMPTY_PATH, ENOENT},
27f08c3bdfSopenharmony_ci	{"invalid-flags", &fsmfd, "", AT_FDCWD, MNTPOINT, 0x08, EINVAL},
28f08c3bdfSopenharmony_ci};
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic void run(unsigned int n)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
33f08c3bdfSopenharmony_ci	int fd;
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	TEST(fd = fsopen(tst_device->fs_type, 0));
36f08c3bdfSopenharmony_ci	if (fd == -1) {
37f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "fsopen() failed");
38f08c3bdfSopenharmony_ci		return;
39f08c3bdfSopenharmony_ci	}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	TEST(fsconfig(fd, FSCONFIG_SET_STRING, "source", tst_device->dev, 0));
42f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
43f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
44f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "fsconfig(FSCONFIG_SET_STRING) failed");
45f08c3bdfSopenharmony_ci		return;
46f08c3bdfSopenharmony_ci	}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	TEST(fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0));
49f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
50f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
51f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "fsconfig(FSCONFIG_CMD_CREATE) failed");
52f08c3bdfSopenharmony_ci		return;
53f08c3bdfSopenharmony_ci	}
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	TEST(fsmfd = fsmount(fd, 0, 0));
56f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	if (fsmfd == -1) {
59f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "fsmount() failed");
60f08c3bdfSopenharmony_ci		return;
61f08c3bdfSopenharmony_ci	}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	TEST(move_mount(*tc->from_dirfd, tc->from_pathname, tc->to_dirfd,
64f08c3bdfSopenharmony_ci			tc->to_pathname, tc->flags));
65f08c3bdfSopenharmony_ci	SAFE_CLOSE(fsmfd);
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
68f08c3bdfSopenharmony_ci		SAFE_UMOUNT(MNTPOINT);
69f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s: move_mount() succeeded unexpectedly (index: %d)",
70f08c3bdfSopenharmony_ci			tc->name, n);
71f08c3bdfSopenharmony_ci		return;
72f08c3bdfSopenharmony_ci	}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	if (tc->exp_errno != TST_ERR) {
75f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "%s: move_mount() should fail with %s",
76f08c3bdfSopenharmony_ci			tc->name, tst_strerrno(tc->exp_errno));
77f08c3bdfSopenharmony_ci		return;
78f08c3bdfSopenharmony_ci	}
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	tst_res(TPASS | TTERRNO, "%s: move_mount() failed as expected", tc->name);
81f08c3bdfSopenharmony_ci}
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_cistatic struct tst_test test = {
84f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
85f08c3bdfSopenharmony_ci	.test = run,
86f08c3bdfSopenharmony_ci	.setup = fsopen_supported_by_kernel,
87f08c3bdfSopenharmony_ci	.needs_root = 1,
88f08c3bdfSopenharmony_ci	.format_device = 1,
89f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
90f08c3bdfSopenharmony_ci	.all_filesystems = 1,
91f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *const []){"fuse", NULL},
92f08c3bdfSopenharmony_ci};
93