public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH 0/2] shutdown testing suite
@ 2024-05-27  6:30 Andrea Cervesato
  2024-05-27  6:30 ` [LTP] [PATCH 1/2] Add shutdown01 test Andrea Cervesato
  2024-05-27  6:30 ` [LTP] [PATCH 2/2] Add shutdown02 test Andrea Cervesato
  0 siblings, 2 replies; 5+ messages in thread
From: Andrea Cervesato @ 2024-05-27  6:30 UTC (permalink / raw)
  To: ltp

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.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
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 | 136 ++++++++++++++++++++++++
 testcases/kernel/syscalls/shutdown/shutdown02.c |  75 +++++++++++++
 5 files changed, 223 insertions(+)
---
base-commit: 0358f7a2788eed0cbff7f8519b1a0f7f89d36c13
change-id: 20240524-shutdown-c8d7580e916a

Best regards,
-- 
Andrea Cervesato <andrea.cervesato@suse.com>


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [LTP] [PATCH 1/2] Add shutdown01 test
  2024-05-27  6:30 [LTP] [PATCH 0/2] shutdown testing suite Andrea Cervesato
@ 2024-05-27  6:30 ` Andrea Cervesato
  2024-06-05 20:24   ` Petr Vorel
  2024-05-27  6:30 ` [LTP] [PATCH 2/2] Add shutdown02 test Andrea Cervesato
  1 sibling, 1 reply; 5+ messages in thread
From: Andrea Cervesato @ 2024-05-27  6:30 UTC (permalink / raw)
  To: ltp

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 | 136 ++++++++++++++++++++++++
 4 files changed, 146 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..b68b14ae8
--- /dev/null
+++ b/testcases/kernel/syscalls/shutdown/shutdown01.c
@@ -0,0 +1,136 @@
+// 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"
+
+#define MSGSIZE 4
+#define SOCKETFILE "socket"
+
+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 test_shutdown_recv(void)
+{
+	int client_sock;
+	char buff[MSGSIZE] = {0};
+
+	client_sock = start_test();
+
+	tst_res(TINFO, "Testing SHUT_RD flag");
+
+	TST_EXP_PASS(shutdown(client_sock, SHUT_RD));
+	TST_EXP_EQ_LI(recv(client_sock, buff, MSGSIZE, 0), 0);
+	TST_EXP_EQ_LI(send(client_sock, buff, MSGSIZE, 0), MSGSIZE);
+
+	SAFE_CLOSE(client_sock);
+	TST_CHECKPOINT_WAKE(0);
+}
+
+static void test_shutdown_send(void)
+{
+	int client_sock;
+	char buff[MSGSIZE] = {0};
+
+	client_sock = start_test();
+
+	tst_res(TINFO, "Testing SHUT_WR flag");
+
+	TST_EXP_PASS(shutdown(client_sock, SHUT_WR));
+	TST_EXP_FAIL(recv(client_sock, buff, MSGSIZE, MSG_DONTWAIT), EWOULDBLOCK);
+	TST_EXP_FAIL(send(client_sock, buff, MSGSIZE, MSG_NOSIGNAL), EPIPE);
+
+	SAFE_CLOSE(client_sock);
+	TST_CHECKPOINT_WAKE(0);
+}
+
+static void test_shutdown_both(void)
+{
+	int client_sock;
+	char buff[MSGSIZE] = {0};
+
+	client_sock = start_test();
+
+	tst_res(TINFO, "Testing SHUT_RDWR flag");
+
+	TST_EXP_PASS(shutdown(client_sock, SHUT_RDWR));
+	TST_EXP_EQ_LI(recv(client_sock, buff, MSGSIZE, 0), 0);
+	TST_EXP_FAIL(send(client_sock, buff, MSGSIZE, MSG_NOSIGNAL), EPIPE);
+
+	SAFE_CLOSE(client_sock);
+	TST_CHECKPOINT_WAKE(0);
+}
+
+static void run(void)
+{
+	test_shutdown_recv();
+	test_shutdown_send();
+	test_shutdown_both();
+}
+
+static void setup(void)
+{
+	sock_addr->sun_family = AF_UNIX;
+	memcpy(sock_addr->sun_path, SOCKETFILE, sizeof(SOCKETFILE));
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH 2/2] Add shutdown02 test
  2024-05-27  6:30 [LTP] [PATCH 0/2] shutdown testing suite Andrea Cervesato
  2024-05-27  6:30 ` [LTP] [PATCH 1/2] Add shutdown01 test Andrea Cervesato
@ 2024-05-27  6:30 ` Andrea Cervesato
  1 sibling, 0 replies; 5+ messages in thread
From: Andrea Cervesato @ 2024-05-27  6:30 UTC (permalink / raw)
  To: ltp

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

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

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 runtest/syscalls                                |  1 +
 testcases/kernel/syscalls/shutdown/.gitignore   |  1 +
 testcases/kernel/syscalls/shutdown/shutdown02.c | 75 +++++++++++++++++++++++++
 3 files changed, 77 insertions(+)

diff --git a/runtest/syscalls b/runtest/syscalls
index dc396415e..44a577db3 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1466,6 +1466,7 @@ shmget05 shmget05
 shmget06 shmget06
 
 shutdown01 shutdown01
+shutdown02 shutdown02
 
 sigaction01 sigaction01
 sigaction02 sigaction02
diff --git a/testcases/kernel/syscalls/shutdown/.gitignore b/testcases/kernel/syscalls/shutdown/.gitignore
index 2df24d1ab..fd1ed807d 100644
--- a/testcases/kernel/syscalls/shutdown/.gitignore
+++ b/testcases/kernel/syscalls/shutdown/.gitignore
@@ -1 +1,2 @@
 shutdown01
+shutdown02
diff --git a/testcases/kernel/syscalls/shutdown/shutdown02.c b/testcases/kernel/syscalls/shutdown/shutdown02.c
new file mode 100644
index 000000000..385e27f40
--- /dev/null
+++ b/testcases/kernel/syscalls/shutdown/shutdown02.c
@@ -0,0 +1,75 @@
+// 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() 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
+ */
+
+#include "tst_test.h"
+
+static int file_desc = -1;
+static int valid_sock = -1;
+static int invalid_sock = -1;
+
+static struct sockaddr_in *server_addr;
+
+static struct tcase {
+	int *socket;
+	int flags;
+	int error;
+} tcases[] = {
+	{&invalid_sock, PF_INET, EBADF},
+	{&valid_sock,   -1,      EINVAL},
+	{&valid_sock,   PF_INET, ENOTCONN},
+	{&file_desc,    PF_INET, ENOTSOCK},
+};
+
+static void run(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	TST_EXP_FAIL(shutdown(*tc->socket, tc->flags), tc->error);
+}
+
+static void setup(void)
+{
+	file_desc = SAFE_OPEN("notasocket", O_CREAT, 0640);
+	valid_sock = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
+
+	server_addr->sin_family = AF_INET;
+	server_addr->sin_port = 0;
+	server_addr->sin_addr.s_addr = INADDR_ANY;
+
+	SAFE_BIND(valid_sock,
+		(struct sockaddr *)server_addr,
+		sizeof(struct sockaddr_in));
+}
+
+static void cleanup(void)
+{
+	if (valid_sock != -1)
+		SAFE_CLOSE(valid_sock);
+
+	if (file_desc != -1)
+		SAFE_CLOSE(file_desc);
+}
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+	.bufs = (struct tst_buffers []) {
+		{&server_addr, .size = sizeof(struct sockaddr_in)},
+		{}
+	}
+};

-- 
2.35.3


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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [LTP] [PATCH 1/2] Add shutdown01 test
  2024-05-27  6:30 ` [LTP] [PATCH 1/2] Add shutdown01 test Andrea Cervesato
@ 2024-06-05 20:24   ` Petr Vorel
  2024-06-07  8:08     ` Andrea Cervesato via ltp
  0 siblings, 1 reply; 5+ messages in thread
From: Petr Vorel @ 2024-06-05 20:24 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Hi Andrea,

