blob: 257da61c6e8c7c54b4ab2acd0da70fec14de69cc [file] [log] [blame]
Xing Gufd060ed2014-05-30 15:37:04 +08001/*
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 "test.h"
Xing Gufd060ed2014-05-30 15:37:04 +080036#include "safe_macros.h"
Cyril Hrubis6bd56e62014-06-03 11:09:35 +020037#include "lapi/tee.h"
Xing Gufd060ed2014-05-30 15:37:04 +080038
39#define TEST_FILE "testfile"
40
41#define STR "abcdefghigklmnopqrstuvwxyz"
42#define TEE_TEST_LEN 10
43
44static int fd;
45static int pipes[2];
46
47static struct test_case_t {
48 int *fdin;
49 int *fdout;
50 int exp_errno;
51} test_cases[] = {
52 { &fd, &pipes[1], EINVAL },
53 { &pipes[0], &fd, EINVAL },
54 { &pipes[0], &pipes[1], EINVAL },
55};
56
57static void setup(void);
58static void cleanup(void);
59static void tee_verify(const struct test_case_t *);
60
61char *TCID = "tee02";
62int TST_TOTAL = ARRAY_SIZE(test_cases);
Xing Gufd060ed2014-05-30 15:37:04 +080063
64int main(int ac, char **av)
65{
66 int i, lc;
67 const char *msg;
68
69 msg = parse_opts(ac, av, NULL, NULL);
70 if (msg != NULL)
71 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
72
73 setup();
74
Xing Gufd060ed2014-05-30 15:37:04 +080075 for (lc = 0; TEST_LOOPING(lc); lc++) {
76 tst_count = 0;
77
78 for (i = 0; i < TST_TOTAL; i++)
79 tee_verify(&test_cases[i]);
80 }
81
82 cleanup();
83 tst_exit();
84}
85
86static void setup(void)
87{
88 if ((tst_kvercmp(2, 6, 17)) < 0) {
89 tst_brkm(TCONF, cleanup, "This test can only run on kernels "
90 "that are 2.6.17 or higher");
91 }
92
93 TEST_PAUSE;
94
95 tst_sig(FORK, DEF_HANDLER, cleanup);
96
97 tst_tmpdir();
98
99 fd = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT);
100
101 SAFE_PIPE(cleanup, pipes);
102 SAFE_WRITE(cleanup, 1, pipes[1], STR, sizeof(STR) - 1);
103}
104
105static void tee_verify(const struct test_case_t *tc)
106{
107 TEST(tee(*(tc->fdin), *(tc->fdout), TEE_TEST_LEN, 0));
108
109 if (TEST_RETURN != -1) {
110 tst_resm(TFAIL, "tee() returned %ld, "
111 "expected -1, errno:%d", TEST_RETURN,
112 tc->exp_errno);
113 return;
114 }
115
Xing Gufd060ed2014-05-30 15:37:04 +0800116 if (TEST_ERRNO == tc->exp_errno) {
117 tst_resm(TPASS | TTERRNO, "tee() failed as expected");
118 } else {
119 tst_resm(TFAIL | TTERRNO,
120 "tee() failed unexpectedly; expected: %d - %s",
121 tc->exp_errno, strerror(tc->exp_errno));
122 }
123}
124
125static void cleanup(void)
126{
Xing Gufd060ed2014-05-30 15:37:04 +0800127 if (fd && close(fd) < 0)
128 tst_resm(TWARN | TERRNO, "close fd failed");
129
130 if (pipes[0] && close(pipes[0]) < 0)
131 tst_resm(TWARN | TERRNO, "close pipes[0] failed");
132
133 if (pipes[1] && close(pipes[1]) < 0)
134 tst_resm(TWARN | TERRNO, "close pipes[1] failed");
135
136 tst_rmdir();
137}