aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/pipe/pipe03.c
blob: 89c0911c139523b38b4499f9990599bee654fba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) International Business Machines Corp., 2002
 */

/*
 * Make sure that writing to the read end of a pipe and reading from
 * the write end of a pipe both fail.
 */

#include <unistd.h>
#include <errno.h>
#include "tst_test.h"

static int fd[2];

static void verify_pipe(void)
{
	char buf[2];

	TEST(pipe(fd));
	if (TST_RET == -1) {
		tst_res(TFAIL | TTERRNO, "pipe() failed unexpectedly");
		return;
	}

	TEST(write(fd[0], "A", 1));
	if (TST_RET == -1 && errno == EBADF) {
		tst_res(TPASS | TTERRNO, "expected failure writing "
			"to read end of pipe");
	} else {
		tst_res(TFAIL | TTERRNO, "unexpected failure writing "
			"to read end of pipe");
	}

	TEST(read(fd[1], buf, 1));
	if (TST_RET == -1 && errno == EBADF) {
		tst_res(TPASS | TTERRNO, "expected failure reading "
			"from write end of pipe");
	} else {
		tst_res(TFAIL | TTERRNO, "unexpected failure reading "
			"from write end of pipe");
	}

	SAFE_CLOSE(fd[0]);
	SAFE_CLOSE(fd[1]);
}

static struct tst_test test = {
	.test_all = verify_pipe,
};