1 /* test30: creat() (p) Jan-Mark Wams. email: jms@cs.vu.nl */
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).
10 #include <sys/types.h>
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)
32 char MaxName
[NAME_MAX
+ 1]; /* Name of maximum length */
33 char MaxPath
[PATH_MAX
]; /* Same for path */
34 char ToLongName
[NAME_MAX
+ 2]; /* Name of maximum +1 length */
35 char ToLongPath
[PATH_MAX
+ 1]; /* Same for path, both too long */
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));
52 if (argc
== 2) m
= atoi(argv
[1]);
55 System("rm -rf DIR_30; mkdir DIR_30");
58 superuser
= (geteuid() == 0);
62 for (i
= 0; i
< ITERATIONS
; i
++) {
63 if (m
& 0001) test30a();
64 if (m
& 0002) test30b();
65 if (m
& 0004) test30c();
71 { /* Test normal operation. */
77 struct stat st
, dirst
;
83 System("rm -rf ../DIR_30/*");
85 /* Check if processes have independent new fds */
87 case -1: printf("Can't fork\n"); break;
90 if ((fd1
= creat("myfile", 0644)) != 3) e(1);
91 if (close(fd1
) != 0) e(2);
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);
100 /* Save the dir status. */
103 while (time1
== time((time_t *)0))
106 /* Check if the file status information is updated correctly */
108 System("rm -rf myfile");
111 if ((fd1
= creat("myfile", 0644)) != 3) e(7);
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. */
128 /* Check if c and mtime for "." is updated. */
130 if (st
.st_ctime
<= dirst
.st_ctime
) e(18);
131 if (st
.st_mtime
<= dirst
.st_mtime
) e(19);
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 */
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);
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);
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);
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. */
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);
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);
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);
169 /* Fifo's should be openable with creat(). */
170 if (mkfifo("fifo", 0644) != 0) e(39);
172 /* Creat() should have no effect on FIFO files. */
174 case -1: printf("Can't fork\n"); break;
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);
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? */
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);
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);
207 /* The path should contain only dirs in the prefix. */
208 if ((fd1
= creat("", 0644)) != -1) e(59);
209 if (errno
!= ENOENT
) e(60);
211 if ((fd1
= creat("dir/noso/nono", 0644)) != -1) e(61);
212 if (errno
!= ENOENT
) e(62);
223 System("rm -rf ../DIR_30/*");
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 */
241 System("rm -rf ../DIR_30/*");
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);
248 if (errno
!= EACCES
) e(2);
250 if (mkdir("bar", 0777) != 0) e(3); /* make bar */
252 /* Check if no access on part of path generates the correct error. */
253 System("chmod 577 bar"); /* r-xrwxrwx */
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);
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);
269 System("rm -rf bar");
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. */
278 if ((fd
= creat(ToLongName
, 0777)) != 3) e(13);
279 if (close(fd
) != 0) e(14);
282 # include "error, this case requires dynamic checks and is not handled"
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] = '/';
296 memset(MaxName
, 'a', NAME_MAX
);
297 MaxName
[NAME_MAX
] = '\0';
298 for (i
= 0; i
< PATH_MAX
- 1; i
++) { /* idem path */
302 MaxPath
[PATH_MAX
- 1] = '\0';
304 strcpy(ToLongName
, MaxName
); /* copy them Max to ToLong */
305 strcpy(ToLongPath
, MaxPath
);
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 */
316 int err_num
= errno
; /* Save in case printf clobbers it. */
318 printf("Subtest %d, error %d errno=%d: ", subtest
, n
, errno
);
321 if (errct
++ > MAX_ERROR
) {
322 printf("Too many errors; test aborted\n");
324 system("rm -rf DIR*");
333 System("rm -rf DIR_30");
339 printf("%d errors\n", errct
);