linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Jeff Layton <jlayton@kernel.org>,
	Chuck Lever <chuck.lever@oracle.com>,
	Luis Chamberlain <mcgrof@kernel.org>,
	Kees Cook <keescook@chromium.org>,
	Iurii Zaikin <yzaikin@google.com>
Cc: Kuniyuki Iwashima <kuniyu@amazon.com>,
	Kuniyuki Iwashima <kuni1840@gmail.com>, <netdev@vger.kernel.org>,
	<linux-fsdevel@vger.kernel.org>
Subject: [PATCH v1 net-next 03/13] selftest: sysctl: Add test for flock(LOCK_MAND).
Date: Thu, 25 Aug 2022 17:04:35 -0700	[thread overview]
Message-ID: <20220826000445.46552-4-kuniyu@amazon.com> (raw)
In-Reply-To: <20220826000445.46552-1-kuniyu@amazon.com>

This patch adds test cases for LOCK_MAND:

  - LOCK_MAND with/without LOCK_READ/LOCK_WRITE
  - read/write
    - same fd
    - different fd
      - same PID
      - different PID

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 tools/testing/selftests/sysctl/.gitignore     |   2 +
 tools/testing/selftests/sysctl/Makefile       |   9 +-
 tools/testing/selftests/sysctl/sysctl_flock.c | 157 ++++++++++++++++++
 3 files changed, 163 insertions(+), 5 deletions(-)
 create mode 100644 tools/testing/selftests/sysctl/.gitignore
 create mode 100644 tools/testing/selftests/sysctl/sysctl_flock.c

diff --git a/tools/testing/selftests/sysctl/.gitignore b/tools/testing/selftests/sysctl/.gitignore
new file mode 100644
index 000000000000..a3382ba798a6
--- /dev/null
+++ b/tools/testing/selftests/sysctl/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+/sysctl_flock
diff --git a/tools/testing/selftests/sysctl/Makefile b/tools/testing/selftests/sysctl/Makefile
index 110301f9f5be..eb565b6c8340 100644
--- a/tools/testing/selftests/sysctl/Makefile
+++ b/tools/testing/selftests/sysctl/Makefile
@@ -2,12 +2,11 @@
 # Makefile for sysctl selftests.
 # Expects kernel.sysctl_writes_strict=1.
 
-# No binaries, but make sure arg-less "make" doesn't trigger "run_tests".
-all:
+CFLAGS =  -Wall -Wl,--no-as-needed -O2 -g
+CFLAGS += -D_GNU_SOURCE
 
 TEST_PROGS := sysctl.sh
 
-include ../lib.mk
+TEST_GEN_PROGS += sysctl_flock
 
