Skip to contentSkip to conversation

shutdown testing suite

PATCH Superseded Andrea Cervesato 3 participants 8 messages Jun 7, 2024 – Jun 10, 2024

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover shutdown testing suite

This testing suite is meant to test shutdown() syscall functionalities. The current LTP version uses shutdown() inside many tests, but none of them is specifically running unit tests for it.

This suite comes with both unit tests for shutdown() flags and error codes.

---
Changes in v2:
- shutdown01: move test cases inside .test / .tcnt
- shutdown01: usage of SAFE_RECV / SAFE_SEND
- shutdown01: fixed documentation formatting
- shutdown02: fixed documentation formatting
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrea Cervesato (2):
Add shutdown01 test
Add shutdown02 test

 runtest/syscalls                                |   3 +
 testcases/kernel/syscalls/shutdown/.gitignore   |   2 +
 testcases/kernel/syscalls/shutdown/Makefile     |   7 ++
 testcases/kernel/syscalls/shutdown/shutdown01.c | 121 ++++++++++++++++++++++++
 testcases/kernel/syscalls/shutdown/shutdown02.c |  76 +++++++++++++++
 5 files changed, 209 insertions(+)

---
base-commit: 0358f7a2788eed0cbff7f8519b1a0f7f89d36c13
change-id: 20240524-shutdown-c8d7580e916a

Best regards,

Signature · 5 lines
Andrea Cervesato <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v2 1/2 Add shutdown01 test

From: Andrea Cervesato <[email protected]>

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

---
 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(+)
runtest/syscalls +2 -0
index cf06ee563..dc396415e 100644
@@ -1465,6 +1465,8 @@ shmget04 shmget04
 shmget05 shmget05
 shmget06 shmget06
 
+shutdown01 shutdown01
+
 sigaction01 sigaction01
 sigaction02 sigaction02
 
testcases/kernel/syscalls/shutdown/.gitignore new file +1 -0
new file mode 100644
index 000000000..2df24d1ab
@@ -0,0 +1 @@
+shutdown01
testcases/kernel/syscalls/shutdown/Makefile new file +7 -0
new file mode 100644
index 000000000..8cf1b9024
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/shutdown/shutdown01.c new file +121 -0

121 lines not on this page · raw message

Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v2 1/2
#2 lore raw

Re: [LTP] [PATCH v2 1/2] Add shutdown01 test

Hi Andrea,

TL;DR: generally LGTM, I have few minor details, which I offer to fix before merge (diff below).

Quoted 1 line

+++ b/testcases/kernel/syscalls/shutdown/shutdown01.c

...

Review

Quoted 11 lines
(quoted diff) +11 -0
+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"}

Would you mind to use
1) designated initializers (e.g. .recv_flag = MSG_DONTWAIT,)
2) stringify macro

IMHO it helps readability, that's why it's often used.

#define OP_DESC(x) .shutdown_op = x, .desc = #x

static struct tcase {
	int shutdown_op;
	int recv_flag;
	int recv_err;
	int send_flag;
	int send_err;
	char *desc;
} tcases[] = {
	{OP_DESC(SHUT_RD)},
	{OP_DESC(SHUT_WR), .recv_flag = MSG_DONTWAIT, .recv_err = EWOULDBLOCK,
		.send_flag = MSG_NOSIGNAL, .send_err = EPIPE},
	{OP_DESC(SHUT_RDWR), .send_flag = MSG_NOSIGNAL, .send_err = EPIPE}
};
Quoted 50 lines
(quoted diff) +50 -0
+};
+
+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};
+

Review

Quoted 10 lines
(quoted diff) +10 -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);

very nit:

	if (tc->recv_err)
		TST_EXP_FAIL(recv(client_sock, buff, MSGSIZE, tc->recv_flag), tc->recv_err);
	else
		SAFE_RECV(0, client_sock, buff, MSGSIZE, tc->recv_flag);

If you agree, I can merge with following changes.

Kind regards,
Petr

kernel/syscalls/shutdown/shutdown01.c +13 -12
index ba3853d9c..8e58f23e6 100644
@@ -19,17 +19,19 @@
 #define MSGSIZE 4
 #define SOCKETFILE "socket"
 
+#define OP_DESC(x) .shutdown_op = x, .desc = #x
 static struct tcase {
 	int shutdown_op;
 	int recv_flag;
 	int recv_err;
 	int send_flag;
 	int send_err;
-	char *flag_str;
+	char *desc;
 } 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"}
