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