1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4 *
5 * Basic fsconfig() failure tests.
6 */
7 #include "tst_test.h"
8 #include "lapi/fsmount.h"
9
10 static int fd = -1, temp_fd = -1, invalid_fd = -1;
11 static int aux_0 = 0, aux_1 = 1, aux_fdcwd = AT_FDCWD, aux_minus1 = -1;
12
13 static struct tcase {
14 char *name;
15 int *fd;
16 unsigned int cmd;
17 const char *key;
18 const void *value;
19 int *aux;
20 int exp_errno;
21 } tcases[] = {
22 {"invalid-fd", &invalid_fd, FSCONFIG_SET_FLAG, "user_xattr", NULL, &aux_0, EINVAL},
23 {"invalid-cmd", &fd, 100, "rw", NULL, &aux_0, EOPNOTSUPP},
24 {"set-flag-key", &fd, FSCONFIG_SET_FLAG, NULL, NULL, &aux_0, EINVAL},
25 {"set-flag-value", &fd, FSCONFIG_SET_FLAG, "rw", "foo", &aux_0, EINVAL},
26 {"set-flag-aux", &fd, FSCONFIG_SET_FLAG, "rw", NULL, &aux_1, EINVAL},
27 {"set-string-key", &fd, FSCONFIG_SET_STRING, NULL, "#grand.central.org:root.cell.", &aux_0, EINVAL},
28 {"set-string-value", &fd, FSCONFIG_SET_STRING, "source", NULL, &aux_0, EINVAL},
29 {"set-string-aux", &fd, FSCONFIG_SET_STRING, "source", "#grand.central.org:root.cell.", &aux_1, EINVAL},
30 {"set-binary-key", &fd, FSCONFIG_SET_BINARY, NULL, "foo", &aux_1, EINVAL},
31 {"set-binary-value", &fd, FSCONFIG_SET_BINARY, "sync", NULL, &aux_1, EINVAL},
32 {"set-binary-aux", &fd, FSCONFIG_SET_BINARY, "sync", "foo", &aux_0, EINVAL},
33 {"set-path-key", &fd, FSCONFIG_SET_PATH, NULL, "/dev/foo", &aux_fdcwd, EINVAL},
34 {"set-path-value", &fd, FSCONFIG_SET_PATH, "sync", NULL, &aux_fdcwd, EINVAL},
35 {"set-path-aux", &fd, FSCONFIG_SET_PATH, "sync", "/dev/foo", &aux_minus1, EINVAL},
36 {"set-path-empty-key", &fd, FSCONFIG_SET_PATH_EMPTY, NULL, "/dev/foo", &aux_fdcwd, EINVAL},
37 {"set-path-empty-value", &fd, FSCONFIG_SET_PATH_EMPTY, "sync", NULL, &aux_fdcwd, EINVAL},
38 {"set-path-empty-aux", &fd, FSCONFIG_SET_PATH_EMPTY, "sync", "/dev/foo", &aux_minus1, EINVAL},
39 {"set-fd-key", &fd, FSCONFIG_SET_FD, NULL, NULL, &temp_fd, EINVAL},
40 {"set-fd-value", &fd, FSCONFIG_SET_FD, "sync", "foo", &temp_fd, EINVAL},
41 {"set-fd-aux", &fd, FSCONFIG_SET_FD, "sync", NULL, &aux_minus1, EINVAL},
42 {"cmd-create-key", &fd, FSCONFIG_CMD_CREATE, "foo", NULL, &aux_0, EINVAL},
43 {"cmd-create-value", &fd, FSCONFIG_CMD_CREATE, NULL, "foo", &aux_0, EINVAL},
44 {"cmd-create-aux", &fd, FSCONFIG_CMD_CREATE, NULL, NULL, &aux_1, EINVAL},
45 {"cmd-reconfigure-key", &fd, FSCONFIG_CMD_RECONFIGURE, "foo", NULL, &aux_0, EINVAL},
46 {"cmd-reconfigure-value", &fd, FSCONFIG_CMD_RECONFIGURE, NULL, "foo", &aux_0, EINVAL},
47 {"cmd-reconfigure-aux", &fd, FSCONFIG_CMD_RECONFIGURE, NULL, NULL, &aux_1, EINVAL},
48 };
49
setup(void)50 static void setup(void)
51 {
52 fsopen_supported_by_kernel();
53
54 TEST(fd = fsopen(tst_device->fs_type, 0));
55 if (fd == -1)
56 tst_brk(TBROK | TTERRNO, "fsopen() failed");
57
58 temp_fd = open("testfile", O_RDWR | O_CREAT, 01444);
59 if (temp_fd == -1)
60 tst_brk(TBROK, "Can't obtain temp_fd, open() failed");
61 }
62
cleanup(void)63 static void cleanup(void)
64 {
65 if (temp_fd != -1)
66 SAFE_CLOSE(temp_fd);
67 if (fd != -1)
68 SAFE_CLOSE(fd);
69 }
70
run(unsigned int n)71 static void run(unsigned int n)
72 {
73 struct tcase *tc = &tcases[n];
74
75 TEST(fsconfig(*tc->fd, tc->cmd, tc->key, tc->value, *tc->aux));
76
77 if (TST_RET != -1) {
78 tst_res(TFAIL, "%s: fsconfig() succeeded unexpectedly (index: %d)",
79 tc->name, n);
80 return;
81 }
82
83 if (tc->exp_errno != TST_ERR) {
84 tst_res(TFAIL | TTERRNO, "%s: fsconfig() should fail with %s",
85 tc->name, tst_strerrno(tc->exp_errno));
86 return;
87 }
88
89 tst_res(TPASS | TTERRNO, "%s: fsconfig() failed as expected", tc->name);
90 }
91
92 static struct tst_test test = {
93 .tcnt = ARRAY_SIZE(tcases),
94 .test = run,
95 .setup = setup,
96 .cleanup = cleanup,
97 .needs_tmpdir = 1,
98 .needs_root = 1,
99 .needs_device = 1,
100 };
101