1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2014 Fujitsu Ltd.
4f08c3bdfSopenharmony_ci * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * Description:
8f08c3bdfSopenharmony_ci *   Verify that,
9f08c3bdfSopenharmony_ci *   1) tee() returns -1 and sets errno to EINVAL if fd_in does
10f08c3bdfSopenharmony_ci *      not refer to a pipe.
11f08c3bdfSopenharmony_ci *   2) tee() returns -1 and sets errno to EINVAL if fd_out does
12f08c3bdfSopenharmony_ci *      not refer to a pipe.
13f08c3bdfSopenharmony_ci *   3) tee() returns -1 and sets errno to EINVAL if fd_in and
14f08c3bdfSopenharmony_ci *      fd_out refer to the same pipe.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#define _GNU_SOURCE
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#include <sys/types.h>
20f08c3bdfSopenharmony_ci#include <sys/stat.h>
21f08c3bdfSopenharmony_ci#include <fcntl.h>
22f08c3bdfSopenharmony_ci#include <unistd.h>
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#include "tst_test.h"
25f08c3bdfSopenharmony_ci#include "lapi/tee.h"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#define TEST_FILE "testfile"
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci#define STR "abcdefghigklmnopqrstuvwxyz"
30f08c3bdfSopenharmony_ci#define TEE_TEST_LEN 10
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_cistatic int fd;
33f08c3bdfSopenharmony_cistatic int pipes[2];
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic struct tcase {
36f08c3bdfSopenharmony_ci	int *fdin;
37f08c3bdfSopenharmony_ci	int *fdout;
38f08c3bdfSopenharmony_ci	int exp_errno;
39f08c3bdfSopenharmony_ci} tcases[] = {
40f08c3bdfSopenharmony_ci	{ &fd, &pipes[1], EINVAL },
41f08c3bdfSopenharmony_ci	{ &pipes[0], &fd, EINVAL },
42f08c3bdfSopenharmony_ci	{ &pipes[0], &pipes[1], EINVAL },
43f08c3bdfSopenharmony_ci};
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic void setup(void)
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0644);
48f08c3bdfSopenharmony_ci	SAFE_PIPE(pipes);
49f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, pipes[1], STR, sizeof(STR) - 1);
50f08c3bdfSopenharmony_ci}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void tee_verify(unsigned int n)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	TEST(tee(*(tc->fdin), *(tc->fdout), TEE_TEST_LEN, 0));
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
59f08c3bdfSopenharmony_ci		tst_res(TFAIL, "tee() returned %ld, "
60f08c3bdfSopenharmony_ci			"expected -1, errno:%d", TST_RET,
61f08c3bdfSopenharmony_ci			tc->exp_errno);
62f08c3bdfSopenharmony_ci		return;
63f08c3bdfSopenharmony_ci	}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	if (TST_ERR != tc->exp_errno) {
66f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
67f08c3bdfSopenharmony_ci			"tee() failed unexpectedly; expected: %d - %s",
68f08c3bdfSopenharmony_ci			tc->exp_errno, tst_strerrno(tc->exp_errno));
69f08c3bdfSopenharmony_ci		return;
70f08c3bdfSopenharmony_ci	}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	tst_res(TPASS | TTERRNO, "tee() failed as expected");
73f08c3bdfSopenharmony_ci}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_cistatic void cleanup(void)
76f08c3bdfSopenharmony_ci{
77f08c3bdfSopenharmony_ci	if (fd > 0)
78f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	if (pipes[0] > 0)
81f08c3bdfSopenharmony_ci		SAFE_CLOSE(pipes[0]);
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	if (pipes[1] > 0)
84f08c3bdfSopenharmony_ci		SAFE_CLOSE(pipes[1]);
85f08c3bdfSopenharmony_ci}
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_cistatic struct tst_test test = {
88f08c3bdfSopenharmony_ci	.setup = setup,
89f08c3bdfSopenharmony_ci	.cleanup = cleanup,
90f08c3bdfSopenharmony_ci	.test = tee_verify,
91f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
92f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
93f08c3bdfSopenharmony_ci};
94