[v2] socket: Add new test for shutdown
Checks
Context |
Check |
Description |
redhat-pt-bot/TryBot-apply_patch |
success
|
Patch applied to master at the time it was sent
|
redhat-pt-bot/TryBot-32bit |
success
|
Build for i686
|
linaro-tcwg-bot/tcwg_glibc_build--master-arm |
success
|
Build passed
|
linaro-tcwg-bot/tcwg_glibc_check--master-arm |
success
|
Test passed
|
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 |
success
|
Build passed
|
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 |
success
|
Test passed
|
Commit Message
This commit adds shutdown test with SHUT_RD, SHUT_WR, SHUT_RDWR for an
UNIX socket connection.
---
Changes from v1:
initialize peer_len in do_test_shut_rdwr function.
---
socket/Makefile | 1 +
socket/tst-shutdown.c | 245 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 246 insertions(+)
create mode 100644 socket/tst-shutdown.c
Comments
Sergey Kolosov <skolosov@redhat.com> writes:
> This commit adds shutdown test with SHUT_RD, SHUT_WR, SHUT_RDWR for an
> UNIX socket connection.
Two changes needed:
1. Comments need two spaces after period.
2. SHUT_RDWR test is missing test for SHUT_RD portion.
> + tst-shutdown \
Ok.
> diff --git a/socket/tst-shutdown.c b/socket/tst-shutdown.c
> +/* Test the shutdown function.
> + Copyright (C) 2024 Free Software Foundation, Inc.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library is free software; you can redistribute it and/or
> + modify it under the terms of the GNU Lesser General Public
> + License as published by the Free Software Foundation; either
> + version 2.1 of the License, or (at your option) any later version.
> +
> + The GNU C Library is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with the GNU C Library; if not, see
> + <https://www.gnu.org/licenses/>. */
> +
> +#include <arpa/inet.h>
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <signal.h>
> +#include <stdbool.h>
> +#include <support/check.h>
> +#include <support/xsocket.h>
> +#include <support/xunistd.h>
> +#include <sys/socket.h>
> +#include <stdio.h>
> +
> +#include <fcntl.h>
> +#include <string.h>
> +#include <support/support.h>
> +#include <support/xsignal.h>
> +#include <support/temp_file.h>
Ok.
> +struct connection
> +{
> + int sockets[2];
> +};
> +
> +void
> +establish_connection (struct connection *conn)
> +{
> + if (socketpair (AF_UNIX, SOCK_STREAM, 0, conn->sockets) != 0)
> + {
> + FAIL_EXIT1 ("socketpair (AF_UNIX, SOCK_STREAM, 0): %m\n");
> + }
> +}
Ok.
> +void
> +close_connection (struct connection *conn)
> +{
> + xclose (conn->sockets[0]);
> + xclose (conn->sockets[1]);
> +}
Ok.
> +/* Open a file and check that shutdown fails with the ENOTSOCK error code. */
Note two spaces after period please.
> +void
> +do_test_enotsock (void)
> +{
> + char *tmp_dir = support_create_temp_directory ("tst-shutdown-");
> + char *path_to_file = xasprintf ("%s/shutdown.txt", tmp_dir);
> +
> + /* open file and check that shutdown will fail with ENOTSOCK. */
> + int fd = xopen (path_to_file, O_CREAT | O_RDWR, S_IRWXU);
Ok.
> + int result = shutdown (fd, SHUT_RD);
> + if (result == 0 || errno != ENOTSOCK)
> + {
> + FAIL_EXIT1 ("shutdown should fail with ENOTSOCK");
> + }
> + xclose (fd);
Ok.
> + /* remove file, temp directory will be removed itself. */
> + xunlink (path_to_file);
> +}
Ok.
> +/* Test shutdown with SHUT_RD. */
> +void
> +do_test_shut_rd (void)
> +{
> + struct connection conn;
> + const char *str = "AAAAAAA";
> + int len = 8;
> + int ret;
> + void *s_buf = xmalloc (len);
> + bzero (s_buf, len);
> +
> + establish_connection (&conn);
> + int server = conn.sockets[0];
> + int client = conn.sockets[1];
Ok.
> + /* call shutdown with SHUT_RD on server socket. */
> + if (shutdown (server, SHUT_RD) != 0)
> + {
> + FAIL_EXIT1 ("shutdown with SHUT_RD on socket %d failed", server);
> + }
Reads on server should return 0 now, ok.
> + ret = send (server, str, len, 0);
> + if (ret <= 0)
> + {
> + FAIL_EXIT1 ("send (%d, data, %d): %m", server, len);
> + }
writes to server should still be ok, ok.
> + ret = recv (client, s_buf, len, 0);
> + if (ret <= 0)
> + {
> + FAIL_EXIT1 ("recv (%d, data, %d): %m", client, len);
> + }
Read data just sent, ok.
> + TEST_COMPARE_BLOB (str, len, s_buf, len);
Ok.
> + /* send data should be disallowed on shutdown socket. */
> + errno = 0;
> + ret = send (client, str, len, MSG_NOSIGNAL);
> + if (ret >= 0 || errno != EPIPE)
> + {
> + FAIL_EXIT1 ("Send on SHUT_RD socket should be disallowed: %m");
> + }
Ok.
> + /* recv should return zero and no error. */
> + errno = 0;
> + ret = recv (server, s_buf, len, 0);
> + if (ret != 0 || errno != 0)
> + {
> + FAIL_EXIT1 ("recv should return 0 without error: %m");
> + }
Ok.
> + close_connection (&conn);
> +}
Ok.
> +/* Test shutdown with SHUT_WR. */
> +void
> +do_test_shut_wr (void)
> +{
> + struct connection conn;
> + const char *str1 = "CCCCCCC";
> + const char *str2 = "DDDDDDD";
> + const char *str3 = "EEEEEEE";
> + int len = 8;
> + int ret;
> + void *c_buf = xmalloc (len);
> + void *s_buf = xmalloc (len);
> +
> + establish_connection (&conn);
> + int server = conn.sockets[0];
> + int client = conn.sockets[1];
Ok.
> + xwrite (client, str1, len);
pre-fill, ok.
> + if (shutdown (client, SHUT_WR) != 0)
> + {
> + FAIL_EXIT1 ("shutdown with SHUT_WR on socket %d failed", client);
> + }
No more writes, ok.
> + ret = send (client, str2, len, MSG_NOSIGNAL);
> + if (ret >= 0)
> + {
> + FAIL_EXIT1 ("send on SHUT_WR socket should fail");
> + }
Ok.
> + /* read data written before shutdown and check if it's correct. */
> + xread (server, s_buf, len);
> + TEST_COMPARE_BLOB (str1, len, s_buf, len);
Ok.
> + /* Second read should return zero without error. */
> + errno = 0;
> + if (read (server, s_buf, len) != 0 || errno != 0)
> + {
> + FAIL_EXIT1 ("read after shutdown should return zero without error: %m");
> + }
Ok.
> + /* write some data to socket and check it still can be read on other side. */
> + memcpy (s_buf, str3, len);
> + xwrite (server, s_buf, len);
> +
> + xread (client, c_buf, len);
> + TEST_COMPARE_BLOB (s_buf, len, c_buf, len);
Ok.
> + close_connection (&conn);
> +}
Ok.
> +/* Test shutdown with SHUT_RDWR. */
> +void
> +do_test_shut_rdwr (void)
> +{
> + struct connection conn;
> + struct sockaddr peer;
> + socklen_t peer_len = sizeof (peer);
> +
> + const char *str = "GGGGGGG";
> + int len = 8;
> + int ret;
> + void *s_buf = xmalloc (len);
> + bzero (s_buf, len);
> +
> + establish_connection (&conn);
> + int server = conn.sockets[0];
> + int client = conn.sockets[1];
Ok.
> + xwrite (client, str, len);
Fill buffer, ok
> + if (shutdown (client, SHUT_RDWR) != 0)
> + {
> + FAIL_EXIT1 ("shutdown with SHUT_RDWR on socket %d failed", client);
> + }
Ok.
> + /* verify that socket is still connected. */
> + xgetsockname (client, &peer, &peer_len);
Ok.
> + /* read data written before shutdown. */
> + xread (server, s_buf, len);
> + TEST_COMPARE_BLOB (s_buf, len, str, len);
Ok.
> + /* second read should return zero, but no error. */
> + errno = 0;
> + if (read (server, s_buf, len) != 0 || errno != 0)
> + {
> + FAIL_EXIT1 ("read after shutdown should return zero without error: %m");
> + }
This tests the SHUT_WR side, Ok.
> + /* send some data to shutdown socket and expect error. */
> + errno = 0;
> + ret = send (client, str, len, MSG_NOSIGNAL);
> + if (ret >= 0 || errno != EPIPE)
> + {
> + FAIL_EXIT1 ("send to RDWR shutdown socket should fail with EPIPE");
> + }
This also tests the SHUT_WR side.
> + close_connection (&conn);
> +}
Where is the test for the SHUT_RD side?
> +static int
> +do_test (void)
> +{
> + do_test_enotsock ();
> + do_test_shut_rd ();
> + do_test_shut_wr ();
> + do_test_shut_rdwr ();
> +
> + return 0;
> +}
> +
> +#include <support/test-driver.c>
Ok.
@@ -71,6 +71,7 @@ tests := \
tst-cmsg_cloexec \
tst-cmsghdr \
tst-connect \
+ tst-shutdown \
tst-sockopt \
# tests
new file mode 100644
@@ -0,0 +1,245 @@
+/* Test the shutdown function.
+ Copyright (C) 2024 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <support/check.h>
+#include <support/xsocket.h>
+#include <support/xunistd.h>
+#include <sys/socket.h>
+#include <stdio.h>
+
+#include <fcntl.h>
+#include <string.h>
+#include <support/support.h>
+#include <support/xsignal.h>
+#include <support/temp_file.h>
+
+struct connection
+{
+ int sockets[2];
+};
+
+void
+establish_connection (struct connection *conn)
+{
+ if (socketpair (AF_UNIX, SOCK_STREAM, 0, conn->sockets) != 0)
+ {
+ FAIL_EXIT1 ("socketpair (AF_UNIX, SOCK_STREAM, 0): %m\n");
+ }
+}
+
+void
+close_connection (struct connection *conn)
+{
+ xclose (conn->sockets[0]);
+ xclose (conn->sockets[1]);
+}
+
+/* Open a file and check that shutdown fails with the ENOTSOCK error code. */
+void
+do_test_enotsock (void)
+{
+ char *tmp_dir = support_create_temp_directory ("tst-shutdown-");
+ char *path_to_file = xasprintf ("%s/shutdown.txt", tmp_dir);
+
+ /* open file and check that shutdown will fail with ENOTSOCK. */
+ int fd = xopen (path_to_file, O_CREAT | O_RDWR, S_IRWXU);
+
+ int result = shutdown (fd, SHUT_RD);
+ if (result == 0 || errno != ENOTSOCK)
+ {
+ FAIL_EXIT1 ("shutdown should fail with ENOTSOCK");
+ }
+ xclose (fd);
+
+ /* remove file, temp directory will be removed itself. */
+ xunlink (path_to_file);
+}
+
+/* Test shutdown with SHUT_RD. */
+void
+do_test_shut_rd (void)
+{
+ struct connection conn;
+ const char *str = "AAAAAAA";
+ int len = 8;
+ int ret;
+ void *s_buf = xmalloc (len);
+ bzero (s_buf, len);
+
+ establish_connection (&conn);
+ int server = conn.sockets[0];
+ int client = conn.sockets[1];
+
+ /* call shutdown with SHUT_RD on server socket. */
+ if (shutdown (server, SHUT_RD) != 0)
+ {
+ FAIL_EXIT1 ("shutdown with SHUT_RD on socket %d failed", server);
+ }
+
+ ret = send (server, str, len, 0);
+ if (ret <= 0)
+ {
+ FAIL_EXIT1 ("send (%d, data, %d): %m", server, len);
+ }
+
+ ret = recv (client, s_buf, len, 0);
+ if (ret <= 0)
+ {
+ FAIL_EXIT1 ("recv (%d, data, %d): %m", client, len);
+ }
+
+ TEST_COMPARE_BLOB (str, len, s_buf, len);
+
+ /* send data should be disallowed on shutdown socket. */
+ errno = 0;
+ ret = send (client, str, len, MSG_NOSIGNAL);
+ if (ret >= 0 || errno != EPIPE)
+ {
+ FAIL_EXIT1 ("Send on SHUT_RD socket should be disallowed: %m");
+ }
+
+ /* recv should return zero and no error. */
+ errno = 0;
+ ret = recv (server, s_buf, len, 0);
+ if (ret != 0 || errno != 0)
+ {
+ FAIL_EXIT1 ("recv should return 0 without error: %m");
+ }
+
+ close_connection (&conn);
+}
+
+/* Test shutdown with SHUT_WR. */
+void
+do_test_shut_wr (void)
+{
+ struct connection conn;
+ const char *str1 = "CCCCCCC";
+ const char *str2 = "DDDDDDD";
+ const char *str3 = "EEEEEEE";
+ int len = 8;
+ int ret;
+ void *c_buf = xmalloc (len);
+ void *s_buf = xmalloc (len);
+
+ establish_connection (&conn);
+ int server = conn.sockets[0];
+ int client = conn.sockets[1];
+
+ xwrite (client, str1, len);
+
+ if (shutdown (client, SHUT_WR) != 0)
+ {
+ FAIL_EXIT1 ("shutdown with SHUT_WR on socket %d failed", client);
+ }
+
+ ret = send (client, str2, len, MSG_NOSIGNAL);
+ if (ret >= 0)
+ {
+ FAIL_EXIT1 ("send on SHUT_WR socket should fail");
+ }
+
+ /* read data written before shutdown and check if it's correct. */
+ xread (server, s_buf, len);
+ TEST_COMPARE_BLOB (str1, len, s_buf, len);
+
+ /* Second read should return zero without error. */
+ errno = 0;
+ if (read (server, s_buf, len) != 0 || errno != 0)
+ {
+ FAIL_EXIT1 ("read after shutdown should return zero without error: %m");
+ }
+
+ /* write some data to socket and check it still can be read on other side. */
+ memcpy (s_buf, str3, len);
+ xwrite (server, s_buf, len);
+
+ xread (client, c_buf, len);
+ TEST_COMPARE_BLOB (s_buf, len, c_buf, len);
+
+ close_connection (&conn);
+}
+
+/* Test shutdown with SHUT_RDWR. */
+void
+do_test_shut_rdwr (void)
+{
+ struct connection conn;
+ struct sockaddr peer;
+ socklen_t peer_len = sizeof (peer);
+
+ const char *str = "GGGGGGG";
+ int len = 8;
+ int ret;
+ void *s_buf = xmalloc (len);
+ bzero (s_buf, len);
+
+ establish_connection (&conn);
+ int server = conn.sockets[0];
+ int client = conn.sockets[1];
+
+ xwrite (client, str, len);
+
+ if (shutdown (client, SHUT_RDWR) != 0)
+ {
+ FAIL_EXIT1 ("shutdown with SHUT_RDWR on socket %d failed", client);
+ }
+
+ /* verify that socket is still connected. */
+ xgetsockname (client, &peer, &peer_len);
+
+ /* read data written before shutdown. */
+ xread (server, s_buf, len);
+ TEST_COMPARE_BLOB (s_buf, len, str, len);
+
+ /* second read should return zero, but no error. */
+ errno = 0;
+ if (read (server, s_buf, len) != 0 || errno != 0)
+ {
+ FAIL_EXIT1 ("read after shutdown should return zero without error: %m");
+ }
+
+ /* send some data to shutdown socket and expect error. */
+ errno = 0;
+ ret = send (client, str, len, MSG_NOSIGNAL);
+ if (ret >= 0 || errno != EPIPE)
+ {
+ FAIL_EXIT1 ("send to RDWR shutdown socket should fail with EPIPE");
+ }
+
+ close_connection (&conn);
+}
+
+
+static int
+do_test (void)
+{
+ do_test_enotsock ();
+ do_test_shut_rd ();
+ do_test_shut_wr ();
+ do_test_shut_rdwr ();
+
+ return 0;
+}
+
+#include <support/test-driver.c>