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