pkgin_all: script to auto-install all packages
[minix.git] / test / test30.c
blob4f0302dab841d03f91c7aa5e2203ec217c299e1d
1 /* test30: creat() (p) Jan-Mark Wams. email: jms@cs.vu.nl */
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 */
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>
22 #define MAX_ERROR 4
23 #define ITERATIONS 10
25 #include "common.c"
27 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
28 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
29 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
31 int superuser;
32 char *MaxName; /* Name of maximum length */
33 char MaxPath[PATH_MAX]; /* Same for path */
34 char *ToLongName; /* Name of maximum +1 length */
35 char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
37 void test30a(void);
38 void test30b(void);
39 void test30c(void);
40 void makelongnames(void);
42 int main(int argc, char *argv[])
44 int i, m = 0xFFFF;
46 sync();
47 if (argc == 2) m = atoi(argv[1]);
48 umask(0000);
49 start(30);
50 makelongnames();
51 superuser = (geteuid() == 0);
54 for (i = 0; i < ITERATIONS; i++) {
55 if (m & 0001) test30a();
56 if (m & 0002) test30b();
57 if (m & 0004) test30c();
59 quit();
61 return(-1); /* Unreachable */
64 void test30a()
65 { /* Test normal operation. */
67 #define BUF_SIZE 1024
69 int fd1, fd2;
70 char buf[BUF_SIZE];
71 struct stat st, dirst;
72 time_t time1, time2;
73 int stat_loc, cnt;
75 subtest = 1;
77 System("rm -rf ../DIR_30/*");
79 /* Check if processes have independent new fds */
80 switch (fork()) {
81 case -1: printf("Can't fork\n"); break;
82 case 0:
83 alarm(20);
84 if ((fd1 = creat("myfile", 0644)) != 3) e(1);
85 if (close(fd1) != 0) e(2);
86 exit(0);
87 default:
88 if ((fd1 = creat("myfile", 0644)) != 3) e(3);
89 if (close(fd1) != 0) e(4);
90 if (wait(&stat_loc) == -1) e(5);
91 if (stat_loc != 0) e(6);
94 /* Save the dir status. */
95 Stat(".", &dirst);
96 time(&time1);
97 while (time1 == time((time_t *)0))
100 /* Check if the file status information is updated correctly */
101 cnt = 0;
102 System("rm -rf myfile");
103 do {
104 time(&time1);
105 if ((fd1 = creat("myfile", 0644)) != 3) e(7);
106 Stat("myfile", &st);
107 time(&time2);
108 } while (time1 != time2 && cnt++ < 100);
109 if (cnt >= 100) e(8);
110 if (st.st_uid != geteuid()) e(9); /* Uid should be set. */
111 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
112 if (st.st_gid != getegid()) e(10);
113 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
114 if (!S_ISREG(st.st_mode)) e(11);
115 if ((st.st_mode & 0777) != 0644) e(12);
116 if (st.st_nlink != 1) e(13);
117 if (st.st_ctime != time1) e(14); /* All time fields should be updated */
118 if (st.st_atime != time1) e(15);
119 if (st.st_mtime != time1) e(16);
120 if (st.st_size != 0) e(17); /* File should be trunked. */
122 /* Check if c and mtime for "." is updated. */
123 Stat(".", &st);
124 if (st.st_ctime <= dirst.st_ctime) e(18);
125 if (st.st_mtime <= dirst.st_mtime) e(19);
127 /* Let's see if cread fds are write only. */
128 if (read(fd1, buf, 7) != -1) e(20); /* we can't read */
129 if (errno != EBADF) e(21); /* a write only fd */
130 if (write(fd1, "HELLO", 6) != 6) e(22); /* but we can write */
132 /* No O_APPEND flag should have been used. */
133 if (lseek(fd1, (off_t) 1, SEEK_SET) != 1) e(23);
134 if (write(fd1, "ello", 5) != 5) e(24);
135 Stat("myfile", &st);
136 if (st.st_size != 6) e(25);
137 if (st.st_size == 11) e(26); /* O_APPEND should make it 11. */
138 if (close(fd1) != 0) e(27);
140 /* A creat should set the file offset to 0. */
141 if ((fd1 = creat("myfile", 0644)) != 3) e(28);
142 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 0) e(29);
143 if (close(fd1) != 0) e(30);
145 /* Test if file permission bits and the file ownership are unchanged. */
146 /* So we will see if creat() is just an open() if the file exists. */
147 if (superuser) {
148 System("echo > bar; chmod 073 bar"); /* Make bar 073 */
149 if (chown("bar", 1, 1) != 0) e(1137);
150 fd1 = creat("bar", 0777); /* knock knock */
151 if (fd1 == -1) e(31);
152 Stat("bar", &st);
153 if (st.st_size != (size_t) 0) e(32); /* empty file. */
154 if (write(fd1, "foo", 3) != 3) e(33); /* rewrite bar */
155 if (close(fd1) != 0) e(34);
156 Stat("bar", &st);
157 if (st.st_uid != 1) e(35);
158 if (st.st_gid != 1) e(36);
159 if ((st.st_mode & 0777) != 073) e(37); /* mode still is 077 */
160 if (st.st_size != (size_t) 3) e(38);
163 /* Fifo's should be openable with creat(). */
164 if (mkfifo("fifo", 0644) != 0) e(39);
166 /* Creat() should have no effect on FIFO files. */
167 switch (fork()) {
168 case -1: printf("Can't fork\n"); break;
169 case 0:
170 alarm(20); /* Give child 20 seconds to live. */
171 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(40);
172 if (write(fd1, "I did see Elvis.\n", 18) != 18) e(41);
173 if ((fd2 = creat("fifo", 0644)) != 4) e(42);
174 if (write(fd2, "I DID.\n", 8) != 8) e(43);
175 if (close(fd2) != 0) e(44);
176 if (close(fd1) != 0) e(45);
177 exit(0);
178 default:
179 if ((fd1 = open("fifo", O_RDONLY)) != 3) e(46);
180 if (read(fd1, buf, 18) != 18) e(47);
181 if (strcmp(buf, "I did see Elvis.\n") != 0) e(48);
182 if (strcmp(buf, "I DID.\n") == 0) e(49);
183 if (read(fd1, buf, BUF_SIZE) != 8) e(50);
184 if (strcmp(buf, "I DID.\n") != 0) e(51);
185 if (close(fd1) != 0) e(52);
186 if (wait(&stat_loc) == -1) e(53);
187 if (stat_loc != 0) e(54); /* The alarm went off? */
190 /* Creat() should have no effect on directroys. */
191 System("mkdir dir; touch dir/f1 dir/f2 dir/f3");
192 if ((fd1 = creat("dir", 0644)) != -1) e(55);
193 if (errno != EISDIR) e(56);
194 close(fd1);
196 /* The path should contain only dirs in the prefix. */
197 if ((fd1 = creat("dir/f1/nono", 0644)) != -1) e(57);
198 if (errno != ENOTDIR) e(58);
199 close(fd1);
201 /* The path should contain only dirs in the prefix. */
202 if ((fd1 = creat("", 0644)) != -1) e(59);
203 if (errno != ENOENT) e(60);
204 close(fd1);
205 if ((fd1 = creat("dir/noso/nono", 0644)) != -1) e(61);
206 if (errno != ENOENT) e(62);
207 close(fd1);
211 void test30b()
213 int fd;
215 subtest = 2;
217 System("rm -rf ../DIR_30/*");
219 /* Test maximal file name length. */
220 if ((fd = creat(MaxName, 0777)) != 3) e(1);
221 if (close(fd) != 0) e(2);
222 MaxPath[strlen(MaxPath) - 2] = '/';
223 MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
224 if ((fd = creat(MaxPath, 0777)) != 3) e(3);
225 if (close(fd) != 0) e(4);
226 MaxPath[strlen(MaxPath) - 1] = '/'; /* make ././.../a */
229 void test30c()
231 int fd, does_truncate;
233 subtest = 3;
235 System("rm -rf ../DIR_30/*");
237 if (!superuser) {
238 /* Test if creat is not usable to open files with the wrong mode */
239 System("> nono; chmod 177 nono");
240 fd = creat("nono", 0777);
241 if (fd != -1) e(1);
242 if (errno != EACCES) e(2);
244 if (mkdir("bar", 0777) != 0) e(3); /* make bar */
246 /* Check if no access on part of path generates the correct error. */
247 System("chmod 577 bar"); /* r-xrwxrwx */
248 if (!superuser) {
249 /* Normal users can't creat without write permision. */
250 if (creat("bar/nono", 0666) != -1) e(4);
251 if (errno != EACCES) e(5);
252 if (creat("bar/../nono", 0666) != -1) e(6);
253 if (errno != EACCES) e(7);
255 if (superuser) {
256 /* Super user can still creat stuff. */
257 if ((fd = creat("bar/nono", 0666)) != 3) e(8);
258 if (close(fd) != 0) e(9);
259 if (unlink("bar/nono") != 0) e(10);
262 /* Clean up bar. */
263 System("rm -rf bar");
265 /* Test ToLongName and ToLongPath */
266 does_truncate = does_fs_truncate();
267 fd = creat(ToLongName, 0777);
268 if (does_truncate) {
269 if (fd == -1) e(11);
270 if (close(fd) != 0) e(12);
271 } else {
272 if (fd != -1) e(13);
273 if (errno != ENAMETOOLONG) e(14);
274 (void) close(fd); /* Just in case. */
277 ToLongPath[PATH_MAX - 2] = '/';
278 ToLongPath[PATH_MAX - 1] = 'a';
279 if ((fd = creat(ToLongPath, 0777)) != -1) e(15);
280 if (errno != ENAMETOOLONG) e(16);
281 if (close(fd) != -1) e(17);
282 ToLongPath[PATH_MAX - 1] = '/';
285 void makelongnames()
287 register int i;
288 int max_name_length;
290 max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
291 * the same length, hence runtime check */
292 MaxName = malloc(max_name_length + 1);
293 ToLongName = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
294 memset(MaxName, 'a', max_name_length);
295 MaxName[max_name_length] = '\0';
297 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
298 MaxPath[i++] = '.';
299 MaxPath[i] = '/';
301 MaxPath[PATH_MAX - 1] = '\0';
303 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
304 strcpy(ToLongPath, MaxPath);
306 ToLongName[max_name_length] = 'a';
307 ToLongName[max_name_length+1] = '\0';/* extend ToLongName by one too many */
308 ToLongPath[PATH_MAX - 1] = '/';
309 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */