diff mbox series

[v2,2/2] Add shutdown02 test

Message ID 20240607-shutdown-v2-2-a09ce3290ee1@suse.com
State Changes Requested
Headers show
Series shutdown testing suite | expand

Commit Message

Andrea Cervesato June 7, 2024, 8:13 a.m. UTC
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 | 76 +++++++++++++++++++++++++
 3 files changed, 78 insertions(+)

Comments

Petr Vorel June 10, 2024, 1:44 p.m. UTC | #1
Hi Andrea,

...
> +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).

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

Kind regards,
Petr
Andrea Cervesato June 10, 2024, 2:07 p.m. UTC | #2
Hi Petr,

On 6/10/24 15:44, Petr Vorel wrote:
> Hi Andrea,
>
> ...
>> +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).
>
> Reviewed-by: Petr Vorel <pvorel@suse.cz>
>
> Kind regards,
> Petr

I will fix it now

Andrea
diff mbox series

Patch

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..4aae8469f
--- /dev/null
+++ b/testcases/kernel/syscalls/shutdown/shutdown02.c
@@ -0,0 +1,76 @@ 
+// 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)},
+		{}
+	}
+};