• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
23  *      descriptor fd_out is not valid.
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"
44 #include "safe_macros.h"
45 #include "lapi/splice.h"
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);
83 
main(int ac,char ** av)84 int main(int ac, char **av)
85 {
86 	int i, lc;
87 
88 	tst_parse_opts(ac, av, NULL, NULL);
89 
90 	setup();
91 
92 	for (lc = 0; TEST_LOOPING(lc); lc++) {
93 		tst_count = 0;
94 
95 		for (i = 0; i < TST_TOTAL; i++)
96 			splice_verify(&test_cases[i]);
97 	}
98 
99 	cleanup();
100 	tst_exit();
101 }
102 
setup(void)103 void setup(void)
104 {
105 	if ((tst_kvercmp(2, 6, 17)) < 0) {
106 		tst_brkm(TCONF, cleanup, "This test can only run on kernels "
107 			"that are 2.6.17 or higher");
108 	}
109 
110 	TEST_PAUSE;
111 
112 	tst_sig(FORK, DEF_HANDLER, cleanup);
113 
114 	tst_tmpdir();
115 
116 	SAFE_FILE_PRINTF(cleanup, TEST_FILE, STR);
117 	rdfd = SAFE_OPEN(cleanup, TEST_FILE, O_RDONLY);
118 
119 	wrfd = SAFE_OPEN(cleanup, TEST_FILE2,
120 		O_WRONLY | O_CREAT, 0644);
121 
122 	appendfd = SAFE_OPEN(cleanup, TEST_FILE3,
123 		O_RDWR | O_CREAT | O_APPEND, 0644);
124 
125 	SAFE_PIPE(cleanup, pipes);
126 
127 	SAFE_WRITE(cleanup, 1, pipes[1], STR, sizeof(STR) - 1);
128 }
129 
splice_verify(const struct test_case_t * tc)130 static void splice_verify(const struct test_case_t *tc)
131 {
132 	TEST(splice(*(tc->fdin), tc->offin, *(tc->fdout),
133 		tc->offout, SPLICE_TEST_LEN, 0));
134 
135 	if (TEST_RETURN != -1) {
136 		tst_resm(TFAIL, "splice() returned %ld, "
137 			"expected -1, errno:%d", TEST_RETURN,
138 			tc->exp_errno);
139 		return;
140 	}
141 
142 	if (TEST_ERRNO == tc->exp_errno) {
143 		tst_resm(TPASS | TTERRNO, "splice() failed as expected");
144 	} else {
145 		tst_resm(TFAIL | TTERRNO,
146 			"splice() failed unexpectedly; expected: %d - %s",
147 			tc->exp_errno, strerror(tc->exp_errno));
148 	}
149 }
150 
cleanup(void)151 void cleanup(void)
152 {
153 	if (rdfd && close(rdfd) < 0)
154 		tst_resm(TWARN | TERRNO, "close rdfd failed");
155 
156 	if (wrfd && close(wrfd) < 0)
157 		tst_resm(TWARN | TERRNO, "close wrfd failed");
158 
159 	if (appendfd && close(appendfd) < 0)
160 		tst_resm(TWARN | TERRNO, "close appendfd failed");
161 
162 	if (pipes[0] && close(pipes[0]) < 0)
163 		tst_resm(TWARN | TERRNO, "close pipes[0] failed");
164 
165 	if (pipes[1] && close(pipes[1]) < 0)
166 		tst_resm(TWARN | TERRNO, "close pipes[1] failed");
167 
168 	tst_rmdir();
169 }
170