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