public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Andrea Cervesato <andrea.cervesato@suse.de>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 1/2] Add shutdown01 test
Date: Fri, 07 Jun 2024 10:13:14 +0200	[thread overview]
Message-ID: <20240607-shutdown-v2-1-a09ce3290ee1@suse.com> (raw)
In-Reply-To: <20240607-shutdown-v2-0-a09ce3290ee1@suse.com>

From: Andrea Cervesato <andrea.cervesato@suse.com>

This test verifies the following shutdown() functionalities:

- SHUT_RD should enable send() ops but disable recv() ops
- SHUT_WR should enable recv() ops but disable send() ops
- SHUT_RDWR should disable both recv() and send() ops

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 runtest/syscalls                                |   2 +
 testcases/kernel/syscalls/shutdown/.gitignore   |   1 +
 testcases/kernel/syscalls/shutdown/Makefile     |   7 ++
 testcases/kernel/syscalls/shutdown/shutdown01.c | 121 ++++++++++++++++++++++++
 4 files changed, 131 insertions(+)

diff --git a/runtest/syscalls b/runtest/syscalls
index cf06ee563..dc396415e 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1465,6 +1465,8 @@ shmget04 shmget04
 shmget05 shmget05
 shmget06 shmget06
 
+shutdown01 shutdown01
+
 sigaction01 sigaction01
 sigaction02 sigaction02
 
diff --git a/testcases/kernel/syscalls/shutdown/.gitignore b/testcases/kernel/syscalls/shutdown/.gitignore
new file mode 100644
index 000000000..2df24d1ab
--- /dev/null
+++ b/testcases/kernel/syscalls/shutdown/.gitignore
@@ -0,0 +1 @@
+shutdown01
diff --git a/testcases/kernel/syscalls/shutdown/Makefile b/testcases/kernel/syscalls/shutdown/Makefile
new file mode 100644
index 000000000..8cf1b9024
--- /dev/null
+++ b/testcases/kernel/syscalls/shutdown/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/shutdown/shutdown01.c b/testcases/kernel/syscalls/shutdown/shutdown01.c
new file mode 100644
index 000000000..ba3853d9c
--- /dev/null
+++ b/testcases/kernel/syscalls/shutdown/shutdown01.c
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * This test verifies the following shutdown() functionalities:
+ *
+ * - SHUT_RD should enable send() ops but disable recv() ops
+ * - SHUT_WR should enable recv() ops but disable send() ops
+ * - SHUT_RDWR should disable both recv() and send() ops
+ */
+
+#include "tst_test.h"
+#include "tst_safe_net.h"
+
+#define MSGSIZE 4
+#define SOCKETFILE "socket"
+
+static struct tcase {
+	int shutdown_op;
+	int recv_flag;
+	int recv_err;
+	int send_flag;
+	int send_err;
+	char *flag_str;
+} tcases[] = {
+	{SHUT_RD, 0, 0, 0, 0, "SHUT_RD"},
+	{SHUT_WR, MSG_DONTWAIT, EWOULDBLOCK, MSG_NOSIGNAL, EPIPE, "SHUT_WR"},
+	{SHUT_RDWR, 0, 0, MSG_NOSIGNAL, EPIPE, "SHUT_RDWR"}
+};
+
+static struct sockaddr_un *sock_addr;
+
+static void run_server(void)
+{
+	int server_sock;
+
+	server_sock = SAFE_SOCKET(sock_addr->sun_family, SOCK_STREAM, 0);
+
+	SAFE_BIND(server_sock,
+		(struct sockaddr *)sock_addr,
+		sizeof(struct sockaddr_un));
+	SAFE_LISTEN(server_sock, 10);
+
+	tst_res(TINFO, "Running server on socket file");
+
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
+
+	SAFE_CLOSE(server_sock);
+	SAFE_UNLINK(SOCKETFILE);
+}
+
+static int start_test(void)
+{
+	int client_sock;
+
+	if (!SAFE_FORK()) {
+		run_server();
+		_exit(0);
+	}
+
+	TST_CHECKPOINT_WAIT(0);
+
+	tst_res(TINFO, "Connecting to the server");
+
+	client_sock = SAFE_SOCKET(sock_addr->sun_family, SOCK_STREAM, 0);
+	SAFE_CONNECT(client_sock,
+		(struct sockaddr *)sock_addr,
+		sizeof(struct sockaddr_un));
+
+	return client_sock;
+}
+
+static void run(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+	int client_sock;
+	char buff[MSGSIZE] = {0};
+
+	client_sock = start_test();
+
+	tst_res(TINFO, "Testing %s flag", tc->flag_str);
+
+	TST_EXP_PASS(shutdown(client_sock, tc->shutdown_op));
+
+	if (!tc->recv_err)
+		SAFE_RECV(0, client_sock, buff, MSGSIZE, tc->recv_flag);
+	else
+		TST_EXP_FAIL(recv(client_sock, buff, MSGSIZE, tc->recv_flag), tc->recv_err);
+
+	if (!tc->send_err)
+		SAFE_SEND(MSGSIZE, client_sock, buff, MSGSIZE, tc->send_flag);
+	else
+		TST_EXP_FAIL(send(client_sock, buff, MSGSIZE, tc->send_flag), tc->send_err);
+
+	SAFE_CLOSE(client_sock);
+	TST_CHECKPOINT_WAKE(0);
+
+}
+
+static void setup(void)
+{
+	sock_addr->sun_family = AF_UNIX;
+	memcpy(sock_addr->sun_path, SOCKETFILE, sizeof(SOCKETFILE));
+}
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.forks_child = 1,
+	.needs_checkpoints = 1,
+	.needs_tmpdir = 1,
+	.bufs = (struct tst_buffers []) {
+		{&sock_addr, .size = sizeof(struct sockaddr_un)},
+		{}
+	}
+};

-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2024-06-07  8:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-07  8:13 [LTP] [PATCH v2 0/2] shutdown testing suite Andrea Cervesato
2024-06-07  8:13 ` Andrea Cervesato [this message]
2024-06-10 13:13   ` [LTP] [PATCH v2 1/2] Add shutdown01 test Petr Vorel
2024-06-10 13:17     ` Andrea Cervesato via ltp
2024-06-10 13:52   ` Petr Vorel
2024-06-07  8:13 ` [LTP] [PATCH v2 2/2] Add shutdown02 test Andrea Cervesato
2024-06-10 13:44   ` Petr Vorel
2024-06-10 14:07     ` Andrea Cervesato via ltp

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=20240607-shutdown-v2-1-a09ce3290ee1@suse.com \
    --to=andrea.cervesato@suse.de \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox