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