• 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 	for (i = 0; i < TEST_BLOCK_SIZE; i++)
83 		buffer[i] = i & 0xff;
84 
85 	fd_in = SAFE_OPEN(TESTFILE1, O_WRONLY | O_CREAT | O_TRUNC, 0777);
86 	SAFE_WRITE(SAFE_WRITE_ALL, fd_in, buffer, TEST_BLOCK_SIZE);
87 	SAFE_CLOSE(fd_in);
88 }
89 
cleanup(void)90 static void cleanup(void)
91 {
92 	if (fd_in > 0)
93 		SAFE_CLOSE(fd_in);
94 
95 	if (fd_out > 0)
96 		SAFE_CLOSE(fd_out);
97 }
98 
99 static struct tst_test test = {
100 	.setup = setup,
101 	.cleanup = cleanup,
102 	.test_all = splice_test,
103 	.needs_tmpdir = 1,
104 };
105