/* * setup() - performs all ONE TIME setup for this test. */ void setup() { int nbytes; tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; /* make a temporary directory and cd to it */ tst_tmpdir(); buf_list[0] = buf1; buf_list[1] = buf2; buf_list[2] = buf3; buf_list[3] = NULL; init_buffs(buf_list); sprintf(f_name, "%s.%d", DATA_FILE, getpid()); if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) { tst_brkm(TBROK, cleanup, "open failed: fname = %s, " "errno = %d", f_name, errno); } else { if ((nbytes = write(fd[0], buf_list[2], K_1)) != K_1) { tst_brkm(TBROK, cleanup, "write failed: nbytes " "= %d " "errno = %d", nbytes, errno); } } if (close(fd[0]) < 0) { tst_brkm(TBROK, cleanup, "close failed: errno = %d", errno); } if ((fd[0] = open(f_name, O_RDONLY, 0666)) < 0) { tst_brkm(TBROK, cleanup, "open failed: fname = %s, " "errno = %d", f_name, errno); } fd[1] = -1; /* Invalid file descriptor */ bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); if (bad_addr == MAP_FAILED) { tst_brkm(TBROK, cleanup, "mmap failed"); } rd_iovec[6].iov_base = bad_addr; }
/* * setup() - performs all ONE TIME setup for this test. */ void setup() { int nbytes; tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; /* make a temporary directory and cd to it */ tst_tmpdir(); buf_list[0] = buf1; buf_list[1] = buf2; buf_list[2] = buf3; buf_list[3] = NULL; init_buffs(buf_list); sprintf(f_name, "%s.%d", DATA_FILE, getpid()); if ((fd = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) { tst_brkm(TBROK, cleanup, "open failed: fname = %s, " "errno = %d", f_name, errno); } else { if ((nbytes = write(fd, buf_list[2], K_1)) != K_1) { tst_brkm(TBROK, cleanup, "write failed: nbytes " "= %d errno = %d", nbytes, errno); } } if (close(fd) < 0) { tst_brkm(TBROK, cleanup, "close failed: errno = %d", errno); } if ((fd = open(f_name, O_RDONLY, 0666)) < 0) { tst_brkm(TBROK, cleanup, "open failed: fname = %s, " "errno = %d", f_name, errno); } }
int main(int argc, char **argv) { int nbytes, ret; int lc; char *msg; if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { Tst_count = 0; buf_list[0] = buf1; buf_list[1] = buf2; buf_list[2] = buf3; buf_list[3] = NULL; fd[1] = -1; /* Invalid file descriptor */ if (signal(SIGTERM, sighandler) == SIG_ERR) tst_brkm(TBROK | TERRNO, cleanup, "signal(SIGTERM, ..) failed"); if (signal(SIGPIPE, sighandler) == SIG_ERR) tst_brkm(TBROK | TERRNO, cleanup, "signal(SIGPIPE, ..) failed"); init_buffs(buf_list); if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) == -1) tst_brkm(TBROK | TERRNO, cleanup, "open(.., O_WRONLY|O_CREAT, ..) failed"); else if ((nbytes = write(fd[0], buf_list[2], K_1)) != K_1) tst_brkm(TBROK | TERRNO, cleanup, "write failed"); if (close(fd[0]) == -1) tst_brkm(TBROK | TERRNO, cleanup, "close failed"); if ((fd[0] = open(f_name, O_RDWR, 0666)) == -1) tst_brkm(TBROK | TERRNO, cleanup, "open failed"); //block1: /* given vector length -1, writev return EINVAL. */ tst_resm(TPASS, "Enter Block 1"); TEST(writev(fd[0], wr_iovec, 1)); if (TEST_RETURN == -1) { if (TEST_ERRNO == EINVAL) tst_resm(TPASS, "Received EINVAL as expected"); else tst_resm(TFAIL, "Expected errno = EINVAL, " "got %d", TEST_ERRNO); } else tst_resm(TFAIL, "writev failed to fail"); tst_resm(TINFO, "Exit block 1"); //block2: /* This testcases doesn't look like what it intent to do * 1. it is not using the wr_iovec initialized * 2. read() and following message is not consistent */ tst_resm(TPASS, "Enter block 2"); if (lseek(fd[0], CHUNK * 6, 0) == -1) tst_resm(TBROK | TERRNO, "block2: 1st lseek failed"); if ((ret = writev(fd[0], (wr_iovec + 6), 3)) == CHUNK) { if (lseek(fd[0], CHUNK * 6, 0) == -1) tst_brkm(TBROK | TERRNO, cleanup, "block2: 2nd lseek failed"); if ((nbytes = read(fd[0], buf_list[0], CHUNK)) != CHUNK) tst_resm(TFAIL, "read failed; expected nbytes " "= 1024, got = %d", nbytes); else if (memcmp((buf_list[0] + CHUNK * 6), (buf_list[2] + CHUNK * 6), CHUNK) != 0) tst_resm(TFAIL, "writev over " "wrote %s", f_name); } else tst_resm(TFAIL | TERRNO, "writev failed unexpectedly"); tst_resm(TINFO, "Exit block 2"); //block3: /* given 1 bad vector buffer with good ones, writev success */ tst_resm(TPASS, "Enter block 3"); if (lseek(fd[0], CHUNK * 6, 0) == -1) tst_brkm(TBROK | TERRNO, cleanup, "block3: 1st lseek failed"); if ((nbytes = writev(fd[0], (wr_iovec + 6), 3)) == -1) { if (errno == EFAULT) tst_resm(TFAIL, "Got EFAULT"); } if (lseek(fd[0], 0, 0) == -1) tst_brkm(TBROK, cleanup, "block3: 2nd lseek failed"); if ((nbytes = read(fd[0], buf_list[0], K_1)) != K_1) { tst_resm(TFAIL | TERRNO, "read failed; expected nbytes = 1024, got = %d", nbytes); } else if (memcmp((buf_list[0] + CHUNK * 6), (buf_list[2] + CHUNK * 6), CHUNK * 3) != 0) tst_resm(TFAIL, "writev overwrote file"); tst_resm(TINFO, "Exit block 3"); //block4: /* given bad file discriptor, writev return EBADF. */ tst_resm(TPASS, "Enter block 4"); TEST(writev(fd[1], (wr_iovec + 9), 1)); if (TEST_RETURN == -1) { if (TEST_ERRNO == EBADF) tst_resm(TPASS, "Received EBADF as expected"); else tst_resm(TFAIL, "expected errno = EBADF, " "got %d", TEST_ERRNO); } else tst_resm(TFAIL, "writev returned a " "positive value"); tst_resm(TINFO, "Exit block 4"); //block5: /* given invalid vector count, writev return EINVAL */ tst_resm(TPASS, "Enter block 5"); TEST(writev(fd[0], (wr_iovec + 10), -1)); if (TEST_RETURN == -1) { if (TEST_ERRNO == EINVAL) tst_resm(TPASS, "Received EINVAL as expected"); else tst_resm(TFAIL, "expected errno = EINVAL, " "got %d", TEST_ERRNO); } else tst_resm(TFAIL, "writev returned a " "positive value"); tst_resm(TINFO, "Exit block 5"); //block6: /* given no buffer vector, writev success */ tst_resm(TPASS, "Enter block 6"); TEST(writev(fd[0], (wr_iovec + 11), 0)); if (TEST_RETURN == -1) tst_resm(TFAIL | TTERRNO, "writev failed"); else tst_resm(TPASS, "writev wrote 0 iovectors"); tst_resm(TINFO, "Exit block 6"); //block7: /* given 4 vectors, 2 are NULL, 1 with 0 length and 1 with fixed length, * writev success writing fixed length. */ tst_resm(TPASS, "Enter block 7"); if (lseek(fd[0], CHUNK * 12, 0) == -1) tst_resm(TBROK, "lseek failed"); else if ((ret = writev(fd[0], (wr_iovec + 12), 4)) != CHUNK) tst_resm(TFAIL, "writev failed writing %d bytes, " "followed by two NULL vectors", CHUNK); else tst_resm(TPASS, "writev passed writing %d bytes, " "followed by two NULL vectors", CHUNK); tst_resm(TINFO, "Exit block 7"); //block8: /* try to write to a closed pipe, writev return EPIPE. */ tst_resm(TPASS, "Enter block 8"); if (pipe(pfd) == -1) tst_resm(TFAIL | TERRNO, "pipe failed"); else { if (close(pfd[0]) == -1) tst_resm(TFAIL | TERRNO, "close failed"); else if (writev(pfd[1], (wr_iovec + 12), 1) == -1 && in_sighandler) { if (errno == EPIPE) tst_resm(TPASS, "Received EPIPE as " "expected"); else tst_resm(TFAIL | TERRNO, "didn't get EPIPE"); } else tst_resm(TFAIL, "writev returned a positive " "value"); } tst_resm(TINFO, "Exit block 8"); } cleanup(); tst_exit(); }
int main(int argc, char **argv) { int nbytes, ret; int lc; /* loop counter */ char *msg; /* message returned from parse_opts */ /* parse standard options */ if ((msg = parse_opts(argc, argv, (option_t *) NULL, NULL)) != (char *)NULL) { tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); /*NOTREACHED*/ } /* set "tstdir", and "testfile" vars */ setup(); /* The following loop checks looping state if -i option given */ for (lc = 0; TEST_LOOPING(lc); lc++) { /* reset Tst_count in case we are looping */ Tst_count = 0; buf_list[0] = buf1; buf_list[1] = buf2; buf_list[2] = buf3; buf_list[3] = (char *)NULL; fd[1] = -1; /* Invalid file descriptor */ if (signal(SIGTERM, sighandler) == SIG_ERR) { perror("signal: SIGTERM"); cleanup(); /*NOTREACHED*/ } if (signal(SIGPIPE, sighandler) == SIG_ERR) { perror("signal: SIGPIPE"); cleanup(); /*NOTREACHED*/ } init_buffs(buf_list); if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) { tst_resm(TFAIL, "open failed: fname = %s, errno = %d", f_name, errno); cleanup(); /*NOTREACHED*/ } else if ((nbytes = write(fd[0], buf_list[2], K_1)) != K_1) { tst_resm(TFAIL, "write failed: nbytes = %d, " "errno = %d", nbytes, errno); cleanup(); /*NOTREACHED*/ } if (close(fd[0]) < 0) { tst_resm(TFAIL, "close failed: errno: %d", errno); cleanup(); /*NOTREACHED*/ } if ((fd[0] = open(f_name, O_RDWR, 0666)) < 0) { tst_resm(TFAIL, "open failed: fname = %s, errno = %d", f_name, errno); cleanup(); /*NOTREACHED*/ } //block1: /* given vector length -1, writev() return EINVAL. */ tst_resm(TINFO, "Enter Block 1"); fail = 0; TEST(writev(fd[0], wr_iovec, 1)); if (TEST_RETURN < 0) { TEST_ERROR_LOG(TEST_ERRNO); if (TEST_ERRNO == EINVAL) { tst_resm(TINFO, "Received EINVAL as expected"); } else { tst_resm(TFAIL, "Expected errno = EINVAL, " "got %d", TEST_ERRNO); fail = 1; } } else { tst_resm(TFAIL, "writev() failed to fail"); fail = 1; } if (fail) { tst_resm(TINFO, "block 1 FAILED"); } else { tst_resm(TINFO, "block 1 PASSED"); } tst_resm(TINFO, "Exit block 1"); //block2: /* This testcases doesn't look like what it intent to do * 1. it is not using the wr_iovec initialized * 2. read() and following message is not consistent */ tst_resm(TINFO, "Enter block 2"); fail = 0; if (l_seek(fd[0], CHUNK * 6, 0) < 0) { TEST_ERROR_LOG(errno); tst_resm(TBROK, "block2: 1st lseek failed"); fail = 1; } if ((ret = writev(fd[0], (wr_iovec + 6), 3)) == CHUNK) { if (l_seek(fd[0], CHUNK * 6, 0) < 0) { TEST_ERROR_LOG(errno); tst_resm(TFAIL, "block2: 2nd lseek failed"); fail = 1; } if ((nbytes = read(fd[0], buf_list[0], CHUNK)) != CHUNK) { perror("read error"); tst_resm(TFAIL, "expected nbytes = 1024, " "got = %d", nbytes); fail = 1; } else if (memcmp((buf_list[0] + CHUNK * 6), (buf_list[2] + CHUNK * 6), CHUNK) != 0) { tst_resm(TFAIL, "Error: writev() over " "wrote %s", f_name); fail = 1; } } else { tst_resm(TFAIL, "writev() failed unexpectedly"); fail = 1; } if (fail) { tst_resm(TINFO, "block 2 FAILED"); } else { tst_resm(TINFO, "block 2 PASSED"); } tst_resm(TINFO, "Exit block 2"); //block3: /* given 1 bad vector buffer with good ones, writev() success */ tst_resm(TINFO, "Enter block 3"); fail = 0; if (lseek(fd[0], CHUNK * 6, 0) < 0) { TEST_ERROR_LOG(errno); tst_resm(TFAIL, "block3: 1st lseek failed"); fail = 1; } if ((nbytes = writev(fd[0], (wr_iovec + 6), 3)) < 0) { TEST_ERROR_LOG(errno); if (errno == EFAULT) { tst_resm(TFAIL, "Got EFAULT"); fail = 1; } } if (l_seek(fd[0], 0, 0) < 0) { TEST_ERROR_LOG(errno); tst_resm(TFAIL, "block3: 2nd lseek failed"); fail = 1; } if ((nbytes = read(fd[0], buf_list[0], K_1)) != K_1) { perror("read error"); tst_resm(TFAIL, "expected nbytes = 1024, got = %d", nbytes); fail = 1; } else if (memcmp((buf_list[0] + CHUNK * 6), (buf_list[2] + CHUNK * 6), CHUNK * 3) != 0) { tst_resm(TFAIL, "Error: writev() over wrote %s", f_name); fail = 1; } if (fail) { tst_resm(TINFO, "block 3 FAILED"); } else { tst_resm(TINFO, "block 3 PASSED"); } tst_resm(TINFO, "Exit block 3"); //block4: /* given bad file discriptor, writev() return EBADF. */ tst_resm(TINFO, "Enter block 4"); fail = 0; TEST(writev(fd[1], (wr_iovec + 9), 1)); if (TEST_RETURN < 0) { TEST_ERROR_LOG(TEST_ERRNO); if (TEST_ERRNO == EBADF) { tst_resm(TINFO, "Received EBADF as expected"); } else { tst_resm(TFAIL, "expected errno = EBADF, " "got %d", TEST_ERRNO); fail = 1; } } else { tst_resm(TFAIL, "Error: writev() returned a " "positive value"); fail = 1; } if (fail) { tst_resm(TINFO, "block 4 FAILED"); } else { tst_resm(TINFO, "block 4 PASSED"); } tst_resm(TINFO, "Exit block 4"); //block5: /* given invalid vector count, writev() return EINVAL */ tst_resm(TINFO, "Enter block 5"); fail = 0; TEST(writev(fd[0], (wr_iovec + 10), -1)); if (TEST_RETURN < 0) { TEST_ERROR_LOG(TEST_ERRNO); if (TEST_ERRNO == EINVAL) { tst_resm(TINFO, "Received EINVAL as expected"); } else { tst_resm(TFAIL, "expected errno = EINVAL, " "got %d", TEST_ERRNO); fail = 1; } } else { tst_resm(TFAIL, "Error: writev() returned a " "positive value"); fail = 1; } if (fail) { tst_resm(TINFO, "block 5 FAILED"); } else { tst_resm(TINFO, "block 5 PASSED"); } tst_resm(TINFO, "Exit block 5"); //block6: /* given no buffer vector, writev() success */ tst_resm(TINFO, "Enter block 6"); fail = 0; TEST(writev(fd[0], (wr_iovec + 11), 0)); if (TEST_RETURN < 0) { TEST_ERROR_LOG(TEST_ERRNO); tst_resm(TFAIL, "writev() failed with unexpected errno " "%d", TEST_ERRNO); fail = 1; } else { tst_resm(TPASS, "writev() wrote 0 iovectors"); } if (fail) { tst_resm(TINFO, "block 6 FAILED"); } else { tst_resm(TINFO, "block 6 PASSED"); } tst_resm(TINFO, "Exit block 6"); //block7: /* given 4 vectors, 2 are NULL, 1 with 0 length and 1 with fixed length, * writev() success writing fixed length. */ tst_resm(TINFO, "Enter block 7"); fail = 0; l_seek(fd[0], CHUNK * 12, 0); if ((ret = writev(fd[0], (wr_iovec + 12), 4)) != CHUNK) { tst_resm(TFAIL, "writev() failed writing %d bytes, " "followed by two NULL vectors", CHUNK); fail = 1; } else { tst_resm(TPASS, "writev passed writing %d bytes, " "followed by two NULL vectors", CHUNK); } if (fail) { tst_resm(TINFO, "block 7 FAILED"); } else { tst_resm(TINFO, "block 7 PASSED"); } tst_resm(TINFO, "Exit block 7"); //block8: /* try to write to a closed pipe, writev() return EPIPE. */ tst_resm(TINFO, "Enter block 8"); fail = 0; if (pipe(pfd) < 0) { TEST_ERROR_LOG(errno); perror("pipe"); tst_resm(TFAIL, "pipe failed: errno = %d", errno); fail = 1; } else { if (close(pfd[0]) < 0) { TEST_ERROR_LOG(errno); perror("close"); tst_resm(TFAIL, "close failed: errno = %d", errno); fail = 1; } else if ((writev(pfd[1], (wr_iovec + 12), 1) < 0) && in_sighandler) { TEST_ERROR_LOG(errno); if (errno == EPIPE) { tst_resm(TINFO, "Received EPIPE as " "expected"); } else { tst_resm(TFAIL, "expected errno = " "EPIPE, got %d", errno); fail = 1; } } else { tst_resm(TFAIL, "Error: writev() returned a " "positive value"); fail = 1; } } if (fail) { tst_resm(TINFO, "block 8 FAILED"); } else { tst_resm(TINFO, "block 8 PASSED"); } tst_resm(TINFO, "Exit block 8"); } close(fd[0]); close(fd[1]); cleanup(); /*NOTREACHED*/ return 0; }