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