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 fsmount() failure tests.
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci#include "tst_test.h"
8f08c3bdfSopenharmony_ci#include "lapi/fsmount.h"
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ciint fd = -1, invalid_fd = -1;
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#define MNTPOINT	"mntpoint"
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_cistatic struct tcase {
15f08c3bdfSopenharmony_ci	char *name;
16f08c3bdfSopenharmony_ci	int *fd;
17f08c3bdfSopenharmony_ci	unsigned int flags;
18f08c3bdfSopenharmony_ci	unsigned int mount_attrs;
19f08c3bdfSopenharmony_ci	int exp_errno;
20f08c3bdfSopenharmony_ci} tcases[] = {
21f08c3bdfSopenharmony_ci	{"invalid-fd", &invalid_fd, FSMOUNT_CLOEXEC, 0, EBADF},
22f08c3bdfSopenharmony_ci	{"invalid-flags", &fd, 0x02, 0, EINVAL},
23f08c3bdfSopenharmony_ci	{"invalid-attrs", &fd, FSMOUNT_CLOEXEC, 0x100, EINVAL},
24f08c3bdfSopenharmony_ci};
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic void cleanup(void)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	if (fd != -1)
29f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
30f08c3bdfSopenharmony_ci}
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_cistatic void setup(void)
33f08c3bdfSopenharmony_ci{
34f08c3bdfSopenharmony_ci	fsopen_supported_by_kernel();
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	TEST(fd = fsopen(tst_device->fs_type, 0));
37f08c3bdfSopenharmony_ci	if (fd == -1)
38f08c3bdfSopenharmony_ci		tst_brk(TBROK | TTERRNO, "fsopen() failed");
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	TEST(fsconfig(fd, FSCONFIG_SET_STRING, "source", tst_device->dev, 0));
41f08c3bdfSopenharmony_ci	if (TST_RET == -1)
42f08c3bdfSopenharmony_ci		tst_brk(TBROK | TTERRNO, "fsconfig(FSCONFIG_SET_STRING) failed");
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	TEST(fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0));
45f08c3bdfSopenharmony_ci	if (TST_RET == -1)
46f08c3bdfSopenharmony_ci		tst_brk(TBROK | TTERRNO, "fsconfig(FSCONFIG_CMD_CREATE) failed");
47f08c3bdfSopenharmony_ci}
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_cistatic void run(unsigned int n)
50f08c3bdfSopenharmony_ci{
51f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	TEST(fsmount(*tc->fd, tc->flags, tc->mount_attrs));
54f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
55f08c3bdfSopenharmony_ci		SAFE_CLOSE(TST_RET);
56f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s: fsmount() succeeded unexpectedly (index: %d)",
57f08c3bdfSopenharmony_ci			tc->name, n);
58f08c3bdfSopenharmony_ci		return;
59f08c3bdfSopenharmony_ci	}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	if (tc->exp_errno != TST_ERR) {
62f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "%s: fsmount() 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: fsmount() 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	.cleanup = cleanup,
75f08c3bdfSopenharmony_ci	.needs_root = 1,
76f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
77f08c3bdfSopenharmony_ci	.format_device = 1,
78f08c3bdfSopenharmony_ci	.all_filesystems = 1,
79f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *const []){"fuse", NULL},
80f08c3bdfSopenharmony_ci};
81