source: trunk/minix/test/test27.c@ 11

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

Minix 3.1.2a

File size: 9.9 KB
RevLine 
[9]1/* test27: stat() fstat() Author: Jan-Mark Wams (jms@cs.vu.nl) */
2
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <unistd.h>
7#include <stdlib.h>
8#include <errno.h>
9#include <limits.h>
10#include <string.h>
11#include <limits.h>
12#include <time.h>
13#include <stdio.h>
14
15#define MODE_MASK (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID)
16#define MAX_ERROR 4
17#define ITERATIONS 2
18
19#define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
20#define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
21
22int errct = 0;
23int subtest = 1;
24int superuser;
25char MaxName[NAME_MAX + 1]; /* Name of maximum length */
26char MaxPath[PATH_MAX];
27char ToLongName[NAME_MAX + 2]; /* Name of maximum +1 length */
28char ToLongPath[PATH_MAX + 1];
29
30_PROTOTYPE(void main, (int argc, char *argv[]));
31_PROTOTYPE(void test27a, (void));
32_PROTOTYPE(void test27b, (void));
33_PROTOTYPE(void test27c, (void));
34_PROTOTYPE(void makelongnames, (void));
35_PROTOTYPE(void e, (int __n));
36_PROTOTYPE(void quit, (void));
37
38void main(argc, argv)
39int argc;
40char *argv[];
41{
42 int i, m = 0xFFFF;
43
44 sync();
45 if (argc == 2) m = atoi(argv[1]);
46 printf("Test 27 ");
47 fflush(stdout);
48 System("rm -rf DIR_27; mkdir DIR_27");
49 Chdir("DIR_27");
50 superuser = (getuid() == 0);
51 makelongnames();
52
53 for (i = 0; i < ITERATIONS; i++) {
54 if (m & 0001) test27a();
55 if (m & 0002) test27b();
56 if (m & 0004) test27c();
57 }
58 quit();
59}
60
61void test27a()
62{ /* Test Normal operation. */
63 struct stat st1, st2;
64 time_t time1, time2;
65 int fd, pfd[2];
66
67 subtest = 1;
68
69 time(&time1); /* get time before */
70 while (time1 >= time((time_t *)0))
71 ; /* Wait for time to change. */
72 System("echo 7bytes > foo; chmod 4750 foo");
73 if (stat("foo", &st1) != 0) e(1); /* get foo's info */
74 time(&time2);
75 while (time2 >= time((time_t *)0))
76 ; /* Wait for next second. */
77 time(&time2); /* get time after */
78 if ((st1.st_mode & MODE_MASK) != 04750) e(2);
79 if (st1.st_nlink != 1) e(3); /* check stat */
80 if (st1.st_uid != geteuid()) e(4);
81#if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
82 if (st1.st_gid != getegid()) e(5);
83#endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
84 if (st1.st_size != (size_t) 7) e(6);
85 if (st1.st_atime <= time1) e(7);
86 if (st1.st_atime >= time2) e(8);
87 if (st1.st_ctime <= time1) e(9);
88 if (st1.st_ctime >= time2) e(10);
89 if (st1.st_mtime <= time1) e(11);
90 if (st1.st_mtime >= time2) e(12);
91
92 /* Compair stat and fstat. */
93 System("echo 7bytes > bar");
94 fd = open("bar", O_RDWR | O_APPEND); /* the bar is open! */
95 if (fd != 3) e(13); /* should be stderr + 1 */
96 if (stat("bar", &st1) != 0) e(14); /* get bar's info */
97 if (fstat(fd, &st2) != 0) e(15); /* get bar's info */
98
99 /* St1 en st2 should be the same. */
100 if (st1.st_dev != st2.st_dev) e(16);
101 if (st1.st_ino != st2.st_ino) e(17);
102 if (st1.st_mode != st2.st_mode) e(18);
103 if (st1.st_nlink != st2.st_nlink) e(19);
104 if (st1.st_uid != st2.st_uid) e(20);
105 if (st1.st_gid != st2.st_gid) e(21);
106 if (st1.st_size != st2.st_size) e(22);
107 if (st1.st_atime != st2.st_atime) e(23);
108 if (st1.st_ctime != st2.st_ctime) e(24);
109 if (st1.st_mtime != st2.st_mtime) e(25);
110 time(&time1); /* wait a sec. */
111 while (time1 >= time((time_t *)0))
112 ;
113 System("chmod 755 bar"); /* chainge mode */
114 System("rm -f foobar; ln bar foobar"); /* chainge # links */
115 if (write(fd, "foo", 4) != 4) e(26); /* write a bit (or two) */
116 if (stat("bar", &st2) != 0) e(27); /* get new info */
117 if (st2.st_dev != st1.st_dev) e(28);
118 if (st2.st_ino != st1.st_ino) e(29); /* compair the fealds */
119 if ((st2.st_mode & MODE_MASK) != 0755) e(30);
120 if (!S_ISREG(st2.st_mode)) e(31);
121 if (st2.st_nlink != st1.st_nlink + 1) e(32);
122 if (st2.st_uid != st1.st_uid) e(33);
123 if (st2.st_gid != st1.st_gid) e(34);
124 if (st2.st_size != (size_t) 11) e(35);
125 if (st2.st_atime != st1.st_atime) e(36);
126 if (st2.st_ctime <= st1.st_ctime) e(37);
127 if (st2.st_mtime <= st1.st_mtime) e(38);
128 if (close(fd) != 0) e(39); /* sorry the bar is closed */
129
130 /* Check special file. */
131 if (stat("/dev/tty", &st1) != 0) e(40);
132 if (!S_ISCHR(st1.st_mode)) e(41);
133#ifdef _MINIX
134 if (stat("/dev/ram", &st1) != 0) e(42);
135 if (!S_ISBLK(st1.st_mode)) e(43);
136#endif
137
138 /* Check fifos. */
139 time(&time1);
140 while (time1 >= time((time_t *)0))
141 ;
142 if (mkfifo("fifo", 0640) != 0) e(44);
143 if (stat("fifo", &st1) != 0) e(45); /* get fifo's info */
144 time(&time2);
145 while (time2 >= time((time_t *)0))
146 ;
147 time(&time2);
148 if (!S_ISFIFO(st1.st_mode)) e(46);
149 if (st1.st_nlink != 1) e(47); /* check the stat info */
150 if (st1.st_uid != geteuid()) e(48);
151#if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
152 if (st1.st_gid != getegid()) e(49);
153#endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
154 if (st1.st_size != (size_t) 0) e(50);
155 if (st1.st_atime <= time1) e(51);
156 if (st1.st_atime >= time2) e(52);
157 if (st1.st_ctime <= time1) e(53);
158 if (st1.st_ctime >= time2) e(54);
159 if (st1.st_mtime <= time1) e(55);
160 if (st1.st_mtime >= time2) e(56);
161
162 /* Note: the st_mode of a fstat on a pipe should contain a isfifo bit. */
163 /* Check pipes. */
164 time(&time1);
165 while (time1 >= time((time_t *)0))
166 ;
167 if (pipe(pfd) != 0) e(57);
168 if (fstat(pfd[0], &st1) != 0) e(58); /* get pipe input info */
169 time(&time2);
170 while (time2 >= time((time_t *)0))
171 ;
172 time(&time2);
173 if (!(S_ISFIFO(st1.st_mode))) e(59); /* check stat struct */
174 if (st1.st_uid != geteuid()) e(60);
175 if (st1.st_gid != getegid()) e(61);
176 if (st1.st_size != (size_t) 0) e(62);
177 if (st1.st_atime <= time1) e(63);
178 if (st1.st_atime >= time2) e(64);
179 if (st1.st_ctime <= time1) e(65);
180 if (st1.st_ctime >= time2) e(66);
181 if (st1.st_mtime <= time1) e(67);
182 if (st1.st_mtime >= time2) e(68);
183 if (fstat(pfd[1], &st1) != 0) e(69); /* get pipe output info */
184 if (!(S_ISFIFO(st1.st_mode))) e(70);
185 if (st1.st_uid != geteuid()) e(71);
186 if (st1.st_gid != getegid()) e(72);
187 if (st1.st_size != (size_t) 0) e(73);
188 if (st1.st_atime < time1) e(74);
189 if (st1.st_atime > time2) e(75);
190 if (st1.st_ctime < time1) e(76);
191 if (st1.st_ctime > time2) e(77);
192 if (st1.st_mtime < time1) e(78);
193 if (st1.st_mtime > time2) e(79);
194 if (close(pfd[0]) != 0) e(80);
195 if (close(pfd[1]) != 0) e(81);/* close pipe */
196
197 /* Check dirs. */
198 time(&time1);
199 while (time1 >= time((time_t *)0))
200 ;
201 System("mkdir dir");
202 if (stat("dir", &st1) != 0) e(82); /* get dir info */
203 time(&time2);
204 while (time2 >= time((time_t *)0))
205 ;
206 time(&time2);
207 if (!(S_ISDIR(st1.st_mode))) e(83); /* check stat struct */
208 if (st1.st_uid != geteuid()) e(84);
209#if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
210 if (st1.st_gid != getegid()) e(85);
211#endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
212 if (st1.st_atime < time1) e(86);
213 if (st1.st_atime > time2) e(87);
214 if (st1.st_ctime < time1) e(88);
215 if (st1.st_ctime > time2) e(89);
216 if (st1.st_mtime < time1) e(90);
217 if (st1.st_mtime > time2) e(91);
218 System("rm -rf ../DIR_27/*");
219}
220
221void test27b()
222{ /* Test maxima. */
223 struct stat st;
224 int fd;
225
226 subtest = 2;
227
228 /* Check stats on maximum length files names. */
229 if (mkdir(MaxName, 0777) != 0) e(1);
230 if (stat(MaxName, &st) != 0) e(2);
231 if ((fd = open(MaxName, O_RDONLY)) != 3) e(3);
232 if (fstat(fd, &st) != 0) e(4);
233 if (close(fd) != 0) e(5);
234 if (rmdir(MaxName) != 0) e(6);
235 if (stat(MaxPath, &st) != 0) e(7);
236 if ((fd = open(MaxPath, O_RDONLY)) != 3) e(8);
237 if (fstat(fd, &st) != 0) e(9);
238 if (close(fd) != 0) e(10);
239 System("rm -rf ../DIR_27/*");
240}
241
242void test27c()
243{ /* Test error response. */
244 struct stat st;
245 int fd, i;
246
247 subtest = 3;
248
249 System("echo Hi > foo"); /* Make a file called foo. */
250 /* Check if a un searchable dir is handled ok. */
251 Chdir(".."); /* cd .. */
252 System("chmod 677 DIR_27"); /* no search permission */
253 if (stat("DIR_27/nono", &st) != -1) e(1);
254 if (superuser) {
255 if (errno != ENOENT) e(2); /* su has access */
256 }
257 if (!superuser) {
258 if (errno != EACCES) e(3); /* we don't ;-) */
259 }
260 System("chmod 777 DIR_27");
261 Chdir("DIR_27"); /* back to test dir */
262
263 /* Check on ToLongName etc. */
264#ifdef _POSIX_NO_TRUNC
265# if _POSIX_NO_TRUNC - 0 != -1
266 if (stat(ToLongName, &st) != -1) e(4); /* name is too long */
267 if (errno != ENAMETOOLONG) e(5);
268# endif
269#else
270# include "error, this case requires dynamic checks and is not handled"
271#endif
272 if (stat(ToLongPath, &st) != -1) e(6); /* path is too long */
273 if (errno != ENAMETOOLONG) e(7);
274
275 /* Test some common errors. */
276 if (stat("nono", &st) != -1) e(8); /* nono nonexistent */
277 if (errno != ENOENT) e(9);
278 if (stat("", &st) != -1) e(10); /* try empty */
279 if (errno != ENOENT) e(11);
280 if (stat("foo/bar", &st) != -1) e(12); /* foo is a file */
281 if (errno != ENOTDIR) e(13);
282
283 /* Test fstat on file descriptors that are not open. */
284 for (i = 3; i < 6; i++) {
285 if (fstat(i, &st) != -1) e(14);
286 if (errno != EBADF) e(15);
287 }
288
289 /* Test if a just closed file is `fstat()'-able. */
290 if ((fd = open("foo", O_RDONLY)) != 3) e(16); /* open foo */
291 if (fstat(fd, &st) != 0) e(17); /* get stat */
292 if (close(fd) != 0) e(18); /* close it */
293 if (fstat(fd, &st) != -1) e(19); /* get stat */
294 if (errno != EBADF) e(20);
295 System("rm -rf ../DIR_27/*");
296}
297
298void makelongnames()
299{
300 register int i;
301
302 memset(MaxName, 'a', NAME_MAX);
303 MaxName[NAME_MAX] = '\0';
304 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
305 MaxPath[i++] = '.';
306 MaxPath[i] = '/';
307 }
308 MaxPath[PATH_MAX - 1] = '\0';
309
310 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
311 strcpy(ToLongPath, MaxPath);
312
313 ToLongName[NAME_MAX] = 'a';
314 ToLongName[NAME_MAX + 1] = '\0'; /* extend ToLongName by one too many */
315 ToLongPath[PATH_MAX - 1] = '/';
316 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
317}
318
319void e(n)
320int n;
321{
322 int err_num = errno; /* Save in case printf clobbers it. */
323
324 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
325 errno = err_num;
326 perror("");
327 if (errct++ > MAX_ERROR) {
328 printf("Too many errors; test aborted\n");
329 chdir("..");
330 system("rm -rf DIR*");
331 exit(1);
332 }
333 errno = 0;
334}
335
336void quit()
337{
338 Chdir("..");
339 System("rm -rf DIR_27");
340
341 if (errct == 0) {
342 printf("ok\n");
343 exit(0);
344 } else {
345 printf("%d errors\n", errct);
346 exit(1);
347 }
348}
Note: See TracBrowser for help on using the repository browser.