source: trunk/minix/test/test28.c@ 12

Last change on this file since 12 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 13.4 KB
Line 
1 /* test28: mkdir() rmdir() Author: Jan-Mark Wams (jms@cs.vu.nl) */
2
3/*
4** Not tested readonly file systems (EROFS.)
5** Not tested fs full (ENOSPC.)
6** Not really tested EBUSY.
7** Not tested unlinking busy directories.
8*/
9
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <sys/wait.h>
13#include <string.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <errno.h>
18#include <dirent.h>
19#include <limits.h>
20#include <time.h>
21#include <stdio.h>
22
23#define MAX_ERROR 4
24#define ITERATIONS 2
25
26#define DIRENT0 ((struct dirent *) NULL)
27
28#define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
29#define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
30
31int errct = 0;
32int subtest = 1;
33int superuser;
34char MaxName[NAME_MAX + 1]; /* Name of maximum length */
35char MaxPath[PATH_MAX];
36char ToLongName[NAME_MAX + 2]; /* Name of maximum +1 length */
37char ToLongPath[PATH_MAX + 1];
38
39_PROTOTYPE(void main, (int argc, char *argv[]));
40_PROTOTYPE(void test28a, (void));
41_PROTOTYPE(void test28c, (void));
42_PROTOTYPE(void test28b, (void));
43_PROTOTYPE(void makelongnames, (void));
44_PROTOTYPE(void e, (int n));
45_PROTOTYPE(void quit, (void));
46
47void main(argc, argv)
48int argc;
49char *argv[];
50{
51 int i, m = 0xFFFF;
52
53 sync();
54 if (argc == 2) m = atoi(argv[1]);
55 printf("Test 28 ");
56 fflush(stdout);
57 superuser = (getuid() == 0);
58 makelongnames();
59 system("chmod 777 DIR_28/* DIR_28/*/* > /dev/null 2>&1");
60 System("rm -rf DIR_28; mkdir DIR_28");
61 Chdir("DIR_28");
62 umask(0000); /* no umask */
63
64 for (i = 0; i < ITERATIONS; i++) {
65 if (m & 0001) test28a();
66 if (m & 0002) test28b();
67 if (m & 0004) test28c();
68 }
69 quit();
70}
71
72void test28a()
73{
74 int mode; /* used in for loop */
75 struct stat st;
76 time_t time1, time2;
77 DIR *dirp;
78 struct dirent *dep;
79 int dot = 0, dotdot = 0;
80
81 subtest = 1;
82
83 System("rm -rf foo /tmp/foo");/* clean up junk */
84
85 /* Check relative path names */
86 if (mkdir("./foo", 0777) != 0) e(1); /* make a dir foo */
87 if (mkdir("./foo/bar", 0777) != 0) e(2); /* make foo/bar */
88 if (rmdir("foo/bar") != 0) e(3); /* delete bar */
89 if (mkdir("foo/../foo/bar", 0777) != 0) e(4); /* make bar again */
90 if (rmdir("./foo/bar") != 0) e(5); /* and remove again */
91
92 /* Foo should be empty (ie. contain only "." and ".." */
93 if ((dirp = opendir("foo")) == (DIR *) NULL) e(6); /* open foo */
94 if ((dep = readdir(dirp)) == DIRENT0) e(7); /* get first entry */
95 if (strcmp(dep->d_name, ".") == 0) dot += 1; /* record what it is */
96 if (strcmp(dep->d_name, "..") == 0) dotdot += 1;
97 if ((dep = readdir(dirp)) == DIRENT0) e(8); /* get second entry */
98 if (strcmp(dep->d_name, ".") == 0) dot += 1; /* record again */
99 if (strcmp(dep->d_name, "..") == 0) dotdot += 1;
100 if ((dep = readdir(dirp)) != DIRENT0) e(9); /* no 3d entry */
101 if (dot == 1 && dotdot != 1) e(10); /* only . and .. */
102 if (closedir(dirp) != 0) e(11); /* close foo */
103 if (rmdir("./foo") != 0) e(12); /* remove dir foo */
104
105 /* Check absolute path names */
106 if (mkdir("/tmp/foo", 0777) != 0) e(13);
107 if (mkdir("/tmp/foo/bar", 0777) != 0) e(14);
108 if (rmdir("/tmp/foo/bar") != 0) e(15); /* make some dirs */
109 if (rmdir("/tmp/foo") != 0) e(16);
110
111 /* Check the mode arument for mkdir() */
112 for (mode = 0; mode <= 0777; mode++) {
113 if (mkdir("foo", mode) != 0) e(17); /* make foo */
114 if (stat("foo", &st) != 0) e(18);
115 if ((st.st_mode & 0777) != mode) e(19); /* check it's mode */
116 if (rmdir("foo") != 0) e(20); /* and remove it */
117 }
118
119 /* Check the stat */
120 time(&time1);
121 while (time1 >= time((time_t *)0))
122 ;
123 if (mkdir("foo", 0765) != 0) e(21); /* make foo */
124 if (stat("foo", &st) != 0) e(22);
125 time(&time2);
126 while (time2 >= time((time_t *)0))
127 ;
128 time(&time2);
129 if (st.st_nlink != 2) e(23);
130 if (st.st_uid != geteuid()) e(24);
131 if (st.st_gid != getegid()) e(25);
132 if (st.st_size < 0) e(26);
133 if ((st.st_mode & 0777) != 0765) e(27);
134 if (st.st_atime <= time1) e(28);
135 if (st.st_atime >= time2) e(29);
136 if (st.st_ctime <= time1) e(30);
137 if (st.st_ctime >= time2) e(31);
138 if (st.st_mtime <= time1) e(32);
139 if (st.st_mtime >= time2) e(33);
140
141 /* Check if parent is updated */
142 if (stat(".", &st) != 0) e(34);
143 time(&time2);
144 while (time2 >= time((time_t *)0))
145 ;
146 time(&time2);
147 if (st.st_ctime <= time1) e(35);
148 if (st.st_ctime >= time2) e(36);
149 if (st.st_mtime <= time1) e(37);
150 if (st.st_mtime >= time2) e(38);
151 time(&time1);
152 while (time1 >= time((time_t *)0))
153 ;
154 if (rmdir("foo") != 0) e(39);
155 if (stat(".", &st) != 0) e(40);
156 time(&time2);
157 while (time2 >= time((time_t *)0))
158 ;
159 time(&time2);
160 if (st.st_ctime <= time1) e(41);
161 if (st.st_ctime >= time2) e(42);
162 if (st.st_mtime <= time1) e(43);
163 if (st.st_mtime >= time2) e(44);
164}
165
166void test28b()
167{ /* Test critical values. */
168 struct stat st;
169 DIR *dirp;
170 struct dirent *dep;
171 int fd; /* file descriptor */
172 int other = 0, dot = 0, dotdot = 0; /* dirent counters */
173 int rmdir_result; /* tmp var */
174 nlink_t nlink;
175 static char bar[20];
176 int stat_loc;
177
178 subtest = 2;
179
180 System("rm -rf ../DIR_28/*");
181
182 /* Check funny but valid path names */
183 if (mkdir("/../../..////.//../tmp/foo/", 0777) != 0) e(1);
184 if (mkdir("/tmp/foo//////..//foo//../foo/bar/", 0777) != 0) e(2);
185 if (rmdir("///tmp/..//tmp/foo/bar//../..//foo/bar") != 0) e(3);
186 if (mkdir("///tmp/foo/foobar//", 0777) != 0) e(4);
187 if (rmdir("/tmp/foo/foobar//") != 0) e(5);
188 if (rmdir("/.././/././/tmp/foo///////////////") != 0) e(6);
189 if (rmdir("/tmp/foo") != -1) e(7); /* try again */
190
191 /* Test max path ed. */
192 if (mkdir(MaxName, 0777) != 0) e(9); /* make dir MaxName */
193 if (rmdir(MaxName) != 0) e(10); /* and remove it */
194 MaxPath[strlen(MaxPath) - 2] = '/'; /* convert MaxPath */
195 MaxPath[strlen(MaxPath) - 1] = 'a'; /* to ././.../a */
196 if (mkdir(MaxPath, 0777) != 0) e(11); /* it should be */
197 if (rmdir(MaxPath) != 0) e(12); /* ok */
198
199 /* Test too long path ed. */
200 if (mkdir(ToLongName, 0777) != 0) e(17); /* Try ToLongName */
201 if (rmdir(ToLongName) != 0) e(18); /* and remove it */
202 ToLongPath[strlen(ToLongPath) - 2] = '/'; /* make ToLongPath */
203 ToLongPath[strlen(ToLongPath) - 1] = 'a'; /* contain ././.../a */
204 if (mkdir(ToLongPath, 0777) != -1) e(19); /* it should */
205 if (errno != ENAMETOOLONG) e(20); /* not be ok */
206 if (rmdir(ToLongPath) != -1) e(21);
207 if (errno != ENAMETOOLONG) e(22);
208
209 if (mkdir("foo", 0777) != 0) e(23);
210 System("touch foo/xyzzy");
211#if 0
212 /* Test what happens if the parent link count > LINK_MAX. */
213 /* This takes too long. */
214 for (nlink = 1; nlink < LINK_MAX; nlink++) { /* make all */
215 sprintf(bar, "foo/bar.%d", nlink);
216 if (link("foo/xyzzy", bar) != 0) e(24);
217 }
218 if (stat("foo/xyzzy", &st) != 0) e(25); /* foo now */
219 if (st.st_nlink != LINK_MAX) e(26); /* is full */
220 if (link("foo/xyzzy", "nono") != -1) e(27); /* no more */
221 if (errno != EMLINK) e(28); /* entrys. */
222 System("rm -rf foo/nono"); /* Just in case. */
223#endif
224
225 /* Test if rmdir removes only empty dirs */
226 if (rmdir("foo") != -1) e(29);/* not empty */
227 if (errno != EEXIST && errno != ENOTEMPTY) e(30);
228 /* Test if rmdir removes a dir with an empty file (it shouldn't.) */
229 System("rm -rf foo"); /* cleanup */
230 if (mkdir("foo", 0777) != 0) e(31);
231 System("> foo/empty"); /* > empty */
232 if (rmdir("foo") != -1) e(32);/* not empty */
233 if (errno != EEXIST && errno != ENOTEMPTY) e(33);
234 if (unlink("foo/empty") != 0) e(34); /* rm empty */
235
236 /* See what happens if foo is linked. */
237 if (superuser) {
238 if (link("foo", "footoo") != 0) e(35); /* foo still */
239 if (rmdir("footoo") != 0) e(36); /* exist */
240 if (chdir("footoo") != -1) e(37); /* footoo */
241 if (errno != ENOENT) e(38); /* is gone */
242 }
243#ifdef _MINIX
244 /* Some implementations might allow users to link directories. */
245 if (!superuser) {
246 if (link("foo", "footoo") != -1) e(39);
247 if (errno != EPERM) e(40);
248 if (unlink("foo") != -1) e(41);
249 if (errno != EPERM) e(42);
250 }
251#endif
252
253 /* See if ".." and "." are removed from the dir, and if it is
254 * unwriteable
255 * Note, we can not remove any files in the PARENT
256 * process, because this
257 * will make readdir unpredicatble. (see
258 * 1003.1 page 84 line 30.) However
259 * removal of the directory is
260 * not specified in the standard.
261 */
262 System("rm -rf /tmp/sema[12].07");
263 switch (fork()) {
264 case -1: printf("Can't fork\n"); break;
265
266 case 0:
267 alarm(20);
268 if ((fd = open("foo", O_RDONLY)) <= 2) e(43); /* open */
269 if ((dirp = opendir("foo")) == (DIR *) NULL) e(44); /* opendir */
270 /* UpA downB */
271 system(">/tmp/sema1.07; while test -f /tmp/sema1.07; do sleep 1;done");
272 while ((dep = readdir(dirp)) != DIRENT0) {
273 if (strcmp(dep->d_name, "..") == 0)
274 dotdot += 1;
275 else if (strcmp(dep->d_name, ".") == 0)
276 dot += 1;
277 else
278 other += 1;
279 }
280 if (dotdot != 0) e(45); /* no entrys */
281 if (dot != 0) e(46); /* shoul be */
282 if (other != 0) e(47); /* left or */
283
284 /* No new files (entrys) are allowed on foo */
285 if (creat("foo/nono", 0777) != -1) e(48); /* makeable */
286 if (closedir(dirp) != 0) e(49); /* close foo */
287 system("while test ! -f /tmp/sema2.07; do sleep 1; done"); /* downA */
288 System("rm -f /tmp/sema2.07"); /* clean up */
289
290 /* Foo still exist, so we should be able to get a fstat */
291 if (fstat(fd, &st) != 0) e(50);
292 if (st.st_nlink != (nlink_t) 0) e(51); /* 0 left */
293 if (close(fd) != 0) e(52); /* last one */
294 exit(0);
295
296 default:
297 system("while test ! -f /tmp/sema1.07; do sleep 1; done"); /* downA */
298 if (rmdir("foo") != 0) e(53); /* cleanerup */
299 System("rm -f /tmp/sema1.07"); /* upB */
300 if (chdir("foo") != -1) e(54); /* it should */
301 if (errno != ENOENT) e(55); /* be gone */
302 System("> /tmp/sema2.07"); /* upA */
303 if (wait(&stat_loc) == -1) e(56);
304 if (stat_loc != 0) e(57);
305 }
306
307 /* See if foo isn't accessible any more */
308 if (chdir("foo") != -1) e(58);
309 if (errno != ENOENT) e(59);
310
311 /* Let's see if we can get a EBUSSY..... */
312 if (mkdir("foo", 0777) != 0) e(60); /* mkdir foo */
313 System("rm -f /tmp/sema.07"); /* unness */
314 switch (fork()) {
315 case -1: printf("Can't fork\n"); break;
316 case 0:
317 alarm(20);
318 if (chdir("foo") != 0) e(61); /* child goes */
319 System("> /tmp/sema.07"); /* upA */
320 system("while test -f /tmp/sema.07; do sleep 1; done"); /* downB */
321 sleep(1);
322 exit(0);
323 default:
324 system("while test ! -f /tmp/sema.07; do sleep 1; done"); /* downA */
325 rmdir_result = rmdir("foo"); /* try remove */
326 if (rmdir_result == -1) { /* if it failed */
327 if (errno != EBUSY) e(62); /* foo is busy */
328 } else {
329 if (rmdir_result != 0) e(63);
330 if (rmdir("foo") != -1) e(64); /* not removable */
331 if (errno != ENOENT) e(65); /* again. */
332 if (chdir("foo") != -1) e(66); /* we can't go */
333 if (errno != ENOENT) e(67); /* there any more */
334 if (mkdir("foo", 0777) != 0) e(68); /* we can remake foo */
335 }
336 System("rm -f /tmp/sema.07"); /* upB */
337 if (wait(&stat_loc) == -1) e(69);
338 if (stat_loc != 0) e(70);
339 }
340 if (rmdir("foo") != 0) e(71); /* clean up */
341}
342
343void test28c()
344{ /* Test error handeling. */
345 subtest = 3;
346
347 System("rm -rf ../DIR_28/*");
348 System("rm -rf foo /tmp/foo");/* clean up junk */
349
350 /* Test common errors */
351 if (mkdir("foo", 0777) != 0) e(1); /* mkdir shouldn't fail */
352 if (mkdir("foo", 0777) != -1) e(2); /* should fail the 2d time */
353 if (errno != EEXIST) e(3); /* because it exists already */
354 if (rmdir("foo") != 0) e(4); /* rmdir shouldn't fail */
355 if (rmdir("foo") != -1) e(5); /* but it should now because */
356 if (errno != ENOENT) e(6); /* it's gone the 1st time */
357 /* Test on access etc. */
358 if (mkdir("foo", 0777) != 0) e(7);
359 if (mkdir("foo/bar", 0777) != 0) e(8);
360 if (!superuser) {
361 System("chmod 677 foo");/* make foo inaccesable */
362 if (mkdir("foo/foo", 0777) != -1) e(9);
363 if (errno != EACCES) e(10);
364 if (rmdir("foo/bar") != -1) e(11);
365 if (errno != EACCES) e(12);
366 System("chmod 577 foo");/* make foo unwritable */
367 if (mkdir("foo/foo", 0777) != -1) e(13);
368 if (errno != EACCES) e(14);
369 if (rmdir("foo/bar") != -1) e(15);
370 if (errno != EACCES) e(16);
371 System("chmod 777 foo");/* make foo full accessable */
372 }
373 if (rmdir("foo/bar") != 0) e(17); /* bar should be removable */
374 if (mkdir("foo/no/foo", 0777) != -1) e(18); /* Note: "no" doesn't exist */
375 if (errno != ENOENT) e(19);
376 if (mkdir("", 0777) != -1) e(20); /* empty string isn't ok */
377 if (errno != ENOENT) e(21);
378 if (rmdir("") != -1) e(22); /* empty string isn't ok */
379 if (errno != ENOENT) e(23);
380 System("> foo/no"); /* make a file "no" */
381 if (mkdir("foo/no/foo", 0777) != -1) e(24);
382 if (errno != ENOTDIR) e(25); /* note: "no" is not a a dir */
383 if (rmdir("foo/no/foo") != -1) e(26);
384 if (errno != ENOTDIR) e(27);
385 System("rm -rf foo"); /* clean up */
386}
387
388void makelongnames()
389{
390 register int i;
391
392 memset(MaxName, 'a', NAME_MAX);
393 MaxName[NAME_MAX] = '\0';
394 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
395 MaxPath[i++] = '.';
396 MaxPath[i] = '/';
397 }
398 MaxPath[PATH_MAX - 1] = '\0';
399
400 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
401 strcpy(ToLongPath, MaxPath);
402
403 ToLongName[NAME_MAX] = 'a';
404 ToLongName[NAME_MAX + 1] = '\0'; /* extend ToLongName by one too many */
405 ToLongPath[PATH_MAX - 1] = '/';
406 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
407}
408
409void e(n)
410int n;
411{
412 int err_num = errno; /* Save in case printf clobbers it. */
413
414 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
415 errno = err_num;
416 perror("");
417 if (errct++ > MAX_ERROR) {
418 printf("Too many errors; test aborted\n");
419 chdir("..");
420 system("rm -rf DIR*");
421 exit(1);
422 }
423 errno = 0;
424}
425
426void quit()
427{
428 Chdir("..");
429 System("rm -rf DIR_28");
430
431 if (errct == 0) {
432 printf("ok\n");
433 exit(0);
434 } else {
435 printf("%d errors\n", errct);
436 exit(1);
437 }
438}
Note: See TracBrowser for help on using the repository browser.