1 | /* |
---|
2 | * This test exercises the following routines: |
---|
3 | * |
---|
4 | * + readv |
---|
5 | * + writev |
---|
6 | * |
---|
7 | * COPYRIGHT (c) 1989-2009. |
---|
8 | * On-Line Applications Research Corporation (OAR). |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.com/license/LICENSE. |
---|
13 | * |
---|
14 | * $Id$ |
---|
15 | */ |
---|
16 | |
---|
17 | |
---|
18 | #ifdef HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <fcntl.h> |
---|
23 | #include <unistd.h> |
---|
24 | #include <errno.h> |
---|
25 | #include <utime.h> |
---|
26 | #include <string.h> |
---|
27 | #include <inttypes.h> |
---|
28 | |
---|
29 | #include <stdio.h> |
---|
30 | #include <unistd.h> |
---|
31 | #include <sys/uio.h> |
---|
32 | |
---|
33 | #if defined(__rtems__) |
---|
34 | #include <rtems.h> |
---|
35 | #include <rtems/libio.h> |
---|
36 | #include <pmacros.h> |
---|
37 | #else |
---|
38 | #define TRUE 1 |
---|
39 | #define FALSE 0 |
---|
40 | #include <stdlib.h> |
---|
41 | #define rtems_test_exit(_s) exit(_s) |
---|
42 | #endif |
---|
43 | |
---|
44 | #define TESTFILE "testfile1.tst" |
---|
45 | |
---|
46 | |
---|
47 | /* This buffer size is assumed in the iovec initialization below */ |
---|
48 | #define MAX_BUFFER 1000 |
---|
49 | unsigned char PatternBuffer[MAX_BUFFER]; |
---|
50 | unsigned char ReadBuffer[MAX_BUFFER]; |
---|
51 | |
---|
52 | /* |
---|
53 | * fillPatternBuffer function |
---|
54 | * |
---|
55 | * Fill the test buffer. |
---|
56 | * |
---|
57 | * Returns: TRUE if buffer filled |
---|
58 | * FALSE if buffer failed to fill |
---|
59 | * |
---|
60 | */ |
---|
61 | |
---|
62 | int fillPatternBuffer(void) |
---|
63 | { |
---|
64 | int retval = TRUE; |
---|
65 | int i; |
---|
66 | |
---|
67 | for (i=0 ; i<200 ; i++ ) PatternBuffer[i] = 'a'; |
---|
68 | for ( ; i<400 ; i++ ) PatternBuffer[i] = 'b'; |
---|
69 | for ( ; i<600 ; i++ ) PatternBuffer[i] = 'c'; |
---|
70 | for ( ; i<800 ; i++ ) PatternBuffer[i] = 'd'; |
---|
71 | for ( ; i<1000 ; i++ ) PatternBuffer[i] = 'e'; |
---|
72 | return retval; |
---|
73 | } |
---|
74 | |
---|
75 | /* |
---|
76 | * doFunctionalTest function |
---|
77 | * |
---|
78 | * Write a file with writev and then read it back with readv. |
---|
79 | * |
---|
80 | * Returns: TRUE if all operations worked as expected |
---|
81 | * FALSE if an operation did not work as expected. |
---|
82 | * |
---|
83 | */ |
---|
84 | |
---|
85 | int doFunctionalTest(void) { |
---|
86 | FILE *fp; |
---|
87 | int fd; |
---|
88 | struct iovec rdvec[4]; |
---|
89 | struct iovec wrvec[4]; |
---|
90 | int rc; |
---|
91 | |
---|
92 | |
---|
93 | /* |
---|
94 | * Setup the iovec |
---|
95 | */ |
---|
96 | wrvec[0].iov_base = &PatternBuffer[0]; |
---|
97 | wrvec[0].iov_len = 100; |
---|
98 | wrvec[1].iov_base = &PatternBuffer[100]; |
---|
99 | wrvec[1].iov_len = 200; |
---|
100 | wrvec[2].iov_base = &PatternBuffer[300]; |
---|
101 | wrvec[2].iov_len = 300; |
---|
102 | wrvec[3].iov_base = &PatternBuffer[600]; |
---|
103 | wrvec[3].iov_len = 400; |
---|
104 | |
---|
105 | rdvec[0].iov_base = &ReadBuffer[0]; |
---|
106 | rdvec[0].iov_len = 400; |
---|
107 | rdvec[1].iov_base = &ReadBuffer[400]; |
---|
108 | rdvec[1].iov_len = 300; |
---|
109 | rdvec[2].iov_base = &ReadBuffer[700]; |
---|
110 | rdvec[2].iov_len = 200; |
---|
111 | rdvec[3].iov_base = &ReadBuffer[900]; |
---|
112 | rdvec[3].iov_len = 100; |
---|
113 | |
---|
114 | /* |
---|
115 | * Write the File |
---|
116 | */ |
---|
117 | fp = fopen(TESTFILE, "wt"); |
---|
118 | if ( fp == NULL ) { |
---|
119 | printf( "fopen for write: %d=%s\n", errno, strerror(errno)); |
---|
120 | return FALSE; |
---|
121 | } |
---|
122 | fd = fileno(fp); |
---|
123 | |
---|
124 | rc = writev(fd, wrvec, 4); |
---|
125 | if ( rc <= 0 ) { |
---|
126 | printf( "writev: %d=%s\n", errno, strerror(errno) ); |
---|
127 | return FALSE; |
---|
128 | } |
---|
129 | |
---|
130 | fclose(fp); |
---|
131 | |
---|
132 | puts("File written using writev .. OK"); |
---|
133 | |
---|
134 | /* |
---|
135 | * Now read it back and check it |
---|
136 | */ |
---|
137 | |
---|
138 | fp = fopen(TESTFILE, "rt"); |
---|
139 | if ( fp == NULL ) { |
---|
140 | printf( "fopen for write: %d=%s\n", errno, strerror(errno)); |
---|
141 | return FALSE; |
---|
142 | } |
---|
143 | fd = fileno(fp); |
---|
144 | |
---|
145 | rc = readv(fd, rdvec, 4); |
---|
146 | if ( rc <= 0 ) { |
---|
147 | printf( "rd: %d=%s\n", errno, strerror(errno) ); |
---|
148 | return FALSE; |
---|
149 | } |
---|
150 | |
---|
151 | if ( memcmp( PatternBuffer, ReadBuffer, MAX_BUFFER ) ) { |
---|
152 | puts("readv .. Buffers do not match"); |
---|
153 | return FALSE; |
---|
154 | } |
---|
155 | |
---|
156 | puts("File read using readv .. OK"); |
---|
157 | |
---|
158 | return TRUE; |
---|
159 | } |
---|
160 | |
---|
161 | /* |
---|
162 | * doErrorTest function |
---|
163 | * |
---|
164 | * Hit all the error cases in readv/writev. |
---|
165 | * |
---|
166 | * Returns: TRUE if all operations worked as expected |
---|
167 | * FALSE if an operation did not work as expected. |
---|
168 | * |
---|
169 | */ |
---|
170 | |
---|
171 | int doErrorTest(void) |
---|
172 | { |
---|
173 | FILE *fp; |
---|
174 | int fd; |
---|
175 | struct iovec vec[4]; |
---|
176 | int rc; |
---|
177 | |
---|
178 | /* |
---|
179 | * Open and close the file to get a bad file descriptor |
---|
180 | */ |
---|
181 | fp = fopen(TESTFILE, "wt"); |
---|
182 | if ( fp == NULL ) { |
---|
183 | printf( "fopen for error 1: %d=%s\n", errno, strerror(errno)); |
---|
184 | return FALSE; |
---|
185 | } |
---|
186 | fd = fileno(fp); |
---|
187 | fclose(fp); |
---|
188 | |
---|
189 | /* writev -- bad file descriptor */ |
---|
190 | puts("writev bad file descriptor -- EBADF"); |
---|
191 | rc = writev(fd, vec, 4); |
---|
192 | if ( (rc != -1) || (errno != EBADF) ) { |
---|
193 | printf( "writev error 1: %d=%s\n", errno, strerror(errno) ); |
---|
194 | return FALSE; |
---|
195 | } |
---|
196 | |
---|
197 | /* readv -- bad file descriptor */ |
---|
198 | puts("readv bad file descriptor -- EBADF"); |
---|
199 | rc = read(fd, vec, 4); |
---|
200 | if ( (rc != -1) || (errno != EBADF) ) { |
---|
201 | printf( "readv error 1: %d=%s\n", errno, strerror(errno) ); |
---|
202 | return FALSE; |
---|
203 | } |
---|
204 | |
---|
205 | /* |
---|
206 | * Open the file for the rest of the tests |
---|
207 | */ |
---|
208 | fp = fopen(TESTFILE, "w+"); |
---|
209 | if ( fp == NULL ) { |
---|
210 | printf( "fopen for error 2: %d=%s\n", errno, strerror(errno)); |
---|
211 | return FALSE; |
---|
212 | } |
---|
213 | fd = fileno(fp); |
---|
214 | |
---|
215 | /* writev -- bad iovec pointer */ |
---|
216 | puts("writev bad iovec pointer -- EINVAL"); |
---|
217 | rc = writev(fd, NULL, 4); |
---|
218 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
219 | printf( "writev error 2: %d=%s\n", errno, strerror(errno) ); |
---|
220 | fclose(fp); |
---|
221 | return FALSE; |
---|
222 | } |
---|
223 | |
---|
224 | /* readv -- bad iovec pointer */ |
---|
225 | puts("readv bad iovec pointer -- EINVAL"); |
---|
226 | rc = readv(fd, NULL, 4); |
---|
227 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
228 | printf( "readv error 2: %d=%s\n", errno, strerror(errno) ); |
---|
229 | fclose(fp); |
---|
230 | return FALSE; |
---|
231 | } |
---|
232 | |
---|
233 | /* writev -- bad iovcnt 0 */ |
---|
234 | puts("readv bad iovcnt of 0 -- EINVAL"); |
---|
235 | rc = writev(fd, vec, 0); |
---|
236 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
237 | printf( "writev error 3: %d=%s\n", errno, strerror(errno) ); |
---|
238 | fclose(fp); |
---|
239 | return FALSE; |
---|
240 | } |
---|
241 | |
---|
242 | /* readv -- bad iovcnt 0 */ |
---|
243 | puts("readv bad iovcnt of 0 -- EINVAL"); |
---|
244 | rc = readv(fd, vec, 0); |
---|
245 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
246 | printf( "readv error 3: %d=%s\n", errno, strerror(errno) ); |
---|
247 | fclose(fp); |
---|
248 | return FALSE; |
---|
249 | } |
---|
250 | |
---|
251 | /* writev -- bad iovcnt negative */ |
---|
252 | puts("writev bad iovcnt negative -- EINVAL"); |
---|
253 | rc = writev(fd, vec, -2); |
---|
254 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
255 | printf( "writev error 4: %d=%s\n", errno, strerror(errno) ); |
---|
256 | fclose(fp); |
---|
257 | return FALSE; |
---|
258 | } |
---|
259 | |
---|
260 | /* readv -- bad iovcnt negative */ |
---|
261 | puts("readv bad iovcnt negative -- EINVAL"); |
---|
262 | rc = readv(fd, vec, -100); |
---|
263 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
264 | printf( "readv error 4: %d=%s\n", errno, strerror(errno) ); |
---|
265 | fclose(fp); |
---|
266 | return FALSE; |
---|
267 | } |
---|
268 | |
---|
269 | /* writev -- bad iov[i].iov_base */ |
---|
270 | vec[0].iov_base = vec; |
---|
271 | vec[0].iov_len = 100; |
---|
272 | vec[1].iov_base = NULL; |
---|
273 | vec[1].iov_len = 100; |
---|
274 | puts("writev bad iov[i].iov_base -- EINVAL"); |
---|
275 | rc = writev(fd, vec, 2); |
---|
276 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
277 | printf( "writev error 5: %d=%s\n", errno, strerror(errno) ); |
---|
278 | fclose(fp); |
---|
279 | return FALSE; |
---|
280 | } |
---|
281 | |
---|
282 | /* readv -- bad iov[i].iov_base */ |
---|
283 | vec[0].iov_base = vec; |
---|
284 | vec[0].iov_len = 100; |
---|
285 | vec[1].iov_base = NULL; |
---|
286 | vec[1].iov_len = 100; |
---|
287 | puts("readv bad iov[i].iov_base -- EINVAL"); |
---|
288 | rc = readv(fd, vec, 2); |
---|
289 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
290 | printf( "readv error 5: %d=%s\n", errno, strerror(errno) ); |
---|
291 | fclose(fp); |
---|
292 | return FALSE; |
---|
293 | } |
---|
294 | |
---|
295 | /* writev -- bad iov[i].iov_len < 0 */ |
---|
296 | vec[0].iov_base = vec; |
---|
297 | vec[0].iov_len = 100; |
---|
298 | vec[1].iov_base = vec; |
---|
299 | vec[1].iov_len = -10; |
---|
300 | puts("writev bad iov[i].iov_len < 0 -- EINVAL"); |
---|
301 | rc = writev(fd, vec, 2); |
---|
302 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
303 | printf( "writev error 6: %d=%s\n", errno, strerror(errno) ); |
---|
304 | fclose(fp); |
---|
305 | return FALSE; |
---|
306 | } |
---|
307 | |
---|
308 | /* readv -- bad iov[i].iov_len = 0 */ |
---|
309 | vec[0].iov_base = vec; |
---|
310 | vec[0].iov_len = 100; |
---|
311 | vec[1].iov_base = vec; |
---|
312 | vec[1].iov_len = -1024; |
---|
313 | puts("readv bad iov[i].iov_len = 0 -- EINVAL"); |
---|
314 | rc = readv(fd, vec, 2); |
---|
315 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
316 | printf( "readv error 6: %d=%s\n", errno, strerror(errno) ); |
---|
317 | fclose(fp); |
---|
318 | return FALSE; |
---|
319 | } |
---|
320 | |
---|
321 | /* writev -- iov_len total overflows */ |
---|
322 | vec[0].iov_base = vec; |
---|
323 | vec[0].iov_len = SIZE_MAX; |
---|
324 | vec[1].iov_base = vec; |
---|
325 | vec[1].iov_len = SIZE_MAX; |
---|
326 | vec[2].iov_base = vec; |
---|
327 | vec[2].iov_len = SIZE_MAX; |
---|
328 | puts("writev iov_len total overflows -- EINVAL"); |
---|
329 | rc = writev(fd, vec, 3); |
---|
330 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
331 | printf( "writev error 7: rc=%d %d=%s\n", rc, errno, strerror(errno) ); |
---|
332 | fclose(fp); |
---|
333 | return FALSE; |
---|
334 | } |
---|
335 | |
---|
336 | /* readv -- iov_len total overflows */ |
---|
337 | vec[0].iov_base = vec; |
---|
338 | vec[0].iov_len = SIZE_MAX; |
---|
339 | vec[1].iov_base = vec; |
---|
340 | vec[1].iov_len = SIZE_MAX; |
---|
341 | puts("readv iov_len total overflows -- EINVAL"); |
---|
342 | rc = readv(fd, vec, 2); |
---|
343 | if ( (rc != -1) || (errno != EINVAL) ) { |
---|
344 | printf( "read error 7: rc=%d %d=%s\n", rc, errno, strerror(errno) ); |
---|
345 | fclose(fp); |
---|
346 | return FALSE; |
---|
347 | } |
---|
348 | |
---|
349 | /* writev -- all zero length buffers */ |
---|
350 | vec[0].iov_base = vec; |
---|
351 | vec[0].iov_len = 0; |
---|
352 | vec[1].iov_base = vec; |
---|
353 | vec[1].iov_len = 0; |
---|
354 | puts("writev iov_len works with no effect -- OK"); |
---|
355 | rc = writev(fd, vec, 2); |
---|
356 | if ( (rc != 0) ) { |
---|
357 | printf( "writev error 8: %d=%s\n", errno, strerror(errno) ); |
---|
358 | fclose(fp); |
---|
359 | return FALSE; |
---|
360 | } |
---|
361 | |
---|
362 | /* readv -- all zero length buffers */ |
---|
363 | vec[0].iov_base = vec; |
---|
364 | vec[0].iov_len = 0; |
---|
365 | vec[1].iov_base = vec; |
---|
366 | vec[1].iov_len = 0; |
---|
367 | puts("readv iov_len works with no effect -- OK"); |
---|
368 | rc = readv(fd, vec, 2); |
---|
369 | if ( (rc != 0) ) { |
---|
370 | printf( "readv error 8: %d=%s\n", errno, strerror(errno) ); |
---|
371 | fclose(fp); |
---|
372 | return FALSE; |
---|
373 | } |
---|
374 | |
---|
375 | fclose(fp); |
---|
376 | return TRUE; |
---|
377 | } |
---|
378 | |
---|
379 | |
---|
380 | /* --------------------------------------------------------------- |
---|
381 | * Main function |
---|
382 | * |
---|
383 | * main entry point to the test |
---|
384 | * |
---|
385 | * --------------------------------------------------------------- |
---|
386 | */ |
---|
387 | |
---|
388 | #if defined(__rtems__) |
---|
389 | int test_main(void) |
---|
390 | #else |
---|
391 | int main( |
---|
392 | int argc, |
---|
393 | char **argv |
---|
394 | ) |
---|
395 | #endif |
---|
396 | { |
---|
397 | puts( "*** POSIX TEST READV/WRITEV ***" ); |
---|
398 | |
---|
399 | if ( fillPatternBuffer() != TRUE ) { |
---|
400 | puts("Error filling pattern buffer" ); |
---|
401 | rtems_test_exit(0); |
---|
402 | } |
---|
403 | |
---|
404 | if (doErrorTest() != TRUE) { |
---|
405 | puts("Error during error test!!!!"); |
---|
406 | rtems_test_exit(0); |
---|
407 | } |
---|
408 | if (doFunctionalTest() != TRUE) { |
---|
409 | puts("Error during functional test!!!!"); |
---|
410 | rtems_test_exit(0); |
---|
411 | } |
---|
412 | |
---|
413 | unlink(TESTFILE); |
---|
414 | puts( "*** END OF TEST PSXRDWRV ***" ); |
---|
415 | rtems_test_exit(0); |
---|
416 | } |
---|