• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4  */
5 
6 /*
7  * Description:
8  * The testcase checks the various errnos of the unlink(2).
9  * 1) unlink() returns ENOENT if file doesn't exist.
10  * 2) unlink() returns ENOENT if path is empty.
11  * 3) unlink() returns ENOENT if path contains a non-existent file.
12  * 4) unlink() returns EFAULT if address is invalid.
13  * 5) unlink() returns ENOTDIR if path contains a regular file.
14  * 6) unlink() returns ENAMETOOLONG if path contains a regular file.
15  */
16 
17 #include <errno.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/param.h>	/* for PATH_MAX */
21 #include "tst_test.h"
22 
23 static char longpathname[PATH_MAX + 2];
24 
25 static struct test_case_t {
26 	char *name;
27 	char *desc;
28 	int exp_errno;
29 } tcases[] = {
30 	{"nonexistfile", "non-existent file", ENOENT},
31 	{"", "path is empty string", ENOENT},
32 	{"nefile/file", "path contains a non-existent file", ENOENT},
33 	{NULL, "invalid address", EFAULT},
34 	{"file/file", "path contains a regular file", ENOTDIR},
35 	{longpathname, "pathname too long", ENAMETOOLONG},
36 };
37 
verify_unlink(unsigned int n)38 static void verify_unlink(unsigned int n)
39 {
40 	struct test_case_t *tc = &tcases[n];
41 
42 	TEST(unlink(tc->name));
43 	if (TST_RET != -1) {
44 		tst_res(TFAIL, "unlink(<%s>) succeeded unexpectedly",
45 			tc->desc);
46 		return;
47 	}
48 
49 	if (TST_ERR == tc->exp_errno) {
50 		tst_res(TPASS | TTERRNO, "unlink(<%s>) failed as expected",
51 			tc->desc);
52 	} else {
53 		tst_res(TFAIL | TTERRNO,
54 			"unlink(<%s>) failed, expected errno: %s",
55 			tc->desc, tst_strerrno(tc->exp_errno));
56 	}
57 }
58 
setup(void)59 static void setup(void)
60 {
61 	unsigned int n;
62 
63 	SAFE_TOUCH("file", 0777, NULL);
64 
65 	memset(longpathname, 'a', PATH_MAX + 2);
66 
67 	for (n = 0; n < ARRAY_SIZE(tcases); n++) {
68 		if (!tcases[n].name)
69 			tcases[n].name = tst_get_bad_addr(NULL);
70 	}
71 }
72 
73 static struct tst_test test = {
74 	.needs_tmpdir = 1,
75 	.setup = setup,
76 	.tcnt = ARRAY_SIZE(tcases),
77 	.test = verify_unlink,
78 };
79