1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) International Business Machines Corp., 2001
4 */
5
6/*
7 * Basic test for pipe().
8 */
9
10#include <errno.h>
11#include <string.h>
12#include "tst_test.h"
13
14static int fds[2];
15
16static void verify_pipe(void)
17{
18	int rd_size, wr_size;
19	char wrbuf[] = "abcdefghijklmnopqrstuvwxyz";
20	char rdbuf[128];
21
22	memset(rdbuf, 0, sizeof(rdbuf));
23
24	TEST(pipe(fds));
25
26	if (TST_RET == -1) {
27		tst_res(TFAIL | TTERRNO, "pipe()");
28		return;
29	}
30
31	wr_size = SAFE_WRITE(SAFE_WRITE_ALL, fds[1], wrbuf, sizeof(wrbuf));
32	rd_size = SAFE_READ(0, fds[0], rdbuf, sizeof(rdbuf));
33
34	if (rd_size != wr_size) {
35		tst_res(TFAIL, "read() returned %d, expected %d",
36		        rd_size, wr_size);
37		return;
38	}
39
40	if ((strncmp(rdbuf, wrbuf, wr_size)) != 0) {
41		tst_res(TFAIL, "Wrong data were read back");
42		return;
43	}
44
45	SAFE_CLOSE(fds[0]);
46	SAFE_CLOSE(fds[1]);
47
48	tst_res(TPASS, "pipe() functionality is correct");
49}
50
51static struct tst_test test = {
52	.test_all = verify_pipe,
53};
54