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