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 <sys/sendfile.h>
22#include "tst_test.h"
23
24static int in_fd;
25static int out_fd;
26static int negative_fd = -1;
27
28struct test_case_t {
29	int *in_fd;
30	int *out_fd;
31	const char *desc;
32} tc[] = {
33	{&in_fd, &negative_fd, "out_fd=-1"},
34	{&in_fd, &in_fd, "out_fd=O_RDONLY"},
35	{&negative_fd, &out_fd, "in_fd=-1"},
36	{&out_fd, &out_fd, "out_fd=O_WRONLY"},
37};
38
39static void setup(void)
40{
41	in_fd = SAFE_OPEN("in_file", O_CREAT | O_RDONLY, 0600);
42	out_fd = SAFE_CREAT("out_file", 0600);
43}
44
45static void cleanup(void)
46{
47	SAFE_CLOSE(in_fd);
48	SAFE_CLOSE(out_fd);
49}
50
51static void run(unsigned int i)
52{
53	TST_EXP_FAIL2(sendfile(*(tc[i].out_fd), *(tc[i].in_fd), NULL, 1),
54		     EBADF, "sendfile(..) with %s", tc[i].desc);
55}
56
57static struct tst_test test = {
58	.tcnt = ARRAY_SIZE(tc),
59	.needs_tmpdir = 1,
60	.cleanup = cleanup,
61	.setup = setup,
62	.test = run,
63};
64