diff mbox series

[2/2] Add shutdown02 test

Message ID 20240527-shutdown-v1-2-1feffca5e3df@suse.com
State Accepted
Headers show
Series shutdown testing suite | expand

Commit Message

Andrea Cervesato May 27, 2024, 6:30 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 | 75 +++++++++++++++++++++++++
 3 files changed, 77 insertions(+)
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..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)},
+		{}
+	}
+};