1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4 *
5 * Basic fsmount() failure tests.
6 */
7 #include "tst_test.h"
8 #include "lapi/fsmount.h"
9
10 int fd = -1, invalid_fd = -1;
11
12 #define MNTPOINT "mntpoint"
13
14 static struct tcase {
15 char *name;
16 int *fd;
17 unsigned int flags;
18 unsigned int mount_attrs;
19 int exp_errno;
20 } tcases[] = {
21 {"invalid-fd", &invalid_fd, FSMOUNT_CLOEXEC, 0, EBADF},
22 {"invalid-flags", &fd, 0x02, 0, EINVAL},
23 {"invalid-attrs", &fd, FSMOUNT_CLOEXEC, 0x100, EINVAL},
24 };
25
cleanup(void)26 static void cleanup(void)
27 {
28 if (fd != -1)
29 SAFE_CLOSE(fd);
30 }
31
setup(void)32 static void setup(void)
33 {
34 fsopen_supported_by_kernel();
35
36 TEST(fd = fsopen(tst_device->fs_type, 0));
37 if (fd == -1)
38 tst_brk(TBROK | TTERRNO, "fsopen() failed");
39
40 TEST(fsconfig(fd, FSCONFIG_SET_STRING, "source", tst_device->dev, 0));
41 if (TST_RET == -1)
42 tst_brk(TBROK | TTERRNO, "fsconfig(FSCONFIG_SET_STRING) failed");
43
44 TEST(fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0));
45 if (TST_RET == -1)
46 tst_brk(TBROK | TTERRNO, "fsconfig(FSCONFIG_CMD_CREATE) failed");
47 }
48
run(unsigned int n)49 static void run(unsigned int n)
50 {
51 struct tcase *tc = &tcases[n];
52
53 TEST(fsmount(*tc->fd, tc->flags, tc->mount_attrs));
54 if (TST_RET != -1) {
55 SAFE_CLOSE(TST_RET);
56 tst_res(TFAIL, "%s: fsmount() succeeded unexpectedly (index: %d)",
57 tc->name, n);
58 return;
59 }
60
61 if (tc->exp_errno != TST_ERR) {
62 tst_res(TFAIL | TTERRNO, "%s: fsmount() should fail with %s",
63 tc->name, tst_strerrno(tc->exp_errno));
64 return;
65 }
66
67 tst_res(TPASS | TTERRNO, "%s: fsmount() failed as expected", tc->name);
68 }
69
70 static struct tst_test test = {
71 .tcnt = ARRAY_SIZE(tcases),
72 .test = run,
73 .setup = setup,
74 .cleanup = cleanup,
75 .needs_root = 1,
76 .mntpoint = MNTPOINT,
77 .format_device = 1,
78 .all_filesystems = 1,
79 .skip_filesystems = (const char *const []){"fuse", NULL},
80 };
81