blob: 4bdc54798d11f73144a12a6c1e13d54a5924eff1 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +01003 * AUTHOR : Richard Logan
4 * CO-PILOT : William Roske
5 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00006 *
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
Wanlong Gaofed96412012-10-24 10:10:29 +080023 * with this program; if not, write the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000025 *
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 */
plars865695b2001-08-27 22:15:12 +000036
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010037/*
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 */
plars865695b2001-08-27 22:15:12 +000043#include <sys/types.h>
44#include <sys/fcntl.h>
45#include <sys/stat.h>
46#include <errno.h>
47#include <string.h>
48#include <signal.h>
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010049#include <sys/param.h>
plars1ad84512002-07-23 13:11:18 +000050#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000051#include "test.h"
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010052#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000053
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010054static char *bad_addr = 0;
plars865695b2001-08-27 22:15:12 +000055
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010056static char longpath[PATH_MAX + 2];
robbiewd34d5812005-07-11 22:28:09 +000057#if !defined(UCLINUX)
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010058char high_addr[64];
robbiewd34d5812005-07-11 22:28:09 +000059#endif
plars865695b2001-08-27 22:15:12 +000060
61struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000062 char *file1;
63 char *desc1;
64 char *file2;
65 char *desc2;
66 int exp_errno;
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010067} test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000068 /* first path is invalid */
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010069 {"nonexistfile", "non-existent file", "nefile", "nefile", ENOENT},
70 {"", "path is empty string", "nefile", "nefile", ENOENT},
71 {"neefile/file", "path contains a non-existent file", "nefile",
72 "nefile", ENOENT},
73 {"regfile/file", "path contains a regular file", "nefile", "nefile",
74 ENOTDIR},
75 {longpath, "pathname too long", "nefile", "nefile", ENAMETOOLONG},
robbiewd34d5812005-07-11 22:28:09 +000076#if !defined(UCLINUX)
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010077 {high_addr, "address beyond address space", "nefile", "nefile", EFAULT},
robbiewd34d5812005-07-11 22:28:09 +000078#endif
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010079 {(char *)-1, "negative address", "nefile", "nefile", EFAULT},
80 /* second path is invalid */
81 {"regfile", "regfile", "", "empty string", ENOENT},
82 {"regfile", "regfile", "neefile/file",
83 "path contains a non-existent file", ENOENT},
84 {"regfile", "regfile", "file/file",
85 "path contains a regular file", ENOENT},
86 {"regfile", "regfile", longpath, "pathname too long", ENAMETOOLONG},
robbiewd34d5812005-07-11 22:28:09 +000087#if !defined(UCLINUX)
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010088 {"regfile", "regfile", high_addr,
89 "address beyond address space", EFAULT},
robbiewd34d5812005-07-11 22:28:09 +000090#endif
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010091 {"regfile", "regfile", (char *)-1, "negative address", EFAULT},
92 /* two existing files */
93 {"regfile", "regfile", "regfile2", "regfile2", EEXIST},
plars865695b2001-08-27 22:15:12 +000094};
95
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010096char *TCID = "link04";
97int TST_TOTAL = ARRAY_SIZE(test_cases);
98
99static void setup(void);
100static void cleanup(void);
101
subrata_modak56207ce2009-03-23 13:35:39 +0000102int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000103{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200104 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200105 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000106 char *fname1, *fname2;
107 char *desc1, *desc2;
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100108 int i;
subrata_modak56207ce2009-03-23 13:35:39 +0000109
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100110 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +0000111 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000112
subrata_modak56207ce2009-03-23 13:35:39 +0000113 setup();
plars865695b2001-08-27 22:15:12 +0000114
subrata_modak56207ce2009-03-23 13:35:39 +0000115 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000116
Caspar Zhangd59a6592013-03-07 14:59:12 +0800117 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000118
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100119 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000120
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100121 fname1 = test_cases[i].file1;
122 desc1 = test_cases[i].desc1;
123 fname2 = test_cases[i].file2;
124 desc2 = test_cases[i].desc2;
plars865695b2001-08-27 22:15:12 +0000125
robbiewd34d5812005-07-11 22:28:09 +0000126#if !defined(UCLINUX)
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100127 if (fname1 == high_addr)
subrata_modak56207ce2009-03-23 13:35:39 +0000128 fname1 = get_high_address();
plars865695b2001-08-27 22:15:12 +0000129
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100130 if (fname2 == high_addr)
subrata_modak56207ce2009-03-23 13:35:39 +0000131 fname2 = get_high_address();
robbiewd34d5812005-07-11 22:28:09 +0000132#endif
plars865695b2001-08-27 22:15:12 +0000133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 TEST(link(fname1, fname2));
plars865695b2001-08-27 22:15:12 +0000135
subrata_modak56207ce2009-03-23 13:35:39 +0000136 if (TEST_RETURN == -1) {
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100137 if (TEST_ERRNO == test_cases[i].exp_errno) {
138 tst_resm(TPASS | TTERRNO,
139 "link(<%s>, <%s>)",
140 desc1, desc2);
141 } else {
142 tst_resm(TFAIL | TTERRNO,
143 "link(<%s>, <%s>) Failed "
144 "expected errno: %d",
145 desc1, desc2,
146 test_cases[i].exp_errno);
147 }
subrata_modak56207ce2009-03-23 13:35:39 +0000148 } else {
149 tst_resm(TFAIL,
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100150 "link(<%s>, <%s>) returned %ld, "
151 "expected -1, errno:%d",
subrata_modak56207ce2009-03-23 13:35:39 +0000152 desc1, desc2, TEST_RETURN,
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100153 test_cases[i].exp_errno);
subrata_modak56207ce2009-03-23 13:35:39 +0000154 }
155 }
plars865695b2001-08-27 22:15:12 +0000156
Garrett Cooper2c282152010-12-16 00:55:50 -0800157 }
plars865695b2001-08-27 22:15:12 +0000158
subrata_modak56207ce2009-03-23 13:35:39 +0000159 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800160 tst_exit();
161}
plars865695b2001-08-27 22:15:12 +0000162
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100163static void setup(void)
plars865695b2001-08-27 22:15:12 +0000164{
subrata_modak56207ce2009-03-23 13:35:39 +0000165 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000166
subrata_modak56207ce2009-03-23 13:35:39 +0000167 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000168
subrata_modak56207ce2009-03-23 13:35:39 +0000169 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000170
vapier62b16cf2007-02-09 20:48:23 +0000171#if !defined(UCLINUX)
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100172 bad_addr = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
173 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
174 test_cases[6].file1 = bad_addr;
175 test_cases[12].file2 = bad_addr;
vapier62b16cf2007-02-09 20:48:23 +0000176#endif
plars1ad84512002-07-23 13:11:18 +0000177
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100178 memset(longpath, 'a', PATH_MAX+1);
179 SAFE_TOUCH(cleanup, "regfile", 0777, NULL);
180 SAFE_TOUCH(cleanup, "regfile2", 0777, NULL);
181 SAFE_MKDIR(cleanup, "dir", 0777);
Garrett Cooper2c282152010-12-16 00:55:50 -0800182}
plars865695b2001-08-27 22:15:12 +0000183
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100184static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000185{
subrata_modak56207ce2009-03-23 13:35:39 +0000186 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700187}