tar: use utime() to restore timestamps
[minix.git] / test / test28.c
blobc610bc728c1e5d6e102715d9ba6f51c7d61a5f12
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 #include "common.c"
28 #define DIRENT0 ((struct dirent *) NULL)
30 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
31 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
33 int subtest = 1;
34 int superuser;
35 char *MaxName; /* Name of maximum length */
36 char MaxPath[PATH_MAX];
37 char *ToLongName; /* Name of maximum +1 length */
38 char ToLongPath[PATH_MAX + 1];
40 void test28a(void);
41 void test28c(void);
42 void test28b(void);
43 void makelongnames(void);
45 int main(int argc, char *argv[])
47 int i, m = 0xFFFF;
49 sync();
50 if (argc == 2) m = atoi(argv[1]);
51 start(28);
52 superuser = (getuid() == 0);
53 makelongnames();
54 umask(0000); /* no umask */
56 for (i = 0; i < ITERATIONS; i++) {
57 if (m & 0001) test28a();
58 if (m & 0002) test28b();
59 if (m & 0004) test28c();
61 quit();
63 return(-1); /* Unreachable */
66 void test28a()
68 int mode; /* used in for loop */
69 struct stat st;
70 time_t time1, time2;
71 DIR *dirp;
72 struct dirent *dep;
73 int dot = 0, dotdot = 0;
75 subtest = 1;
77 System("rm -rf foo /tmp/foo");/* clean up junk */
79 /* Check relative path names */
80 if (mkdir("./foo", 0777) != 0) e(1); /* make a dir foo */
81 if (mkdir("./foo/bar", 0777) != 0) e(2); /* make foo/bar */
82 if (rmdir("foo/bar") != 0) e(3); /* delete bar */
83 if (mkdir("foo/../foo/bar", 0777) != 0) e(4); /* make bar again */
84 if (rmdir("./foo/bar") != 0) e(5); /* and remove again */
86 /* Foo should be empty (ie. contain only "." and ".." */
87 if ((dirp = opendir("foo")) == (DIR *) NULL) e(6); /* open foo */
88 if ((dep = readdir(dirp)) == DIRENT0) e(7); /* get first entry */
89 if (strcmp(dep->d_name, ".") == 0) dot += 1; /* record what it is */
90 if (strcmp(dep->d_name, "..") == 0) dotdot += 1;
91 if ((dep = readdir(dirp)) == DIRENT0) e(8); /* get second entry */
92 if (strcmp(dep->d_name, ".") == 0) dot += 1; /* record again */
93 if (strcmp(dep->d_name, "..") == 0) dotdot += 1;
94 if ((dep = readdir(dirp)) != DIRENT0) e(9); /* no 3d entry */
95 if (dot == 1 && dotdot != 1) e(10); /* only . and .. */
96 if (closedir(dirp) != 0) e(11); /* close foo */
97 if (rmdir("./foo") != 0) e(12); /* remove dir foo */
99 /* Check absolute path names */
100 if (mkdir("/tmp/foo", 0777) != 0) e(13);
101 if (mkdir("/tmp/foo/bar", 0777) != 0) e(14);
102 if (rmdir("/tmp/foo/bar") != 0) e(15); /* make some dirs */
103 if (rmdir("/tmp/foo") != 0) e(16);
105 /* Check the mode arument for mkdir() */
106 for (mode = 0; mode <= 0777; mode++) {
107 if (mkdir("foo", mode) != 0) e(17); /* make foo */
108 if (stat("foo", &st) != 0) e(18);
109 if ((st.st_mode & 0777) != mode) e(19); /* check it's mode */
110 if (rmdir("foo") != 0) e(20); /* and remove it */
113 /* Check the stat */
114 time(&time1);
115 while (time1 >= time((time_t *)0))
117 if (mkdir("foo", 0765) != 0) e(21); /* make foo */
118 if (stat("foo", &st) != 0) e(22);
119 time(&time2);
120 while (time2 >= time((time_t *)0))
122 time(&time2);
123 if (st.st_nlink != 2) e(23);
124 if (st.st_uid != geteuid()) e(24);
125 if (st.st_gid != getegid()) e(25);
126 if (st.st_size < 0) e(26);
127 if ((st.st_mode & 0777) != 0765) e(27);
128 if (st.st_atime <= time1) e(28);
129 if (st.st_atime >= time2) e(29);
130 if (st.st_ctime <= time1) e(30);
131 if (st.st_ctime >= time2) e(31);
132 if (st.st_mtime <= time1) e(32);
133 if (st.st_mtime >= time2) e(33);
135 /* Check if parent is updated */
136 if (stat(".", &st) != 0) e(34);
137 time(&time2);
138 while (time2 >= time((time_t *)0))
140 time(&time2);
141 if (st.st_ctime <= time1) e(35);
142 if (st.st_ctime >= time2) e(36);
143 if (st.st_mtime <= time1) e(37);
144 if (st.st_mtime >= time2) e(38);
145 time(&time1);
146 while (time1 >= time((time_t *)0))
148 if (rmdir("foo") != 0) e(39);
149 if (stat(".", &st) != 0) e(40);
150 time(&time2);
151 while (time2 >= time((time_t *)0))
153 time(&time2);
154 if (st.st_ctime <= time1) e(41);
155 if (st.st_ctime >= time2) e(42);
156 if (st.st_mtime <= time1) e(43);
157 if (st.st_mtime >= time2) e(44);
160 void test28b()
161 { /* Test critical values. */
162 struct stat st;
163 DIR *dirp;
164 struct dirent *dep;
165 int fd; /* file descriptor */
166 int other = 0, dot = 0, dotdot = 0; /* dirent counters */
167 int r; /* Intermediate result */
168 int rmdir_result; /* tmp var */
169 int stat_loc, does_truncate;
171 subtest = 2;
173 System("rm -rf ../DIR_28/*");
175 /* Check funny but valid path names */
176 if (mkdir("/../../..////.//../tmp/foo/", 0777) != 0) e(1);
177 if (mkdir("/tmp/foo//////..//foo//../foo/bar/", 0777) != 0) e(2);
178 if (rmdir("///tmp/..//tmp/foo/bar//../..//foo/bar") != 0) e(3);
179 if (mkdir("///tmp/foo/foobar//", 0777) != 0) e(4);
180 if (rmdir("/tmp/foo/foobar//") != 0) e(5);
181 if (rmdir("/.././/././/tmp/foo///////////////") != 0) e(6);
182 if (rmdir("/tmp/foo") != -1) e(7); /* try again */
184 /* Test max path ed. */
185 if (mkdir(MaxName, 0777) != 0) e(9); /* make dir MaxName */
186 if (rmdir(MaxName) != 0) e(10); /* and remove it */
187 MaxPath[strlen(MaxPath) - 2] = '/'; /* convert MaxPath */
188 MaxPath[strlen(MaxPath) - 1] = 'a'; /* to ././.../a */
189 if (mkdir(MaxPath, 0777) != 0) e(11); /* it should be */
190 if (rmdir(MaxPath) != 0) e(12); /* ok */
192 /* Test too long path ed. */
193 does_truncate = does_fs_truncate();
194 r = mkdir(ToLongName, 0777);
195 if (does_truncate ) {
196 /* FS truncates names, mkdir should've worked */
197 if (r != 0) e(13); /* Try ToLongName */
198 if (rmdir(ToLongName) != 0) e(14); /* and remove it */
199 } else {
200 /* Too long, should've failed with ENAMETOOLONG */
201 if (r == 0) e(15);
202 if (errno != ENAMETOOLONG) e(16);
204 ToLongPath[strlen(ToLongPath) - 2] = '/'; /* make ToLongPath */
205 ToLongPath[strlen(ToLongPath) - 1] = 'a'; /* contain ././.../a */
206 if (mkdir(ToLongPath, 0777) != -1) e(17); /* it should */
207 if (errno != ENAMETOOLONG) e(18); /* not be ok */
208 if (rmdir(ToLongPath) != -1) e(19);
209 if (errno != ENAMETOOLONG) e(20);
211 if (mkdir("foo", 0777) != 0) e(21);
212 System("touch foo/xyzzy");
214 /* Test if rmdir removes only empty dirs */
215 if (rmdir("foo") != -1) e(29);/* not empty */
216 if (errno != EEXIST && errno != ENOTEMPTY) e(30);
217 /* Test if rmdir removes a dir with an empty file (it shouldn't.) */
218 System("rm -rf foo"); /* cleanup */
219 if (mkdir("foo", 0777) != 0) e(31);
220 System("> foo/empty"); /* > empty */
221 if (rmdir("foo") != -1) e(32);/* not empty */
222 if (errno != EEXIST && errno != ENOTEMPTY) e(33);
223 if (unlink("foo/empty") != 0) e(34); /* rm empty */
225 /* See what happens if foo is linked. */
226 #if 0
227 if (superuser) {
228 if (link("foo", "footoo") != 0) e(35); /* foo still */
229 if (rmdir("footoo") != 0) e(36); /* exist */
230 if (chdir("footoo") != -1) e(37); /* footoo */
231 if (errno != ENOENT) e(38); /* is gone */
233 #endif
234 #ifdef _MINIX
235 /* Some implementations might allow users to link directories. */
236 if (!superuser) {
237 if (link("foo", "footoo") != -1) e(39);
238 if (errno != EPERM) e(40);
239 if (unlink("foo") != -1) e(41);
240 if (errno != EPERM) e(42);
242 #endif
244 /* See if ".." and "." are removed from the dir, and if it is
245 * unwriteable
246 * Note, we can not remove any files in the PARENT
247 * process, because this
248 * will make readdir unpredicatble. (see
249 * 1003.1 page 84 line 30.) However
250 * removal of the directory is
251 * not specified in the standard.
253 System("rm -rf /tmp/sema[12].07");
254 switch (fork()) {
255 case -1: printf("Can't fork\n"); break;
257 case 0:
258 alarm(20);
259 if ((fd = open("foo", O_RDONLY)) <= 2) e(43); /* open */
260 if ((dirp = opendir("foo")) == (DIR *) NULL) e(44); /* opendir */
261 /* UpA downB */
262 system(">/tmp/sema1.07; while test -f /tmp/sema1.07; do sleep 1;done");
263 while ((dep = readdir(dirp)) != DIRENT0) {
264 if (strcmp(dep->d_name, "..") == 0)
265 dotdot += 1;
266 else if (strcmp(dep->d_name, ".") == 0)
267 dot += 1;
268 else
269 other += 1;
271 if (dotdot != 0) e(45); /* no entrys */
272 if (dot != 0) e(46); /* shoul be */
273 if (other != 0) e(47); /* left or */
275 /* No new files (entrys) are allowed on foo */
276 if (creat("foo/nono", 0777) != -1) e(48); /* makeable */
277 if (closedir(dirp) != 0) e(49); /* close foo */
278 system("while test ! -f /tmp/sema2.07; do sleep 1; done"); /* downA */
279 System("rm -f /tmp/sema2.07"); /* clean up */
281 /* Foo still exist, so we should be able to get a fstat */
282 if (fstat(fd, &st) != 0) e(50);
283 if (st.st_nlink != (nlink_t) 0) e(51); /* 0 left */
284 if (close(fd) != 0) e(52); /* last one */
285 exit(0);
287 default:
288 system("while test ! -f /tmp/sema1.07; do sleep 1; done"); /* downA */
289 if (rmdir("foo") != 0) e(53); /* cleanerup */
290 System("rm -f /tmp/sema1.07"); /* upB */
291 if (chdir("foo") != -1) e(54); /* it should */
292 if (errno != ENOENT) e(55); /* be gone */
293 System("> /tmp/sema2.07"); /* upA */
294 if (wait(&stat_loc) == -1) e(56);
295 if (stat_loc != 0) e(57);
298 /* See if foo isn't accessible any more */
299 if (chdir("foo") != -1) e(58);
300 if (errno != ENOENT) e(59);
302 /* Let's see if we can get a EBUSSY..... */
303 if (mkdir("foo", 0777) != 0) e(60); /* mkdir foo */
304 System("rm -f /tmp/sema.07"); /* unness */
305 switch (fork()) {
306 case -1: printf("Can't fork\n"); break;
307 case 0:
308 alarm(20);
309 if (chdir("foo") != 0) e(61); /* child goes */
310 System("> /tmp/sema.07"); /* upA */
311 system("while test -f /tmp/sema.07; do sleep 1; done"); /* downB */
312 sleep(1);
313 exit(0);
314 default:
315 system("while test ! -f /tmp/sema.07; do sleep 1; done"); /* downA */
316 rmdir_result = rmdir("foo"); /* try remove */
317 if (rmdir_result == -1) { /* if it failed */
318 if (errno != EBUSY) e(62); /* foo is busy */
319 } else {
320 if (rmdir_result != 0) e(63);
321 if (rmdir("foo") != -1) e(64); /* not removable */
322 if (errno != ENOENT) e(65); /* again. */
323 if (chdir("foo") != -1) e(66); /* we can't go */
324 if (errno != ENOENT) e(67); /* there any more */
325 if (mkdir("foo", 0777) != 0) e(68); /* we can remake foo */
327 System("rm -f /tmp/sema.07"); /* upB */
328 if (wait(&stat_loc) == -1) e(69);
329 if (stat_loc != 0) e(70);
331 if (rmdir("foo") != 0) e(71); /* clean up */
334 void test28c()
335 { /* Test error handeling. */
336 subtest = 3;
338 System("rm -rf ../DIR_28/*");
339 System("rm -rf foo /tmp/foo");/* clean up junk */
341 /* Test common errors */
342 if (mkdir("foo", 0777) != 0) e(1); /* mkdir shouldn't fail */
343 if (mkdir("foo", 0777) != -1) e(2); /* should fail the 2d time */
344 if (errno != EEXIST) e(3); /* because it exists already */
345 if (rmdir("foo") != 0) e(4); /* rmdir shouldn't fail */
346 if (rmdir("foo") != -1) e(5); /* but it should now because */
347 if (errno != ENOENT) e(6); /* it's gone the 1st time */
348 /* Test on access etc. */
349 if (mkdir("foo", 0777) != 0) e(7);
350 if (mkdir("foo/bar", 0777) != 0) e(8);
351 if (!superuser) {
352 System("chmod 677 foo");/* make foo inaccesable */
353 if (mkdir("foo/foo", 0777) != -1) e(9);
354 if (errno != EACCES) e(10);
355 if (rmdir("foo/bar") != -1) e(11);
356 if (errno != EACCES) e(12);
357 System("chmod 577 foo");/* make foo unwritable */
358 if (mkdir("foo/foo", 0777) != -1) e(13);
359 if (errno != EACCES) e(14);
360 if (rmdir("foo/bar") != -1) e(15);
361 if (errno != EACCES) e(16);
362 System("chmod 777 foo");/* make foo full accessable */
364 if (rmdir("foo/bar") != 0) e(17); /* bar should be removable */
365 if (mkdir("foo/no/foo", 0777) != -1) e(18); /* Note: "no" doesn't exist */
366 if (errno != ENOENT) e(19);
367 if (mkdir("", 0777) != -1) e(20); /* empty string isn't ok */
368 if (errno != ENOENT) e(21);
369 if (rmdir("") != -1) e(22); /* empty string isn't ok */
370 if (errno != ENOENT) e(23);
371 System("> foo/no"); /* make a file "no" */
372 if (mkdir("foo/no/foo", 0777) != -1) e(24);
373 if (errno != ENOTDIR) e(25); /* note: "no" is not a a dir */
374 if (rmdir("foo/no/foo") != -1) e(26);
375 if (errno != ENOTDIR) e(27);
376 System("rm -rf foo"); /* clean up */
379 void makelongnames()
381 register int i;
382 int max_name_length;
384 max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
385 * the same length, hence runtime check */
386 MaxName = malloc(max_name_length + 1);
387 ToLongName = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
388 memset(MaxName, 'a', max_name_length);
389 MaxName[max_name_length] = '\0';
391 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
392 MaxPath[i++] = '.';
393 MaxPath[i] = '/';
395 MaxPath[PATH_MAX - 1] = '\0';
397 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
398 strcpy(ToLongPath, MaxPath);
400 ToLongName[max_name_length] = 'a';
401 ToLongName[max_name_length+1] = '\0';/* extend ToLongName by one too many */
402 ToLongPath[PATH_MAX - 1] = '/';
403 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */