All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v4 3/3] syscalls/pipe2_03: Add new test for pipe2 O_DIRECT flag
Date: Wed, 22 Apr 2020 18:45:29 +0800	[thread overview]
Message-ID: <1587552329-21738-3-git-send-email-xuyang2018.jy@cn.fujitsu.com> (raw)
In-Reply-To: <1587552329-21738-1-git-send-email-xuyang2018.jy@cn.fujitsu.com>

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                           |   1 +
 testcases/kernel/syscalls/pipe2/.gitignore |   1 +
 testcases/kernel/syscalls/pipe2/pipe2_03.c | 172 +++++++++++++++++++++
 3 files changed, 174 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_03.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 9bb72beb2..16add02d3 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -916,6 +916,7 @@ pipe13 pipe13
 
 pipe2_01 pipe2_01
 pipe2_02 pipe2_02
+pipe2_03 pipe2_03
 pipe2_04 pipe2_04
 
 pivot_root01 pivot_root01
diff --git a/testcases/kernel/syscalls/pipe2/.gitignore b/testcases/kernel/syscalls/pipe2/.gitignore
index 773450a48..ede1da65e 100644
--- a/testcases/kernel/syscalls/pipe2/.gitignore
+++ b/testcases/kernel/syscalls/pipe2/.gitignore
@@ -1,3 +1,4 @@
 /pipe2_01
 /pipe2_02
+/pipe2_03
 /pipe2_04
