1 /*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17 /*
18 * Description:
19 * Verify that,
20 * 1) tee() returns -1 and sets errno to EINVAL if fd_in does
21 * not refer to a pipe.
22 * 2) tee() returns -1 and sets errno to EINVAL if fd_out does
23 * not refer to a pipe.
24 * 3) tee() returns -1 and sets errno to EINVAL if fd_in and
25 * fd_out refer to the same pipe.
26 */
27
28 #define _GNU_SOURCE
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include "tst_test.h"
36 #include "lapi/tee.h"
37
38 #define TEST_FILE "testfile"
39
40 #define STR "abcdefghigklmnopqrstuvwxyz"
41 #define TEE_TEST_LEN 10
42
43 static int fd;
44 static int pipes[2];
45
46 static struct tcase {
47 int *fdin;
48 int *fdout;
49 int exp_errno;
50 } tcases[] = {
51 { &fd, &pipes[1], EINVAL },
52 { &pipes[0], &fd, EINVAL },
53 { &pipes[0], &pipes[1], EINVAL },
54 };
55
setup(void)56 static void setup(void)
57 {
58 fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT);
59 SAFE_PIPE(pipes);
60 SAFE_WRITE(1, pipes[1], STR, sizeof(STR) - 1);
61 }
62
tee_verify(unsigned int n)63 static void tee_verify(unsigned int n)
64 {
65 struct tcase *tc = &tcases[n];
66
67 TEST(tee(*(tc->fdin), *(tc->fdout), TEE_TEST_LEN, 0));
68
69 if (TST_RET != -1) {
70 tst_res(TFAIL, "tee() returned %ld, "
71 "expected -1, errno:%d", TST_RET,
72 tc->exp_errno);
73 return;
74 }
75
76 if (TST_ERR != tc->exp_errno) {
77 tst_res(TFAIL | TTERRNO,
78 "tee() failed unexpectedly; expected: %d - %s",
79 tc->exp_errno, tst_strerrno(tc->exp_errno));
80 return;
81 }
82
83 tst_res(TPASS | TTERRNO, "tee() failed as expected");
84 }
85
cleanup(void)86 static void cleanup(void)
87 {
88 if (fd > 0)
89 SAFE_CLOSE(fd);
90
91 if (pipes[0] > 0)
92 SAFE_CLOSE(pipes[0]);
93
94 if (pipes[1] > 0)
95 SAFE_CLOSE(pipes[1]);
96 }
97
98 static struct tst_test test = {
99 .setup = setup,
100 .cleanup = cleanup,
101 .test = tee_verify,
102 .tcnt = ARRAY_SIZE(tcases),
103 .needs_tmpdir = 1,
104 .min_kver = "2.6.17",
105 };
106