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