• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2006
3  *  Author Yi Yang <yyangcdl@cn.ibm.com>
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program;  if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 /*
20  * DESCRIPTION
21  *	This test case will verify basic function of splice
22  *	added by kernel 2.6.17 or up.
23  *
24  */
25 
26 #define _GNU_SOURCE
27 
28 #include <errno.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
33 
34 #include "tst_test.h"
35 #include "lapi/splice.h"
36 
37 #define TEST_BLOCK_SIZE 1024
38 
39 #define TESTFILE1 "splice_testfile_1"
40 #define TESTFILE2 "splice_testfile_2"
41 
42 static char buffer[TEST_BLOCK_SIZE];
43 static int fd_in, fd_out;
44 
check_file(void)45 static void check_file(void)
46 {
47 	int i;
48 	char splicebuffer[TEST_BLOCK_SIZE];
49 
50 	fd_out = SAFE_OPEN(TESTFILE2, O_RDONLY);
51 	SAFE_READ(1, fd_out, splicebuffer, TEST_BLOCK_SIZE);
52 
53 	for (i = 0; i < TEST_BLOCK_SIZE; i++) {
54 		if (buffer[i] != splicebuffer[i])
55 			break;
56 	}
57 
58 	if (i < TEST_BLOCK_SIZE)
59 		tst_res(TFAIL, "Wrong data read from the buffer at %i", i);
60 	else
61 		tst_res(TPASS, "Written data has been read back correctly");
62 
63 	SAFE_CLOSE(fd_out);
64 }
65 
splice_test(void)66 static void splice_test(void)
67 {
68 	int pipes[2];
69 	int ret;
70 
71 	fd_in = SAFE_OPEN(TESTFILE1, O_RDONLY);
72 	fd_out = SAFE_OPEN(TESTFILE2, O_WRONLY | O_CREAT | O_TRUNC, 0666);
73 	SAFE_PIPE(pipes);
74 
75 	ret = splice(fd_in, NULL, pipes[1], NULL, TEST_BLOCK_SIZE, 0);
76 	if (ret < 0)
77 		tst_brk(TBROK | TERRNO, "splice(fd_in, pipe) failed");
78 
79 	ret = splice(pipes[0], NULL, fd_out, NULL, TEST_BLOCK_SIZE, 0);
80 	if (ret < 0)
81 		tst_brk(TBROK | TERRNO, "splice(pipe, fd_out) failed");
82 
83 	SAFE_CLOSE(fd_in);
84 	SAFE_CLOSE(fd_out);
85 	SAFE_CLOSE(pipes[0]);
86 	SAFE_CLOSE(pipes[1]);
87 
88 	check_file();
89 }
90 
setup(void)91 static void setup(void)
92 {
93 	int i;
94 
95 	if (tst_fs_type(".") == TST_NFS_MAGIC) {
96 		if  (tst_kvercmp(2, 6, 32) < 0)
97 			tst_brk(TCONF, "Cannot do splice on a file"
98 				" on NFS filesystem before 2.6.32");
99 	}
100 
101 	for (i = 0; i < TEST_BLOCK_SIZE; i++)
102 		buffer[i] = i & 0xff;
103 
104 	fd_in = SAFE_OPEN(TESTFILE1, O_WRONLY | O_CREAT | O_TRUNC, 0777);
105 	SAFE_WRITE(1, fd_in, buffer, TEST_BLOCK_SIZE);
106 	SAFE_CLOSE(fd_in);
107 }
108 
cleanup(void)109 static void cleanup(void)
110 {
111 	if (fd_in > 0)
112 		SAFE_CLOSE(fd_in);
113 
114 	if (fd_out > 0)
115 		SAFE_CLOSE(fd_out);
116 }
117 
118 static struct tst_test test = {
119 	.setup = setup,
120 	.cleanup = cleanup,
121 	.test_all = splice_test,
122 	.needs_tmpdir = 1,
123 	.min_kver = "2.6.17",
124 };
125