diff --git a/testcases/kernel/syscalls/pipe2/pipe2_03.c b/testcases/kernel/syscalls/pipe2/pipe2_03.c
new file mode 100644
index 000000000..0314f9eda
--- /dev/null
+++ b/testcases/kernel/syscalls/pipe2/pipe2_03.c
@@ -0,0 +1,172 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ *
+ * This case is designed to test the basic functionality about the
+ * O_DIRECT flag of pipe2.
+ *
+ * It includes three sub tests.
+ * 1) Each write(2) to the pipe is dealt with as a separate packet, and
+ * read(2)s from the pipe will read one packet at a time.
+ * 2) Writes of greater than PIPE_BUF bytes (see pipe(7)) will be split
+ * into multiple packet.
+ * 3)If a read(2) specifies a buffer size that is smaller than the next
+ * packet, then the requested number of bytes are read, and the excess
+ * bytes in the packet are discarded.
+ */
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <linux/limits.h>
+#include "lapi/fcntl.h"
+#include "tst_test.h"
+
+static int fds[2], packet_num, pipe_size;
+static char *wrbuf;
+static char *rdbuf;
+static void check_peer_rw(void);
+static void check_split(void);
+static void check_discard(void);
+
+static void (*test_func[])(void) = {check_peer_rw, check_split, check_discard};
+
+static void check_peer_rw(void)
+{
+	int i, pid;
+
+	SAFE_PIPE2(fds, O_DIRECT | O_NONBLOCK);
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		SAFE_CLOSE(fds[1]);
+		memset(rdbuf, 0, pipe_size);
+		TST_CHECKPOINT_WAIT(0);
+		for (i = 0; i < packet_num; i++) {
+			TEST(SAFE_READ(0, fds[0], rdbuf, pipe_size));
+			if (TST_RET != 1)
+				tst_res(TFAIL,
+					"Each read(2) doesn't read a separate packet, return %ld", TST_RET);
+		}
+		tst_res(TPASS, "Each read(2) reads a separate packet");
+		 _exit(0);
+	}
+
+	SAFE_CLOSE(fds[0]);
+	for (i = 0; i < packet_num; i++)
+		SAFE_WRITE(1, fds[1], "x", 1);
+
+	TEST(write(fds[1], "x", 1));
+	if (TST_RET != -1) {
+		tst_res(TFAIL, "write succeeded unexpectedly");
+	} else {
+		if (TST_ERR == EAGAIN)
+			tst_res(TPASS, "Each write(2) uses a separate packet");
+		else
+			tst_res(TFAIL | TTERRNO, "write failed, expected EAGAIN but got");
+	}
+	TST_CHECKPOINT_WAKE(0);
+	tst_reap_children();
+	SAFE_CLOSE(fds[1]);
+}
+
+static void check_split(void)
+{
+	int i, pid;
+
+	SAFE_PIPE2(fds, O_DIRECT);
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		SAFE_CLOSE(fds[1]);
+		memset(rdbuf, 0, pipe_size);
+		TST_CHECKPOINT_WAIT(0);
+		for (i = 0; i < 2; i++) {
+			TEST(SAFE_READ(0, fds[0], rdbuf, pipe_size));
+			if (TST_RET != PIPE_BUF)
+				tst_res(TFAIL,
+					"write(higner than PIPE_BUF) split into multiple packet, return %ld", TST_RET);
+		}
+		tst_res(TPASS, "write(higner than PIPE_BUF) split into multiple packet");
+		 _exit(0);
+	}
+	SAFE_CLOSE(fds[0]);
+	SAFE_WRITE(1, fds[1], wrbuf, PIPE_BUF * 2);
+	TST_CHECKPOINT_WAKE(0);
+	tst_reap_children();
+	SAFE_CLOSE(fds[1]);
+}
+
+static void check_discard(void)
+{
+	int pid;
+	char tmp_buf[20];
+	char tmp_secondbuf[20];
+
+	SAFE_PIPE2(fds, O_DIRECT);
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		SAFE_CLOSE(fds[1]);
+		TST_CHECKPOINT_WAIT(0);
+		TEST(SAFE_READ(0, fds[0], tmp_buf, 20));
+		if (TST_RET != 20)
+			tst_res(TFAIL,
+				"the excess bytes in the packet isn't discarded by read, return %ld", TST_RET);
+		TEST(SAFE_READ(0, fds[0], tmp_secondbuf, 20));
+		if (TST_RET == 1) {
+			if (!memcmp(tmp_secondbuf, "1", 1))
+				tst_res(TPASS,
+					"the excess bytes in the packet is discarded by read, only read 1");
+			else
+				tst_res(TFAIL,
+					"the excess bytes in the packet is discarded by read, expect 1 got %s", tmp_secondbuf);
+		}
+		 _exit(0);
+	}
+	SAFE_CLOSE(fds[0]);
+	SAFE_WRITE(1, fds[1], wrbuf, PIPE_BUF);
+	SAFE_WRITE(1, fds[1], "1", 1);
+	TST_CHECKPOINT_WAKE(0);
+	tst_reap_children();
+	SAFE_CLOSE(fds[1]);
+}
+
+static void verify_pipe2(unsigned int n)
+{
+	(*test_func[n])();
+}
+
+static void setup(void)
+{
+	SAFE_PIPE2(fds, O_DIRECT);
+	pipe_size = SAFE_FCNTL(fds[1], F_GETPIPE_SZ);
+	wrbuf = SAFE_MALLOC(PIPE_BUF * 2);
+	rdbuf = SAFE_MALLOC(pipe_size);
+	memset(wrbuf, 'x', PIPE_BUF * 2);
+	packet_num = pipe_size / PIPE_BUF;
+	SAFE_CLOSE(fds[0]);
+	SAFE_CLOSE(fds[1]);
+}
+
+static void cleanup(void)
+{
+	if (fds[0] > 0)
+		SAFE_CLOSE(fds[0]);
+	if (fds[1] > 0)
+		SAFE_CLOSE(fds[1]);
+	if (wrbuf)
+		free(wrbuf);
+	if (rdbuf)
+		free(rdbuf);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.forks_child = 1,
+	.test = verify_pipe2,
+	.tcnt = ARRAY_SIZE(test_func),
+	.needs_checkpoints = 1,
+};
-- 
2.23.0




  parent reply	other threads:[~2020-04-22 10:45 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-22 10:45 [LTP] [PATCH v4 1/3] syscalls/pipe2_01: convert into new API Yang Xu
2020-04-22 10:45 ` [LTP] [PATCH v4 2/3] syscalls/pipe2_02: Add new test for pipe2 O_CLOEXEC flag Yang Xu
2020-04-22 10:45 ` Yang Xu [this message]
2020-04-23  8:08   ` [LTP] [PATCH v4 3/3] syscalls/pipe2_03: Add new test for pipe2 O_DIRECT flag Li Wang
2020-04-23  8:45     ` Yang Xu
2020-04-23 10:04       ` Li Wang
2020-04-23 10:30         ` Yang Xu
2020-04-26 11:59           ` Li Wang
2020-04-26 17:45             ` Linus Torvalds
2020-04-26 17:45               ` Linus Torvalds
2020-04-30  3:23               ` Yang Xu
2020-04-30  3:23                 ` Yang Xu
2020-04-30  6:57                 ` Li Wang
2020-04-26  9:31 ` [LTP] [PATCH v4 1/3] syscalls/pipe2_01: convert into new API Li Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1587552329-21738-3-git-send-email-xuyang2018.jy@cn.fujitsu.com \
    --to=xuyang2018.jy@cn.fujitsu.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.