plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1303 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * mkdir03 |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * check mkdir() with various error conditions that should produce |
| 26 | * EFAULT, ENAMETOOLONG, EEXIST, ENOENT and ENOTDIR |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * Setup: |
| 30 | * Setup signal handling. |
| 31 | * Pause for SIGUSR1 if option specified. |
| 32 | * Create temporary directory. |
| 33 | * |
| 34 | * Test: |
| 35 | * Loop if the proper options are given. |
| 36 | * Loop through the test cases |
| 37 | * call the test case specific setup routine if necessary |
| 38 | * call mkdir() using the TEST macro |
| 39 | * if the call succeeds |
| 40 | * print a FAIL message and continue |
| 41 | * Log the errno value |
| 42 | * if the errno is expected |
| 43 | * issue a PASS message |
| 44 | * else |
| 45 | * issue a FAIL message |
| 46 | * Cleanup: |
| 47 | * Print errno log and/or timing stats if options given |
| 48 | * Delete the temporary directory created. |
| 49 | * USAGE |
| 50 | * mkdir03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 51 | * where, -c n : Run n copies concurrently. |
| 52 | * -e : Turn on errno logging. |
| 53 | * -i n : Execute test n times. |
| 54 | * -I x : Execute test for x seconds. |
| 55 | * -P x : Pause for x seconds between iterations. |
| 56 | * -t : Turn on syscall timing. |
| 57 | * |
| 58 | * HISTORY |
| 59 | * 07/2001 Ported by Wayne Boyer |
| 60 | * |
| 61 | * RESTRICTIONS |
| 62 | * None. |
| 63 | * |
| 64 | */ |
| 65 | |
| 66 | #include <errno.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 67 | #include <sys/types.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | #include <sys/stat.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 69 | #include <sys/mman.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 70 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | #include "test.h" |
| 72 | #include "usctest.h" |
| 73 | |
| 74 | void setup(); |
| 75 | void cleanup(); |
| 76 | void setup1(); |
| 77 | void setup2(); |
| 78 | void setup3(); |
| 79 | void setup4(); |
| 80 | void setup5(); |
| 81 | |
| 82 | #define PERMS 0777 |
| 83 | #define PERMS2 0277 |
| 84 | |
| 85 | #define NAMELEN 50 |
| 86 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | char *TCID = "mkdir03"; /* Test program identifier. */ |
subrata_modak | 8293edd | 2007-08-28 06:45:50 +0000 | [diff] [blame] | 88 | int fileHandle, fileHandle2 = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
| 90 | char tstdir3[NAMELEN]; |
| 91 | char tstdir4[NAMELEN]; |
| 92 | char tstdir5[NAMELEN]; |
| 93 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | char long_dir[] = |
| 95 | "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | int exp_enos[] = { EFAULT, ENAMETOOLONG, EEXIST, ENOENT, ENOTDIR, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | char *bad_addr = 0; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | char *dir; |
| 103 | int perms; |
| 104 | int error; |
| 105 | void (*setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | } TC[] = { |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 107 | #if !defined(UCLINUX) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | /* try to create a directory with an illegal name/address */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | { |
| 110 | (void *)-1, PERMS, EFAULT, NULL}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 111 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | /* try to create a directory using a name that is too long */ |
| 113 | { |
| 114 | long_dir, PERMS2, ENAMETOOLONG, NULL}, |
| 115 | /* try to create a directory with the same name as an existing file */ |
| 116 | { |
| 117 | tstdir3, PERMS, EEXIST, setup3}, |
| 118 | /* try to create a directory under a directory that doesn't exist */ |
| 119 | { |
| 120 | tstdir4, PERMS, ENOENT, setup4}, |
| 121 | /* |
| 122 | * try to create a directory under a path with a non-directory |
| 123 | * component |
| 124 | */ |
| 125 | { |
| 126 | tstdir5, PERMS, ENOTDIR, setup5} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | int TST_TOTAL = sizeof(TC) / sizeof(TC[0]); |
vapier | 721ecd8 | 2006-08-06 04:30:48 +0000 | [diff] [blame] | 130 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 133 | int lc; |
| 134 | char *msg; |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 135 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * parse standard options |
| 139 | */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 140 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 141 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
| 145 | * perform global setup for test |
| 146 | */ |
| 147 | setup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 148 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | /* set the expected errnos... */ |
| 150 | TEST_EXP_ENOS(exp_enos); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 151 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | /* |
| 153 | * check looping state if -i option given |
| 154 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 156 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 157 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
| 159 | /* loop through the test cases */ |
| 160 | for (i = 0; i < TST_TOTAL; i++) { |
| 161 | |
| 162 | /* perform test specific setup if necessary */ |
| 163 | if (TC[i].setupfunc != NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | (*TC[i].setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | TEST(mkdir(TC[i].dir, TC[i].perms)); |
| 168 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | if (TEST_RETURN != -1) { |
| 170 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 171 | continue; |
| 172 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | TEST_ERROR_LOG(TEST_ERRNO); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | if (TEST_ERRNO == TC[i].error) { |
| 177 | tst_resm(TPASS, "expected failure - " |
| 178 | "errno = %d : %s", TEST_ERRNO, |
| 179 | strerror(TEST_ERRNO)); |
| 180 | } else { |
| 181 | tst_resm(TFAIL, "unexpected error - %d : %s - " |
| 182 | "expected %d", TEST_ERRNO, |
| 183 | strerror(TEST_ERRNO), TC[i].error); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 186 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 187 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | /* |
| 189 | * cleanup and exit |
| 190 | */ |
| 191 | cleanup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 192 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 193 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 194 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * setup3() - performs all ONE TIME setup for this test case 3. |
| 198 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 199 | void setup3() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | { |
| 201 | char tstfile3[NAMELEN]; |
| 202 | |
| 203 | /* Initialize the test directory name and file name */ |
| 204 | sprintf(tstfile3, "tst.%d", getpid()); |
| 205 | sprintf(tstdir3, "%s", tstfile3); |
| 206 | |
| 207 | /* create a file */ |
mridge | 106effa | 2004-08-25 15:31:38 +0000 | [diff] [blame] | 208 | if ((fileHandle = creat(tstfile3, PERMS)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | tst_brkm(TBROK, cleanup, "file creation failed is setup3"); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * setup4() - performs all ONE TIME setup for this test case 4. |
| 215 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 216 | void setup4() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | { |
| 218 | char tstdir[NAMELEN]; |
| 219 | struct stat statbuf; |
| 220 | |
| 221 | /* Initialize the test directory name */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 222 | sprintf(tstdir, "tstdir4.%d", getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | sprintf(tstdir4, "%s/tst", tstdir); |
| 224 | /* |
| 225 | sprintf(tstdir4, "%s/tst", tstdir4); |
| 226 | |
| 227 | This fails with EACCES ^^^^^^^ |
| 228 | add this as testcase? |
| 229 | |
| 230 | */ |
| 231 | |
| 232 | /* make sure tstdir4 does not exist */ |
| 233 | if (stat(tstdir4, &statbuf) != -1) { |
| 234 | tst_brkm(TBROK, cleanup, "directory exists - test #4"); |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * setup5() - performs all ONE TIME setup for this test case 5. |
| 241 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 242 | void setup5() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 243 | { |
| 244 | char tstfile5[NAMELEN]; |
| 245 | |
| 246 | /* Initialize the test directories name and file name */ |
| 247 | sprintf(tstfile5, "tstfile5.%d", getpid()); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 248 | sprintf(tstdir5, "%s/tst", tstfile5); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 249 | |
| 250 | /* create a file */ |
subrata_modak | 8293edd | 2007-08-28 06:45:50 +0000 | [diff] [blame] | 251 | if ((fileHandle2 = creat(tstfile5, PERMS)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 252 | tst_brkm(TBROK, cleanup, "creat a file failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 253 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* |
| 257 | * setup() - performs all ONE TIME setup for this test. |
| 258 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 259 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 260 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 261 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 262 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 263 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 264 | TEST_PAUSE; |
| 265 | |
| 266 | /* Create a temporary directory and make it current. */ |
| 267 | tst_tmpdir(); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 268 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 269 | #if !defined(UCLINUX) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 270 | bad_addr = mmap(0, 1, PROT_NONE, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 271 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
vapier | 0722a2b | 2006-05-12 15:44:11 +0000 | [diff] [blame] | 272 | if (bad_addr == MAP_FAILED) { |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 273 | tst_brkm(TBROK, cleanup, "mmap failed"); |
| 274 | } |
| 275 | TC[0].dir = bad_addr; |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 276 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 277 | } |
| 278 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 279 | /* |
| 280 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 281 | * completion or premature exit. |
| 282 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 283 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 284 | { |
| 285 | /* |
| 286 | * print timing stats if that option was specified. |
| 287 | * print errno log if that option was specified. |
| 288 | */ |
subrata_modak | 8293edd | 2007-08-28 06:45:50 +0000 | [diff] [blame] | 289 | close(fileHandle); |
| 290 | close(fileHandle2); |
mridge | 106effa | 2004-08-25 15:31:38 +0000 | [diff] [blame] | 291 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 292 | TEST_CLEANUP; |
| 293 | |
| 294 | /* |
| 295 | * Remove the temporary directory. |
| 296 | */ |
| 297 | tst_rmdir(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 298 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 299 | /* |
| 300 | * Exit with return code appropriate for results. |
| 301 | */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 302 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 303 | } |