source: trunk/minix/test/test24.c@ 10

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

Minix 3.1.2a

File size: 11.6 KB
RevLine 
[9]1/* Test24: opendir, readdir, rewinddir, closedir Author: Jan-Mark Wams */
2
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <sys/wait.h>
6#include <string.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <limits.h>
10#include <fcntl.h>
11#include <dirent.h>
12#include <errno.h>
13#include <time.h>
14#include <stdio.h>
15
16_PROTOTYPE(void main, (int argc, char *argv[]));
17_PROTOTYPE(void chk_dir, (DIR * dirpntr));
18_PROTOTYPE(void test24a, (void));
19_PROTOTYPE(void test24b, (void));
20_PROTOTYPE(void test24c, (void));
21_PROTOTYPE(void makelongnames, (void));
22_PROTOTYPE(void e, (int number));
23_PROTOTYPE(void quit, (void));
24
25#define OVERFLOW_DIR_NR (OPEN_MAX + 1)
26#define MAX_ERROR 4
27#define ITERATIONS 5
28
29#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)
32
33int errct = 0;
34int subtest = 1;
35int superuser;
36
37char MaxName[NAME_MAX + 1]; /* Name of maximum length */
38char MaxPath[PATH_MAX]; /* Same for path */
39char ToLongName[NAME_MAX + 2]; /* Name of maximum +1 length */
40char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
41
42void main(argc, argv)
43int argc;
44char *argv[];
45{
46 int i, m = 0xFFFF;
47
48 sync();
49 if (argc == 2) m = atoi(argv[1]);
50 printf("Test 24 ");
51 fflush(stdout);
52 System("rm -rf DIR_24; mkdir DIR_24");
53 Chdir("DIR_24");
54 makelongnames();
55 superuser = (geteuid() == 0);
56
57 for (i = 0; i < ITERATIONS; i++) {
58 if (m & 0001) test24a();
59 if (m & 0002) test24b();
60 if (m & 0004) test24c();
61 }
62 quit();
63}
64
65void test24a()
66{ /* Test normal operations. */
67 int fd3, fd4, fd5;
68 DIR *dirp;
69 int j, ret, fd, flags;
70 struct stat st1, st2;
71 int stat_loc;
72 time_t time1;
73
74 subtest = 1;
75
76 System("rm -rf ../DIR_24/*");
77
78 if ((fd = dup(0)) != 3) e(1); /* dup stdin */
79 close(fd); /* free the fd again */
80 dirp = opendir("/"); /* open "/" */
81 if (dirp == ((DIR *) NULL)) e(2); /* has to succseed */
82 if ((fd = dup(0)) <= 2) e(3); /* dup stdin */
83 if (fd > 3) { /* if opendir() uses fd 3 */
84 flags = fcntl(3, F_GETFD); /* get fd fags of 3 */
85 if (!(flags & FD_CLOEXEC)) e(4); /* it should be closed on */
86 } /* exec..() calls */
87 close(fd); /* free the fd again */
88 ret = closedir(dirp); /* close, we don't need it */
89 if (ret == -1) e(5); /* closedir () unsucces full */
90 if (ret != 0) e(6); /* should be 0 or -1 */
91 if ((fd = dup(0)) != 3) e(7); /* see if next fd is same */
92 close(fd); /* free the fd again */
93
94 System("rm -rf foo; mkdir foo");
95 Chdir("foo");
96 System("touch f1 f2 f3 f4 f5"); /* make f1 .. f5 */
97 System("rm f[24]"); /* creat `holes' in entrys */
98 Chdir("..");
99
100 if ((dirp = opendir("foo")) == ((DIR *) NULL)) e(8); /* open foo */
101 chk_dir(dirp); /* test if foo's ok */
102 for (j = 0; j < 10; j++) {
103 errno = j * 47 % 7; /* there should */
104 if (readdir(dirp) != DIRENT0) e(9); /* be nomore dir */
105 if (errno != j * 47 % 7) e(10); /* entrys */
106 }
107 rewinddir(dirp); /* rewind foo */
108 chk_dir(dirp); /* test foosok */
109 for (j = 0; j < 10; j++) {
110 errno = j * 23 % 7; /* there should */
111 if (readdir(dirp) != DIRENT0) e(11); /* be nomore dir */
112 if (errno != j * 23 % 7) e(12); /* entrys */
113 }
114 if ((fd4 = creat("foo/f4", 0666)) <= 2) e(13); /* Open a file. */
115 System("rm foo/f4"); /* Kill entry. */
116 rewinddir(dirp); /* Rewind foo. */
117 if ((fd3 = open("foo/f3", O_WRONLY)) <= 2) e(14); /* Open more files. */
118 if ((fd5 = open("foo/f5", O_WRONLY)) <= 2) e(15);
119 if (write(fd3, "Hello", 6) != 6) e(16);
120 if (write(fd4, "Hello", 6) != 6) e(17); /* write some data */
121 if (close(fd5) != 0) e(18);
122 chk_dir(dirp);
123 for (j = 0; j < 10; j++) {
124 errno = j * 101 % 7; /* there should */
125 if (readdir(dirp) != DIRENT0) e(19); /* be nomore dir */
126 if (errno != j * 101 % 7) e(20); /* entrys */
127 }
128 if (close(fd4) != 0) e(21); /* shouldn't matter */
129 if (close(fd3) != 0) e(22); /* when we do this */
130 if (closedir(dirp) != 0) e(23); /* close foo again */
131
132 Chdir("foo");
133 if ((dirp = opendir(".//")) == ((DIR *) NULL)) e(24); /* open foo again */
134 Chdir("..");
135 chk_dir(dirp); /* foosok? */
136 for (j = 0; j < 10; j++) {
137 errno = (j * 101) % 7; /* there should */
138 if (readdir(dirp) != DIRENT0) e(25); /* be nomore dir */
139 if (errno != (j * 101) % 7) e(26); /* entrys */
140 }
141
142 if (closedir(dirp) != 0) e(27); /* It should be closable */
143
144 stat("foo", &st1); /* get stat */
145 time(&time1);
146 while (time1 >= time((time_t *)0))
147 ;
148 if ((dirp = opendir("foo")) == ((DIR *) NULL)) e(28); /* open, */
149 if (readdir(dirp) == DIRENT0) e(29); /* read and */
150 stat("foo", &st2); /* get new stat */
151 if (st1.st_atime > st2.st_atime) e(30); /* st_atime check */
152
153 switch (fork()) {
154 case -1: printf("Can't fork\n"); break;
155 case 0:
156 rewinddir(dirp); /* rewind childs dirp */
157 if (readdir(dirp) == DIRENT0) e(31); /* read should be ok */
158 if (closedir(dirp) != 0) e(32); /* close child'd foo */
159 exit(0); /* 0 stops here */
160 default:
161 if (wait(&stat_loc) == -1) e(33); /* PARENT wait()'s */
162 break;
163 }
164 if (closedir(dirp) != 0) e(34); /* close parent's foo */
165}
166
167void test24b()
168{
169/* See what happens with too many dir's open. Check if file size seems ok,
170 * and independency.
171 */
172
173 int i, j; /* i = highest open dir count */
174 DIR *dirp[OVERFLOW_DIR_NR], *dp;
175 struct dirent *dep, *dep1, *dep2;
176 char name[NAME_MAX + 2]; /* buffer for file name, and count */
177 int dot = 0, dotdot = 0;
178
179 subtest = 2;
180
181 System("rm -rf ../DIR_24/*");
182
183 for (i = 0; i < OVERFLOW_DIR_NR; i++) {
184 dirp[i] = opendir("/");
185 if (dirp[i] == ((DIR *) NULL)) {
186 if (errno != EMFILE) e(1);
187 break;
188 }
189 }
190 if (i <= 4) e(2); /* sounds resanable */
191 if (i >= OVERFLOW_DIR_NR) e(3); /* might be to small */
192 for (j = 0; j < i; j++) {
193 if (closedir(dirp[(j + 5) % i]) != 0) e(4); /* neat! */
194 }
195
196 /* Now check if number of bytes in d_name can go up till NAME_MAX */
197 System("rm -rf foo; mkdir foo");
198 Chdir("foo");
199 name[0] = 0;
200 for (i = 0; i <= NAME_MAX; i++) {
201 if (strcat(name, "X") != name) e(5);
202 close(creat(name, 0666)); /* fails once on */
203 } /* XX..XX, 1 too long */
204 Chdir("..");
205 /* Now change i-th X to Y in name buffer record file of length i. */
206 if ((dp = opendir("foo")) == ((DIR *) NULL)) e(6);
207 while ((dep = readdir(dp)) != DIRENT0) {
208 if (strcmp("..", dep->d_name) == 0)
209 dotdot++;
210 else if (strcmp(".", dep->d_name) == 0)
211 dot++;
212 else
213 name[strlen(dep->d_name)] += 1; /* 'X' + 1 == 'Y' */
214 }
215 if (closedir(dp) != 0) e(7);
216 for (i = 1; i <= NAME_MAX; i++) { /* Check if every length */
217 if (name[i] != 'Y') e(8); /* has been seen once. */
218 }
219
220 /* Check upper and lower bound. */
221 if (name[0] != 'X') e(9);
222 if (name[NAME_MAX + 1] != '\0') e(10);
223
224 /* Now check if two simultaniouse open dirs do the same */
225 if ((dirp[1] = opendir("foo")) == ((DIR *) NULL)) e(11);
226 if ((dirp[2] = opendir("foo")) == ((DIR *) NULL)) e(12);
227 if ((dep1 = readdir(dirp[1])) == DIRENT0) e(13);
228 if ((dep2 = readdir(dirp[2])) == DIRENT0) e(14);
229 if (dep1->d_name == dep2->d_name) e(15); /* 1 & 2 Should be */
230 strcpy(name, dep2->d_name); /* differand buffers */
231 if (strcmp(dep1->d_name, name) != 0) e(16); /* But hold the same */
232 if ((dep1 = readdir(dirp[1])) == DIRENT0) e(17);
233 if ((dep1 = readdir(dirp[1])) == DIRENT0) e(18); /* lose some entries */
234 if ((dep1 = readdir(dirp[1])) == DIRENT0) e(19); /* Using dirp 1 has */
235 if (dep1->d_name == dep2->d_name) e(20); /* no effect on 2 */
236 if (strcmp(dep2->d_name, name) != 0) e(21);
237 rewinddir(dirp[1]); /* Rewinding dirp 1 */
238 if ((dep2 = readdir(dirp[2])) == DIRENT0) e(22); /* can't effect 2 */
239 if (strcmp(dep2->d_name, name) == 0) e(23); /* Must be next */
240 if (closedir(dirp[1]) != 0) e(24); /* Closing dirp 1 */
241 if ((dep2 = readdir(dirp[2])) == DIRENT0) e(25); /* can't effect 2 */
242 if (strcmp(dep2->d_name, name) == 0) e(26); /* Must be next */
243 if (closedir(dirp[2]) != 0) e(27);
244}
245
246void test24c()
247{
248/* Test whether wrong things go wrong right. */
249
250 DIR *dirp;
251
252 subtest = 3;
253
254 System("rm -rf ../DIR_24/*");
255
256 if (opendir("foo/bar/nono") != ((DIR *) NULL)) e(1); /* nonexistent */
257 if (errno != ENOENT) e(2);
258 System("mkdir foo; chmod 677 foo"); /* foo inaccesable */
259 if (opendir("foo/bar/nono") != ((DIR *) NULL)) e(3);
260 if (superuser) {
261 if (errno != ENOENT) e(4); /* su has access */
262 System("chmod 377 foo");
263 if ((dirp = opendir("foo")) == ((DIR *) NULL)) e(5);
264 if (closedir(dirp) != 0) e(6);
265 }
266 if (!superuser) {
267 if (errno != EACCES) e(7); /* we don't ;-) */
268 System("chmod 377 foo");
269 if (opendir("foo") != ((DIR *) NULL)) e(8);
270 }
271 System("chmod 777 foo");
272
273 if (mkdir(MaxName, 0777) != 0) e(9); /* make longdir */
274 if ((dirp = opendir(MaxName)) == ((DIR *) NULL)) e(10); /* open it */
275 if (closedir(dirp) != 0) e(11); /* close it */
276 if (rmdir(MaxName) != 0) e(12); /* then remove it */
277 if ((dirp = opendir(MaxPath)) == ((DIR *) NULL)) e(13); /* open '.' */
278 if (closedir(dirp) != 0) e(14); /* close it */
279#if 0 /* XXX - anything could happen with the bad pointer */
280 if (closedir(dirp) != -1) e(15); /* close it again */
281 if (closedir(dirp) != -1) e(16); /* and again */
282#endif /* 0 */
283 if (opendir(ToLongName) != ((DIR *) NULL)) e(17); /* is too long */
284#ifdef _POSIX_NO_TRUNC
285# if _POSIX_NO_TRUNC - 0 != -1
286 if (errno != ENAMETOOLONG) e(18);
287# else
288 if (errno != ENOENT) e(19);
289# endif
290#else
291# include "error, this case requires dynamic checks and is not handled"
292#endif
293 if (opendir(ToLongPath) != ((DIR *) NULL)) e(20); /* path is too long */
294 if (errno != ENAMETOOLONG) e(21);
295 System("touch foo/abc"); /* make a file */
296 if (opendir("foo/abc") != ((DIR *) NULL)) e(22); /* not a dir */
297 if (errno != ENOTDIR) e(23);
298}
299
300void chk_dir(dirp) /* dir should contain */
301DIR *dirp; /* (`f1', `f3', `f5', `.', `..') */
302{ /* no more, no less */
303 int f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0, /* counters for all */
304 other = 0, dot = 0, dotdot = 0; /* possible entrys */
305 int i;
306 struct dirent *dep;
307 char *fname;
308 int oldsubtest = subtest;
309
310 subtest = 4;
311
312 for (i = 0; i < 5; i++) { /* 3 files and `.' and `..' == 5 entrys */
313 dep = readdir(dirp);
314 if (dep == DIRENT0) { /* not einough */
315 if (dep == DIRENT0) e(1);
316 break;
317 }
318 fname = dep->d_name;
319 if (strcmp(fname, ".") == 0)
320 dot++;
321 else if (strcmp(fname, "..") == 0)
322 dotdot++;
323 else if (strcmp(fname, "f1") == 0)
324 f1++;
325 else if (strcmp(fname, "f2") == 0)
326 f2++;
327 else if (strcmp(fname, "f3") == 0)
328 f3++;
329 else if (strcmp(fname, "f4") == 0)
330 f4++;
331 else if (strcmp(fname, "f5") == 0)
332 f5++;
333 else
334 other++;
335 } /* do next dir entry */
336
337 if (dot != 1) e(2); /* Check the entrys */
338 if (dotdot != 1) e(3);
339 if (f1 != 1) e(4);
340 if (f3 != 1) e(5);
341 if (f5 != 1) e(6);
342 if (f2 != 0) e(7);
343 if (f4 != 0) e(8);
344 if (other != 0) e(9);
345
346 subtest = oldsubtest;
347}
348
349void makelongnames()
350{
351 register int i;
352
353 memset(MaxName, 'a', NAME_MAX);
354 MaxName[NAME_MAX] = '\0';
355 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
356 MaxPath[i++] = '.';
357 MaxPath[i] = '/';
358 }
359 MaxPath[PATH_MAX - 1] = '\0';
360
361 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
362 strcpy(ToLongPath, MaxPath);
363
364 ToLongName[NAME_MAX] = 'a';
365 ToLongName[NAME_MAX + 1] = '\0'; /* extend ToLongName by one too many */
366 ToLongPath[PATH_MAX - 1] = '/';
367 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
368}
369
370void e(n)
371int n;
372{
373 int err_num = errno; /* Save in case printf clobbers it. */
374
375 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
376 errno = err_num;
377 perror("");
378 if (errct++ > MAX_ERROR) {
379 printf("Too many errors; test aborted\n");
380 chdir("..");
381 system("rm -rf DIR*");
382 exit(1);
383 }
384 errno = 0;
385}
386
387void quit()
388{
389 Chdir("..");
390 System("rm -rf DIR_24");
391
392 if (errct == 0) {
393 printf("ok\n");
394 exit(0);
395 } else {
396 printf("%d errors\n", errct);
397 exit(1);
398 }
399}
Note: See TracBrowser for help on using the repository browser.