xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fsopen/fsopen02.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 fsopen() 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 const char *invalid_fs = "invalid";
11*49cdfc7eSAndroid Build Coastguard Worker const char *valid_fs;
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 	const char **fs;
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-fs", &invalid_fs, 0, ENODEV},
20*49cdfc7eSAndroid Build Coastguard Worker 	{"invalid-flags", &valid_fs, 0x10, EINVAL},
21*49cdfc7eSAndroid Build Coastguard Worker };
22*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)23*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
24*49cdfc7eSAndroid Build Coastguard Worker {
25*49cdfc7eSAndroid Build Coastguard Worker 	fsopen_supported_by_kernel();
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	valid_fs = tst_device->fs_type;
28*49cdfc7eSAndroid Build Coastguard Worker }
29*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)30*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fsopen(*tc->fs, tc->flags));
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
37*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(TST_RET);
38*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s: fsopen() succeeded unexpectedly (index: %d)",
39*49cdfc7eSAndroid Build Coastguard Worker 			tc->name, n);
40*49cdfc7eSAndroid Build Coastguard Worker 		return;
41*49cdfc7eSAndroid Build Coastguard Worker 	}
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_errno != TST_ERR) {
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "%s: fsopen() should fail with %s",
45*49cdfc7eSAndroid Build Coastguard Worker 			tc->name, tst_strerrno(tc->exp_errno));
46*49cdfc7eSAndroid Build Coastguard Worker 		return;
47*49cdfc7eSAndroid Build Coastguard Worker 	}
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS | TTERRNO, "%s: fsopen() failed as expected", tc->name);
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
53*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
54*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
55*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
56*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
57*49cdfc7eSAndroid Build Coastguard Worker 	.needs_device = 1,
58*49cdfc7eSAndroid Build Coastguard Worker };
59