From patchwork Tue Sep 17 10:17:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Doucha X-Patchwork-Id: 1163273 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=2001:1418:10:5::2; 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.cz Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46XfFP3p4Rz9s00 for ; Tue, 17 Sep 2019 20:18:01 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id E64403C20CC for ; Tue, 17 Sep 2019 12:17:58 +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 [IPv6:2001:4b78:1:20::6]) by picard.linux.it (Postfix) with ESMTP id 9CAA13C2079 for ; Tue, 17 Sep 2019 12:17:41 +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 80490140174E for ; Tue, 17 Sep 2019 12:17:40 +0200 (CEST) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id B9B25AF58; Tue, 17 Sep 2019 10:17:39 +0000 (UTC) From: Martin Doucha To: ltp@lists.linux.it Date: Tue, 17 Sep 2019 12:17:05 +0200 Message-Id: <20190917101706.10013-2-mdoucha@suse.cz> X-Mailer: git-send-email 2.22.1 In-Reply-To: <20190917101706.10013-1-mdoucha@suse.cz> References: <20190917101706.10013-1-mdoucha@suse.cz> MIME-Version: 1.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_HELO_NONE,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 1/2] Update syscalls/fsync03 to new API X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Martin Doucha Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Martin Doucha --- testcases/kernel/syscalls/fsync/fsync03.c | 152 +++++----------------- 1 file changed, 34 insertions(+), 118 deletions(-) diff --git a/testcases/kernel/syscalls/fsync/fsync03.c b/testcases/kernel/syscalls/fsync/fsync03.c index 60d15f429..82fd52070 100644 --- a/testcases/kernel/syscalls/fsync/fsync03.c +++ b/testcases/kernel/syscalls/fsync/fsync03.c @@ -1,141 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* - * - * Copyright (c) International Business Machines Corp., 2001 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Copyright (c) Wayne Boyer, International Business Machines Corp., 2001 + * Copyright (c) 2019 Martin Doucha */ /* - * NAME - * fsync03.c - * - * DESCRIPTION - * Testcase to check that fsync(2) sets errno correctly. - * - * ALGORITHM - * 1. Call fsync() with an invalid fd, and test for EBADF. - * 2. Call fsync() on a pipe(fd), and expect EINVAL. - * - * USAGE: - * fsync03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] - * where, -c n : Run n copies concurrently. - * -e : Turn on errno logging. - * -i n : Execute test n times. - * -I x : Execute test for x seconds. - * -P x : Pause for x seconds between iterations. - * -t : Turn on syscall timing. - * - * HISTORY - * 07/2001 Ported by Wayne Boyer - * - * RESTRICTIONS - * NONE + * Test Description: + * Testcase to check that fsync(2) sets errno correctly. + * 1. Call fsync() with an invalid fd, and test for EBADF. + * 2. Call fsync() on a pipe(fd), and expect EINVAL. */ #include #include -#include "test.h" -#include "safe_macros.h" +#include "tst_test.h" -void setup(void); -void cleanup(void); +static int pfd[2]; /* fd's for the pipe() call in setup() */ +static int bfd = -1; /* an invalid fd */ -int fd[2]; /* fd's for the pipe() call in setup() */ -int pfd; /* holds the value for fd[1] */ -int bfd = -1; /* an invalid fd */ - -struct test_case_t { +struct test_case { int *fd; int error; } TC[] = { /* EBADF - fd is invalid (-1) */ - { - &bfd, EBADF}, - /* EINVAL - fsync() on pipe should not succeed. */ - { - &pfd, EINVAL} + {&bfd, EBADF}, + /* EINVAL - fsync() on pipe should not succeed. */ + {pfd, EINVAL} }; -char *TCID = "fsync03"; -int TST_TOTAL = 2; - -int main(int ac, char **av) -{ - int lc; - int i; - - tst_parse_opts(ac, av, NULL, NULL); - - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - - tst_count = 0; +static void test_fsync(unsigned int n) { + struct test_case *tc = TC + n; - /* loop through the test cases */ - for (i = 0; i < TST_TOTAL; i++) { - - TEST(fsync(*(TC[i].fd))); - - if (TEST_RETURN != -1) { - tst_resm(TFAIL, "call succeeded unexpectedly"); - continue; - } - - if (TEST_ERRNO == TC[i].error) { - tst_resm(TPASS, "expected failure - " - "errno = %d : %s", TEST_ERRNO, - strerror(TEST_ERRNO)); - } else { - tst_resm(TFAIL, "unexpected error - %d : %s - " - "expected %d", TEST_ERRNO, - strerror(TEST_ERRNO), TC[i].error); - } - } + if (!fsync(*tc->fd)) { + tst_res(TFAIL, "fsync() succeeded unexpectedly"); + } else if (errno != tc->error) { + tst_res(TFAIL | TERRNO, "Unexpected error"); + } else { + tst_res(TPASS, "fsync() failed as expected"); } - cleanup(); - - tst_exit(); } -/* - * setup() - performs all ONE TIME setup for this test. - */ -void setup(void) -{ - - tst_sig(NOFORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - /* make a temporary directory and cd to it */ - tst_tmpdir(); - - SAFE_PIPE(cleanup, fd); - - pfd = fd[1]; +static void setup(void) { + SAFE_PIPE(pfd); } -/* - * cleanup() - performs all ONE TIME cleanup for this test at - * completion or premature exit. - */ -void cleanup(void) -{ - - /* delete the test directory created in setup() */ - tst_rmdir(); - +static void cleanup(void) { + close(pfd[0]); + close(pfd[1]); } + +static struct tst_test test = { + .test = test_fsync, + .tcnt = ARRAY_SIZE(TC), + .setup = setup, + .cleanup = cleanup +}; From patchwork Tue Sep 17 10:17:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Doucha X-Patchwork-Id: 1163272 X-Patchwork-Delegate: petr.vorel@gmail.com 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=2001:1418:10:5::2; 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.cz Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46XfFM5PHfz9sCJ for ; Tue, 17 Sep 2019 20:17:59 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 7D1203C20AA for ; Tue, 17 Sep 2019 12:17:51 +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 [IPv6:2001:4b78:1:20::6]) by picard.linux.it (Postfix) with ESMTP id 9A4B93C2084 for ; Tue, 17 Sep 2019 12:17:41 +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 7860F140174B for ; Tue, 17 Sep 2019 12:17:40 +0200 (CEST) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id B9B02AF4E; Tue, 17 Sep 2019 10:17:39 +0000 (UTC) From: Martin Doucha To: ltp@lists.linux.it Date: Tue, 17 Sep 2019 12:17:06 +0200 Message-Id: <20190917101706.10013-3-mdoucha@suse.cz> X-Mailer: git-send-email 2.22.1 In-Reply-To: <20190917101706.10013-1-mdoucha@suse.cz> References: <20190917101706.10013-1-mdoucha@suse.cz> MIME-Version: 1.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_HELO_NONE,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 2/2] Improve coverage in syscalls/fsync03 X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Martin Doucha Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Adds the following test cases where fsync is supposed to fail: - Closed non-negative file descriptor (EBADF) - FIFO (EINVAL) - Socket (EINVAL) Signed-off-by: Martin Doucha --- testcases/kernel/syscalls/fsync/fsync03.c | 83 ++++++++++++++++++----- 1 file changed, 65 insertions(+), 18 deletions(-) diff --git a/testcases/kernel/syscalls/fsync/fsync03.c b/testcases/kernel/syscalls/fsync/fsync03.c index 82fd52070..d574b07d7 100644 --- a/testcases/kernel/syscalls/fsync/fsync03.c +++ b/testcases/kernel/syscalls/fsync/fsync03.c @@ -7,31 +7,75 @@ /* * Test Description: * Testcase to check that fsync(2) sets errno correctly. - * 1. Call fsync() with an invalid fd, and test for EBADF. - * 2. Call fsync() on a pipe(fd), and expect EINVAL. + * 1. Call fsync() on a pipe(fd), and expect EINVAL. + * 2. Call fsync() on a socket(fd), and expect EINVAL. + * 3. Call fsync() on a closed fd, and test for EBADF. + * 4. Call fsync() on an invalid fd, and test for EBADF. + * 5. Call fsync() on a fifo(fd), and expect EINVAL. */ #include #include #include "tst_test.h" -static int pfd[2]; /* fd's for the pipe() call in setup() */ -static int bfd = -1; /* an invalid fd */ +#define FIFO_PATH "fifo" + +#define PIPE_CASE 0 +#define SOCKET_CASE 1 +#define CLOSED_CASE 2 + +/* fd's for the pipe() call in setup() */ +static int pfd[2]; +/* FIFO must be opened for reading first, otherwise open(fifo, O_WRONLY) + will block. */ +static int fifo_rfd; struct test_case { - int *fd; + int fd; int error; -} TC[] = { - /* EBADF - fd is invalid (-1) */ - {&bfd, EBADF}, + const char *path; +} testcase_list[] = { /* EINVAL - fsync() on pipe should not succeed. */ - {pfd, EINVAL} + {-1, EINVAL, NULL}, + /* EINVAL - fsync() on socket should not succeed. */ + {-1, EINVAL, NULL}, + /* EBADF - fd is closed */ + {-1, EBADF, NULL}, + /* EBADF - fd is invalid (-1) */ + {-1, EBADF, NULL}, + /* EINVAL - fsync() on fifo should not succeed. */ + {-1, EINVAL, FIFO_PATH}, }; +static void setup(void) { + SAFE_MKFIFO(FIFO_PATH, 0644); + SAFE_PIPE(pfd); + + testcase_list[CLOSED_CASE].fd = pfd[0]; + testcase_list[PIPE_CASE].fd = pfd[1]; + fifo_rfd = SAFE_OPEN(FIFO_PATH, O_RDONLY | O_NONBLOCK); + testcase_list[SOCKET_CASE].fd = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0); + + // Do not open any file descriptors after this line unless you close + // them before the next test run. + SAFE_CLOSE(testcase_list[CLOSED_CASE].fd); +} + static void test_fsync(unsigned int n) { - struct test_case *tc = TC + n; + struct test_case *tc = testcase_list + n; + int fd = tc->fd, result; - if (!fsync(*tc->fd)) { + if (tc->path) { + fd = SAFE_OPEN(tc->path, O_WRONLY); + } + + result = fsync(fd); + + if (tc->path) { + close(fd); + } + + if (!result) { tst_res(TFAIL, "fsync() succeeded unexpectedly"); } else if (errno != tc->error) { tst_res(TFAIL | TERRNO, "Unexpected error"); @@ -40,18 +84,21 @@ static void test_fsync(unsigned int n) { } } -static void setup(void) { - SAFE_PIPE(pfd); -} - static void cleanup(void) { - close(pfd[0]); - close(pfd[1]); + // close fifo_rfd instead of the already closed test FD + testcase_list[CLOSED_CASE].fd = fifo_rfd; + + for (int i = 0; i < ARRAY_SIZE(testcase_list); i++) { + if (testcase_list[i].fd >= 0) { + close(testcase_list[i].fd); + } + } } static struct tst_test test = { .test = test_fsync, - .tcnt = ARRAY_SIZE(TC), + .tcnt = ARRAY_SIZE(testcase_list), + .needs_tmpdir = 1, .setup = setup, .cleanup = cleanup };