blob: 500ca6c3c90b5768ab5f687c45b8855447b00709 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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>
plars74948ad2002-11-14 16:16:14 +000067#include <sys/types.h>
plars865695b2001-08-27 22:15:12 +000068#include <sys/stat.h>
plars1ad84512002-07-23 13:11:18 +000069#include <sys/mman.h>
plars74948ad2002-11-14 16:16:14 +000070#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000071#include "test.h"
72#include "usctest.h"
73
74void setup();
75void cleanup();
76void setup1();
77void setup2();
78void setup3();
79void setup4();
80void setup5();
81
82#define PERMS 0777
83#define PERMS2 0277
84
85#define NAMELEN 50
86
subrata_modak56207ce2009-03-23 13:35:39 +000087char *TCID = "mkdir03"; /* Test program identifier. */
subrata_modak8293edd2007-08-28 06:45:50 +000088int fileHandle, fileHandle2 = 0;
plars865695b2001-08-27 22:15:12 +000089
90char tstdir3[NAMELEN];
91char tstdir4[NAMELEN];
92char tstdir5[NAMELEN];
93
subrata_modak56207ce2009-03-23 13:35:39 +000094char long_dir[] =
95 "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
plars865695b2001-08-27 22:15:12 +000096
subrata_modak56207ce2009-03-23 13:35:39 +000097int exp_enos[] = { EFAULT, ENAMETOOLONG, EEXIST, ENOENT, ENOTDIR, 0 };
plars865695b2001-08-27 22:15:12 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099char *bad_addr = 0;
plars1ad84512002-07-23 13:11:18 +0000100
plars865695b2001-08-27 22:15:12 +0000101struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +0000102 char *dir;
103 int perms;
104 int error;
105 void (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000106} TC[] = {
vapierb5ed1f62006-08-24 04:16:32 +0000107#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +0000108 /* try to create a directory with an illegal name/address */
subrata_modak56207ce2009-03-23 13:35:39 +0000109 {
110 (void *)-1, PERMS, EFAULT, NULL},
vapierb5ed1f62006-08-24 04:16:32 +0000111#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000112 /* 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}
plars865695b2001-08-27 22:15:12 +0000127};
128
subrata_modak56207ce2009-03-23 13:35:39 +0000129int TST_TOTAL = sizeof(TC) / sizeof(TC[0]);
vapier721ecd82006-08-06 04:30:48 +0000130
subrata_modak56207ce2009-03-23 13:35:39 +0000131int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000132{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200133 int lc;
134 char *msg;
plars74948ad2002-11-14 16:16:14 +0000135 int i;
plars865695b2001-08-27 22:15:12 +0000136
137 /*
138 * parse standard options
139 */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800140 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800141 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000142 }
143
144 /*
145 * perform global setup for test
146 */
147 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +0000148
plars865695b2001-08-27 22:15:12 +0000149 /* set the expected errnos... */
150 TEST_EXP_ENOS(exp_enos);
subrata_modakbdbaec52009-02-26 12:14:51 +0000151
plars865695b2001-08-27 22:15:12 +0000152 /*
153 * check looping state if -i option given
154 */
subrata_modak56207ce2009-03-23 13:35:39 +0000155 for (lc = 0; TEST_LOOPING(lc); lc++) {
156
Caspar Zhangd59a6592013-03-07 14:59:12 +0800157 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000158
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_modak56207ce2009-03-23 13:35:39 +0000164 (*TC[i].setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000165 }
166
167 TEST(mkdir(TC[i].dir, TC[i].perms));
168
subrata_modak56207ce2009-03-23 13:35:39 +0000169 if (TEST_RETURN != -1) {
170 tst_resm(TFAIL, "call succeeded unexpectedly");
171 continue;
172 }
plars865695b2001-08-27 22:15:12 +0000173
subrata_modak56207ce2009-03-23 13:35:39 +0000174 TEST_ERROR_LOG(TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000175
subrata_modak56207ce2009-03-23 13:35:39 +0000176 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);
plars865695b2001-08-27 22:15:12 +0000184 }
185 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800186 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000187
plars865695b2001-08-27 22:15:12 +0000188 /*
189 * cleanup and exit
190 */
191 cleanup();
subrata_modakbdbaec52009-02-26 12:14:51 +0000192
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800193 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800194}
plars865695b2001-08-27 22:15:12 +0000195
196/*
197 * setup3() - performs all ONE TIME setup for this test case 3.
198 */
subrata_modak56207ce2009-03-23 13:35:39 +0000199void setup3()
plars865695b2001-08-27 22:15:12 +0000200{
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 */
mridge106effa2004-08-25 15:31:38 +0000208 if ((fileHandle = creat(tstfile3, PERMS)) == -1) {
plars865695b2001-08-27 22:15:12 +0000209 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_modak56207ce2009-03-23 13:35:39 +0000216void setup4()
plars865695b2001-08-27 22:15:12 +0000217{
218 char tstdir[NAMELEN];
219 struct stat statbuf;
220
221 /* Initialize the test directory name */
subrata_modak56207ce2009-03-23 13:35:39 +0000222 sprintf(tstdir, "tstdir4.%d", getpid());
plars865695b2001-08-27 22:15:12 +0000223 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_modak56207ce2009-03-23 13:35:39 +0000242void setup5()
plars865695b2001-08-27 22:15:12 +0000243{
244 char tstfile5[NAMELEN];
245
246 /* Initialize the test directories name and file name */
247 sprintf(tstfile5, "tstfile5.%d", getpid());
subrata_modak56207ce2009-03-23 13:35:39 +0000248 sprintf(tstdir5, "%s/tst", tstfile5);
plars865695b2001-08-27 22:15:12 +0000249
250 /* create a file */
subrata_modak8293edd2007-08-28 06:45:50 +0000251 if ((fileHandle2 = creat(tstfile5, PERMS)) == -1) {
plars865695b2001-08-27 22:15:12 +0000252 tst_brkm(TBROK, cleanup, "creat a file failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800253 }
plars865695b2001-08-27 22:15:12 +0000254}
255
256/*
257 * setup() - performs all ONE TIME setup for this test.
258 */
subrata_modak56207ce2009-03-23 13:35:39 +0000259void setup()
plars865695b2001-08-27 22:15:12 +0000260{
Garrett Cooper2c282152010-12-16 00:55:50 -0800261
plars865695b2001-08-27 22:15:12 +0000262 tst_sig(NOFORK, DEF_HANDLER, cleanup);
vapierb5ed1f62006-08-24 04:16:32 +0000263
plars865695b2001-08-27 22:15:12 +0000264 TEST_PAUSE;
265
266 /* Create a temporary directory and make it current. */
267 tst_tmpdir();
plars1ad84512002-07-23 13:11:18 +0000268
vapier62b16cf2007-02-09 20:48:23 +0000269#if !defined(UCLINUX)
robbiewd34d5812005-07-11 22:28:09 +0000270 bad_addr = mmap(0, 1, PROT_NONE,
subrata_modak56207ce2009-03-23 13:35:39 +0000271 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
vapier0722a2b2006-05-12 15:44:11 +0000272 if (bad_addr == MAP_FAILED) {
plars1ad84512002-07-23 13:11:18 +0000273 tst_brkm(TBROK, cleanup, "mmap failed");
274 }
275 TC[0].dir = bad_addr;
vapierb5ed1f62006-08-24 04:16:32 +0000276#endif
plars865695b2001-08-27 22:15:12 +0000277}
278
plars865695b2001-08-27 22:15:12 +0000279/*
280 * cleanup() - performs all ONE TIME cleanup for this test at
281 * completion or premature exit.
282 */
subrata_modak56207ce2009-03-23 13:35:39 +0000283void cleanup()
plars865695b2001-08-27 22:15:12 +0000284{
285 /*
286 * print timing stats if that option was specified.
287 * print errno log if that option was specified.
288 */
subrata_modak8293edd2007-08-28 06:45:50 +0000289 close(fileHandle);
290 close(fileHandle2);
mridge106effa2004-08-25 15:31:38 +0000291
plars865695b2001-08-27 22:15:12 +0000292 TEST_CLEANUP;
293
294 /*
295 * Remove the temporary directory.
296 */
297 tst_rmdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000298
plars865695b2001-08-27 22:15:12 +0000299 /*
300 * Exit with return code appropriate for results.
301 */
Garrett Cooper2c282152010-12-16 00:55:50 -0800302
Chris Dearmanec6edca2012-10-17 19:54:01 -0700303}