1 /*
2  * Copyright (c) International Business Machines  Corp., 2002
3  *
4  * This program is free software;  you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.
16  */
17 
18 /*
19  * Make sure that writing to the read end of a pipe and reading from
20  * the write end of a pipe both fail.
21  */
22 
23 #include <unistd.h>
24 #include <errno.h>
25 #include "tst_test.h"
26 
27 static int fd[2];
28 
verify_pipe(void)29 static void verify_pipe(void)
30 {
31 	char buf[2];
32 
33 	TEST(pipe(fd));
34 	if (TEST_RETURN == -1) {
35 		tst_res(TFAIL | TTERRNO, "pipe() failed unexpectedly");
36 		return;
37 	}
38 
39 	TEST(write(fd[0], "A", 1));
40 	if (TEST_RETURN == -1 && errno == EBADF) {
41 		tst_res(TPASS | TTERRNO, "expected failure writing "
42 			"to read end of pipe");
43 	} else {
44 		tst_res(TFAIL | TTERRNO, "unexpected failure writing "
45 			"to read end of pipe");
46 	}
47 
48 	TEST(read(fd[1], buf, 1));
49 	if (TEST_RETURN == -1 && errno == EBADF) {
50 		tst_res(TPASS | TTERRNO, "expected failure reading "
51 			"from write end of pipe");
52 	} else {
53 		tst_res(TFAIL | TTERRNO, "unexpected failure reading "
54 			"from write end of pipe");
55 	}
56 
57 	SAFE_CLOSE(fd[0]);
58 	SAFE_CLOSE(fd[1]);
59 }
60 
61 static struct tst_test test = {
62 	.tid = "pipe03",
63 	.test_all = verify_pipe,
64 };
65