xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/unlink/unlink07.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) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2002-2022
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Verify that unlink(2) fails with
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - ENOENT when file does not exist
13*49cdfc7eSAndroid Build Coastguard Worker  * - ENOENT when pathname is empty
14*49cdfc7eSAndroid Build Coastguard Worker  * - ENOENT when a component in pathname does not exist
15*49cdfc7eSAndroid Build Coastguard Worker  * - EFAULT when pathname points outside the accessible address space
16*49cdfc7eSAndroid Build Coastguard Worker  * - ENOTDIR when a component used as a directory in pathname is not,
17*49cdfc7eSAndroid Build Coastguard Worker  * in fact, a directory
18*49cdfc7eSAndroid Build Coastguard Worker  * - ENAMETOOLONG when pathname is too long
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static char longpathname[PATH_MAX + 2];
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
30*49cdfc7eSAndroid Build Coastguard Worker 	char *name;
31*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
32*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
33*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
34*49cdfc7eSAndroid Build Coastguard Worker 	{"nonexistfile", "non-existent file", ENOENT},
35*49cdfc7eSAndroid Build Coastguard Worker 	{"", "path is empty string", ENOENT},
36*49cdfc7eSAndroid Build Coastguard Worker 	{"nefile/file", "path contains a non-existent file", ENOENT},
37*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, "invalid address", EFAULT},
38*49cdfc7eSAndroid Build Coastguard Worker 	{"file/file", "path contains a regular file", ENOTDIR},
39*49cdfc7eSAndroid Build Coastguard Worker 	{longpathname, "pathname too long", ENAMETOOLONG},
40*49cdfc7eSAndroid Build Coastguard Worker };
41*49cdfc7eSAndroid Build Coastguard Worker 
verify_unlink(unsigned int n)42*49cdfc7eSAndroid Build Coastguard Worker static void verify_unlink(unsigned int n)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case_t *tc = &tcases[n];
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(unlink(tc->name), tc->exp_errno, "%s", tc->desc);
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)49*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker 	size_t n;
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH("file", 0777, NULL);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	memset(longpathname, 'a', PATH_MAX + 2);
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	for (n = 0; n < ARRAY_SIZE(tcases); n++) {
58*49cdfc7eSAndroid Build Coastguard Worker 		if (!tcases[n].name)
59*49cdfc7eSAndroid Build Coastguard Worker 			tcases[n].name = tst_get_bad_addr(NULL);
60*49cdfc7eSAndroid Build Coastguard Worker 	}
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
64*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
65*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
66*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
67*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_unlink,
68*49cdfc7eSAndroid Build Coastguard Worker };
69