Minor change to path lookup that fixes the bug that creating a file
[minix3.git] / test / test35.c
blob94cd13ec193cc4bd4f1cc8b32603a09c68098d54
1 /* test35: utime() Author: Jan-Mark Wams (jms@cs.vu.nl) */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/wait.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <limits.h>
11 #include <utime.h>
12 #include <errno.h>
13 #include <time.h>
14 #include <ctype.h>
15 #include <stdio.h>
17 #define MAX_ERROR 1
18 #define ITERATIONS 10
19 #define N 100
21 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
22 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
23 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
24 #define Mkfifo(f) if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
25 #define Mkdir(f) if (mkdir(f,0777)!=0) printf("Can't make dir %s\n", f)
26 #define Creat(f) if (close(creat(f,0777))!=0) printf("Can't creat %s\n",f)
27 #define Time(t) if (time(t) == (time_t)-1) printf("Time error\n")
28 #define Chown(f,u,g) if (chown(f,u,g) != 0) printf("Can't chown %s\n", f)
29 #define Chmod(f,m) if (chmod(f,m) != 0) printf("Can't chmod %s\n", f)
31 #define PASSWD_FILE "/etc/passwd"
33 int errct = 0;
34 int subtest = 1;
35 int I_can_chown;
36 int superuser;
37 char MaxName[NAME_MAX + 1]; /* Name of maximum length */
38 char MaxPath[PATH_MAX]; /* Same for path */
39 char NameTooLong[NAME_MAX + 2]; /* Name of maximum +1 length */
40 char PathTooLong[PATH_MAX + 1]; /* Same for path, both too long */
42 _PROTOTYPE(void main, (int argc, char *argv[]));
43 _PROTOTYPE(void test35a, (void));
44 _PROTOTYPE(void test35b, (void));
45 _PROTOTYPE(void test35c, (void));
46 _PROTOTYPE(void makelongnames, (void));
47 _PROTOTYPE(void e, (int number));
48 _PROTOTYPE(void quit, (void));
49 _PROTOTYPE(void getids, (uid_t * uid, gid_t * gid));
51 void main(argc, argv)
52 int argc;
53 char *argv[];
55 int i, m = 0xFFFF;
57 sync();
58 if (argc == 2) m = atoi(argv[1]);
59 printf("Test 35 ");
60 fflush(stdout);
61 System("rm -rf DIR_35; mkdir DIR_35");
62 Chdir("DIR_35");
63 makelongnames();
64 superuser = (geteuid() == 0);
66 #ifdef _POSIX_CHOWN_RESTRICTED
67 # if _POSIX_CHOWN_RESTRICTED - 0 != -1
68 I_can_chown = superuser;
69 # else
70 I_can_chown = 1;
71 # endif
72 #else
73 # include "error, this case requires dynamic checks and is not handled"
74 #endif
76 for (i = 0; i < ITERATIONS; i++) {
77 if (m & 0001) test35a();
78 if (m & 0002) test35b();
79 if (m & 0004) test35c();
81 quit();
84 void test35a()
85 { /* Test normal operation. */
86 struct stat st;
87 struct utimbuf ub;
88 time_t time1, time2;
89 int cnt;
91 subtest = 1;
93 /* Creat scratch file. */
94 Creat("foo");
96 /* Set file times back two seconds. */
97 Stat("foo", &st);
98 ub.actime = st.st_atime - 2;
99 ub.modtime = st.st_mtime - 2;
100 Time(&time1);
101 utime("foo", &ub);
102 Time(&time2);
103 Stat("foo", &st);
104 if (ub.actime != st.st_atime) e(1);
105 if (ub.modtime != st.st_mtime) e(2);
107 /* The status changed time sould be changed. */
108 #ifndef V1_FILESYSTEM
109 if (st.st_ctime < time1) e(3);
110 #endif
111 if (st.st_ctime > time2) e(4);
113 /* Add twenty seconds. */
114 Stat("foo", &st);
115 ub.actime = st.st_atime + 20;
116 ub.modtime = st.st_mtime + 20;
117 Time(&time1);
118 utime("foo", &ub);
119 Time(&time2);
120 Stat("foo", &st);
121 if (ub.actime != st.st_atime) e(5);
122 if (ub.modtime != st.st_mtime) e(6);
123 if (st.st_ctime < time1) e(7);
124 #ifndef V1_FILESYSTEM
125 if (st.st_ctime > time2) e(8);
126 #endif
128 /* Try 100 times to do utime in less than one second. */
129 cnt = 0;
130 do {
131 Time(&time1);
132 utime("foo", (struct utimbuf *) NULL);
133 Time(&time2);
134 } while (time1 != time2 && cnt++ < 100);
135 if (time1 == time2) {
136 Stat("foo", &st);
137 Time(&time2);
138 if (st.st_atime != time1) e(9);
139 if (st.st_mtime != time1) e(10);
140 } else {
141 Stat("foo", &st);
142 if (st.st_atime > time2) e(11);
143 if (st.st_mtime > time2) e(12);
144 Time(&time2);
145 if (st.st_atime < time1) e(13);
146 if (st.st_mtime < time1) e(14);
148 if (st.st_ctime < time1) e(15);
149 if (st.st_ctime > time2) e(16);
151 System("rm -rf ../DIR_35/*");
154 void test35b()
156 subtest = 2;
158 /* MaxPath and MaxName checkup. */
159 Creat(MaxName);
160 MaxPath[strlen(MaxPath) - 2] = '/';
161 MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
162 Creat(MaxPath);
163 if (utime(MaxName, NULL) != 0) e(1);
164 if (utime(MaxPath, NULL) != 0) e(2);
166 /* The owner doesn't need write permisson to set times. */
167 Creat("foo");
168 if (chmod("foo", 0) != 0) e(3);
169 if (utime("foo", NULL) != 0) e(4);
170 if (chmod("foo", 0777) != 0) e(5);
171 if (utime("foo", NULL) != 0) e(6);
173 System("rm -rf ../DIR_35/*");
176 void test35c()
178 gid_t gid, gid2;
179 uid_t uid, uid2;
180 struct utimbuf ub;
181 int stat_loc;
183 subtest = 3;
185 /* Access problems. */
186 Mkdir("bar");
187 Creat("bar/tryme");
188 if (superuser) {
189 Chmod("bar", 0000); /* No search permisson at all. */
190 if (utime("bar/tryme", NULL) != 0) e(1);
192 if (!superuser) {
193 Chmod("bar", 0677); /* No search permisson. */
194 if (utime("bar/tryme", NULL) != -1) e(2);
195 if (errno != EACCES) e(3);
197 Chmod("bar", 0777);
199 if (I_can_chown) {
200 switch (fork()) {
201 case -1: printf("Can't fork\n"); break;
202 case 0:
203 alarm(20);
205 /* Get two differend non root uids. */
206 if (superuser) {
207 getids(&uid, &gid);
208 if (uid == 0) getids(&uid, &gid);
209 if (uid == 0) e(4);
211 if (!superuser) {
212 uid = geteuid();
213 gid = getegid();
215 getids(&uid2, &gid);
216 if (uid == uid2) getids(&uid2, &gid2);
217 if (uid == uid2) e(5);
219 /* Creat a number of files for root, user and user2. */
220 Creat("rootfile"); /* Owned by root. */
221 Chmod("rootfile", 0600);
222 Chown("rootfile", 0, 0);
223 Creat("user2file"); /* Owned by user 2, writeable. */
224 Chmod("user2file", 0020);
225 Chown("user2file", uid2, gid);
226 Creat("user2private"); /* Owned by user 2, privately. */
227 Chmod("user2private", 0600);
228 Chown("user2private", uid2, gid);
230 if (superuser) {
231 setgid(gid);
232 setuid(uid);
235 /* We now are user ``uid'' from group ``gid''. */
236 ub.actime = (time_t) 12345L;
237 ub.modtime = (time_t) 12345L;
239 if (utime("rootfile", NULL) != -1) e(6);
240 if (errno != EACCES) e(7);
241 if (utime("rootfile", &ub) != -1) e(8);
242 if (errno != EPERM) e(9);
244 if (utime("user2file", NULL) != 0) e(10);
245 if (utime("user2file", &ub) != -1) e(11);
246 if (errno != EPERM) e(12);
248 if (utime("user2private", NULL) != -1) e(13);
249 if (errno != EACCES) e(14);
250 if (utime("user2private", &ub) != -1) e(15);
251 if (errno != EPERM) e(16);
253 exit(errct ? 1 : 0);
254 default:
255 wait(&stat_loc);
256 if (stat_loc != 0) e(17); /* Alarm? */
260 /* Test names that are too long. */
261 #ifdef _POSIX_NO_TRUNC
262 # if _POSIX_NO_TRUNC - 0 != -1
263 /* Not exist might also be a propper response? */
264 if (utime(NameTooLong, NULL) != -1) e(18);
265 if (errno != ENAMETOOLONG) e(19);
266 # else
267 Creat(NameTooLong);
268 if (utime(NameTooLong, NULL) != 0) e(20);
269 # endif
270 #else
271 # include "error, this case requires dynamic checks and is not handled"
272 #endif
274 /* Make PathTooLong contain ././.../a */
275 PathTooLong[strlen(PathTooLong) - 2] = '/';
276 PathTooLong[strlen(PathTooLong) - 1] = 'a';
277 Creat("a");
278 if (utime(PathTooLong, NULL) != -1) e(21);
279 if (errno != ENAMETOOLONG) e(22);
281 /* Non existing file name. */
282 if (utime("nonexist", NULL) != -1) e(23);
283 if (errno != ENOENT) e(24);
285 /* Empty file name. */
286 if (utime("", NULL) != -1) e(25);
287 if (errno != ENOENT) e(26);
289 System("rm -rf ../DIR_35/*");
292 void makelongnames()
294 register int i;
296 memset(MaxName, 'a', NAME_MAX);
297 MaxName[NAME_MAX] = '\0';
298 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
299 MaxPath[i++] = '.';
300 MaxPath[i] = '/';
302 MaxPath[PATH_MAX - 1] = '\0';
304 strcpy(NameTooLong, MaxName); /* copy them Max to TooLong */
305 strcpy(PathTooLong, MaxPath);
307 NameTooLong[NAME_MAX] = 'a';
308 NameTooLong[NAME_MAX + 1] = '\0'; /* extend NameTooLong by one too many*/
309 PathTooLong[PATH_MAX - 1] = '/';
310 PathTooLong[PATH_MAX] = '\0'; /* inc PathTooLong by one */
313 void e(n)
314 int n;
316 int err_num = errno; /* Save in case printf clobbers it. */
318 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
319 errno = err_num;
320 perror("");
321 if (errct++ > MAX_ERROR) {
322 printf("Too many errors; test aborted\n");
323 chdir("..");
324 system("rm -rf DIR* > /dev/null 2>/dev/null");
325 exit(1);
327 errno = 0;
330 void quit()
332 Chdir("..");
333 System("rm -rf DIR_35");
335 if (errct == 0) {
336 printf("ok\n");
337 exit(0);
338 } else {
339 printf("%d errors\n", errct);
340 exit(1);
344 /* Getids returns a valid uid and gid. Is used PASSWD FILE.
345 ** It assumes the following format for a passwd file line:
346 ** <user_name>:<passwd>:<uid>:<gid>:<other_stuff>
347 ** If no uids and gids can be found, it will only return 0 ids.
349 void getids(r_uid, r_gid)
350 uid_t *r_uid;
351 gid_t *r_gid;
353 char line[N];
354 char *p;
355 uid_t uid;
356 gid_t gid;
357 FILE *fp;
358 int i;
360 static uid_t a_uid[N]; /* Array for uids. */
361 static gid_t a_gid[N]; /* Array for gids. */
362 static int nuid = 0, ngid = 0;/* The number of user & group ids. */
363 static int cuid = 0, cgid = 0;/* The current id index. */
365 /* If we don't have any uids go read some from the passwd file. */
366 if (nuid == 0) {
367 a_uid[nuid++] = 0; /* Root uid and gid. */
368 a_gid[ngid++] = 0;
369 if ((fp = fopen(PASSWD_FILE, "r")) == NULL) {
370 printf("Can't open ");
371 perror(PASSWD_FILE);
373 while (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
374 p = strchr(line, ':');
375 if (p != NULL) p = strchr(p + 1, ':');
376 if (p != NULL) {
377 p++;
378 uid = 0;
379 while (isdigit(*p)) {
380 uid *= 10;
381 uid += (uid_t) (*p - '0');
382 p++;
384 if (*p != ':') continue;
385 p++;
386 gid = 0;
387 while (isdigit(*p)) {
388 gid *= 10;
389 gid += (gid_t) (*p - '0');
390 p++;
392 if (*p != ':') continue;
393 if (nuid < N) {
394 for (i = 0; i < nuid; i++)
395 if (a_uid[i] == uid) break;
396 if (i == nuid) a_uid[nuid++] = uid;
398 if (ngid < N) {
399 for (i = 0; i < ngid; i++)
400 if (a_gid[i] == gid) break;
401 if (i == ngid) a_gid[ngid++] = gid;
403 if (nuid >= N && ngid >= N) break;
406 if (fp != NULL) fclose(fp);
409 /* We now have uids and gids in a_uid and a_gid. */
410 if (cuid >= nuid) cuid = 0;
411 if (cgid >= ngid) cgid = 0;
412 *r_uid = a_uid[cuid++];
413 *r_gid = a_gid[cgid++];