-# Nothing to clean up.
-clean:
+include ../lib.mk
diff --git a/tools/testing/selftests/sysctl/sysctl_flock.c b/tools/testing/selftests/sysctl/sysctl_flock.c
new file mode 100644
index 000000000000..4c4be10ae0d3
--- /dev/null
+++ b/tools/testing/selftests/sysctl/sysctl_flock.c
@@ -0,0 +1,157 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright Amazon.com Inc. or its affiliates. */
+
+#include <unistd.h>
+#include <sys/file.h>
+#include <sys/types.h>
+
+#include "../kselftest_harness.h"
+
+#define SYSCTL_PATH	"/proc/sys/net/ipv4/tcp_migrate_req"
+#define SYSCTL_BUFLEN	8
+
+FIXTURE(proc_sysctl_flock)
+{
+	int fd;
+	char buf[SYSCTL_BUFLEN];
+	int len;
+};
+
+FIXTURE_VARIANT(proc_sysctl_flock)
+{
+	int cmd;
+};
+
+FIXTURE_VARIANT_ADD(proc_sysctl_flock, lock_mand)
+{
+	.cmd = LOCK_MAND,
+};
+
+FIXTURE_VARIANT_ADD(proc_sysctl_flock, lock_mand_read)
+{
+	.cmd = LOCK_MAND | LOCK_READ,
+};
+
+FIXTURE_VARIANT_ADD(proc_sysctl_flock, lock_mand_write)
+{
+	.cmd = LOCK_MAND | LOCK_WRITE,
+};
+
+FIXTURE_VARIANT_ADD(proc_sysctl_flock, lock_mand_read_write)
+{
+	.cmd = LOCK_MAND | LOCK_READ | LOCK_WRITE,
+};
+
+int proc_sysctl_open(struct __test_metadata *_metadata)
+{
+	int fd;
+
+	fd = open(SYSCTL_PATH, O_RDWR);
+	ASSERT_NE(-1, fd);
+
+	return fd;
+}
+
+FIXTURE_SETUP(proc_sysctl_flock)
+{
+	self->fd = proc_sysctl_open(_metadata);
+	ASSERT_EQ(0, flock(self->fd, variant->cmd));
+
+	self->len = read(self->fd, self->buf, sizeof(self->buf));
+	ASSERT_NE(-1, self->len);
+
+	ASSERT_EQ(self->len, write(self->fd, self->buf, self->len));
+}
+
+FIXTURE_TEARDOWN(proc_sysctl_flock)
+{
+	flock(self->fd, LOCK_UN);
+	close(self->fd);
+}
+
+int is_readable(int cmd)
+{
+	return cmd & LOCK_READ;
+}
+
+int is_writable(int cmd)
+{
+	return cmd & LOCK_WRITE;
+}
+
+void proc_sysctl_newfd_read(struct __test_metadata *_metadata,
+			    FIXTURE_DATA(proc_sysctl_flock) *self,
+			    const FIXTURE_VARIANT(proc_sysctl_flock) *variant)
+{
+	char buf[SYSCTL_BUFLEN];
+	int err, fd;
+
+	fd = proc_sysctl_open(_metadata);
+
+	err = read(fd, buf, SYSCTL_BUFLEN);
+	if (is_readable(variant->cmd)) {
+		ASSERT_EQ(self->len, err);
+	} else {
+		ASSERT_EQ(-1, err);
+	}
+
+	close(fd);
+}
+
+void proc_sysctl_newfd_write(struct __test_metadata *_metadata,
+			     FIXTURE_DATA(proc_sysctl_flock) *self,
+			     const FIXTURE_VARIANT(proc_sysctl_flock) *variant)
+{
+	int err, fd;
+
+	fd = proc_sysctl_open(_metadata);
+
+	err = write(fd, self->buf, self->len);
+	if (is_writable(variant->cmd)) {
+		ASSERT_EQ(self->len, err);
+	} else {
+		ASSERT_EQ(-1, err);
+	}
+
+	close(fd);
+}
+
+void proc_sysctl_fork(struct __test_metadata *_metadata,
+		      FIXTURE_DATA(proc_sysctl_flock) *self,
+		      const FIXTURE_VARIANT(proc_sysctl_flock) *variant,
+		      int read)
+{
+	int pid, status;
+
+	pid = fork();
+	if (pid == 0) {
+		if (read)
+			return proc_sysctl_newfd_read(_metadata, self, variant);
+		else
+			return proc_sysctl_newfd_write(_metadata, self, variant);
+	}
+
+	waitpid(pid, &status, 0);
+}
+
+TEST_F(proc_sysctl_flock, test_newfd_read)
+{
+	proc_sysctl_newfd_read(_metadata, self, variant);
+}
+
+TEST_F(proc_sysctl_flock, test_newfd_write)
+{
+	proc_sysctl_newfd_write(_metadata, self, variant);
+}
+
+TEST_F(proc_sysctl_flock, test_fork_newfd_read)
+{
+	proc_sysctl_fork(_metadata, self, variant, 1);
+}
+
+TEST_F(proc_sysctl_flock, test_fork_newfd_write)
+{
+	proc_sysctl_fork(_metadata, self, variant, 0);
+}
+
+TEST_HARNESS_MAIN
-- 
2.30.2


  parent reply	other threads:[~2022-08-26  0:06 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-26  0:04 [PATCH v1 net-next 00/13] tcp/udp: Introduce optional per-netns hash table Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 01/13] fs/lock: Revive LOCK_MAND Kuniyuki Iwashima
2022-08-26 10:02   ` Jeff Layton
2022-08-26 16:48     ` Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 02/13] sysctl: Support LOCK_MAND for read/write Kuniyuki Iwashima
2022-08-26  0:04 ` Kuniyuki Iwashima [this message]
2022-08-26  0:04 ` [PATCH v1 net-next 04/13] net: Introduce init2() for pernet_operations Kuniyuki Iwashima
2022-08-26 15:20   ` Eric Dumazet
2022-08-26 17:03     ` Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 05/13] tcp: Clean up some functions Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 06/13] tcp: Set NULL to sk->sk_prot->h.hashinfo Kuniyuki Iwashima
2022-08-26 15:40   ` Eric Dumazet
2022-08-26 17:26     ` Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 07/13] tcp: Access &tcp_hashinfo via net Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 08/13] tcp: Introduce optional per-netns ehash Kuniyuki Iwashima
2022-08-26 15:24   ` Eric Dumazet
2022-08-26 17:19     ` Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 09/13] udp: Clean up some functions Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 10/13] udp: Set NULL to sk->sk_prot->h.udp_table Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 11/13] udp: Set NULL to udp_seq_afinfo.udp_table Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 12/13] udp: Access &udp_table via net Kuniyuki Iwashima
2022-08-26  0:04 ` [PATCH v1 net-next 13/13] udp: Introduce optional per-netns hash table Kuniyuki Iwashima
2022-08-26 15:17 ` [PATCH v1 net-next 00/13] tcp/udp: " Eric Dumazet
2022-08-26 16:51   ` Kuniyuki Iwashima

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=20220826000445.46552-4-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=chuck.lever@oracle.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=jlayton@kernel.org \
    --cc=keescook@chromium.org \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=yzaikin@google.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).