• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2006
4  *  Author Yi Yang <yyangcdl@cn.ibm.com>
5  */
6 /*
7  * DESCRIPTION
8  *	This test case will verify basic function of splice
9  *	added by kernel 2.6.17 or up.
10  *
11  */
12 
13 #define _GNU_SOURCE
14 
15 #include <errno.h>
16 #include <string.h>
17 #include <signal.h>
18 #include <sys/types.h>
19 #include <fcntl.h>
20 
21 #include "tst_test.h"
22 #include "lapi/splice.h"
23 
24 #define TEST_BLOCK_SIZE 1024
25 
26 #define TESTFILE1 "splice_testfile_1"
27 #define TESTFILE2 "splice_testfile_2"
28 
29 static char buffer[TEST_BLOCK_SIZE];
30 static int fd_in, fd_out;
31 
check_file(void)32 static void check_file(void)
33 {
34 	int i;
35 	char splicebuffer[TEST_BLOCK_SIZE];
36 
37 	fd_out = SAFE_OPEN(TESTFILE2, O_RDONLY);
38 	SAFE_READ(1, fd_out, splicebuffer, TEST_BLOCK_SIZE);
39 
40 	for (i = 0; i < TEST_BLOCK_SIZE; i++) {
41 		if (buffer[i] != splicebuffer[i])
42 			break;
43 	}
44 
45 	if (i < TEST_BLOCK_SIZE)
46 		tst_res(TFAIL, "Wrong data read from the buffer at %i", i);
47 	else
48 		tst_res(TPASS, "Written data has been read back correctly");
49 
50 	SAFE_CLOSE(fd_out);
51 }
52 
splice_test(void)53 static void splice_test(void)
54 {
55 	int pipes[2];
56 	int ret;
57 
58 	fd_in = SAFE_OPEN(TESTFILE1, O_RDONLY);
59 	fd_out = SAFE_OPEN(TESTFILE2, O_WRONLY | O_CREAT | O_TRUNC, 0666);
60 	SAFE_PIPE(pipes);
61 
62 	ret = splice(fd_in, NULL, pipes[1], NULL, TEST_BLOCK_SIZE, 0);
63 	if (ret < 0)
64 		tst_brk(TBROK | TERRNO, "splice(fd_in, pipe) failed");
65 
66 	ret = splice(pipes[0], NULL, fd_out, NULL, TEST_BLOCK_SIZE, 0);
67 	if (ret < 0)
68 		tst_brk(TBROK | TERRNO, "splice(pipe, fd_out) failed");
69 
70 	SAFE_CLOSE(fd_in);
71 	SAFE_CLOSE(fd_out);
72 	SAFE_CLOSE(pipes[0]);
73 	SAFE_CLOSE(pipes[1]);
74 
75 	check_file();
76 }
77 
setup(void)78 static void setup(void)
79 {
80 	int i;
81 
82 	if (tst_fs_type(".") == TST_NFS_MAGIC) {
83 		if  (tst_kvercmp(2, 6, 32) < 0)
84 			tst_brk(TCONF, "Cannot do splice on a file"
85 				" on NFS filesystem before 2.6.32");
86 	}
87 
88 	for (i = 0; i < TEST_BLOCK_SIZE; i++)
89 		buffer[i] = i & 0xff;
90 
91 	fd_in = SAFE_OPEN(TESTFILE1, O_WRONLY | O_CREAT | O_TRUNC, 0777);
92 	SAFE_WRITE(1, fd_in, buffer, TEST_BLOCK_SIZE);
93 	SAFE_CLOSE(fd_in);
94 }
95 
cleanup(void)96 static void cleanup(void)
97 {
98 	if (fd_in > 0)
99 		SAFE_CLOSE(fd_in);
100 
101 	if (fd_out > 0)
102 		SAFE_CLOSE(fd_out);
103 }
104 
105 static struct tst_test test = {
106 	.setup = setup,
107 	.cleanup = cleanup,
108 	.test_all = splice_test,
109 	.needs_tmpdir = 1,
110 	.min_kver = "2.6.17",
111 };
112