VM: simplify slab allocator
[minix.git] / test / test31.c
blob70ee0d8ecb73d777ed679f0d6ee66da272c3f4d3
1 /* test31: mkfifo() 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 <errno.h>
12 #include <time.h>
13 #include <stdio.h>
15 #define MAX_ERROR 4
16 #define ITERATIONS 10
18 #include "common.c"
20 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
21 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
22 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
24 int superuser;
25 char *MaxName; /* Name of maximum length */
26 char MaxPath[PATH_MAX]; /* Same for path */
27 char *ToLongName; /* Name of maximum +1 length */
28 char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
30 void test31a(void);
31 void test31b(void);
32 void test31c(void);
33 void makelongnames(void);
35 int main(int argc, char *argv[])
37 int i, m = 0xFFFF;
39 sync();
40 if (argc == 2) m = atoi(argv[1]);
41 umask(0000);
42 start(31);
43 makelongnames();
44 superuser = (geteuid() == 0);
47 for (i = 0; i < ITERATIONS; i++) {
48 if (m & 0001) test31a();
49 if (m & 0002) test31b();
50 if (m & 0004) test31c();
52 quit();
53 return 1;
56 void test31a()
57 { /* Test normal operation. */
59 #define BUF_SIZE 1024
61 int fd;
62 char buf[BUF_SIZE];
63 struct stat st, dirst;
64 time_t time1, time2;
65 int stat_loc, cnt;
67 subtest = 1;
69 System("rm -rf ../DIR_31/*");
71 /* Check if the file status information is updated correctly */
72 cnt = 0;
73 Stat(".", &dirst);
74 time(&time1);
75 while (time1 == time((time_t *)0))
78 do {
79 unlink("fifo");
80 time(&time1);
81 if (mkfifo("fifo", 0644) != 0) e(1);
82 Stat("fifo", &st);
83 time(&time2);
84 } while (time1 != time2 && cnt++ < 100);
86 if (cnt >= 100) e(2);
87 if (st.st_uid != geteuid()) e(3); /* Uid should be set. */
88 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
89 if (st.st_gid != getegid()) e(4);
90 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
91 if (!S_ISFIFO(st.st_mode)) e(5);
92 if ((st.st_mode & 0777) != 0644) e(6);
93 if (st.st_nlink != 1) e(7);
94 if (st.st_ctime != time1) e(8);
95 if (st.st_atime != time1) e(9);
96 if (st.st_mtime != time1) e(10);
97 if (st.st_size != 0) e(11); /* File should be empty. */
99 /* Check if status for "." is updated. */
100 Stat(".", &st);
101 if (st.st_ctime <= dirst.st_ctime) e(12);
102 if (st.st_mtime <= dirst.st_mtime) e(13);
104 /* Basic checking if a fifo file created with mkfifo() is a pipe. */
105 alarm(10); /* in case fifo hangs */
106 switch (fork()) {
107 case -1: printf("Can't fork\n"); break;
108 case 0:
109 if ((fd = open("fifo", O_RDONLY)) != 3) e(14);
110 if (read(fd, buf, BUF_SIZE) != 7) e(15);
111 if (strcmp(buf, "banana") != 0) e(16);
112 if (close(fd) != 0) e(17);
113 if ((fd = open("fifo", O_WRONLY)) != 3) e(18);
114 if (write(fd, "thanks", 7) != 7) e(19);
115 if (close(fd) != 0) e(20);
116 exit(0);
118 default:
119 if ((fd = open("fifo", O_WRONLY)) != 3) e(21);
120 if (write(fd, "banana", 7) != 7) e(22);
121 if (close(fd) != 0) e(23);
122 if ((fd = open("fifo", O_RDONLY)) != 3) e(24);
123 if (read(fd, buf, BUF_SIZE) != 7) e(25);
124 if (strcmp(buf, "thanks") != 0) e(26);
125 if (close(fd) != 0) e(27);
126 wait(&stat_loc);
127 if (stat_loc != 0) e(28); /* Alarm? */
129 alarm(0);
132 void test31b()
134 subtest = 2;
136 System("rm -rf ../DIR_31/*");
138 /* Test maximal file name length. */
139 if (mkfifo(MaxName, 0777) != 0) e(1);
140 if (unlink(MaxName) != 0) e(2);
141 MaxPath[strlen(MaxPath) - 2] = '/';
142 MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
143 if (mkfifo(MaxPath, 0777) != 0) e(3);
144 if (unlink(MaxPath) != 0) e(4);
145 MaxPath[strlen(MaxPath) - 1] = '/'; /* make ././.../a */
148 void test31c()
150 int does_truncate;
151 subtest = 3;
153 System("rm -rf ../DIR_31/*");
155 /* Check if mkfifo() removes, files, fifos, dirs. */
156 if (mkfifo("fifo", 0777) != 0) e(1);
157 System("mkdir dir; > file");
158 if (mkfifo("fifo", 0777) != -1) e(2);
159 if (errno != EEXIST) e(3);
160 if (mkfifo("dir", 0777) != -1) e(4);
161 if (errno != EEXIST) e(5);
162 if (mkfifo("file", 0777) != -1) e(6);
163 if (errno != EEXIST) e(7);
165 /* Test empty path. */
166 if (mkfifo("", 0777) != -1) e(8);
167 if (errno != ENOENT) e(9);
168 if (mkfifo("/tmp/noway/out", 0777) != -1) e(10);
169 if (errno != ENOENT) e(11);
171 /* Test if path prefix is a directory. */
172 if (mkfifo("/etc/passwd/nono", 0777) != -1) e(12);
173 if (errno != ENOTDIR) e(13);
175 mkdir("bar", 0777); /* make bar */
177 /* Check if no access on part of path generates the correct error. */
178 System("chmod 577 bar"); /* r-xrwxrwx */
179 if (!superuser) {
180 if (mkfifo("bar/nono", 0666) != -1) e(14);
181 if (errno != EACCES) e(15);
183 if (superuser) {
184 if (mkfifo("bar/nono", 0666) != 0) e(14);
185 if (unlink("bar/nono") != 0) e(666);
187 System("chmod 677 bar"); /* rw-rwxrwx */
188 if (!superuser) {
189 if (mkfifo("bar/../nono", 0666) != -1) e(16);
190 if (errno != EACCES) e(17);
192 if (unlink("nono") != -1) e(18);
194 /* Clean up bar. */
195 System("rm -rf bar");
197 /* Test ToLongName and ToLongPath */
198 does_truncate = does_fs_truncate();
199 if (does_truncate) {
200 if (mkfifo(ToLongName, 0777) != 0) e(19);
201 } else {
202 if (mkfifo(ToLongName, 0777) != -1) e(20);
203 if (errno != ENAMETOOLONG) e(21);
206 ToLongPath[PATH_MAX - 2] = '/';
207 ToLongPath[PATH_MAX - 1] = 'a';
208 if (mkfifo(ToLongPath, 0777) != -1) e(22);
209 if (errno != ENAMETOOLONG) e(23);
210 ToLongPath[PATH_MAX - 1] = '/';
213 void makelongnames()
215 register int i;
216 int max_name_length;
218 max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
219 * the same length, hence runtime check */
220 MaxName = malloc(max_name_length + 1);
221 ToLongName = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
222 memset(MaxName, 'a', max_name_length);
223 MaxName[max_name_length] = '\0';
225 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
226 MaxPath[i++] = '.';
227 MaxPath[i] = '/';
229 MaxPath[PATH_MAX - 1] = '\0';
231 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
232 strcpy(ToLongPath, MaxPath);
234 ToLongName[max_name_length] = 'a';
235 ToLongName[max_name_length+1] = '\0';/* extend ToLongName by one too many */
236 ToLongPath[PATH_MAX - 1] = '/';
237 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */