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