Xing Gu | cf90050 | 2014-05-30 16:05:25 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Fujitsu Ltd. |
| 3 | * Author: Xing Gu <gux.fnst@cn.fujitsu.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | /* |
| 18 | * Description: |
| 19 | * Verify that, |
| 20 | * 1) splice() returns -1 and sets errno to EBADF if the file |
| 21 | * descriptor fd_in is not valid. |
| 22 | * 2) splice() returns -1 and sets errno to EBADF if the file |
Cyril Hrubis | 6bd56e6 | 2014-06-03 11:09:35 +0200 | [diff] [blame] | 23 | * descriptor fd_out is not valid. |
Xing Gu | cf90050 | 2014-05-30 16:05:25 +0800 | [diff] [blame] | 24 | * 3) splice() returns -1 and sets errno to EBADF if the file |
| 25 | * descriptor fd_in does not have proper read-write mode. |
| 26 | * 4) splice() returns -1 and sets errno to EINVAL if target |
| 27 | * file is opened in append mode. |
| 28 | * 5) splice() returns -1 and sets errno to EINVAL if neither |
| 29 | * of the descriptors refer to a pipe. |
| 30 | * 6) splice() returns -1 and sets errno to ESPIPE if off_in is |
| 31 | * not NULL when the file descriptor fd_in refers to a pipe. |
| 32 | * 7) splice() returns -1 and sets errno to ESPIPE if off_out is |
| 33 | * not NULL when the file descriptor fd_out refers to a pipe. |
| 34 | */ |
| 35 | |
| 36 | #define _GNU_SOURCE |
| 37 | |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/stat.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | #include "test.h" |
Xing Gu | cf90050 | 2014-05-30 16:05:25 +0800 | [diff] [blame] | 44 | #include "safe_macros.h" |
Cyril Hrubis | 6bd56e6 | 2014-06-03 11:09:35 +0200 | [diff] [blame] | 45 | #include "lapi/splice.h" |
Xing Gu | cf90050 | 2014-05-30 16:05:25 +0800 | [diff] [blame] | 46 | |
| 47 | #define TEST_FILE "testfile" |
| 48 | #define TEST_FILE2 "testfile2" |
| 49 | #define TEST_FILE3 "testfile3" |
| 50 | |
| 51 | #define STR "abcdefghigklmnopqrstuvwxyz" |
| 52 | #define SPLICE_TEST_LEN 10 |
| 53 | |
| 54 | static int badfd = -1; |
| 55 | static int rdfd; |
| 56 | static int wrfd; |
| 57 | static int appendfd; |
| 58 | static int pipes[2]; |
| 59 | static loff_t offset; |
| 60 | |
| 61 | static struct test_case_t { |
| 62 | int *fdin; |
| 63 | loff_t *offin; |
| 64 | int *fdout; |
| 65 | loff_t *offout; |
| 66 | int exp_errno; |
| 67 | } test_cases[] = { |
| 68 | { &badfd, NULL, &pipes[1], NULL, EBADF }, |
| 69 | { &pipes[0], NULL, &badfd, NULL, EBADF }, |
| 70 | { &wrfd, NULL, &pipes[1], NULL, EBADF }, |
| 71 | { &pipes[0], NULL, &appendfd, NULL, EINVAL }, |
| 72 | { &rdfd, NULL, &wrfd, NULL, EINVAL }, |
| 73 | { &pipes[0], &offset, &wrfd, NULL, ESPIPE }, |
| 74 | { &rdfd, NULL, &pipes[1], &offset, ESPIPE }, |
| 75 | }; |
| 76 | |
| 77 | static void setup(void); |
| 78 | static void cleanup(void); |
| 79 | static void splice_verify(const struct test_case_t *); |
| 80 | |
| 81 | char *TCID = "splice03"; |
| 82 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
Xing Gu | cf90050 | 2014-05-30 16:05:25 +0800 | [diff] [blame] | 83 | |
| 84 | int main(int ac, char **av) |
| 85 | { |
| 86 | int i, lc; |
| 87 | const char *msg; |
| 88 | |
| 89 | msg = parse_opts(ac, av, NULL, NULL); |
| 90 | if (msg != NULL) |
| 91 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 92 | |
| 93 | setup(); |
| 94 | |
Xing Gu | cf90050 | 2014-05-30 16:05:25 +0800 | [diff] [blame] | 95 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 96 | tst_count = 0; |
| 97 | |
| 98 | for (i = 0; i < TST_TOTAL; i++) |
| 99 | splice_verify(&test_cases[i]); |
| 100 | } |
| 101 | |
| 102 | cleanup(); |
| 103 | tst_exit(); |
| 104 | } |
| 105 | |
| 106 | void setup(void) |
| 107 | { |
| 108 | if ((tst_kvercmp(2, 6, 17)) < 0) { |
| 109 | tst_brkm(TCONF, cleanup, "This test can only run on kernels " |
| 110 | "that are 2.6.17 or higher"); |
| 111 | } |
| 112 | |
| 113 | TEST_PAUSE; |
| 114 | |
| 115 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 116 | |
| 117 | tst_tmpdir(); |
| 118 | |
| 119 | SAFE_FILE_PRINTF(cleanup, TEST_FILE, STR); |
| 120 | rdfd = SAFE_OPEN(cleanup, TEST_FILE, O_RDONLY); |
| 121 | |
| 122 | wrfd = SAFE_OPEN(cleanup, TEST_FILE2, |
| 123 | O_WRONLY | O_CREAT, 0644); |
| 124 | |
| 125 | appendfd = SAFE_OPEN(cleanup, TEST_FILE3, |
| 126 | O_RDWR | O_CREAT | O_APPEND, 0644); |
| 127 | |
| 128 | SAFE_PIPE(cleanup, pipes); |
| 129 | |
| 130 | SAFE_WRITE(cleanup, 1, pipes[1], STR, sizeof(STR) - 1); |
| 131 | } |
| 132 | |
| 133 | static void splice_verify(const struct test_case_t *tc) |
| 134 | { |
| 135 | TEST(splice(*(tc->fdin), tc->offin, *(tc->fdout), |
| 136 | tc->offout, SPLICE_TEST_LEN, 0)); |
| 137 | |
| 138 | if (TEST_RETURN != -1) { |
| 139 | tst_resm(TFAIL, "splice() returned %ld, " |
| 140 | "expected -1, errno:%d", TEST_RETURN, |
| 141 | tc->exp_errno); |
| 142 | return; |
| 143 | } |
| 144 | |
Xing Gu | cf90050 | 2014-05-30 16:05:25 +0800 | [diff] [blame] | 145 | if (TEST_ERRNO == tc->exp_errno) { |
| 146 | tst_resm(TPASS | TTERRNO, "splice() failed as expected"); |
| 147 | } else { |
| 148 | tst_resm(TFAIL | TTERRNO, |
| 149 | "splice() failed unexpectedly; expected: %d - %s", |
| 150 | tc->exp_errno, strerror(tc->exp_errno)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void cleanup(void) |
| 155 | { |
Xing Gu | cf90050 | 2014-05-30 16:05:25 +0800 | [diff] [blame] | 156 | if (rdfd && close(rdfd) < 0) |
| 157 | tst_resm(TWARN | TERRNO, "close rdfd failed"); |
| 158 | |
| 159 | if (wrfd && close(wrfd) < 0) |
| 160 | tst_resm(TWARN | TERRNO, "close wrfd failed"); |
| 161 | |
| 162 | if (appendfd && close(appendfd) < 0) |
| 163 | tst_resm(TWARN | TERRNO, "close appendfd failed"); |
| 164 | |
| 165 | if (pipes[0] && close(pipes[0]) < 0) |
| 166 | tst_resm(TWARN | TERRNO, "close pipes[0] failed"); |
| 167 | |
| 168 | if (pipes[1] && close(pipes[1]) < 0) |
| 169 | tst_resm(TWARN | TERRNO, "close pipes[1] failed"); |
| 170 | |
| 171 | tst_rmdir(); |
| 172 | } |