+	{OP_DESC(SHUT_RD)},
+	{OP_DESC(SHUT_WR), .recv_flag = MSG_DONTWAIT, .recv_err = EWOULDBLOCK,
+		.send_flag = MSG_NOSIGNAL, .send_err = EPIPE},
+	{OP_DESC(SHUT_RDWR), .send_flag = MSG_NOSIGNAL, .send_err = EPIPE}
 };
 
 static struct sockaddr_un *sock_addr;
@@ -82,23 +84,22 @@ static void run(unsigned int n)
 
 	client_sock = start_test();
 
-	tst_res(TINFO, "Testing %s flag", tc->flag_str);
+	tst_res(TINFO, "Testing %s flag", tc->desc);
 
 	TST_EXP_PASS(shutdown(client_sock, tc->shutdown_op));
 
-	if (!tc->recv_err)
-		SAFE_RECV(0, client_sock, buff, MSGSIZE, tc->recv_flag);
-	else
+	if (tc->recv_err)
 		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
+		SAFE_RECV(0, client_sock, buff, MSGSIZE, tc->recv_flag);
+
+	if (tc->send_err)
 		TST_EXP_FAIL(send(client_sock, buff, MSGSIZE, tc->send_flag), tc->send_err);
+	else
+		SAFE_SEND(MSGSIZE, client_sock, buff, MSGSIZE, tc->send_flag);
 
 	SAFE_CLOSE(client_sock);
 	TST_CHECKPOINT_WAKE(0);
-
 }
 
 static void setup(void)
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by Andrea Cervesato
#3 lore raw

Re: [LTP] [PATCH v2 1/2] Add shutdown01 test

Hi Petr,

I agree with the proposed changes. Thanks, feel free to merge.

Quoted 162 lines · Petr Vorel

Hi Andrea,

TL;DR: generally LGTM, I have few minor details, which I offer to fix before merge (diff below).

Quoted 1 line

+++ b/testcases/kernel/syscalls/shutdown/shutdown01.c

...

Review

Quoted 11 lines
(quoted diff) +11 -0
+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"}

Would you mind to use
1) designated initializers (e.g. .recv_flag = MSG_DONTWAIT,)
2) stringify macro

IMHO it helps readability, that's why it's often used.

#define OP_DESC(x) .shutdown_op = x, .desc = #x

static struct tcase {
	int shutdown_op;
	int recv_flag;
	int recv_err;
	int send_flag;
	int send_err;
	char *desc;
} tcases[] = {
	{OP_DESC(SHUT_RD)},
	{OP_DESC(SHUT_WR), .recv_flag = MSG_DONTWAIT, .recv_err = EWOULDBLOCK,
		.send_flag = MSG_NOSIGNAL, .send_err = EPIPE},
	{OP_DESC(SHUT_RDWR), .send_flag = MSG_NOSIGNAL, .send_err = EPIPE}
};
Quoted 50 lines
(quoted diff) +50 -0
+};
+
+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};
+

Review

Quoted 10 lines
(quoted diff) +10 -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);

very nit:

	if (tc->recv_err)
		TST_EXP_FAIL(recv(client_sock, buff, MSGSIZE, tc->recv_flag), tc->recv_err);
	else
		SAFE_RECV(0, client_sock, buff, MSGSIZE, tc->recv_flag);

If you agree, I can merge with following changes.

Kind regards,
Petr

kernel/syscalls/shutdown/shutdown01.c +11 -11
index ba3853d9c..8e58f23e6 100644
@@ -19,17 +19,19 @@
  #define MSGSIZE 4
  #define SOCKETFILE "socket"
 
+#define OP_DESC(x) .shutdown_op = x, .desc = #x
  static struct tcase {
  	int shutdown_op;
  	int recv_flag;
  	int recv_err;
  	int send_flag;
  	int send_err;
-	char *flag_str;
+	char *desc;
  } 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"}
+	{OP_DESC(SHUT_RD)},
+	{OP_DESC(SHUT_WR), .recv_flag = MSG_DONTWAIT, .recv_err = EWOULDBLOCK,
+		.send_flag = MSG_NOSIGNAL, .send_err = EPIPE},
+	{OP_DESC(SHUT_RDWR), .send_flag = MSG_NOSIGNAL, .send_err = EPIPE}
  };
 
  static struct sockaddr_un *sock_addr;
