source: trunk/minix/test/test23.c@ 20

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

Minix 3.1.2a

File size: 12.2 KB
Line 
1/* test23: chdir(), getcwd() Author: Jan-Mark Wams (jms@cs.vu.nl) */
2
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <sys/dir.h>
6#include <limits.h>
7#include <errno.h>
8#include <unistd.h>
9#include <stdlib.h>
10#include <string.h>
11#include <fcntl.h>
12#include <stdio.h>
13
14#define MAX_ERROR 4
15#define ITERATIONS 3
16
17#define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
18#define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
19
20int errct;
21int subtest;
22int superuser; /* True if we are root. */
23
24char cwd[PATH_MAX]; /* Space for path names. */
25char cwd2[PATH_MAX];
26char buf[PATH_MAX];
27char MaxName[NAME_MAX + 1]; /* Name of maximum length */
28char MaxPath[PATH_MAX]; /* Same for path */
29char ToLongName[NAME_MAX + 2]; /* Name of maximum +1 length */
30char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
31
32_PROTOTYPE(void main, (int argc, char *argv[]));
33_PROTOTYPE(void test23a, (void));
34_PROTOTYPE(void test23b, (void));
35_PROTOTYPE(void test23c, (void));
36_PROTOTYPE(void makelongnames, (void)); /* Fill MaxName etc. */
37_PROTOTYPE(char *last_index, (char *string, int ch));
38_PROTOTYPE(char *my_getcwd, (char *buf, int size));
39_PROTOTYPE(void e, (int number));
40_PROTOTYPE(void quit, (void));
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 23 ");
51 fflush(stdout);
52 System("rm -rf DIR_23; mkdir DIR_23");
53 Chdir("DIR_23");
54 makelongnames();
55 superuser = (geteuid() == 0);
56
57 for (i = 0; i < ITERATIONS; i++) {
58 if (m & 0001) test23a(); /* Test normal operation */
59 if (m & 0002) test23b(); /* Test critical operation */
60 if (m & 0004) test23c(); /* Test error operation */
61 }
62
63 quit();
64}
65
66void test23a()
67{ /* Test normal operation. */
68 register int i;
69
70 subtest = 1;
71
72 System("rm -rf ../DIR_23/*");
73
74 /* Let's do some fiddeling with path names. */
75 if (getcwd(cwd, PATH_MAX) != cwd) e(1);
76 if (chdir(cwd) != 0) e(2);
77 if (getcwd(buf, PATH_MAX) != buf) e(3);
78 if (strcmp(buf, cwd) != 0) e(4);
79 if (chdir(".") != 0) e(5);
80 if (getcwd(buf, PATH_MAX) != buf) e(6);
81 if (strcmp(buf, cwd) != 0) e(7);
82 if (chdir("./././.") != 0) e(8);
83 if (getcwd(buf, PATH_MAX) != buf) e(9);
84 if (strcmp(buf, cwd) != 0) e(10);
85
86 /* Creat a working dir named "foo", remove any previous residues. */
87 System("rm -rf foo");
88 if (mkdir("foo", 0777) != 0) e(11);
89
90 /* Do some more fiddeling with path names. */
91 if (chdir("foo/.././foo/..") != 0) e(12); /* change to cwd */
92 if (getcwd(buf, PATH_MAX) != buf) e(13);
93 if (strcmp(buf, cwd) != 0) e(13);
94 if (chdir("foo") != 0) e(14); /* change to foo */
95 if (chdir("..") != 0) e(15); /* and back again */
96 if (getcwd(buf, PATH_MAX) != buf) e(16);
97 if (strcmp(buf, cwd) != 0) e(17);
98
99 /* Make 30 sub dirs, eg. ./bar/bar/bar/bar/bar...... */
100 System("rm -rf bar"); /* get ridd of bar */
101 for (i = 0; i < 30; i++) {
102 if (mkdir("bar", 0777) != 0) e(18);
103 if (chdir("bar") != 0) e(19); /* change to bar */
104 }
105 for (i = 0; i < 30; i++) {
106 if (chdir("..") != 0) e(20); /* and back again */
107 if (rmdir("bar") != 0) e(21);
108 }
109
110 /* Make sure we are back where we started. */
111 if (getcwd(buf, PATH_MAX) != buf) e(22);
112 if (strcmp(buf, cwd) != 0) e(23);
113 System("rm -rf bar"); /* just incase */
114
115 /* Do some normal checks on `Chdir()' and `getcwd()' */
116 if (chdir("/") != 0) e(24);
117 if (getcwd(buf, PATH_MAX) != buf) e(25);
118 if (strcmp(buf, "/") != 0) e(26);
119 if (chdir("..") != 0) e(27); /* go to parent of / */
120 if (getcwd(buf, PATH_MAX) != buf) e(28);
121 if (strcmp(buf, "/") != 0) e(29);
122 if (chdir(cwd) != 0) e(30);
123 if (getcwd(buf, PATH_MAX) != buf) e(31);
124 if (strcmp(buf, cwd) != 0) e(32);
125 if (chdir("/etc") != 0) e(33); /* /etc might be on RAM */
126 if (getcwd(buf, PATH_MAX) != buf) e(34); /* might make a difference */
127 if (strcmp(buf, "/etc") != 0) e(35);
128 if (chdir(cwd) != 0) e(36);
129 if (getcwd(buf, PATH_MAX) != buf) e(37);
130 if (strcmp(buf, cwd) != 0) e(38);
131 if (chdir(".//.//") != 0) e(39); /* .//.// == current dir */
132 if (getcwd(buf, PATH_MAX) != buf) e(40);
133 if (strcmp(buf, cwd) != 0) e(41); /* we might be at '/' */
134#ifdef _MINIX
135 /* XXX - my_getcwd() is old rubbish. It reads the directory directly instead
136 * of through the directory library. It uses a fixed size buffer instead of
137 * a size related to PATH_MAX, NAME_MAX or the size required.
138 */
139 if (my_getcwd(buf, PATH_MAX) != buf) e(42); /* get cwd my way */
140 if (strcmp(cwd, buf) != 0) e(43);
141#endif
142 System("rm -rf foo");
143}
144
145void test23b()
146{ /* Test critical values. */
147 subtest = 2;
148
149 System("rm -rf ../DIR_23/*");
150
151 /* Fiddle with the size (2nt) parameter of `getcwd ()'. */
152 if (getcwd(cwd, PATH_MAX) != cwd) e(1); /* get cwd */
153 if (getcwd(buf, strlen(cwd)) != (char *) 0) e(2); /* size 1 to small */
154 if (errno != ERANGE) e(3);
155 if (getcwd(buf, PATH_MAX) != buf) e(4);
156 if (strcmp(buf, cwd) != 0) e(5);
157 Chdir(cwd); /* getcwd might cd / */
158 if (getcwd(buf, strlen(cwd) + 1) != buf) e(6); /* size just ok */
159 if (getcwd(buf, PATH_MAX) != buf) e(7);
160 if (strcmp(buf, cwd) != 0) e(8);
161
162 /* Let's see how "MaxName" and "ToLongName" are handled. */
163 if (mkdir(MaxName, 0777) != 0) e(9);
164 if (chdir(MaxName) != 0) e(10);
165 if (chdir("..") != 0) e(11);
166 if (rmdir(MaxName) != 0) e(12);
167 if (getcwd(buf, PATH_MAX) != buf) e(13);
168 if (strcmp(buf, cwd) != 0) e(14);
169 if (chdir(MaxPath) != 0) e(15);
170 if (getcwd(buf, PATH_MAX) != buf) e(16);
171 if (strcmp(buf, cwd) != 0) e(17);
172
173 if (chdir(ToLongName) != -1) e(18);
174#ifdef _POSIX_NO_TRUNC
175# if _POSIX_NO_TRUNC - 0 != -1
176 if (errno != ENAMETOOLONG) e(20);
177# else
178 if (errno != ENOENT) e(20);
179# endif
180#else
181# include "error, this case requires dynamic checks and is not handled"
182#endif
183
184 if (getcwd(buf, PATH_MAX) != buf) e(21);
185 if (strcmp(buf, cwd) != 0) e(22);
186 if (chdir(ToLongPath) != -1) e(23);
187 if (errno != ENAMETOOLONG) e(24);
188 if (getcwd(buf, PATH_MAX) != buf) e(25);
189 if (strcmp(buf, cwd) != 0) e(26);
190}
191
192void test23c()
193{ /* Check reaction to errors */
194 subtest = 3;
195
196 System("rm -rf ../DIR_23/*");
197
198 if (getcwd(cwd, PATH_MAX) != cwd) e(1); /* get cwd */
199
200 /* Creat a working dir named "foo", remove any previous residues. */
201 System("rm -rf foo; mkdir foo");
202
203 /* Check some obviouse errors. */
204 if (chdir("") != -1) e(2);
205 if (errno != ENOENT) e(3);
206 if (getcwd(buf, PATH_MAX) != buf) e(4);
207 if (strcmp(buf, cwd) != 0) e(5);
208 if (getcwd(buf, 0) != (char *) 0) e(6);
209 if (errno != EINVAL) e(7);
210 if (getcwd(buf, PATH_MAX) != buf) e(8);
211 if (strcmp(buf, cwd) != 0) e(9);
212 if (getcwd(buf, 0) != (char *) 0) e(10);
213 if (errno != EINVAL) e(11);
214 if (getcwd(buf, PATH_MAX) != buf) e(12);
215 if (strcmp(buf, cwd) != 0) e(13);
216 if (chdir(cwd) != 0) e(14); /* getcwd might be buggy. */
217
218 /* Change the mode of foo, and check the effect. */
219 if (chdir("foo") != 0) e(15); /* change to foo */
220 if (mkdir("bar", 0777) != 0) e(16); /* make a new dir bar */
221 if (getcwd(cwd2, PATH_MAX) != cwd2) e(17); /* get the new cwd */
222 if (getcwd(buf, 3) != (char *) 0) e(18); /* size is too small */
223 if (errno != ERANGE) e(19);
224 if (getcwd(buf, PATH_MAX) != buf) e(20);
225 if (strcmp(buf, cwd2) != 0) e(21);
226 Chdir(cwd2); /* getcwd() might cd / */
227 System("chmod 377 ."); /* make foo unreadable */
228 if (getcwd(buf, PATH_MAX) != buf) e(22); /* dir not readable */
229 if (getcwd(buf, PATH_MAX) != buf) e(23);
230 if (strcmp(buf, cwd2) != 0) e(24);
231 if (chdir("bar") != 0) e(25); /* at .../foo/bar */
232 if (!superuser) {
233 if (getcwd(buf, PATH_MAX) != (char *) 0) e(26);
234 if (errno != EACCES) e(27);
235 }
236 if (superuser) {
237 if (getcwd(buf, PATH_MAX) != buf) e(28);
238 }
239 if (chdir(cwd2) != 0) e(29);
240 if (getcwd(buf, PATH_MAX) != buf) e(30);
241 if (strcmp(buf, cwd2) != 0) e(31);
242 System("chmod 677 ."); /* make foo inaccessable */
243 if (!superuser) {
244 if (getcwd(buf, PATH_MAX) != (char *) 0) e(32); /* try to get cwd */
245 if (errno != EACCES) e(33); /* but no access */
246 if (chdir("..") != -1) e(34); /* try to get back */
247 if (errno != EACCES) e(35); /* again no access */
248 if (chdir(cwd) != 0) e(36); /* get back to cwd */
249 /* `Chdir()' might do path optimizing, it shouldn't. */
250 if (chdir("foo/..") != -1) e(37); /* no op */
251 if (chdir("foo") != -1) e(38); /* try to cd to foo */
252 if (errno != EACCES) e(39); /* no have access */
253 if (getcwd(buf, PATH_MAX) != buf) e(40);
254 if (strcmp(buf, cwd) != 0) e(41);
255 }
256 if (superuser) {
257 if (getcwd(buf, PATH_MAX) != buf) e(42);
258 if (strcmp(buf, cwd2) != 0) e(43);
259 if (chdir("..") != 0) e(44); /* get back to cwd */
260 if (chdir("foo") != 0) e(45); /* get back to foo */
261 if (chdir(cwd) != 0) e(46); /* get back to cwd */
262 }
263 if (getcwd(buf, PATH_MAX) != buf) e(47); /* check we are */
264 if (strcmp(buf, cwd) != 0) e(48); /* back at cwd. */
265 Chdir(cwd); /* just in case... */
266
267 if (chdir("/etc/passwd") != -1) e(49); /* try to change to a file */
268 if (errno != ENOTDIR) e(50);
269 if (getcwd(buf, PATH_MAX) != buf) e(51);
270 if (strcmp(buf, cwd) != 0) e(52);
271 if (chdir("/notexist") != -1) e(53);
272 if (errno != ENOENT) e(54);
273 if (getcwd(buf, PATH_MAX) != buf) e(55);
274 if (strcmp(buf, cwd) != 0) e(56);
275 System("chmod 777 foo");
276 if (chdir("foo") != 0) e(57);
277
278 /* XXX - this comment was botched by 'pretty'. */
279 /* * Since `foo' is the cwd, it should not be removeable but * if it
280 * were, this code would be found here; *
281 *
282 * System ("cd .. ; rm -rf foo"); remove foo * if (chdir (".")
283 * != -1) e(); try go to. * if (errno != ENOENT) e();
284 * hould not be an entry * if (chdir ("..") != -1) e(); try
285 * to get back * if (errno != ENOENT) e(); should not be
286 * an entry * if (getcwd (buf, PATH_MAX) != (char *)0) e(); don't
287 * know where we are *
288 *
289 * What should errno be now ? The cwd might be gone if te superuser *
290 * removed the cwd. (Might even have linked it first.) But this *
291 * testing should be done by the test program for `rmdir()'. */
292 if (chdir(cwd) != 0) e(58);
293}
294
295void makelongnames()
296{
297 register int i;
298
299 memset(MaxName, 'a', NAME_MAX);
300 MaxName[NAME_MAX] = '\0';
301 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
302 MaxPath[i++] = '.';
303 MaxPath[i] = '/';
304 }
305 MaxPath[PATH_MAX - 1] = '\0';
306
307 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
308 strcpy(ToLongPath, MaxPath);
309
310 ToLongName[NAME_MAX] = 'a';
311 ToLongName[NAME_MAX + 1] = '\0'; /* extend ToLongName by one too many */
312 ToLongPath[PATH_MAX - 1] = '/';
313 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
314}
315
316/* The following code, is take from pwd written by Adri Koppes */
317
318/* My_getcwd() helper. */
319char *last_index(string, ch)
320char *string;
321char ch;
322{
323 register char *retval = '\0';
324
325 while (*string != '\0') {
326 if (*string == ch) retval = string;
327 string++;
328 }
329 return(retval);
330}
331
332char *my_getcwd(buf, size)
333char *buf;
334int size;
335{ /* should be like getcwd() */
336 int sd;
337 register int fd;
338 register char *n;
339 char name[128];
340 struct stat s, st, sk;
341 struct direct d;
342
343 if (size <= 0) return(char *) 0;
344
345 *buf = '\0';
346 *name = '\0';
347 stat(".", &s);
348 do {
349 if ((fd = open("..", O_RDONLY)) < 0) return(char *) 0;
350 st.st_dev = s.st_dev;
351 st.st_ino = s.st_ino;
352 stat("..", &s);
353 Chdir("..");
354 sd = sizeof(struct direct);
355 if (s.st_dev == st.st_dev) {
356 do {
357 if (read(fd, (char *) &d, sd) < sd) return(char *) 0;
358 } while (d.d_ino != st.st_ino);
359 } else {
360 do {
361 if (read(fd, (char *) &d, sd) < sd) return(char *) 0;
362 stat(d.d_name, &sk);
363 } while ((sk.st_dev != st.st_dev) ||
364 (sk.st_ino != st.st_ino));
365 }
366 close(fd);
367 if (strcmp(".", d.d_name) != 0) {
368 strcat(name, "/");
369 strcat(name, d.d_name);
370 }
371 } while ((s.st_ino != st.st_ino) || (s.st_dev != st.st_dev));
372 if (*name == '\0')
373 strncat(buf, "/", size);
374 else
375 while ((n = last_index(name, '/')) != NULL) {
376 n[NAME_MAX] = '\0';
377 strncat(buf, n, size - strlen(buf));
378 *n = '\0';
379 }
380 strncat(buf, name, size - strlen(buf));
381 buf[size - 1] = '\0';
382 Chdir(buf); /* get back there */
383 return buf;
384}
385
386void e(n)
387int n;
388{
389 int err_num = errno; /* Save in case printf clobbers it. */
390
391 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
392 errno = err_num;
393 perror("");
394 if (errct++ > MAX_ERROR) {
395 printf("Too many errors; test aborted\n");
396 chdir("..");
397 system("rm -rf DIR*");
398 exit(1);
399 }
400 errno = 0;
401}
402
403void quit()
404{
405 Chdir("..");
406 System("rm -rf DIR_23");
407
408 if (errct == 0) {
409 printf("ok\n");
410 exit(0);
411 } else {
412 printf("%d errors\n", errct);
413 exit(1);
414 }
415}
Note: See TracBrowser for help on using the repository browser.