From patchwork Mon Jun 24 21:58:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Kolosov X-Patchwork-Id: 1951825 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=TgmKQRbg; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W7MLc1LsXz20X6 for ; Tue, 25 Jun 2024 07:59:20 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 59C54384AB7D for ; Mon, 24 Jun 2024 21:59:18 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 0649B3858D35 for ; Mon, 24 Jun 2024 21:58:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0649B3858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0649B3858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719266339; cv=none; b=JVMTNJy5gLXwZlBMBwwTep8HbMdFAul0y3yq7QLWKJBMMdjCX3gyLrZsYSVS+4NlN1xlIU/EFKifYfRrcP2svdYc4qERjn9PwYaN7ASHURmi1d08m8ARxXI+sFJKRvlcXvPhAkA6H1cgG/Fo8FVD0dcqeeZrkp5tW6/E+OB8y3I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719266339; c=relaxed/simple; bh=IoEFYn6rK3AZXVMZFc4zWGSsGlslE0P5CqFQESY8u1U=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=geEy8ZkSP+1ucrYdVFO2WLZtcQLn1rzWFXa3cHQBlk3aJcE4xDXv2IjqWUk94G31O5mAr9NBpWoHcvWHZJkwMPYP9sMHePow1+RYnoazmBg/2peFl/T9mmyYPFCWhl2goxMcIhjGpA/GjNqEcgyRq9YEMBhlBGrJ0Htym/Vdgcw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719266337; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=lLWLQ5779uqfC+PT4nRfdyeOlNC+JB72nqLl2zzUxQc=; b=TgmKQRbg4ZPebTm7D1zmzaocy1+BGQBxsyVf7aOG+vqeRE/SSwYHVE38hzMlJrB3PHREpg M/yNyD68F75UB44V7HxlmeuTC/1dryovuuhphecAhksxnmc1r0Ga0wcXzYI/uF5MneUOHI 7UHzn5cfipDVbmm0Y6iHhwzVXa0rQII= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-645-62g5XVA6NrWAHPp2LJ7sIg-1; Mon, 24 Jun 2024 17:58:56 -0400 X-MC-Unique: 62g5XVA6NrWAHPp2LJ7sIg-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 935621956096 for ; Mon, 24 Jun 2024 21:58:55 +0000 (UTC) Received: from sk-carbon-x9.redhat.com (unknown [10.45.225.184]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7DCDA300021A; Mon, 24 Jun 2024 21:58:54 +0000 (UTC) From: Sergey Kolosov To: libc-alpha@sourceware.org Cc: Sergey Kolosov Subject: [PATCH v3] socket: Add new test for shutdown Date: Mon, 24 Jun 2024 23:58:24 +0200 Message-ID: <20240624215850.106338-1-skolosov@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org This commit adds shutdown test with SHUT_RD, SHUT_WR, SHUT_RDWR for an UNIX socket connection. --- Changes from v2: 1. Fix two spaces after period in comments. 2. Add test for SHUT_RD portion in SHUT_RDWR test. --- socket/Makefile | 1 + socket/tst-shutdown.c | 267 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 268 insertions(+) create mode 100644 socket/tst-shutdown.c diff --git a/socket/Makefile b/socket/Makefile index fc1bd0a260..df732fa9b7 100644 --- a/socket/Makefile +++ b/socket/Makefile @@ -71,6 +71,7 @@ tests := \ tst-cmsg_cloexec \ tst-cmsghdr \ tst-connect \ + tst-shutdown \ tst-sockopt \ # tests diff --git a/socket/tst-shutdown.c b/socket/tst-shutdown.c new file mode 100644 index 0000000000..e168fd06bf --- /dev/null +++ b/socket/tst-shutdown.c @@ -0,0 +1,267 @@ +/* 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +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 *str1 = "FFFFFFF"; + const char *str2 = "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]; + + /* Send some data to both sockets before shutdown. */ + xwrite (client, str1, len); + xwrite (server, str2, len); + + /* Call shutdown with SHUT_RDWR on client socket. */ + 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 (client, s_buf, len); + TEST_COMPARE_BLOB (s_buf, len, str2, len); + + /* Second read should return zero, but no error. */ + errno = 0; + if (read (client, 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 (server, str2, len, MSG_NOSIGNAL); + if (ret >= 0 || errno != EPIPE) + { + FAIL_EXIT1 ("send to RDWR shutdown socket should fail with EPIPE"); + } + + /* Read data written before shutdown. */ + xread (server, s_buf, len); + TEST_COMPARE_BLOB (s_buf, len, str1, 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, str1, 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