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 = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 01444);
59 }
60
cleanup(void)61 static void cleanup(void)
62 {
63 if (temp_fd != -1)
64 SAFE_CLOSE(temp_fd);
65 if (fd != -1)
66 SAFE_CLOSE(fd);
67 }
68
run(unsigned int n)69 static void run(unsigned int n)
70 {
71 struct tcase *tc = &tcases[n];
72
73 TEST(fsconfig(*tc->fd, tc->cmd, tc->key, tc->value, *tc->aux));
74
75 if (TST_RET != -1) {
76 tst_res(TFAIL, "%s: fsconfig() succeeded unexpectedly (index: %d)",
77 tc->name, n);
78 return;
79 }
80
81 if (tc->exp_errno != TST_ERR) {
82 tst_res(TFAIL | TTERRNO, "%s: fsconfig() should fail with %s",
83 tc->name, tst_strerrno(tc->exp_errno));
84 return;
85 }
86
87 tst_res(TPASS | TTERRNO, "%s: fsconfig() failed as expected", tc->name);
88 }
89
90 static struct tst_test test = {
91 .tcnt = ARRAY_SIZE(tcases),
92 .test = run,
93 .setup = setup,
94 .cleanup = cleanup,
95 .needs_root = 1,
96 .needs_device = 1,
97 };
98