source: trunk/minix/test/test40.c@ 12

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

Minix 3.1.2a

File size: 10.1 KB
Line 
1/* test40: link() unlink() Aithor: Jan-Mark Wams (jms@cs.vu.nl) */
2
3/*
4 * Not tested readonly file systems
5 * Not tested fs full
6 * Not tested unlinking bussy files
7 */
8
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <stdlib.h>
12#include <unistd.h>
13#include <fcntl.h>
14#include <errno.h>
15#include <limits.h>
16#include <string.h>
17#include <time.h>
18#include <stdio.h>
19
20int errct = 0;
21int subtest = 1;
22int superuser;
23char MaxName[NAME_MAX + 1]; /* Name of maximum length */
24char MaxPath[PATH_MAX]; /* Same for path */
25char ToLongName[NAME_MAX + 2]; /* Name of maximum +1 length */
26char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
27
28#define MAX_ERROR 4
29#define ITERATIONS 2 /* LINK_MAX is high, so time consuming. */
30
31#define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
32#define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
33#define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
34
35_PROTOTYPE(void main, (int argc, char *argv[]));
36_PROTOTYPE(void test40a, (void));
37_PROTOTYPE(void test40b, (void));
38_PROTOTYPE(void test40c, (void));
39_PROTOTYPE(int stateq, (struct stat *stp1, struct stat *stp2));
40_PROTOTYPE(void makelongnames, (void));
41_PROTOTYPE(void e, (int __n));
42_PROTOTYPE(void quit, (void));
43
44void main(argc, argv)
45int argc;
46char *argv[];
47{
48 int i, m = 0xFFFF;
49
50 sync();
51 if (argc == 2) m = atoi(argv[1]);
52 printf("Test 40 ");
53 fflush(stdout);
54 System("rm -rf DIR_40; mkdir DIR_40");
55 Chdir("DIR_40");
56 superuser = (getuid() == 0);
57 makelongnames();
58
59 for (i = 0; i < ITERATIONS; i++) {
60 if (m & 0001) test40a();
61 if (m & 0002) test40b();
62 if (m & 0004) test40c();
63 }
64 quit();
65}
66
67void test40a()
68{ /* Test normal operation. */
69 struct stat st1, st2, st3;
70 time_t time1;
71
72 subtest = 1;
73
74 /* Clean up any residu. */
75 System("rm -rf ../DIR_40/*");
76
77 System("touch foo"); /* make source file */
78 Stat("foo", &st1); /* get info of foo */
79 Stat(".", &st2); /* and the cwd */
80 time(&time1);
81 while (time1 >= time((time_t *)0))
82 ; /* wait a sec */
83 if (link("foo", "bar") != 0) e(1); /* link foo to bar */
84 Stat("foo", &st3); /* get new status */
85 if (st1.st_nlink + 1 != st3.st_nlink) e(2); /* link count foo up 1 */
86#ifndef V1_FILESYSTEM
87 if (st1.st_ctime >= st3.st_ctime) e(3); /* check stattime changed */
88#endif
89 Stat(".", &st1); /* get parend dir info */
90 if (st2.st_ctime >= st1.st_ctime) e(4); /* ctime and mtime */
91 if (st2.st_mtime >= st1.st_mtime) e(5); /* should be updated */
92 Stat("bar", &st2); /* get info of bar */
93 if (st2.st_nlink != st3.st_nlink) e(6); /* link count foo == bar */
94 if (st2.st_ino != st3.st_ino) e(7); /* ino should be same */
95 if (st2.st_mode != st3.st_mode) e(8); /* check mode same */
96 if (st2.st_uid != st3.st_uid) e(9); /* check uid same */
97 if (st2.st_gid != st3.st_gid) e(10); /* check gid same */
98 if (st2.st_size != st3.st_size) e(11); /* check size */
99 if (st2.st_ctime != st3.st_ctime) e(12); /* check ctime */
100 if (st2.st_atime != st3.st_atime) e(13); /* check atime */
101 if (st2.st_mtime != st3.st_mtime) e(14); /* check mtime */
102 Stat("foo", &st1); /* get fooinfo */
103 Stat(".", &st2); /* get dir info */
104 time(&time1);
105 while (time1 >= time((time_t *)0))
106 ; /* wait a sec */
107 if (unlink("bar") != 0) e(15);/* rm bar */
108 if (stat("bar", &st2) != -1) e(16); /* it's gone */
109 Stat("foo", &st3); /* get foo again */
110 if (st1.st_nlink != st3.st_nlink + 1) e(17); /* link count back to normal */
111#ifndef V1_FILESYSTEM
112 if (st1.st_ctime >= st3.st_ctime) e(18); /* check ctime */
113#endif
114 Stat(".", &st3); /* get parend dir info */
115 if (st2.st_ctime >= st3.st_ctime) e(19); /* ctime and mtime */
116 if (st2.st_mtime >= st3.st_mtime) e(20); /* should be updated */
117}
118
119void test40b()
120{
121 register int nlink;
122 char bar[30];
123 struct stat st, st2;
124
125 subtest = 2;
126
127 /* Clean up any residu. */
128 System("rm -rf ../DIR_40/*");
129
130 /* Test what happens if we make LINK_MAX number of links. */
131 System("touch foo");
132 for (nlink = 2; nlink <= LINK_MAX; nlink++) {
133 sprintf(bar, "bar.%d", nlink);
134 if (link("foo", bar) != 0) e(2);
135 Stat(bar, &st);
136 if (st.st_nlink != nlink) e(3);
137 Stat("foo", &st);
138 if (st.st_nlink != nlink) e(4);
139 }
140
141 /* Check if we have LINK_MAX links that are all the same. */
142 Stat("foo", &st);
143 if (st.st_nlink != LINK_MAX) e(5);
144 for (nlink = 2; nlink <= LINK_MAX; nlink++) {
145 sprintf(bar, "bar.%d", nlink);
146 Stat(bar, &st2);
147 if (!stateq(&st, &st2)) e(6);
148 }
149
150 /* Test no more links are possible. */
151 if (link("foo", "nono") != -1) e(7);
152 if (stat("nono", &st) != -1) e(8);
153 Stat("foo", &st);
154 if (st.st_nlink != LINK_MAX) e(9); /* recheck the number of links */
155
156 /* Now unlink() the bar.### files */
157 for (nlink = LINK_MAX; nlink >= 2; nlink--) {
158 sprintf(bar, "bar.%d", nlink);
159 Stat(bar, &st);
160 if (st.st_nlink != nlink) e(10);
161 Stat("foo", &st2);
162 if (!stateq(&st, &st2)) e(11);
163 if (unlink(bar) != 0) e(12);
164 }
165 Stat("foo", &st);
166 if (st.st_nlink != 1) e(13); /* number of links back to 1 */
167
168 /* Test max path ed. */
169 if (link("foo", MaxName) != 0) e(14); /* link to MaxName */
170 if (unlink(MaxName) != 0) e(15); /* and remove it */
171 MaxPath[strlen(MaxPath) - 2] = '/';
172 MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
173 if (link("foo", MaxPath) != 0) e(16); /* it should be */
174 if (unlink(MaxPath) != 0) e(17); /* (un)linkable */
175
176 System("rm -f ../DIR_40/*"); /* clean cwd */
177}
178
179void test40c()
180{
181 subtest = 3;
182
183 /* Clean up any residu. */
184 System("rm -rf ../DIR_40/*");
185
186 /* Check some simple things. */
187 if (link("bar/nono", "nono") != -1) e(1); /* nonexistent */
188 if (errno != ENOENT) e(2);
189 Chdir("..");
190 System("touch DIR_40/foo");
191 System("chmod 677 DIR_40"); /* make inaccesable */
192 if (!superuser) {
193 if (unlink("DIR_40/foo") != -1) e(3);
194 if (errno != EACCES) e(4);
195 }
196 if (link("DIR_40/bar/nono", "DIR_40/nono") != -1) e(5); /* nono no be */
197 if (superuser) {
198 if (errno != ENOENT) e(6); /* su has access */
199 }
200 if (!superuser) {
201 if (errno != EACCES) e(7); /* we don't ;-) */
202 }
203 System("chmod 577 DIR_40"); /* make unwritable */
204 if (superuser) {
205 if (link("DIR_40/foo", "DIR_40/nono") != 0) e(8);
206 if (unlink("DIR_40/nono") != 0) e(9);
207 }
208 if (!superuser) {
209 if (link("DIR_40/foo", "DIR_40/nono") != -1) e(10);
210 if (errno != EACCES) e(11);
211 if (unlink("DIR_40/foo") != -1) e(12); /* try to rm foo/foo */
212 if (errno != EACCES) e(13);
213 }
214 System("chmod 755 DIR_40"); /* back to normal */
215 Chdir("DIR_40");
216
217 /* Too-long path and name test */
218 ToLongPath[strlen(ToLongPath) - 2] = '/';
219 ToLongPath[strlen(ToLongPath) - 1] = 'a'; /* make ././.../a */
220 if (link("foo", ToLongPath) != -1) e(18); /* path is too long */
221 if (errno != ENAMETOOLONG) e(19);
222 if (unlink(ToLongPath) != -1) e(20); /* path is too long */
223 if (errno != ENAMETOOLONG) e(21);
224 if (link("foo", "foo") != -1) e(22); /* try linking foo to foo */
225 if (errno != EEXIST) e(23);
226 if (link("foo", "bar") != 0) e(24); /* make a link to bar */
227 if (link("foo", "bar") != -1) e(25); /* try linking to bar again */
228 if (errno != EEXIST) e(26);
229 if (link("foo", "bar") != -1) e(27); /* try linking to bar again */
230 if (errno != EEXIST) e(28);
231 if (unlink("nono") != -1) e(29); /* try rm <not exist> */
232 if (errno != ENOENT) e(30);
233 if (unlink("") != -1) e(31); /* try unlinking empty */
234 if (errno != ENOENT) e(32);
235 if (link("foo", "") != -1) e(33); /* try linking to "" */
236 if (errno != ENOENT) e(34);
237 if (link("", "foo") != -1) e(35); /* try linking "" */
238 if (errno != ENOENT) e(36);
239 if (link("", "") != -1) e(37);/* try linking "" to "" */
240 if (errno != ENOENT) e(38);
241 if (link("/foo/bar/foo", "a") != -1) e(39); /* try no existing path */
242 if (errno != ENOENT) e(40);
243 if (link("foo", "/foo/bar/foo") != -1) e(41); /* try no existing path */
244 if (errno != ENOENT) e(42);
245 if (link("/a/b/c", "/d/e/f") != -1) e(43); /* try no existing path */
246 if (errno != ENOENT) e(44);
247 if (link("abc", "a") != -1) e(45); /* try no existing file */
248 if (errno != ENOENT) e(46);
249 if (link("foo/bar", "bar") != -1) e(47); /* foo is a file */
250 if (errno != ENOTDIR) e(48);
251 if (link("foo", "foo/bar") != -1) e(49); /* foo is not a dir */
252 if (errno != ENOTDIR) e(50);
253 if (unlink("foo/bar") != -1) e(51); /* foo still no dir */
254 if (errno != ENOTDIR) e(52);
255 if (!superuser) {
256 if (link(".", "root") != -1) e(55);
257 if (errno != EPERM) e(56); /* noroot can't */
258 if (unlink("root") != -1) e(57);
259 if (errno != ENOENT) e(58);
260 }
261 if (mkdir("dir", 0777) != 0) e(59);
262 if (superuser) {
263 if (rmdir("dir") != 0) e(63);
264 }
265 if (!superuser) {
266 if (unlink("dir") != -1) e(64);
267 if (errno != EPERM) e(65); /* that ain't w'rkn */
268 if (rmdir("dir") != 0) e(66); /* that's the way to do it */
269 }
270}
271
272int stateq(stp1, stp2)
273struct stat *stp1, *stp2;
274{
275 if (stp1->st_dev != stp2->st_dev) return 0;
276 if (stp1->st_ino != stp2->st_ino) return 0;
277 if (stp1->st_mode != stp2->st_mode) return 0;
278 if (stp1->st_nlink != stp2->st_nlink) return 0;
279 if (stp1->st_uid != stp2->st_uid) return 0;
280 if (stp1->st_gid != stp2->st_gid) return 0;
281 if (stp1->st_rdev != stp2->st_rdev) return 0;
282 if (stp1->st_size != stp2->st_size) return 0;
283 if (stp1->st_atime != stp2->st_atime) return 0;
284 if (stp1->st_mtime != stp2->st_mtime) return 0;
285 if (stp1->st_ctime != stp2->st_ctime) return 0;
286 return 1;
287}
288
289void makelongnames()
290{
291 register int i;
292
293 memset(MaxName, 'a', NAME_MAX);
294 MaxName[NAME_MAX] = '\0';
295 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
296 MaxPath[i++] = '.';
297 MaxPath[i] = '/';
298 }
299 MaxPath[PATH_MAX - 1] = '\0';
300
301 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
302 strcpy(ToLongPath, MaxPath);
303
304 ToLongName[NAME_MAX] = 'a';
305 ToLongName[NAME_MAX + 1] = '\0'; /* extend ToLongName by one
306 * too many */
307 ToLongPath[PATH_MAX - 1] = '/';
308 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
309}
310
311void e(n)
312int n;
313{
314 int err_num = errno; /* Save in case printf clobbers it. */
315
316 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
317 errno = err_num;
318 perror("");
319 if (errct++ > MAX_ERROR) {
320 printf("Too many errors; test aborted\n");
321 chdir("..");
322 system("rm -rf DIR*");
323 exit(1);
324 }
325 errno = 0;
326}
327
328void quit()
329{
330 chdir("..");
331 system("rm -rf DIR_40");
332
333 if (errct == 0) {
334 printf("ok\n");
335 exit(0);
336 } else {
337 printf("%d errors\n", errct);
338 exit(1);
339 }
340}
Note: See TracBrowser for help on using the repository browser.