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