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 fspick() failure tests.
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci#include "tst_test.h"
8f08c3bdfSopenharmony_ci#include "lapi/fsmount.h"
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci#define MNTPOINT		"mntpoint"
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_cistatic struct tcase {
13f08c3bdfSopenharmony_ci	char *name;
14f08c3bdfSopenharmony_ci	int dirfd;
15f08c3bdfSopenharmony_ci	const char *pathname;
16f08c3bdfSopenharmony_ci	unsigned int flags;
17f08c3bdfSopenharmony_ci	int exp_errno;
18f08c3bdfSopenharmony_ci} tcases[] = {
19f08c3bdfSopenharmony_ci	{"invalid-fd", -1, MNTPOINT, FSPICK_NO_AUTOMOUNT | FSPICK_CLOEXEC, EBADF},
20f08c3bdfSopenharmony_ci	{"invalid-path", AT_FDCWD, "invalid", FSPICK_NO_AUTOMOUNT | FSPICK_CLOEXEC, ENOENT},
21f08c3bdfSopenharmony_ci	{"invalid-flags", AT_FDCWD, MNTPOINT, 0x10, EINVAL},
22f08c3bdfSopenharmony_ci};
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic void run(unsigned int n)
25f08c3bdfSopenharmony_ci{
26f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci	TEST(fspick(tc->dirfd, tc->pathname, tc->flags));
29f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
30f08c3bdfSopenharmony_ci		SAFE_CLOSE(TST_RET);
31f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s: fspick() succeeded unexpectedly (index: %d)",
32f08c3bdfSopenharmony_ci			tc->name, n);
33f08c3bdfSopenharmony_ci		return;
34f08c3bdfSopenharmony_ci	}
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	if (tc->exp_errno != TST_ERR) {
37f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "%s: fspick() should fail with %s",
38f08c3bdfSopenharmony_ci			tc->name, tst_strerrno(tc->exp_errno));
39f08c3bdfSopenharmony_ci		return;
40f08c3bdfSopenharmony_ci	}
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	tst_res(TPASS | TTERRNO, "%s: fspick() failed as expected", tc->name);
43f08c3bdfSopenharmony_ci}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic struct tst_test test = {
46f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
47f08c3bdfSopenharmony_ci	.test = run,
48f08c3bdfSopenharmony_ci	.setup = fsopen_supported_by_kernel,
49f08c3bdfSopenharmony_ci	.needs_root = 1,
50f08c3bdfSopenharmony_ci	.mount_device = 1,
51f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
52f08c3bdfSopenharmony_ci	.all_filesystems = 1,
53f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *const []){"fuse", NULL},
54f08c3bdfSopenharmony_ci};
55