• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Description:
20  * The testcase checks the various errnos of the unlink(2).
21  * 1) unlink() returns ENOENT if file doesn't exist.
22  * 2) unlink() returns ENOENT if path is empty.
23  * 3) unlink() returns ENOENT if path contains a non-existent file.
24  * 4) unlink() returns EFAULT if address is invalid.
25  * 5) unlink() returns ENOTDIR if path contains a regular file.
26  * 6) unlink() returns ENAMETOOLONG if path contains a regular file.
27  */
28 
29 #include <errno.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/param.h>	/* for PATH_MAX */
33 #include "tst_test.h"
34 
35 static char longpathname[PATH_MAX + 2];
36 
37 static struct test_case_t {
38 	char *name;
39 	char *desc;
40 	int exp_errno;
41 } tcases[] = {
42 	{"nonexistfile", "non-existent file", ENOENT},
43 	{"", "path is empty string", ENOENT},
44 	{"nefile/file", "path contains a non-existent file", ENOENT},
45 	{NULL, "invalid address", EFAULT},
46 	{"file/file", "path contains a regular file", ENOTDIR},
47 	{longpathname, "pathname too long", ENAMETOOLONG},
48 };
49 
verify_unlink(unsigned int n)50 static void verify_unlink(unsigned int n)
51 {
52 	struct test_case_t *tc = &tcases[n];
53 
54 	TEST(unlink(tc->name));
55 	if (TST_RET != -1) {
56 		tst_res(TFAIL, "unlink(<%s>) succeeded unexpectedly",
57 			tc->desc);
58 		return;
59 	}
60 
61 	if (TST_ERR == tc->exp_errno) {
62 		tst_res(TPASS | TTERRNO, "unlink(<%s>) failed as expected",
63 			tc->desc);
64 	} else {
65 		tst_res(TFAIL | TTERRNO,
66 			"unlink(<%s>) failed, expected errno: %s",
67 			tc->desc, tst_strerrno(tc->exp_errno));
68 	}
69 }
70 
setup(void)71 static void setup(void)
72 {
73 	unsigned int n;
74 
75 	SAFE_TOUCH("file", 0777, NULL);
76 
77 	memset(longpathname, 'a', PATH_MAX + 2);
78 
79 	for (n = 0; n < ARRAY_SIZE(tcases); n++) {
80 		if (!tcases[n].name)
81 			tcases[n].name = tst_get_bad_addr(NULL);
82 	}
83 }
84 
85 static struct tst_test test = {
86 	.needs_tmpdir = 1,
87 	.setup = setup,
88 	.tcnt = ARRAY_SIZE(tcases),
89 	.test = verify_unlink,
90 };
91