1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * 07/2001 Ported by Wayne Boyer
5 */
6
7 /*\
8 * [Description]
9 *
10 * Testcase to test that sendfile(2) system call returns EBADF when passing
11 * wrong out_fd or in_fd.
12 *
13 * There are four cases:
14 *
15 * - in_fd == -1
16 * - out_fd = -1
17 * - in_fd opened with O_WRONLY
18 * - out_fd opened with O_RDONLY
19 */
20
21 #include <stdio.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/sendfile.h>
25
26 #include "tst_test.h"
27
28 static int in_fd;
29 static int out_fd;
30 static int negative_fd = -1;
31
32 struct test_case_t {
33 int *in_fd;
34 int *out_fd;
35 const char *desc;
36 } tc[] = {
37 {&in_fd, &negative_fd, "out_fd=-1"},
38 {&in_fd, &in_fd, "out_fd=O_RDONLY"},
39 {&negative_fd, &out_fd, "in_fd=-1"},
40 {&out_fd, &out_fd, "out_fd=O_WRONLY"},
41 };
42
setup(void)43 static void setup(void)
44 {
45 in_fd = SAFE_OPEN("in_file", O_CREAT | O_RDONLY, 0600);
46 out_fd = SAFE_CREAT("out_file", 0600);
47 }
48
cleanup(void)49 static void cleanup(void)
50 {
51 SAFE_CLOSE(in_fd);
52 SAFE_CLOSE(out_fd);
53 }
54
run(unsigned int i)55 static void run(unsigned int i)
56 {
57 TST_EXP_FAIL(sendfile(*(tc[i].out_fd), *(tc[i].in_fd), NULL, 1),
58 EBADF, "sendfile(..) with %s", tc[i].desc);
59 }
60
61 static struct tst_test test = {
62 .tcnt = ARRAY_SIZE(tc),
63 .needs_tmpdir = 1,
64 .cleanup = cleanup,
65 .setup = setup,
66 .test = run,
67 };
68