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