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