From patchwork Fri Jul 27 07:30:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Moese X-Patchwork-Id: 950020 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.de Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41cLMW30Pgz9s0w for ; Fri, 27 Jul 2018 17:34:45 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id DA1EC3E755F for ; Fri, 27 Jul 2018 09:34:37 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [217.194.8.6]) by picard.linux.it (Postfix) with ESMTP id 23EBE3E7548 for ; Fri, 27 Jul 2018 09:34:35 +0200 (CEST) Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-6.smtp.seeweb.it (Postfix) with ESMTPS id F3A201401B27 for ; Fri, 27 Jul 2018 09:34:34 +0200 (CEST) Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id CF61BAD17 for ; Fri, 27 Jul 2018 07:34:33 +0000 (UTC) From: Michael Moese To: ltp@lists.linux.it Date: Fri, 27 Jul 2018 09:30:59 +0200 Message-Id: <20180727073057.22798-1-mmoese@suse.de> X-Mailer: git-send-email 2.18.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-0.0 required=7.0 tests=SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-6.smtp.seeweb.it Subject: [LTP] [PATCH v2] add new testcase for bind X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" With 0fb44559ffd6 af_unix: move unix_mknod() out of bindlock the behavior of bind() for STREAM UNIX sockets changed in case a socket that is already bound is passed to bind() again. This testcase fails for the new behavior and passes for the old one. Signed-off-by: Michael Moese Acked-by: Petr Vorel --- Changes to v1: I just cleaned the includes and comments as suggested. runtest/syscalls | 1 + testcases/kernel/syscalls/bind/.gitignore | 1 + testcases/kernel/syscalls/bind/bind03.c | 101 ++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 testcases/kernel/syscalls/bind/bind03.c diff --git a/runtest/syscalls b/runtest/syscalls index dc72484cb..e8b93a65b 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -29,6 +29,7 @@ asyncio02 asyncio02 bind01 bind01 bind02 bind02 +bind03 bind03 bdflush01 bdflush01 diff --git a/testcases/kernel/syscalls/bind/.gitignore b/testcases/kernel/syscalls/bind/.gitignore index ba46f4fa2..4ebea9ee7 100644 --- a/testcases/kernel/syscalls/bind/.gitignore +++ b/testcases/kernel/syscalls/bind/.gitignore @@ -1,2 +1,3 @@ /bind01 /bind02 +/bind03 diff --git a/testcases/kernel/syscalls/bind/bind03.c b/testcases/kernel/syscalls/bind/bind03.c new file mode 100644 index 000000000..9afb1cd99 --- /dev/null +++ b/testcases/kernel/syscalls/bind/bind03.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2018 Michael Moese + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +/* The commit 0fb44559ffd6 af_unix: move unix_mknod() out of bindlock + * changed the behavior of bind() for STREAM UNIX domain sockets if + */ + +#include +#include +#include +#include +#include + +#include "tst_test.h" +#include "tst_safe_net.h" + +#define SOCK_NAME "socket.1" + + +static int sock1, sock2; + +static char dir_path[PATH_MAX]; +static char sockpath[PATH_MAX]; + +void run(void) +{ + struct sockaddr_un sun; + + sock1 = SAFE_SOCKET(PF_UNIX, SOCK_STREAM, 0); + + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + if (snprintf(sockpath, sizeof(sockpath), "%s/%s", dir_path, SOCK_NAME) + >= PATH_MAX) { + tst_res(TBROK, "bind_test: snprintf(sockpath)"); + return; + } + if (snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", sockpath) + >= (int) sizeof(sun.sun_path)) { + tst_res(TBROK, "bind_test: snprintf(sun.sun_path)"); + return; + } + + SAFE_BIND(sock1, (struct sockaddr *)&sun, sizeof(sun)); + + /* + * Once a STREAM UNIX domain socket has been bound, it can't be + * rebound. Expected error is EINVAL. + */ + if (bind(sock1, (struct sockaddr *)&sun, sizeof(sun)) == 0) { + tst_res(TFAIL, "re-binding of socket succeeded"); + return; + } + + if (errno != EINVAL) { + tst_res(TFAIL | TERRNO, "expected EINVAL"); + return; + } + + sock2 = SAFE_SOCKET(PF_UNIX, SOCK_STREAM, 0); + + /* + * Since a socket is already bound to the pathname, it can't be bound + * to a second socket. Expected error is EADDRINUSE. + */ + if (bind(sock2, (struct sockaddr *)&sun, sizeof(sun)) == 0) { + tst_res(TFAIL, "bind() succeeded with already bound pathname!"); + return; + } + + if (errno != EADDRINUSE) { + tst_res(TFAIL | TERRNO, "expected to fail with EADDRINUSE"); + return; + } + + tst_res(TPASS, "bind() failed with EADDRINUSE as expected"); +} + + +static void setup(void) +{ + strncpy(dir_path, "/tmp/unix_bind.XXXXXXX", PATH_MAX); + if (mkdtemp(dir_path) == NULL) + tst_res(TBROK, "cannot create temporary directory"); +} + +static void cleanup(void) +{ + close(sock1); + close(sock2); + unlink(sockpath); + rmdir(dir_path); +} + +static struct tst_test test = { + .setup = setup, + .cleanup = cleanup, + .test_all = run, +};