source: rtems/testsuites/psxtests/psxrdwrv/test.c @ 2b36355b

4.115
Last change on this file since 2b36355b was 6c2de60, checked in by Joel Sherrill <joel.sherrill@…>, on 05/11/12 at 19:12:11

psxtests - Eliminate missing prototype warnings

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