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