1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2014 Fujitsu Ltd.
4 * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
5 */
6 /*
7 * Description:
8 * Verify that,
9 * 1) splice() returns -1 and sets errno to EBADF if the file
10 * descriptor fd_in is not valid.
11 * 2) splice() returns -1 and sets errno to EBADF if the file
12 * descriptor fd_out is not valid.
13 * 3) splice() returns -1 and sets errno to EBADF if the file
14 * descriptor fd_in does not have proper read-write mode.
15 * 4) splice() returns -1 and sets errno to EINVAL if target
16 * file is opened in append mode.
17 * 5) splice() returns -1 and sets errno to EINVAL if neither
18 * of the descriptors refer to a pipe.
19 * 6) splice() returns -1 and sets errno to ESPIPE if off_in is
20 * not NULL when the file descriptor fd_in refers to a pipe.
21 * 7) splice() returns -1 and sets errno to ESPIPE if off_out is
22 * not NULL when the file descriptor fd_out refers to a pipe.
23 */
24
25 #define _GNU_SOURCE
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #include "tst_test.h"
33 #include "lapi/splice.h"
34
35 #define TEST_FILE "testfile"
36 #define TEST_FILE2 "testfile2"
37 #define TEST_FILE3 "testfile3"
38
39 #define STR "abcdefghigklmnopqrstuvwxyz"
40 #define SPLICE_TEST_LEN 10
41
42 static int badfd = -1;
43 static int rdfd;
44 static int wrfd;
45 static int appendfd;
46 static int pipes[2];
47 static loff_t offset;
48
49 static struct tcase {
50 int *fdin;
51 loff_t *offin;
52 int *fdout;
53 loff_t *offout;
54 int exp_errno;
55 } tcases[] = {
56 { &badfd, NULL, &pipes[1], NULL, EBADF },
57 { &pipes[0], NULL, &badfd, NULL, EBADF },
58 { &wrfd, NULL, &pipes[1], NULL, EBADF },
59 { &pipes[0], NULL, &appendfd, NULL, EINVAL },
60 { &rdfd, NULL, &wrfd, NULL, EINVAL },
61 { &pipes[0], &offset, &wrfd, NULL, ESPIPE },
62 { &rdfd, NULL, &pipes[1], &offset, ESPIPE },
63 };
64
setup(void)65 static void setup(void)
66 {
67 SAFE_FILE_PRINTF(TEST_FILE, STR);
68 rdfd = SAFE_OPEN(TEST_FILE, O_RDONLY);
69
70 wrfd = SAFE_OPEN(TEST_FILE2, O_WRONLY | O_CREAT, 0644);
71
72 appendfd = SAFE_OPEN(TEST_FILE3, O_RDWR | O_CREAT | O_APPEND, 0644);
73
74 SAFE_PIPE(pipes);
75
76 SAFE_WRITE(1, pipes[1], STR, sizeof(STR) - 1);
77 }
78
splice_verify(unsigned int n)79 static void splice_verify(unsigned int n)
80 {
81 struct tcase *tc = &tcases[n];
82
83 TEST(splice(*(tc->fdin), tc->offin, *(tc->fdout),
84 tc->offout, SPLICE_TEST_LEN, 0));
85
86 if (TST_RET != -1) {
87 tst_res(TFAIL, "splice() returned %ld expected %s",
88 TST_RET, tst_strerrno(tc->exp_errno));
89 return;
90 }
91
92 if (TST_ERR != tc->exp_errno) {
93 tst_res(TFAIL | TTERRNO,
94 "splice() failed unexpectedly; expected: %d - %s",
95 tc->exp_errno, tst_strerrno(tc->exp_errno));
96 return;
97 }
98
99 tst_res(TPASS | TTERRNO, "splice() failed as expected");
100 }
101
cleanup(void)102 static void cleanup(void)
103 {
104 if (rdfd > 0)
105 SAFE_CLOSE(rdfd);
106
107 if (wrfd > 0)
108 SAFE_CLOSE(wrfd);
109
110 if (appendfd > 0)
111 SAFE_CLOSE(appendfd);
112
113 if (pipes[0] > 0)
114 SAFE_CLOSE(pipes[0]);
115
116 if (pipes[1] > 0)
117 SAFE_CLOSE(pipes[1]);
118 }
119
120 static struct tst_test test = {
121 .setup = setup,
122 .cleanup = cleanup,
123 .test = splice_verify,
124 .tcnt = ARRAY_SIZE(tcases),
125 .needs_tmpdir = 1,
126 .min_kver = "2.6.17",
127 };
128