source: trunk/minix/test/test26.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

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