1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 * AUTHOR : Richard Logan
4 * CO-PILOT : William Roske
5 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it would be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Negative test cases for link(2).
39 *
40 * This test program should contain test cases where link will fail regardless
41 * of who executed it (i.e. joe-user or root)
42 */
43 #include <sys/types.h>
44 #include <fcntl.h>
45 #include <sys/stat.h>
46 #include <errno.h>
47 #include <string.h>
48 #include <signal.h>
49 #include <sys/param.h>
50 #include <sys/mman.h>
51 #include "test.h"
52 #include "safe_macros.h"
53
54 static char longpath[PATH_MAX + 2];
55
56 struct test_case_t {
57 char *file1;
58 char *desc1;
59 char *file2;
60 char *desc2;
61 int exp_errno;
62 } test_cases[] = {
63 /* first path is invalid */
64 {"nonexistfile", "non-existent file", "nefile", "nefile", ENOENT},
65 {"", "path is empty string", "nefile", "nefile", ENOENT},
66 {"neefile/file", "path contains a non-existent file", "nefile",
67 "nefile", ENOENT},
68 {"regfile/file", "path contains a regular file", "nefile", "nefile",
69 ENOTDIR},
70 {longpath, "pathname too long", "nefile", "nefile", ENAMETOOLONG},
71 {NULL, "invalid address", "nefile", "nefile", EFAULT},
72 /* second path is invalid */
73 {"regfile", "regfile", "", "empty string", ENOENT},
74 {"regfile", "regfile", "neefile/file",
75 "path contains a non-existent file", ENOENT},
76 {"regfile", "regfile", "file/file",
77 "path contains a regular file", ENOENT},
78 {"regfile", "regfile", longpath, "pathname too long", ENAMETOOLONG},
79 {"regfile", "regfile", NULL, "invalid address", EFAULT},
80 /* two existing files */
81 {"regfile", "regfile", "regfile2", "regfile2", EEXIST},
82 };
83
84 char *TCID = "link04";
85 int TST_TOTAL = ARRAY_SIZE(test_cases);
86
87 static void setup(void);
88 static void cleanup(void);
89
main(int ac,char ** av)90 int main(int ac, char **av)
91 {
92 int lc;
93 char *fname1, *fname2;
94 char *desc1, *desc2;
95 int i;
96
97 tst_parse_opts(ac, av, NULL, NULL);
98
99 setup();
100
101 for (lc = 0; TEST_LOOPING(lc); lc++) {
102
103 tst_count = 0;
104
105 for (i = 0; i < TST_TOTAL; i++) {
106
107 fname1 = test_cases[i].file1;
108 desc1 = test_cases[i].desc1;
109 fname2 = test_cases[i].file2;
110 desc2 = test_cases[i].desc2;
111
112 TEST(link(fname1, fname2));
113
114 if (TEST_RETURN == -1) {
115 if (TEST_ERRNO == test_cases[i].exp_errno) {
116 tst_resm(TPASS | TTERRNO,
117 "link(<%s>, <%s>)",
118 desc1, desc2);
119 } else {
120 tst_resm(TFAIL | TTERRNO,
121 "link(<%s>, <%s>) Failed "
122 "expected errno: %d",
123 desc1, desc2,
124 test_cases[i].exp_errno);
125 }
126 } else {
127 tst_resm(TFAIL,
128 "link(<%s>, <%s>) returned %ld, "
129 "expected -1, errno:%d",
130 desc1, desc2, TEST_RETURN,
131 test_cases[i].exp_errno);
132 }
133 }
134
135 }
136
137 cleanup();
138 tst_exit();
139 }
140
setup(void)141 static void setup(void)
142 {
143 int n;
144
145 tst_sig(NOFORK, DEF_HANDLER, cleanup);
146
147 TEST_PAUSE;
148
149 tst_tmpdir();
150
151 memset(longpath, 'a', PATH_MAX+1);
152 SAFE_TOUCH(cleanup, "regfile", 0777, NULL);
153 SAFE_TOUCH(cleanup, "regfile2", 0777, NULL);
154 SAFE_MKDIR(cleanup, "dir", 0777);
155
156 void *bad_addr = tst_get_bad_addr(cleanup);
157
158 for (n = 0; n < TST_TOTAL; n++) {
159 if (!test_cases[n].file1)
160 test_cases[n].file1 = bad_addr;
161
162 if (!test_cases[n].file2)
163 test_cases[n].file2 = bad_addr;
164 }
165 }
166
cleanup(void)167 static void cleanup(void)
168 {
169 tst_rmdir();
170 }
171