...
> diff --git a/testcases/kernel/syscalls/shutdown/shutdown01.c b/testcases/kernel/syscalls/shutdown/shutdown01.c
> new file mode 100644
> index 000000000..b68b14ae8
> --- /dev/null
> +++ b/testcases/kernel/syscalls/shutdown/shutdown01.c
> @@ -0,0 +1,136 @@
> +// 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:
nit: here needs to be blank line to get list formatting.

> + * * 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
> + */
...
> +static void test_shutdown_recv(void)
> +{
> +	int client_sock;
> +	char buff[MSGSIZE] = {0};
Wouldn't it be better to send some data?
Or is it not important, we use guarded buffers?

> +
> +	client_sock = start_test();
> +
> +	tst_res(TINFO, "Testing SHUT_RD flag");
> +
> +	TST_EXP_PASS(shutdown(client_sock, SHUT_RD));
> +	TST_EXP_EQ_LI(recv(client_sock, buff, MSGSIZE, 0), 0);
Interesting, I would expect failure ("If how is SHUT_RD, further receptions will
be disallowed.")

And maybe use SAFE_RECV()?

> +	TST_EXP_EQ_LI(send(client_sock, buff, MSGSIZE, 0), MSGSIZE);
And SAFE_SEND() here?
> +
> +	SAFE_CLOSE(client_sock);
> +	TST_CHECKPOINT_WAKE(0);
> +}

nit: I would also use struct tcase and .tcnt, because all 3 tests are nearly
identical.

Otherwise LGTM.

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Petr

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [LTP] [PATCH 1/2] Add shutdown01 test
  2024-06-05 20:24   ` Petr Vorel
@ 2024-06-07  8:08     ` Andrea Cervesato via ltp
  0 siblings, 0 replies; 5+ messages in thread
From: Andrea Cervesato via ltp @ 2024-06-07  8:08 UTC (permalink / raw)
  To: Petr Vorel, Andrea Cervesato; +Cc: ltp

Hi!

On 6/5/24 22:24, Petr Vorel wrote:
> Hi Andrea,
>
> ...
>> diff --git a/testcases/kernel/syscalls/shutdown/shutdown01.c b/testcases/kernel/syscalls/shutdown/shutdown01.c
>> new file mode 100644
>> index 000000000..b68b14ae8
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/shutdown/shutdown01.c
>> @@ -0,0 +1,136 @@
>> +// 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:
> nit: here needs to be blank line to get list formatting.
>
>> + * * 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
>> + */
> ...
>> +static void test_shutdown_recv(void)
>> +{
>> +	int client_sock;
>> +	char buff[MSGSIZE] = {0};
> Wouldn't it be better to send some data?
> Or is it not important, we use guarded buffers?
In this case, data is not really important because we need to test if 
send/recv are disabled after shutdown().
>
>> +
>> +	client_sock = start_test();
>> +
>> +	tst_res(TINFO, "Testing SHUT_RD flag");
>> +
>> +	TST_EXP_PASS(shutdown(client_sock, SHUT_RD));
>> +	TST_EXP_EQ_LI(recv(client_sock, buff, MSGSIZE, 0), 0);
> Interesting, I would expect failure ("If how is SHUT_RD, further receptions will
> be disallowed.")
>
> And maybe use SAFE_RECV()?
>
>> +	TST_EXP_EQ_LI(send(client_sock, buff, MSGSIZE, 0), MSGSIZE);
> And SAFE_SEND() here?
>> +
>> +	SAFE_CLOSE(client_sock);
>> +	TST_CHECKPOINT_WAKE(0);
>> +}
> nit: I would also use struct tcase and .tcnt, because all 3 tests are nearly
> identical.
>
> Otherwise LGTM.
>
> Reviewed-by: Petr Vorel <pvorel@suse.cz>
>
> Kind regards,
> Petr

I will fix the rest.

Andrea


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-06-07  8:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-27  6:30 [LTP] [PATCH 0/2] shutdown testing suite Andrea Cervesato
2024-05-27  6:30 ` [LTP] [PATCH 1/2] Add shutdown01 test Andrea Cervesato
2024-06-05 20:24   ` Petr Vorel
2024-06-07  8:08     ` Andrea Cervesato via ltp
2024-05-27  6:30 ` [LTP] [PATCH 2/2] Add shutdown02 test Andrea Cervesato

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox