plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 3 | * AUTHOR : Richard Logan |
| 4 | * CO-PILOT : William Roske |
| 5 | * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 23 | * with this program; if not, write the Free Software Foundation, Inc., |
| 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 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 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | #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 Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 49 | #include <sys/param.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 50 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | #include "test.h" |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 52 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 54 | static char *bad_addr = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 56 | static char longpath[PATH_MAX + 2]; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 57 | #if !defined(UCLINUX) |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 58 | char high_addr[64]; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 59 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
| 61 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 62 | char *file1; |
| 63 | char *desc1; |
| 64 | char *file2; |
| 65 | char *desc2; |
| 66 | int exp_errno; |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 67 | } test_cases[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | /* first path is invalid */ |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 69 | {"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}, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 76 | #if !defined(UCLINUX) |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 77 | {high_addr, "address beyond address space", "nefile", "nefile", EFAULT}, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 78 | #endif |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 79 | {(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}, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 87 | #if !defined(UCLINUX) |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 88 | {"regfile", "regfile", high_addr, |
| 89 | "address beyond address space", EFAULT}, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 90 | #endif |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 91 | {"regfile", "regfile", (char *)-1, "negative address", EFAULT}, |
| 92 | /* two existing files */ |
| 93 | {"regfile", "regfile", "regfile2", "regfile2", EEXIST}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 96 | char *TCID = "link04"; |
| 97 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
| 98 | |
| 99 | static void setup(void); |
| 100 | static void cleanup(void); |
| 101 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 104 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 105 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | char *fname1, *fname2; |
| 107 | char *desc1, *desc2; |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 108 | int i; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 110 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 117 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 118 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 119 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 121 | fname1 = test_cases[i].file1; |
| 122 | desc1 = test_cases[i].desc1; |
| 123 | fname2 = test_cases[i].file2; |
| 124 | desc2 = test_cases[i].desc2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 126 | #if !defined(UCLINUX) |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 127 | if (fname1 == high_addr) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | fname1 = get_high_address(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 130 | if (fname2 == high_addr) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | fname2 = get_high_address(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 132 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | TEST(link(fname1, fname2)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | if (TEST_RETURN == -1) { |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 137 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | } else { |
| 149 | tst_resm(TFAIL, |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 150 | "link(<%s>, <%s>) returned %ld, " |
| 151 | "expected -1, errno:%d", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | desc1, desc2, TEST_RETURN, |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 153 | test_cases[i].exp_errno); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 157 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 160 | tst_exit(); |
| 161 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 163 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 171 | #if !defined(UCLINUX) |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 172 | 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; |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 176 | #endif |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 177 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 178 | 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 182 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 184 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 186 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 187 | } |