@@ -82,23 +84,22 @@ static void run(unsigned int n)
 
  	client_sock = start_test();
 
-	tst_res(TINFO, "Testing %s flag", tc->flag_str);
+	tst_res(TINFO, "Testing %s flag", tc->desc);
 
  	TST_EXP_PASS(shutdown(client_sock, tc->shutdown_op));
 
-	if (!tc->recv_err)
-		SAFE_RECV(0, client_sock, buff, MSGSIZE, tc->recv_flag);
-	else
+	if (tc->recv_err)
  		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
+		SAFE_RECV(0, client_sock, buff, MSGSIZE, tc->recv_flag);
+
+	if (tc->send_err)

kernel/syscalls/shutdown/shutdown01.c static void run(unsigned int n) · line 105

Quoted 10 lines
kernel/syscalls/shutdown/shutdown01.c +2 -1
  		TST_EXP_FAIL(send(client_sock, buff, MSGSIZE, tc->send_flag), tc->send_err);
+	else
+		SAFE_SEND(MSGSIZE, client_sock, buff, MSGSIZE, tc->send_flag);
 
  	SAFE_CLOSE(client_sock);
  	TST_CHECKPOINT_WAKE(0);
-
  }
 
  static void setup(void)

Andrea

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Re: [LTP] [PATCH v2 1/2] Add shutdown01 test

In reply to Andrea Cervesato

Hi Andrea,

this patch merged, thank you!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH v2 2/2 Add shutdown02 test

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

This test verifies the following shutdown() errors:

- EBADF sockfd is not a valid file descriptor
- EINVAL An invalid value was specified in how
- ENOTCONN The specified socket is not connected
- ENOTSOCK The file descriptor sockfd does not refer to a socket

---
 runtest/syscalls                                |  1 +
 testcases/kernel/syscalls/shutdown/.gitignore   |  1 +
 testcases/kernel/syscalls/shutdown/shutdown02.c | 76 +++++++++++++++++++++++++
 3 files changed, 78 insertions(+)
runtest/syscalls +1 -0
index dc396415e..44a577db3 100644
@@ -1466,6 +1466,7 @@ shmget05 shmget05
 shmget06 shmget06
 
 shutdown01 shutdown01
+shutdown02 shutdown02
 
 sigaction01 sigaction01
 sigaction02 sigaction02
testcases/kernel/syscalls/shutdown/.gitignore +1 -0
index 2df24d1ab..fd1ed807d 100644
@@ -1 +1,2 @@
 shutdown01
+shutdown02
testcases/kernel/syscalls/shutdown/shutdown02.c new file +76 -0

76 lines not on this page · raw message

Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v2 2/2
#6 lore raw

Re: [LTP] [PATCH v2 2/2] Add shutdown02 test

Hi Andrea,

...

testcases/kernel/syscalls/shutdown/shutdown02.c line 21

Quoted 3 lines
testcases/kernel/syscalls/shutdown/shutdown02.c new file +3 -0
+static int file_desc = -1;
+static int valid_sock = -1;
+static int invalid_sock = -1;

FYI many LTP tests (or even lib/tst_cgroup.c) just expect that 0 is for stdin, thus they happily expect 0 is invalid). e.g.

static int file_desc;

Then in cleanup:
if (valid_sock > 0)

	SAFE_CLOSE(valid_sock);

Sure, your way is correct, but I'd be also OK with 0.

(SAFE_SOCKET() and SAFE_CLOSE() also checks it should be >= 0 for valid file descriptor or just -1 for error).

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] [PATCH v2 2/2] Add shutdown02 test

Hi Petr,

Quoted 17 lines · Petr Vorel

Hi Andrea,

...

testcases/kernel/syscalls/shutdown/shutdown02.c line 21

Quoted 3 lines
testcases/kernel/syscalls/shutdown/shutdown02.c new file +3 -0
+static int file_desc = -1;
+static int valid_sock = -1;
+static int invalid_sock = -1;

FYI many LTP tests (or even lib/tst_cgroup.c) just expect that 0 is for stdin, thus they happily expect 0 is invalid). e.g.

static int file_desc;

Then in cleanup:
if (valid_sock > 0)

	SAFE_CLOSE(valid_sock);

Sure, your way is correct, but I'd be also OK with 0.

Quoted 7 lines

(SAFE_SOCKET() and SAFE_CLOSE() also checks it should be >= 0 for valid file descriptor or just -1 for error).

Kind regards,
Petr

I will fix it now

Andrea

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp