vm: use arch_map2str to print pagefault info, to properly display code addrs
[minix.git] / test / test28.c
blob223726c845f8d83929aadd236471657302c216c6
1 /* test28: mkdir() rmdir() Author: Jan-Mark Wams (jms@cs.vu.nl) */
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 */
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>
23 #define MAX_ERROR 4
24 #define ITERATIONS 2
26 #define DIRENT0 ((struct dirent *) NULL)
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)
31 int errct = 0;
32 int subtest = 1;
33 int superuser;
34 char MaxName[NAME_MAX + 1]; /* Name of maximum length */
35 char MaxPath[PATH_MAX];
36 char ToLongName[NAME_MAX + 2]; /* Name of maximum +1 length */
37 char ToLongPath[PATH_MAX + 1];
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));
47 void main(argc, argv)
48 int argc;
49 char *argv[];
51 int i, m = 0xFFFF;
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 */
64 for (i = 0; i < ITERATIONS; i++) {
65 if (m & 0001) test28a();
66 if (m & 0002) test28b();
67 if (m & 0004) test28c();
69 quit();
72 void test28a()
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;
81 subtest = 1;
83 System("rm -rf foo /tmp/foo");/* clean up junk */
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 */
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 */
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);
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 */
119 /* Check the stat */
120 time(&time1);
121 while (time1 >= time((time_t *)0))
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))
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);
141 /* Check if parent is updated */
142 if (stat(".", &st) != 0) e(34);
143 time(&time2);
144 while (time2 >= time((time_t *)0))
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))
154 if (rmdir("foo") != 0) e(39);
155 if (stat(".", &st) != 0) e(40);
156 time(&time2);
157 while (time2 >= time((time_t *)0))
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);
166 void 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;
178 subtest = 2;
180 System("rm -rf ../DIR_28/*");
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 */
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 */
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);
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);
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
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 */
236 /* See what happens if foo is linked. */
237 #if 0
238 if (superuser) {
239 if (link("foo", "footoo") != 0) e(35); /* foo still */
240 if (rmdir("footoo") != 0) e(36); /* exist */
241 if (chdir("footoo") != -1) e(37); /* footoo */
242 if (errno != ENOENT) e(38); /* is gone */
244 #endif
245 #ifdef _MINIX
246 /* Some implementations might allow users to link directories. */
247 if (!superuser) {
248 if (link("foo", "footoo") != -1) e(39);
249 if (errno != EPERM) e(40);
250 if (unlink("foo") != -1) e(41);
251 if (errno != EPERM) e(42);
253 #endif
255 /* See if ".." and "." are removed from the dir, and if it is
256 * unwriteable
257 * Note, we can not remove any files in the PARENT
258 * process, because this
259 * will make readdir unpredicatble. (see
260 * 1003.1 page 84 line 30.) However
261 * removal of the directory is
262 * not specified in the standard.
264 System("rm -rf /tmp/sema[12].07");
265 switch (fork()) {
266 case -1: printf("Can't fork\n"); break;
268 case 0:
269 alarm(20);
270 if ((fd = open("foo", O_RDONLY)) <= 2) e(43); /* open */
271 if ((dirp = opendir("foo")) == (DIR *) NULL) e(44); /* opendir */
272 /* UpA downB */
273 system(">/tmp/sema1.07; while test -f /tmp/sema1.07; do sleep 1;done");
274 while ((dep = readdir(dirp)) != DIRENT0) {
275 if (strcmp(dep->d_name, "..") == 0)
276 dotdot += 1;
277 else if (strcmp(dep->d_name, ".") == 0)
278 dot += 1;
279 else
280 other += 1;
282 if (dotdot != 0) e(45); /* no entrys */
283 if (dot != 0) e(46); /* shoul be */
284 if (other != 0) e(47); /* left or */
286 /* No new files (entrys) are allowed on foo */
287 if (creat("foo/nono", 0777) != -1) e(48); /* makeable */
288 if (closedir(dirp) != 0) e(49); /* close foo */
289 system("while test ! -f /tmp/sema2.07; do sleep 1; done"); /* downA */
290 System("rm -f /tmp/sema2.07"); /* clean up */
292 /* Foo still exist, so we should be able to get a fstat */
293 if (fstat(fd, &st) != 0) e(50);
294 if (st.st_nlink != (nlink_t) 0) e(51); /* 0 left */
295 if (close(fd) != 0) e(52); /* last one */
296 exit(0);
298 default:
299 system("while test ! -f /tmp/sema1.07; do sleep 1; done"); /* downA */
300 if (rmdir("foo") != 0) e(53); /* cleanerup */
301 System("rm -f /tmp/sema1.07"); /* upB */
302 if (chdir("foo") != -1) e(54); /* it should */
303 if (errno != ENOENT) e(55); /* be gone */
304 System("> /tmp/sema2.07"); /* upA */
305 if (wait(&stat_loc) == -1) e(56);
306 if (stat_loc != 0) e(57);
309 /* See if foo isn't accessible any more */
310 if (chdir("foo") != -1) e(58);
311 if (errno != ENOENT) e(59);
313 /* Let's see if we can get a EBUSSY..... */
314 if (mkdir("foo", 0777) != 0) e(60); /* mkdir foo */
315 System("rm -f /tmp/sema.07"); /* unness */
316 switch (fork()) {
317 case -1: printf("Can't fork\n"); break;
318 case 0:
319 alarm(20);
320 if (chdir("foo") != 0) e(61); /* child goes */
321 System("> /tmp/sema.07"); /* upA */
322 system("while test -f /tmp/sema.07; do sleep 1; done"); /* downB */
323 sleep(1);
324 exit(0);
325 default:
326 system("while test ! -f /tmp/sema.07; do sleep 1; done"); /* downA */
327 rmdir_result = rmdir("foo"); /* try remove */
328 if (rmdir_result == -1) { /* if it failed */
329 if (errno != EBUSY) e(62); /* foo is busy */
330 } else {
331 if (rmdir_result != 0) e(63);
332 if (rmdir("foo") != -1) e(64); /* not removable */
333 if (errno != ENOENT) e(65); /* again. */
334 if (chdir("foo") != -1) e(66); /* we can't go */
335 if (errno != ENOENT) e(67); /* there any more */
336 if (mkdir("foo", 0777) != 0) e(68); /* we can remake foo */
338 System("rm -f /tmp/sema.07"); /* upB */
339 if (wait(&stat_loc) == -1) e(69);
340 if (stat_loc != 0) e(70);
342 if (rmdir("foo") != 0) e(71); /* clean up */
345 void test28c()
346 { /* Test error handeling. */
347 subtest = 3;
349 System("rm -rf ../DIR_28/*");
350 System("rm -rf foo /tmp/foo");/* clean up junk */
352 /* Test common errors */
353 if (mkdir("foo", 0777) != 0) e(1); /* mkdir shouldn't fail */
354 if (mkdir("foo", 0777) != -1) e(2); /* should fail the 2d time */
355 if (errno != EEXIST) e(3); /* because it exists already */
356 if (rmdir("foo") != 0) e(4); /* rmdir shouldn't fail */
357 if (rmdir("foo") != -1) e(5); /* but it should now because */
358 if (errno != ENOENT) e(6); /* it's gone the 1st time */
359 /* Test on access etc. */
360 if (mkdir("foo", 0777) != 0) e(7);
361 if (mkdir("foo/bar", 0777) != 0) e(8);
362 if (!superuser) {
363 System("chmod 677 foo");/* make foo inaccesable */
364 if (mkdir("foo/foo", 0777) != -1) e(9);
365 if (errno != EACCES) e(10);
366 if (rmdir("foo/bar") != -1) e(11);
367 if (errno != EACCES) e(12);
368 System("chmod 577 foo");/* make foo unwritable */
369 if (mkdir("foo/foo", 0777) != -1) e(13);
370 if (errno != EACCES) e(14);
371 if (rmdir("foo/bar") != -1) e(15);
372 if (errno != EACCES) e(16);
373 System("chmod 777 foo");/* make foo full accessable */
375 if (rmdir("foo/bar") != 0) e(17); /* bar should be removable */
376 if (mkdir("foo/no/foo", 0777) != -1) e(18); /* Note: "no" doesn't exist */
377 if (errno != ENOENT) e(19);
378 if (mkdir("", 0777) != -1) e(20); /* empty string isn't ok */
379 if (errno != ENOENT) e(21);
380 if (rmdir("") != -1) e(22); /* empty string isn't ok */
381 if (errno != ENOENT) e(23);
382 System("> foo/no"); /* make a file "no" */
383 if (mkdir("foo/no/foo", 0777) != -1) e(24);
384 if (errno != ENOTDIR) e(25); /* note: "no" is not a a dir */
385 if (rmdir("foo/no/foo") != -1) e(26);
386 if (errno != ENOTDIR) e(27);
387 System("rm -rf foo"); /* clean up */
390 void makelongnames()
392 register int i;
394 memset(MaxName, 'a', NAME_MAX);
395 MaxName[NAME_MAX] = '\0';
396 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
397 MaxPath[i++] = '.';
398 MaxPath[i] = '/';
400 MaxPath[PATH_MAX - 1] = '\0';
402 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
403 strcpy(ToLongPath, MaxPath);
405 ToLongName[NAME_MAX] = 'a';
406 ToLongName[NAME_MAX + 1] = '\0'; /* extend ToLongName by one too many */
407 ToLongPath[PATH_MAX - 1] = '/';
408 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
411 void e(n)
412 int n;
414 int err_num = errno; /* Save in case printf clobbers it. */
416 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
417 errno = err_num;
418 perror("");
419 if (errct++ > MAX_ERROR) {
420 printf("Too many errors; test aborted\n");
421 chdir("..");
422 system("rm -rf DIR*");
423 exit(1);
425 errno = 0;
428 void quit()
430 Chdir("..");
431 System("rm -rf DIR_28");
433 if (errct == 0) {
434 printf("ok\n");
435 exit(0);
436 } else {
437 printf("%d errors\n", errct);
438 exit(1);