1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2002
4 */
5
6 /*
7 * Make sure that writing to the read end of a pipe and reading from
8 * the write end of a pipe both fail.
9 */
10
11 #include <unistd.h>
12 #include <errno.h>
13 #include "tst_test.h"
14
15 static int fd[2];
16
verify_pipe(void)17 static void verify_pipe(void)
18 {
19 char buf[2];
20
21 TEST(pipe(fd));
22 if (TST_RET == -1) {
23 tst_res(TFAIL | TTERRNO, "pipe() failed unexpectedly");
24 return;
25 }
26
27 TEST(write(fd[0], "A", 1));
28 if (TST_RET == -1 && errno == EBADF) {
29 tst_res(TPASS | TTERRNO, "expected failure writing "
30 "to read end of pipe");
31 } else {
32 tst_res(TFAIL | TTERRNO, "unexpected failure writing "
33 "to read end of pipe");
34 }
35
36 TEST(read(fd[1], buf, 1));
37 if (TST_RET == -1 && errno == EBADF) {
38 tst_res(TPASS | TTERRNO, "expected failure reading "
39 "from write end of pipe");
40 } else {
41 tst_res(TFAIL | TTERRNO, "unexpected failure reading "
42 "from write end of pipe");
43 }
44
45 SAFE_CLOSE(fd[0]);
46 SAFE_CLOSE(fd[1]);
47 }
48
49 static struct tst_test test = {
50 .test_all = verify_pipe,
51 };
52