remove extra mkfs.1
[minix.git] / test / test27.c
blob1271daada179bbfc17497fc7474c62ec0c2b29f2
1 /* test27: stat() fstat() Author: Jan-Mark Wams (jms@cs.vu.nl) */
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>
15 #define MODE_MASK (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID)
16 #define MAX_ERROR 4
17 #define ITERATIONS 2
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)
22 #include "common.c"
24 int superuser;
25 char *MaxName; /* Name of maximum length */
26 char MaxPath[PATH_MAX];
27 char *ToLongName; /* Name of maximum +1 length */
28 char ToLongPath[PATH_MAX + 1];
30 void test27a(void);
31 void test27b(void);
32 void test27c(void);
33 void makelongnames(void);
35 int main(int argc, char *argv[])
37 int i, m = 0xFFFF;
39 start(27);
40 if (argc == 2) m = atoi(argv[1]);
41 superuser = (getuid() == 0);
42 makelongnames();
44 for (i = 0; i < ITERATIONS; i++) {
45 if (m & 0001) test27a();
46 if (m & 0002) test27b();
47 if (m & 0004) test27c();
49 quit();
51 return(-1); /* Unreachable */
54 void test27a()
55 { /* Test Normal operation. */
56 struct stat st1, st2;
57 time_t time1, time2;
58 int fd, pfd[2];
60 subtest = 1;
62 time(&time1); /* get time before */
63 while (time1 >= time((time_t *)0))
64 ; /* Wait for time to change. */
65 System("echo 7bytes > foo; chmod 4750 foo");
66 if (stat("foo", &st1) != 0) e(1); /* get foo's info */
67 time(&time2);
68 while (time2 >= time((time_t *)0))
69 ; /* Wait for next second. */
70 time(&time2); /* get time after */
71 if ((st1.st_mode & MODE_MASK) != 04750) e(2);
72 if (st1.st_nlink != 1) e(3); /* check stat */
73 if (st1.st_uid != geteuid()) e(4);
74 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
75 if (st1.st_gid != getegid()) e(5);
76 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
77 if (st1.st_size != (size_t) 7) e(6);
78 if (st1.st_atime <= time1) e(7);
79 if (st1.st_atime >= time2) e(8);
80 if (st1.st_ctime <= time1) e(9);
81 if (st1.st_ctime >= time2) e(10);
82 if (st1.st_mtime <= time1) e(11);
83 if (st1.st_mtime >= time2) e(12);
85 /* Compair stat and fstat. */
86 System("echo 7bytes > bar");
87 fd = open("bar", O_RDWR | O_APPEND); /* the bar is open! */
88 if (fd != 3) e(13); /* should be stderr + 1 */
89 if (stat("bar", &st1) != 0) e(14); /* get bar's info */
90 if (fstat(fd, &st2) != 0) e(15); /* get bar's info */
92 /* St1 en st2 should be the same. */
93 if (st1.st_dev != st2.st_dev) e(16);
94 if (st1.st_ino != st2.st_ino) e(17);
95 if (st1.st_mode != st2.st_mode) e(18);
96 if (st1.st_nlink != st2.st_nlink) e(19);
97 if (st1.st_uid != st2.st_uid) e(20);
98 if (st1.st_gid != st2.st_gid) e(21);
99 if (st1.st_size != st2.st_size) e(22);
100 if (st1.st_atime != st2.st_atime) e(23);
101 if (st1.st_ctime != st2.st_ctime) e(24);
102 if (st1.st_mtime != st2.st_mtime) e(25);
103 time(&time1); /* wait a sec. */
104 while (time1 >= time((time_t *)0))
106 System("chmod 755 bar"); /* chainge mode */
107 System("rm -f foobar; ln bar foobar"); /* chainge # links */
108 if (write(fd, "foo", 4) != 4) e(26); /* write a bit (or two) */
109 if (stat("bar", &st2) != 0) e(27); /* get new info */
110 if (st2.st_dev != st1.st_dev) e(28);
111 if (st2.st_ino != st1.st_ino) e(29); /* compair the fealds */
112 if ((st2.st_mode & MODE_MASK) != 0755) e(30);
113 if (!S_ISREG(st2.st_mode)) e(31);
114 if (st2.st_nlink != st1.st_nlink + 1) e(32);
115 if (st2.st_uid != st1.st_uid) e(33);
116 if (st2.st_gid != st1.st_gid) e(34);
117 if (st2.st_size != (size_t) 11) e(35);
118 if (st2.st_atime != st1.st_atime) e(36);
119 if (st2.st_ctime <= st1.st_ctime) e(37);
120 if (st2.st_mtime <= st1.st_mtime) e(38);
121 if (close(fd) != 0) e(39); /* sorry the bar is closed */
123 /* Check special file. */
124 if (stat("/dev/tty", &st1) != 0) e(40);
125 if (!S_ISCHR(st1.st_mode)) e(41);
126 #ifdef _MINIX
127 if (stat("/dev/ram", &st1) != 0) e(42);
128 if (!S_ISBLK(st1.st_mode)) e(43);
129 #endif
131 /* Check fifos. */
132 time(&time1);
133 while (time1 >= time((time_t *)0))
135 if (mkfifo("fifo", 0640) != 0) e(44);
136 if (stat("fifo", &st1) != 0) e(45); /* get fifo's info */
137 time(&time2);
138 while (time2 >= time((time_t *)0))
140 time(&time2);
141 if (!S_ISFIFO(st1.st_mode)) e(46);
142 if (st1.st_nlink != 1) e(47); /* check the stat info */
143 if (st1.st_uid != geteuid()) e(48);
144 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
145 if (st1.st_gid != getegid()) e(49);
146 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
147 if (st1.st_size != (size_t) 0) e(50);
148 if (st1.st_atime <= time1) e(51);
149 if (st1.st_atime >= time2) e(52);
150 if (st1.st_ctime <= time1) e(53);
151 if (st1.st_ctime >= time2) e(54);
152 if (st1.st_mtime <= time1) e(55);
153 if (st1.st_mtime >= time2) e(56);
155 /* Note: the st_mode of a fstat on a pipe should contain a isfifo bit. */
156 /* Check pipes. */
157 time(&time1);
158 while (time1 >= time((time_t *)0))
160 if (pipe(pfd) != 0) e(57);
161 if (fstat(pfd[0], &st1) != 0) e(58); /* get pipe input info */
162 time(&time2);
163 while (time2 >= time((time_t *)0))
165 time(&time2);
166 if (!(S_ISFIFO(st1.st_mode))) e(59); /* check stat struct */
167 if (st1.st_uid != geteuid()) e(60);
168 if (st1.st_gid != getegid()) e(61);
169 if (st1.st_size != (size_t) 0) e(62);
170 if (st1.st_atime <= time1) e(63);
171 if (st1.st_atime >= time2) e(64);
172 if (st1.st_ctime <= time1) e(65);
173 if (st1.st_ctime >= time2) e(66);
174 if (st1.st_mtime <= time1) e(67);
175 if (st1.st_mtime >= time2) e(68);
176 if (fstat(pfd[1], &st1) != 0) e(69); /* get pipe output info */
177 if (!(S_ISFIFO(st1.st_mode))) e(70);
178 if (st1.st_uid != geteuid()) e(71);
179 if (st1.st_gid != getegid()) e(72);
180 if (st1.st_size != (size_t) 0) e(73);
181 if (st1.st_atime < time1) e(74);
182 if (st1.st_atime > time2) e(75);
183 if (st1.st_ctime < time1) e(76);
184 if (st1.st_ctime > time2) e(77);
185 if (st1.st_mtime < time1) e(78);
186 if (st1.st_mtime > time2) e(79);
187 if (close(pfd[0]) != 0) e(80);
188 if (close(pfd[1]) != 0) e(81);/* close pipe */
190 /* Check dirs. */
191 time(&time1);
192 while (time1 >= time((time_t *)0))
194 System("mkdir dir");
195 if (stat("dir", &st1) != 0) e(82); /* get dir info */
196 time(&time2);
197 while (time2 >= time((time_t *)0))
199 time(&time2);
200 if (!(S_ISDIR(st1.st_mode))) e(83); /* check stat struct */
201 if (st1.st_uid != geteuid()) e(84);
202 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
203 if (st1.st_gid != getegid()) e(85);
204 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
205 if (st1.st_atime < time1) e(86);
206 if (st1.st_atime > time2) e(87);
207 if (st1.st_ctime < time1) e(88);
208 if (st1.st_ctime > time2) e(89);
209 if (st1.st_mtime < time1) e(90);
210 if (st1.st_mtime > time2) e(91);
211 System("rm -rf ../DIR_27/*");
214 void test27b()
215 { /* Test maxima. */
216 struct stat st;
217 int fd;
219 subtest = 2;
221 /* Check stats on maximum length files names. */
222 if (mkdir(MaxName, 0777) != 0) e(1);
223 if (stat(MaxName, &st) != 0) e(2);
224 if ((fd = open(MaxName, O_RDONLY)) != 3) e(3);
225 if (fstat(fd, &st) != 0) e(4);
226 if (close(fd) != 0) e(5);
227 if (rmdir(MaxName) != 0) e(6);
228 if (stat(MaxPath, &st) != 0) e(7);
229 if ((fd = open(MaxPath, O_RDONLY)) != 3) e(8);
230 if (fstat(fd, &st) != 0) e(9);
231 if (close(fd) != 0) e(10);
232 System("rm -rf ../DIR_27/*");
235 void test27c()
236 { /* Test error response. */
237 struct stat st;
238 int fd, i;
240 subtest = 3;
242 System("echo Hi > foo"); /* Make a file called foo. */
243 /* Check if a un searchable dir is handled ok. */
244 Chdir(".."); /* cd .. */
245 System("chmod 677 DIR_27"); /* no search permission */
246 if (stat("DIR_27/nono", &st) != -1) e(1);
247 if (superuser) {
248 if (errno != ENOENT) e(2); /* su has access */
250 if (!superuser) {
251 if (errno != EACCES) e(3); /* we don't ;-) */
253 System("chmod 777 DIR_27");
254 Chdir("DIR_27"); /* back to test dir */
256 /* Check on ToLongName etc. */
257 if (stat(ToLongPath, &st) != -1) e(6); /* path is too long */
258 if (errno != ENAMETOOLONG) e(7);
260 /* Test some common errors. */
261 if (stat("nono", &st) != -1) e(8); /* nono nonexistent */
262 if (errno != ENOENT) e(9);
263 if (stat("", &st) != -1) e(10); /* try empty */
264 if (errno != ENOENT) e(11);
265 if (stat("foo/bar", &st) != -1) e(12); /* foo is a file */
266 if (errno != ENOTDIR) e(13);
268 /* Test fstat on file descriptors that are not open. */
269 for (i = 3; i < 6; i++) {
270 if (fstat(i, &st) != -1) e(14);
271 if (errno != EBADF) e(15);
274 /* Test if a just closed file is `fstat()'-able. */
275 if ((fd = open("foo", O_RDONLY)) != 3) e(16); /* open foo */
276 if (fstat(fd, &st) != 0) e(17); /* get stat */
277 if (close(fd) != 0) e(18); /* close it */
278 if (fstat(fd, &st) != -1) e(19); /* get stat */
279 if (errno != EBADF) e(20);
280 System("rm -rf ../DIR_27/*");
283 void makelongnames()
285 register int i;
286 int max_name_length;
288 max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
289 * the same length, hence runtime check */
290 MaxName = malloc(max_name_length + 1);
291 ToLongName = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
292 memset(MaxName, 'a', max_name_length);
293 MaxName[max_name_length] = '\0';
295 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
296 MaxPath[i++] = '.';
297 MaxPath[i] = '/';
299 MaxPath[PATH_MAX - 1] = '\0';
301 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
302 strcpy(ToLongPath, MaxPath);
304 ToLongName[max_name_length] = 'a';
305 ToLongName[max_name_length+1] = '\0';/* extend ToLongName by one too many */
306 ToLongPath[PATH_MAX - 1] = '/';
307 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */