source: rtems/testsuites/psxtests/psxrdwrv/test.c

Last change on this file was 5594854, checked in by Joel Sherrill <joel@…>, on 04/07/22 at 15:05:28

testsuites/psxtests/psx[n-z]*: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 11.6 KB
RevLine 
[5594854]1/* SPDX-License-Identifier: BSD-2-Clause */
2
[6c2de60]3/**
4 *  @file
5 *
[41f17ba]6 *  This test exercises the following routines:
7 *
[161f54b4]8 *    + readv
9 *    + writev
[6c2de60]10 */
11
12/*
13 *  COPYRIGHT (c) 1989-2012.
[41f17ba]14 *  On-Line Applications Research Corporation (OAR).
15 *
[5594854]16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 *    notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
[41f17ba]36 */
37
38
[cafefbf]39#ifdef HAVE_CONFIG_H
40#include "config.h"
41#endif
42
[41f17ba]43#include <fcntl.h>
44#include <unistd.h>
45#include <errno.h>
46#include <utime.h>
47#include <string.h>
48#include <inttypes.h>
[95a57280]49#include <limits.h>
[41f17ba]50
51#include <stdio.h>
52#include <unistd.h>
53#include <sys/uio.h>
54
55#if defined(__rtems__)
56  #include <rtems.h>
57  #include <rtems/libio.h>
58  #include <pmacros.h>
[698c2e50]59
60  const char rtems_test_name[] = "PSXRDWRV";
[41f17ba]61#else
62  #define TRUE  1
63  #define FALSE 0
64  #include <stdlib.h>
65  #define rtems_test_exit(_s) exit(_s)
66#endif
67
[6c2de60]68/* forward declarations to avoid warnings */
69int test_main(void);
70int fillPatternBuffer(void);
71int doFunctionalTest(void);
72int doErrorTest(void);
[41f17ba]73
[6c2de60]74#define TESTFILE "testfile1.tst"
[41f17ba]75
76/* This buffer size is assumed in the iovec initialization below */
77#define MAX_BUFFER 1000
78unsigned char PatternBuffer[MAX_BUFFER];
79unsigned char ReadBuffer[MAX_BUFFER];
80
81/*
82 * fillPatternBuffer function
83 *
84 * Fill the test buffer.
85 *
[b1274bd9]86 * Returns: TRUE if buffer filled
[41f17ba]87 *          FALSE if buffer failed to fill
88 *
89 */
90int fillPatternBuffer(void)
91{
92  int retval = TRUE;
93  int i;
94
95  for (i=0 ; i<200  ; i++ ) PatternBuffer[i] = 'a';
96  for (    ; i<400  ; i++ ) PatternBuffer[i] = 'b';
97  for (    ; i<600  ; i++ ) PatternBuffer[i] = 'c';
98  for (    ; i<800  ; i++ ) PatternBuffer[i] = 'd';
99  for (    ; i<1000 ; i++ ) PatternBuffer[i] = 'e';
100  return retval;
101}
102
103/*
104 * doFunctionalTest function
105 *
106 * Write a file with writev and then read it back with readv.
107 *
108 * Returns: TRUE if all operations worked as expected
109 *          FALSE if an operation did not work as expected.
110 *
111 */
[6c2de60]112int doFunctionalTest(void)
113{
[41f17ba]114  FILE         *fp;
115  int           fd;
116  struct iovec  rdvec[4];
117  struct iovec  wrvec[4];
118  int           rc;
119
[b1274bd9]120
[41f17ba]121  /*
122   * Setup the iovec
123   */
124  wrvec[0].iov_base = &PatternBuffer[0];
125  wrvec[0].iov_len  = 100;
126  wrvec[1].iov_base = &PatternBuffer[100];
127  wrvec[1].iov_len  = 200;
128  wrvec[2].iov_base = &PatternBuffer[300];
129  wrvec[2].iov_len  = 300;
130  wrvec[3].iov_base = &PatternBuffer[600];
131  wrvec[3].iov_len  = 400;
132
133  rdvec[0].iov_base = &ReadBuffer[0];
134  rdvec[0].iov_len  = 400;
135  rdvec[1].iov_base = &ReadBuffer[400];
136  rdvec[1].iov_len  = 300;
137  rdvec[2].iov_base = &ReadBuffer[700];
138  rdvec[2].iov_len  = 200;
139  rdvec[3].iov_base = &ReadBuffer[900];
140  rdvec[3].iov_len  = 100;
141
142  /*
143   * Write the File
144   */
145  fp = fopen(TESTFILE, "wt");
146  if ( fp == NULL ) {
147    printf( "fopen for write: %d=%s\n", errno, strerror(errno));
148    return FALSE;
149  }
150  fd = fileno(fp);
151
152  rc = writev(fd, wrvec, 4);
153  if ( rc <= 0 ) {
154    printf( "writev: %d=%s\n", errno, strerror(errno) );
155    return FALSE;
156  }
157
158  fclose(fp);
159
160  puts("File written using writev .. OK");
161
162  /*
163   * Now read it back and check it
164   */
165
166  fp = fopen(TESTFILE, "rt");
167  if ( fp == NULL ) {
168    printf( "fopen for write: %d=%s\n", errno, strerror(errno));
169    return FALSE;
170  }
171  fd = fileno(fp);
172
173  rc = readv(fd, rdvec, 4);
174  if ( rc <= 0 ) {
175    printf( "rd: %d=%s\n", errno, strerror(errno) );
176    return FALSE;
177  }
178
179  if ( memcmp( PatternBuffer, ReadBuffer, MAX_BUFFER ) ) {
180    puts("readv .. Buffers do not match");
181    return FALSE;
182  }
183
184  puts("File read using readv .. OK");
185
186  return TRUE;
187}
188
189/*
190 * doErrorTest function
191 *
192 * Hit all the error cases in readv/writev.
193 *
194 * Returns: TRUE if all operations worked as expected
195 *          FALSE if an operation did not work as expected.
196 *
197 */
198int doErrorTest(void)
199{
200  FILE         *fp;
201  int           fd;
202  struct iovec  vec[4];
203  int           rc;
204
205  /*
206   * Open and close the file to get a bad file descriptor
207   */
208  fp = fopen(TESTFILE, "wt");
209  if ( fp == NULL ) {
210    printf( "fopen for error 1: %d=%s\n", errno, strerror(errno));
211    return FALSE;
212  }
213  fd = fileno(fp);
214  fclose(fp);
215
[714cb06]216  /* clear out the vector to ensure it is valid */
217  memset(vec, 0, sizeof(vec));
218
[41f17ba]219  /* writev -- bad file descriptor */
220  puts("writev bad file descriptor -- EBADF");
221  rc = writev(fd, vec, 4);
222  if ( (rc != -1) || (errno != EBADF) ) {
223    printf( "writev error 1: %d=%s\n", errno, strerror(errno) );
224    return FALSE;
225  }
226
227  /* readv -- bad file descriptor */
228  puts("readv bad file descriptor -- EBADF");
229  rc = read(fd, vec, 4);
230  if ( (rc != -1) || (errno != EBADF) ) {
231    printf( "readv error 1: %d=%s\n", errno, strerror(errno) );
232    return FALSE;
233  }
234
235  /*
236   * Open the file for the rest of the tests
237   */
[161f54b4]238  fp = fopen(TESTFILE, "w+");
[41f17ba]239  if ( fp == NULL ) {
240    printf( "fopen for error 2: %d=%s\n", errno, strerror(errno));
241    return FALSE;
242  }
243  fd = fileno(fp);
244
[95a57280]245#ifdef __rtems__
[41f17ba]246  /* writev --  bad iovec pointer */
247  puts("writev bad iovec pointer -- EINVAL");
248  rc = writev(fd, NULL, 4);
249  if ( (rc != -1) || (errno != EINVAL) ) {
250    printf( "writev error 2: %d=%s\n", errno, strerror(errno) );
[b1274bd9]251    fclose(fp);
[41f17ba]252    return FALSE;
253  }
254
255  /* readv --  bad iovec pointer */
256  puts("readv bad iovec pointer -- EINVAL");
257  rc = readv(fd, NULL, 4);
258  if ( (rc != -1) || (errno != EINVAL) ) {
259    printf( "readv error 2: %d=%s\n", errno, strerror(errno) );
[b1274bd9]260    fclose(fp);
[41f17ba]261    return FALSE;
262  }
263
264  /* writev --  bad iovcnt 0 */
[95a57280]265  puts("writev bad iovcnt of 0 -- EINVAL");
[41f17ba]266  rc = writev(fd, vec, 0);
267  if ( (rc != -1) || (errno != EINVAL) ) {
268    printf( "writev error 3: %d=%s\n", errno, strerror(errno) );
[b1274bd9]269    fclose(fp);
[41f17ba]270    return FALSE;
271  }
272
273  /* readv --  bad iovcnt 0 */
274  puts("readv bad iovcnt of 0 -- EINVAL");
275  rc = readv(fd, vec, 0);
276  if ( (rc != -1) || (errno != EINVAL) ) {
277    printf( "readv error 3: %d=%s\n", errno, strerror(errno) );
[b1274bd9]278    fclose(fp);
[41f17ba]279    return FALSE;
280  }
[95a57280]281#endif /* __rtems__ */
[41f17ba]282
283  /* writev --  bad iovcnt negative */
284  puts("writev bad iovcnt negative -- EINVAL");
285  rc = writev(fd, vec, -2);
286  if ( (rc != -1) || (errno != EINVAL) ) {
287    printf( "writev error 4: %d=%s\n", errno, strerror(errno) );
[b1274bd9]288    fclose(fp);
[41f17ba]289    return FALSE;
290  }
291
292  /* readv --  bad iovcnt negative */
293  puts("readv bad iovcnt negative -- EINVAL");
294  rc = readv(fd, vec, -100);
295  if ( (rc != -1) || (errno != EINVAL) ) {
296    printf( "readv error 4: %d=%s\n", errno, strerror(errno) );
[b1274bd9]297    fclose(fp);
[41f17ba]298    return FALSE;
299  }
300
[95a57280]301#ifdef __rtems__
[41f17ba]302  /* writev --  bad iov[i].iov_base */
303  vec[0].iov_base = vec;
304  vec[0].iov_len = 100;
305  vec[1].iov_base = NULL;
306  vec[1].iov_len = 100;
307  puts("writev bad iov[i].iov_base -- EINVAL");
308  rc = writev(fd, vec, 2);
309  if ( (rc != -1) || (errno != EINVAL) ) {
310    printf( "writev error 5: %d=%s\n", errno, strerror(errno) );
[b1274bd9]311    fclose(fp);
[41f17ba]312    return FALSE;
313  }
314
315  /*  readv --  bad iov[i].iov_base */
316  vec[0].iov_base = vec;
317  vec[0].iov_len = 100;
318  vec[1].iov_base = NULL;
319  vec[1].iov_len = 100;
320  puts("readv bad iov[i].iov_base -- EINVAL");
321  rc = readv(fd, vec, 2);
322  if ( (rc != -1) || (errno != EINVAL) ) {
323    printf( "readv error 5: %d=%s\n", errno, strerror(errno) );
[b1274bd9]324    fclose(fp);
[41f17ba]325    return FALSE;
326  }
[95a57280]327#endif /* __rtems__ */
[41f17ba]328
329  /*  writev --  bad iov[i].iov_len < 0 */
330  vec[0].iov_base = vec;
331  vec[0].iov_len = 100;
332  vec[1].iov_base = vec;
333  vec[1].iov_len = -10;
334  puts("writev bad iov[i].iov_len < 0 -- EINVAL");
335  rc = writev(fd, vec, 2);
336  if ( (rc != -1) || (errno != EINVAL) ) {
337    printf( "writev error 6: %d=%s\n", errno, strerror(errno) );
[b1274bd9]338    fclose(fp);
[41f17ba]339    return FALSE;
340  }
341
[95a57280]342  /*  readv --  bad iov[i].iov_len < 0 */
[41f17ba]343  vec[0].iov_base = vec;
344  vec[0].iov_len = 100;
345  vec[1].iov_base = vec;
346  vec[1].iov_len = -1024;
[95a57280]347  puts("readv bad iov[i].iov_len < 0 -- EINVAL");
[41f17ba]348  rc = readv(fd, vec, 2);
349  if ( (rc != -1) || (errno != EINVAL) ) {
350    printf( "readv error 6: %d=%s\n", errno, strerror(errno) );
[b1274bd9]351    fclose(fp);
[41f17ba]352    return FALSE;
353  }
354
355  /*  writev --  iov_len total overflows */
356  vec[0].iov_base = vec;
[18b3b581]357  vec[0].iov_len = SIZE_MAX;
[41f17ba]358  vec[1].iov_base = vec;
[18b3b581]359  vec[1].iov_len = SIZE_MAX;
[41f17ba]360  vec[2].iov_base = vec;
[18b3b581]361  vec[2].iov_len = SIZE_MAX;
[41f17ba]362  puts("writev iov_len total overflows -- EINVAL");
363  rc = writev(fd, vec, 3);
364  if ( (rc != -1) || (errno != EINVAL) ) {
[161f54b4]365    printf( "writev error 7: rc=%d %d=%s\n", rc, errno, strerror(errno) );
[b1274bd9]366    fclose(fp);
[41f17ba]367    return FALSE;
368  }
369
370  /*  readv --  iov_len total overflows */
371  vec[0].iov_base = vec;
[18b3b581]372  vec[0].iov_len = SIZE_MAX;
[41f17ba]373  vec[1].iov_base = vec;
[18b3b581]374  vec[1].iov_len = SIZE_MAX;
[95a57280]375  vec[2].iov_base = vec;
376  vec[2].iov_len = SIZE_MAX;
[41f17ba]377  puts("readv iov_len total overflows -- EINVAL");
[95a57280]378  rc = readv(fd, vec, 3);
[41f17ba]379  if ( (rc != -1) || (errno != EINVAL) ) {
[161f54b4]380    printf( "read error 7: rc=%d %d=%s\n", rc, errno, strerror(errno) );
[b1274bd9]381    fclose(fp);
[41f17ba]382    return FALSE;
383  }
384
385  /*  writev --  all zero length buffers */
386  vec[0].iov_base = vec;
387  vec[0].iov_len = 0;
[c1d8ee4]388  vec[1].iov_base = NULL;
[41f17ba]389  vec[1].iov_len = 0;
390  puts("writev iov_len works with no effect -- OK");
391  rc = writev(fd, vec, 2);
392  if ( (rc != 0) ) {
393    printf( "writev error 8: %d=%s\n", errno, strerror(errno) );
[b1274bd9]394    fclose(fp);
[41f17ba]395    return FALSE;
396  }
397
398  /*  readv --  all zero length buffers */
399  vec[0].iov_base = vec;
400  vec[0].iov_len = 0;
[c1d8ee4]401  vec[1].iov_base = NULL;
[41f17ba]402  vec[1].iov_len = 0;
403  puts("readv iov_len works with no effect -- OK");
404  rc = readv(fd, vec, 2);
405  if ( (rc != 0) ) {
406    printf( "readv error 8: %d=%s\n", errno, strerror(errno) );
[b1274bd9]407    fclose(fp);
[41f17ba]408    return FALSE;
409  }
410
[95a57280]411#ifdef __rtems__
412  puts("readv bad iovcnt of IOV_MAX + 1 -- EINVAL");
413  rc = readv(fd, vec, IOV_MAX + 1);
414  if ( (rc != -1) || (errno != EINVAL) ) {
415    printf( "readv error 9: %d=%s\n", errno, strerror(errno) );
416    fclose(fp);
417    return FALSE;
418  }
419
420  puts("writev bad iovcnt of IOV_MAX + 1 -- EINVAL");
421  rc = writev(fd, vec, IOV_MAX + 1);
422  if ( (rc != -1) || (errno != EINVAL) ) {
423    printf( "writev error 9: %d=%s\n", errno, strerror(errno) );
424    fclose(fp);
425    return FALSE;
426  }
427#endif /* __rtems__ */
428
[41f17ba]429  fclose(fp);
430  return TRUE;
431}
432
433
434/* ---------------------------------------------------------------
435 * Main function
436 *
437 *  main entry point to the test
438 *
439 * ---------------------------------------------------------------
440 */
441
442#if defined(__rtems__)
443int test_main(void)
444#else
445int main(
446  int    argc,
447  char **argv
448)
449#endif
450{
[698c2e50]451  TEST_BEGIN();
[41f17ba]452
453  if ( fillPatternBuffer() != TRUE ) {
454    puts("Error filling pattern buffer" );
455    rtems_test_exit(0);
456  }
457
458  if (doErrorTest() != TRUE) {
459    puts("Error during error test!!!!");
460    rtems_test_exit(0);
461  }
462  if (doFunctionalTest() != TRUE) {
463    puts("Error during functional test!!!!");
464    rtems_test_exit(0);
465  }
466
467  unlink(TESTFILE);
[698c2e50]468  TEST_END();
[41f17ba]469  rtems_test_exit(0);
470}
Note: See TracBrowser for help on using the repository browser.