source: trunk/minix/test/test30.c@ 9

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

Minix 3.1.2a

File size: 9.0 KB
Line 
1/* test30: creat() (p) Jan-Mark Wams. email: jms@cs.vu.nl */
2
3/*
4** Creat() should be equivalent to:
5** open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
6** Since we can not look in the source, we can not assume creat() is
7** a mere sysnonym (= a systemcall synonym).
8*/
9
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <sys/wait.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <string.h>
16#include <fcntl.h>
17#include <limits.h>
18#include <errno.h>
19#include <time.h>
20#include <stdio.h>
21
22#define MAX_ERROR 4
23#define ITERATIONS 10
24
25#define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
26#define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
27#define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
28
29int errct = 0;
30int subtest = 1;
31int superuser;
32char MaxName[NAME_MAX + 1]; /* Name of maximum length */
33char MaxPath[PATH_MAX]; /* Same for path */
34char ToLongName[NAME_MAX + 2]; /* Name of maximum +1 length */
35char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
36
37_PROTOTYPE(void main, (int argc, char *argv[]));
38_PROTOTYPE(void test30a, (void));
39_PROTOTYPE(void test30b, (void));
40_PROTOTYPE(void test30c, (void));
41_PROTOTYPE(void makelongnames, (void));
42_PROTOTYPE(void e, (int number));
43_PROTOTYPE(void quit, (void));
44
45void main(argc, argv)
46int argc;
47char *argv[];
48{
49 int i, m = 0xFFFF;
50
51 sync();
52 if (argc == 2) m = atoi(argv[1]);
53 printf("Test 30 ");
54 fflush(stdout);
55 System("rm -rf DIR_30; mkdir DIR_30");
56 Chdir("DIR_30");
57 makelongnames();
58 superuser = (geteuid() == 0);
59
60 umask(0000);
61
62 for (i = 0; i < ITERATIONS; i++) {
63 if (m & 0001) test30a();
64 if (m & 0002) test30b();
65 if (m & 0004) test30c();
66 }
67 quit();
68}
69
70void test30a()
71{ /* Test normal operation. */
72
73#define BUF_SIZE 1024
74
75 int fd1, fd2;
76 char buf[BUF_SIZE];
77 struct stat st, dirst;
78 time_t time1, time2;
79 int stat_loc, cnt;
80
81 subtest = 1;
82
83 System("rm -rf ../DIR_30/*");
84
85 /* Check if processes have independent new fds */
86 switch (fork()) {
87 case -1: printf("Can't fork\n"); break;
88 case 0:
89 alarm(20);
90 if ((fd1 = creat("myfile", 0644)) != 3) e(1);
91 if (close(fd1) != 0) e(2);
92 exit(0);
93 default:
94 if ((fd1 = creat("myfile", 0644)) != 3) e(3);
95 if (close(fd1) != 0) e(4);
96 if (wait(&stat_loc) == -1) e(5);
97 if (stat_loc != 0) e(6);
98 }
99
100 /* Save the dir status. */
101 Stat(".", &dirst);
102 time(&time1);
103 while (time1 == time((time_t *)0))
104 ;
105
106 /* Check if the file status information is updated correctly */
107 cnt = 0;
108 System("rm -rf myfile");
109 do {
110 time(&time1);
111 if ((fd1 = creat("myfile", 0644)) != 3) e(7);
112 Stat("myfile", &st);
113 time(&time2);
114 } while (time1 != time2 && cnt++ < 100);
115 if (cnt >= 100) e(8);
116 if (st.st_uid != geteuid()) e(9); /* Uid should be set. */
117#if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
118 if (st.st_gid != getegid()) e(10);
119#endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
120 if (!S_ISREG(st.st_mode)) e(11);
121 if (st.st_mode & 0777 != 0644) e(12);
122 if (st.st_nlink != 1) e(13);
123 if (st.st_ctime != time1) e(14); /* All time fields should be updated */
124 if (st.st_atime != time1) e(15);
125 if (st.st_mtime != time1) e(16);
126 if (st.st_size != 0) e(17); /* File should be trunked. */
127
128 /* Check if c and mtime for "." is updated. */
129 Stat(".", &st);
130 if (st.st_ctime <= dirst.st_ctime) e(18);
131 if (st.st_mtime <= dirst.st_mtime) e(19);
132
133 /* Let's see if cread fds are write only. */
134 if (read(fd1, buf, 7) != -1) e(20); /* we can't read */
135 if (errno != EBADF) e(21); /* a write only fd */
136 if (write(fd1, "HELLO", 6) != 6) e(22); /* but we can write */
137
138 /* No O_APPEND flag should have been used. */
139 if (lseek(fd1, (off_t) 1, SEEK_SET) != 1) e(23);
140 if (write(fd1, "ello", 5) != 5) e(24);
141 Stat("myfile", &st);
142 if (st.st_size != 6) e(25);
143 if (st.st_size == 11) e(26); /* O_APPEND should make it 11. */
144 if (close(fd1) != 0) e(27);
145
146 /* A creat should set the file offset to 0. */
147 if ((fd1 = creat("myfile", 0644)) != 3) e(28);
148 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 0) e(29);
149 if (close(fd1) != 0) e(30);
150
151 /* Test if file permission bits and the file ownership are unchanged. */
152 /* So we will see if creat() is just an open() if the file exists. */
153 if (superuser) {
154 System("echo > bar; chmod 073 bar"); /* Make bar 073 */
155 if (chown("bar", 1, 1) != 0) e(1137);
156 fd1 = creat("bar", 0777); /* knock knock */
157 if (fd1 == -1) e(31);
158 Stat("bar", &st);
159 if (st.st_size != (size_t) 0) e(32); /* empty file. */
160 if (write(fd1, "foo", 3) != 3) e(33); /* rewrite bar */
161 if (close(fd1) != 0) e(34);
162 Stat("bar", &st);
163 if (st.st_uid != 1) e(35);
164 if (st.st_gid != 1) e(36);
165 if ((st.st_mode & 0777) != 073) e(37); /* mode still is 077 */
166 if (st.st_size != (size_t) 3) e(38);
167 }
168
169 /* Fifo's should be openable with creat(). */
170 if (mkfifo("fifo", 0644) != 0) e(39);
171
172 /* Creat() should have no effect on FIFO files. */
173 switch (fork()) {
174 case -1: printf("Can't fork\n"); break;
175 case 0:
176 alarm(20); /* Give child 20 seconds to live. */
177 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(40);
178 if (write(fd1, "I did see Elvis.\n", 18) != 18) e(41);
179 if ((fd2 = creat("fifo", 0644)) != 4) e(42);
180 if (write(fd2, "I DID.\n", 8) != 8) e(43);
181 if (close(fd2) != 0) e(44);
182 if (close(fd1) != 0) e(45);
183 exit(0);
184 default:
185 if ((fd1 = open("fifo", O_RDONLY)) != 3) e(46);
186 if (read(fd1, buf, 18) != 18) e(47);
187 if (strcmp(buf, "I did see Elvis.\n") != 0) e(48);
188 if (strcmp(buf, "I DID.\n") == 0) e(49);
189 if (read(fd1, buf, BUF_SIZE) != 8) e(50);
190 if (strcmp(buf, "I DID.\n") != 0) e(51);
191 if (close(fd1) != 0) e(52);
192 if (wait(&stat_loc) == -1) e(53);
193 if (stat_loc != 0) e(54); /* The alarm went off? */
194 }
195
196 /* Creat() should have no effect on directroys. */
197 System("mkdir dir; touch dir/f1 dir/f2 dir/f3");
198 if ((fd1 = creat("dir", 0644)) != -1) e(55);
199 if (errno != EISDIR) e(56);
200 close(fd1);
201
202 /* The path should contain only dirs in the prefix. */
203 if ((fd1 = creat("dir/f1/nono", 0644)) != -1) e(57);
204 if (errno != ENOTDIR) e(58);
205 close(fd1);
206
207 /* The path should contain only dirs in the prefix. */
208 if ((fd1 = creat("", 0644)) != -1) e(59);
209 if (errno != ENOENT) e(60);
210 close(fd1);
211 if ((fd1 = creat("dir/noso/nono", 0644)) != -1) e(61);
212 if (errno != ENOENT) e(62);
213 close(fd1);
214
215}
216
217void test30b()
218{
219 int fd;
220
221 subtest = 2;
222
223 System("rm -rf ../DIR_30/*");
224
225 /* Test maximal file name length. */
226 if ((fd = creat(MaxName, 0777)) != 3) e(1);
227 if (close(fd) != 0) e(2);
228 MaxPath[strlen(MaxPath) - 2] = '/';
229 MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
230 if ((fd = creat(MaxPath, 0777)) != 3) e(3);
231 if (close(fd) != 0) e(4);
232 MaxPath[strlen(MaxPath) - 1] = '/'; /* make ././.../a */
233}
234
235void test30c()
236{
237 int fd;
238
239 subtest = 3;
240
241 System("rm -rf ../DIR_30/*");
242
243 if (!superuser) {
244 /* Test if creat is not usable to open files with the wrong mode */
245 System("> nono; chmod 177 nono");
246 fd = creat("nono", 0777);
247 if (fd != -1) e(1);
248 if (errno != EACCES) e(2);
249 }
250 if (mkdir("bar", 0777) != 0) e(3); /* make bar */
251
252 /* Check if no access on part of path generates the correct error. */
253 System("chmod 577 bar"); /* r-xrwxrwx */
254 if (!superuser) {
255 /* Normal users can't creat without write permision. */
256 if (creat("bar/nono", 0666) != -1) e(4);
257 if (errno != EACCES) e(5);
258 if (creat("bar/../nono", 0666) != -1) e(6);
259 if (errno != EACCES) e(7);
260 }
261 if (superuser) {
262 /* Super user can still creat stuff. */
263 if ((fd = creat("bar/nono", 0666)) != 3) e(8);
264 if (close(fd) != 0) e(9);
265 if (unlink("bar/nono") != 0) e(10);
266 }
267
268 /* Clean up bar. */
269 System("rm -rf bar");
270
271 /* Test ToLongName and ToLongPath */
272#ifdef _POSIX_NO_TRUNC
273# if _POSIX_NO_TRUNC - 0 != -1
274 if ((fd = creat(ToLongName, 0777)) != -1) e(11);
275 if (errno != ENAMETOOLONG) e(12);
276 close(fd); /* Just in case. */
277# else
278 if ((fd = creat(ToLongName, 0777)) != 3) e(13);
279 if (close(fd) != 0) e(14);
280# endif
281#else
282# include "error, this case requires dynamic checks and is not handled"
283#endif
284 ToLongPath[PATH_MAX - 2] = '/';
285 ToLongPath[PATH_MAX - 1] = 'a';
286 if ((fd = creat(ToLongPath, 0777)) != -1) e(15);
287 if (errno != ENAMETOOLONG) e(16);
288 if (close(fd) != -1) e(17);
289 ToLongPath[PATH_MAX - 1] = '/';
290}
291
292void makelongnames()
293{
294 register int i;
295
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] = '/';
301 }
302 MaxPath[PATH_MAX - 1] = '\0';
303
304 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
305 strcpy(ToLongPath, MaxPath);
306
307 ToLongName[NAME_MAX] = 'a';
308 ToLongName[NAME_MAX + 1] = '\0'; /* extend ToLongName by one too many */
309 ToLongPath[PATH_MAX - 1] = '/';
310 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
311}
312
313void e(n)
314int n;
315{
316 int err_num = errno; /* Save in case printf clobbers it. */
317
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*");
325 exit(1);
326 }
327 errno = 0;
328}
329
330void quit()
331{
332 Chdir("..");
333 System("rm -rf DIR_30");
334
335 if (errct == 0) {
336 printf("ok\n");
337 exit(0);
338 } else {
339 printf("%d errors\n", errct);
340 exit(1);
341 }
342}
Note: See TracBrowser for help on using the repository browser.