source: trunk/minix/test/test31.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: 6.6 KB
Line 
1/* test31: mkfifo() Author: Jan-Mark Wams (jms@cs.vu.nl) */
2
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 <errno.h>
12#include <time.h>
13#include <stdio.h>
14
15#define MAX_ERROR 4
16#define ITERATIONS 10
17
18#define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
19#define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
20#define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
21
22int errct = 0;
23int subtest = 1;
24int superuser;
25char MaxName[NAME_MAX + 1]; /* Name of maximum length */
26char MaxPath[PATH_MAX]; /* Same for path */
27char ToLongName[NAME_MAX + 2]; /* Name of maximum +1 length */
28char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
29
30_PROTOTYPE(void main, (int argc, char *argv[]));
31_PROTOTYPE(void test31a, (void));
32_PROTOTYPE(void test31b, (void));
33_PROTOTYPE(void test31c, (void));
34_PROTOTYPE(void makelongnames, (void));
35_PROTOTYPE(void e, (int number));
36_PROTOTYPE(void quit, (void));
37
38void main(argc, argv)
39int argc;
40char *argv[];
41{
42 int i, m = 0xFFFF;
43
44 sync();
45 if (argc == 2) m = atoi(argv[1]);
46 printf("Test 31 ");
47 fflush(stdout);
48 System("rm -rf DIR_31; mkdir DIR_31");
49 Chdir("DIR_31");
50 makelongnames();
51 superuser = (geteuid() == 0);
52
53 umask(0000);
54
55 for (i = 0; i < ITERATIONS; i++) {
56 if (m & 0001) test31a();
57 if (m & 0002) test31b();
58 if (m & 0004) test31c();
59 }
60 quit();
61}
62
63void test31a()
64{ /* Test normal operation. */
65
66#define BUF_SIZE 1024
67
68 int fd;
69 char buf[BUF_SIZE];
70 struct stat st, dirst;
71 time_t time1, time2;
72 int stat_loc, cnt;
73
74 subtest = 1;
75
76 System("rm -rf ../DIR_31/*");
77
78 /* Check if the file status information is updated correctly */
79 System("rm -rf fifo");
80 cnt = 0;
81 Stat(".", &dirst);
82 time(&time1);
83 while (time1 == time((time_t *)0))
84 ;
85
86 do {
87 time(&time1);
88 if (mkfifo("fifo", 0644) != 0) e(1);
89 Stat("fifo", &st);
90 time(&time2);
91 } while (time1 != time2 && cnt++ < 100);
92
93 if (cnt >= 100) e(2);
94 if (st.st_uid != geteuid()) e(3); /* Uid should be set. */
95#if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
96 if (st.st_gid != getegid()) e(4);
97#endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
98 if (!S_ISFIFO(st.st_mode)) e(5);
99 if (st.st_mode & 0777 != 0644) e(6);
100 if (st.st_nlink != 1) e(7);
101 if (st.st_ctime != time1) e(8);
102 if (st.st_atime != time1) e(9);
103 if (st.st_mtime != time1) e(10);
104 if (st.st_size != 0) e(11); /* File should be empty. */
105
106 /* Check if status for "." is updated. */
107 Stat(".", &st);
108 if (st.st_ctime <= dirst.st_ctime) e(12);
109 if (st.st_mtime <= dirst.st_mtime) e(13);
110
111 /* Basic checking if a fifo file created with mkfifo() is a pipe. */
112 alarm(10); /* in case fifo hangs */
113 switch (fork()) {
114 case -1: printf("Can't fork\n"); break;
115 case 0:
116 if ((fd = open("fifo", O_RDONLY)) != 3) e(14);
117 if (read(fd, buf, BUF_SIZE) != 7) e(15);
118 if (strcmp(buf, "banana") != 0) e(16);
119 if (close(fd) != 0) e(17);
120 if ((fd = open("fifo", O_WRONLY)) != 3) e(18);
121 if (write(fd, "thanks", 7) != 7) e(19);
122 if (close(fd) != 0) e(20);
123 exit(0);
124
125 default:
126 if ((fd = open("fifo", O_WRONLY)) != 3) e(21);
127 if (write(fd, "banana", 7) != 7) e(22);
128 if (close(fd) != 0) e(23);
129 if ((fd = open("fifo", O_RDONLY)) != 3) e(24);
130 if (read(fd, buf, BUF_SIZE) != 7) e(25);
131 if (strcmp(buf, "thanks") != 0) e(26);
132 if (close(fd) != 0) e(27);
133 wait(&stat_loc);
134 if (stat_loc != 0) e(28); /* Alarm? */
135 }
136 alarm(0);
137}
138
139void test31b()
140{
141 subtest = 2;
142
143 System("rm -rf ../DIR_31/*");
144
145 /* Test maximal file name length. */
146 if (mkfifo(MaxName, 0777) != 0) e(1);
147 if (unlink(MaxName) != 0) e(2);
148 MaxPath[strlen(MaxPath) - 2] = '/';
149 MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
150 if (mkfifo(MaxPath, 0777) != 0) e(3);
151 if (unlink(MaxPath) != 0) e(4);
152 MaxPath[strlen(MaxPath) - 1] = '/'; /* make ././.../a */
153}
154
155void test31c()
156{
157 subtest = 3;
158
159 System("rm -rf ../DIR_31/*");
160
161 /* Check if mkfifo() removes, files, fifos, dirs. */
162 if (mkfifo("fifo", 0777) != 0) e(1);
163 System("mkdir dir; > file");
164 if (mkfifo("fifo", 0777) != -1) e(2);
165 if (errno != EEXIST) e(3);
166 if (mkfifo("dir", 0777) != -1) e(4);
167 if (errno != EEXIST) e(5);
168 if (mkfifo("file", 0777) != -1) e(6);
169 if (errno != EEXIST) e(7);
170
171 /* Test empty path. */
172 if (mkfifo("", 0777) != -1) e(8);
173 if (errno != ENOENT) e(9);
174 if (mkfifo("/tmp/noway/out", 0777) != -1) e(10);
175 if (errno != ENOENT) e(11);
176
177 /* Test if path prefix is a directory. */
178 if (mkfifo("/etc/passwd/nono", 0777) != -1) e(12);
179 if (errno != ENOTDIR) e(13);
180
181 mkdir("bar", 0777); /* make bar */
182
183 /* Check if no access on part of path generates the correct error. */
184 System("chmod 577 bar"); /* r-xrwxrwx */
185 if (!superuser) {
186 if (mkfifo("bar/nono", 0666) != -1) e(14);
187 if (errno != EACCES) e(15);
188 }
189 if (superuser) {
190 if (mkfifo("bar/nono", 0666) != 0) e(14);
191 if (unlink("bar/nono") != 0) e(666);
192 }
193 System("chmod 677 bar"); /* rw-rwxrwx */
194 if (!superuser) {
195 if (mkfifo("bar/../nono", 0666) != -1) e(16);
196 if (errno != EACCES) e(17);
197 }
198 if (unlink("nono") != -1) e(18);
199
200 /* Clean up bar. */
201 System("rm -rf bar");
202
203 /* Test ToLongName and ToLongPath */
204#ifdef _POSIX_NO_TRUNC
205# if _POSIX_NO_TRUNC - 0 != -1
206 if (mkfifo(ToLongName, 0777) != -1) e(19);
207 if (errno != ENAMETOOLONG) e(20);
208# else
209 if (mkfifo(ToLongName, 0777) != 0) e(21);
210# endif
211#else
212# include "error, this case requires dynamic checks and is not handled"
213#endif
214 ToLongPath[PATH_MAX - 2] = '/';
215 ToLongPath[PATH_MAX - 1] = 'a';
216 if (mkfifo(ToLongPath, 0777) != -1) e(22);
217 if (errno != ENAMETOOLONG) e(23);
218 ToLongPath[PATH_MAX - 1] = '/';
219}
220
221void makelongnames()
222{
223 register int i;
224
225 memset(MaxName, 'a', NAME_MAX);
226 MaxName[NAME_MAX] = '\0';
227 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
228 MaxPath[i++] = '.';
229 MaxPath[i] = '/';
230 }
231 MaxPath[PATH_MAX - 1] = '\0';
232
233 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
234 strcpy(ToLongPath, MaxPath);
235
236 ToLongName[NAME_MAX] = 'a';
237 ToLongName[NAME_MAX + 1] = '\0'; /* extend ToLongName by one too many */
238 ToLongPath[PATH_MAX - 1] = '/';
239 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
240}
241
242void e(n)
243int n;
244{
245 int err_num = errno; /* Save in case printf clobbers it. */
246
247 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
248 errno = err_num;
249 perror("");
250 if (errct++ > MAX_ERROR) {
251 printf("Too many errors; test aborted\n");
252 chdir("..");
253 system("rm -rf DIR*");
254 exit(1);
255 }
256 errno = 0;
257}
258
259void quit()
260{
261 Chdir("..");
262 System("rm -rf DIR_31");
263
264 if (errct == 0) {
265 printf("ok\n");
266 exit(0);
267 } else {
268 printf("%d errors\n", errct);
269 exit(1);
270 }
271}
Note: See TracBrowser for help on using the repository browser.