kernel: restore stacktraces
[minix.git] / test / test26.c
blob5bb9c419684f5b386aeacd3001ccec4900210e8c
1 /* test26: lseek() 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 #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 #define Mkfifo(f) if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
23 #include "common.c"
25 void test26a(void);
26 void test26b(void);
27 void test26c(void);
29 int main(int argc, char *argv[])
31 int i, m = 0xFFFF;
33 sync();
34 if (argc == 2) m = atoi(argv[1]);
36 start(26);
38 for (i = 0; i < 10; i++) {
39 if (m & 0001) test26a();
40 if (m & 0002) test26b();
41 if (m & 0004) test26c();
43 quit();
45 return(-1); /* Unreachable */
48 void test26a()
49 { /* Test normal operation. */
50 int fd;
51 char buf[20];
52 int i, j;
53 struct stat st;
55 subtest = 1;
56 System("rm -rf ../DIR_26/*");
58 System("echo -n hihaho > hihaho");
59 if ((fd = open("hihaho", O_RDONLY)) != 3) e(1);
60 if (lseek(fd, (off_t) 3, SEEK_SET) != (off_t) 3) e(2);
61 if (read(fd, buf, 1) != 1) e(3);
62 if (buf[0] != 'a') e(4);
63 if (lseek(fd, (off_t) - 1, SEEK_END) != 5) e(5);
64 if (read(fd, buf, 1) != 1) e(6);
65 if (buf[0] != 'o') e(7);
67 /* Seek past end of file. */
68 if (lseek(fd, (off_t) 1000, SEEK_END) != 1006) e(8);
69 if (read(fd, buf, 1) != 0) e(9);
71 /* Lseek() should not extend the file. */
72 if (fstat(fd, &st) != 0) e(10);
73 if (st.st_size != (off_t) 6) e(11);
74 if (close(fd) != 0) e(12);
76 /* Probeer lseek met write. */
77 if ((fd = open("hihaho", O_WRONLY)) != 3) e(13);
78 if (lseek(fd, (off_t) 3, SEEK_SET) != (off_t) 3) e(14);
79 if (write(fd, "e", 1) != 1) e(15);
80 if (lseek(fd, (off_t) 1000, SEEK_END) != 1006) e(16);
82 /* Lseek() should not extend the file. */
83 if (fstat(fd, &st) != 0) e(17);
84 if (st.st_size != (off_t) 6) e(18);
85 if (write(fd, "e", 1) != 1) e(19);
87 /* Lseek() and a subsequent write should! */
88 if (fstat(fd, &st) != 0) e(20);
89 if (st.st_size != (off_t) 1007) e(21);
91 if (close(fd) != 0) e(22);
93 /* Check the file, it should start with hiheho. */
94 if ((fd = open("hihaho", O_RDONLY)) != 3) e(23);
95 if (read(fd, buf, 6) != 6) e(24);
96 if (strncmp(buf, "hiheho", 6) != 0) e(25);
98 /* The should be zero bytes and a trailing ``e''. */
99 if (sizeof(buf) < 10) e(26);
100 for (i = 1; i <= 20; i++) {
101 if (read(fd, buf, 10) != 10) e(27);
102 for (j = 0; j < 10; j++)
103 if (buf[j] != '\0') break;
104 if (j != 10) e(28);
105 if (lseek(fd, (off_t) 15, SEEK_CUR) != (off_t) i * 25 + 6) e(29);
108 if (lseek(fd, (off_t) 1006, SEEK_SET) != (off_t) 1006) e(30);
109 if (read(fd, buf, sizeof(buf)) != 1) e(31);
110 if (buf[0] != 'e') e(32);
112 if (lseek(fd, (off_t) - 1, SEEK_END) != (off_t) 1006) e(33);
113 if (read(fd, buf, sizeof(buf)) != 1) e(34);
114 if (buf[0] != 'e') e(35);
116 /* Closing time. */
117 if (close(fd) != 0) e(36);
120 void test26b()
122 int fd1, fd2, fd3;
123 int stat_loc;
125 subtest = 2;
126 System("rm -rf ../DIR_26/*");
128 /* See if childs lseek() is effecting the parent. * See also if
129 * lseeking() on same file messes things up. */
131 /* Creat a file of 11 bytes. */
132 if ((fd1 = open("santa", O_WRONLY | O_CREAT, 0777)) != 3) e(1);
133 if (write(fd1, "ho ho ho ho", 11) != 11) e(2);
134 if (close(fd1) != 0) e(3);
136 /* Open it multiple times. */
137 if ((fd1 = open("santa", O_RDONLY)) != 3) e(4);
138 if ((fd2 = open("santa", O_WRONLY)) != 4) e(5);
139 if ((fd3 = open("santa", O_RDWR)) != 5) e(6);
141 /* Set all offsets different. */
142 if (lseek(fd1, (off_t) 2, SEEK_SET) != 2) e(7);
143 if (lseek(fd2, (off_t) 4, SEEK_SET) != 4) e(8);
144 if (lseek(fd3, (off_t) 7, SEEK_SET) != 7) e(9);
146 /* Have a child process do additional offset changes. */
147 switch (fork()) {
148 case -1: printf("Can't fork\n"); break;
149 case 0:
150 alarm(20);
151 if (lseek(fd1, (off_t) 1, SEEK_CUR) != 3) e(10);
152 if (lseek(fd2, (off_t) 5, SEEK_SET) != 5) e(11);
153 if (lseek(fd3, (off_t) - 4, SEEK_END) != 7) e(12);
154 exit(0);
155 default:
156 wait(&stat_loc);
157 if (stat_loc != 0) e(13); /* Alarm? */
160 /* Check if the new offsets are correct. */
161 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 3) e(14);
162 if (lseek(fd2, (off_t) 0, SEEK_CUR) != 5) e(15);
163 if (lseek(fd3, (off_t) 0, SEEK_CUR) != 7) e(16);
165 /* Close the file. */
166 if (close(fd1) != 0) e(17);
167 if (close(fd2) != 0) e(18);
168 if (close(fd3) != 0) e(19);
171 void test26c()
172 { /* Test error returns. */
173 int fd;
174 int tube[2];
175 int i, stat_loc;
177 subtest = 3;
178 System("rm -rf ../DIR_26/*");
180 /* Fifo's can't be lseeked(). */
181 Mkfifo("fifo");
182 switch (fork()) {
183 case -1: printf("Can't fork\n"); break;
184 case 0:
185 alarm(3); /* Try for max 3 secs. */
186 if ((fd = open("fifo", O_RDONLY)) != 3) e(1);
187 if (lseek(fd, (off_t) 0, SEEK_SET) != (off_t) - 1) e(2);
188 if (errno != ESPIPE) e(3);
189 if (close(fd) != 0) e(4);
190 exit(0);
191 default:
192 if ((fd = open("fifo", O_WRONLY)) != 3) e(5);
193 wait(&stat_loc);
194 if (stat_loc != 0) e(6);/* Alarm? */
195 if (close(fd) != 0) e(7);
198 /* Pipes can't be lseeked() eigther. */
199 if (pipe(tube) != 0) e(8);
200 switch (fork()) {
201 case -1: printf("Can't fork\n"); break;
202 case 0:
203 alarm(3); /* Max 3 sconds wait. */
204 if (lseek(tube[0], (off_t) 0, SEEK_SET) != (off_t) - 1) e(9);
205 if (errno != ESPIPE) e(10);
206 if (lseek(tube[1], (off_t) 0, SEEK_SET) != (off_t) - 1) e(11);
207 if (errno != ESPIPE) e(12);
208 exit(0);
209 default:
210 wait(&stat_loc);
211 if (stat_loc != 0) e(14); /* Alarm? */
214 /* Close the pipe. */
215 if (close(tube[0]) != 0) e(15);
216 if (close(tube[1]) != 0) e(16);
218 /* Whence arument invalid. */
219 System("echo -n contact > file");
220 if ((fd = open("file", O_RDWR)) != 3) e(17);
221 for (i = -1000; i < 1000; i++) {
222 if (i == SEEK_SET || i == SEEK_END || i == SEEK_CUR) continue;
223 if (lseek(fd, (off_t) 0, i) != (off_t) -1) e(18);
224 if (errno != EINVAL) e(19);
226 if (close(fd) != 0) e(20);
228 /* EBADF for bad fides. */
229 for (i = -1000; i < 1000; i++) {
230 if (i >= 0 && i < OPEN_MAX) continue;
231 if (lseek(i, (off_t) 0, SEEK_SET) != (off_t) - 1) e(21);
232 if (lseek(i, (off_t) 0, SEEK_END) != (off_t) - 1) e(22);
233 if (lseek(i, (off_t) 0, SEEK_CUR) != (off_t) - 1) e(23);