diff mbox series

[1/2] Update syscalls/fsync03 to new API

Message ID 20190917101706.10013-2-mdoucha@suse.cz
State Changes Requested
Headers show
Series Increase fsync() coverage - GH#452 | expand

Commit Message

Martin Doucha Sept. 17, 2019, 10:17 a.m. UTC
Signed-off-by: Martin Doucha <mdoucha@suse.com>
---
 testcases/kernel/syscalls/fsync/fsync03.c | 152 +++++-----------------
 1 file changed, 34 insertions(+), 118 deletions(-)

Comments

Cyril Hrubis Oct. 9, 2019, 3:20 p.m. UTC | #1
Hi!
> Signed-off-by: Martin Doucha <mdoucha@suse.com>
> ---
>  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 <mdoucha@suse.cz>
>   */
>  
>  /*
> - * 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:  <for command-line>
> - *  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 <unistd.h>
>  #include <errno.h>
> -#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                        */
                                  ^
				  I would say that these two comments
				  are overcommenting.

> -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;

This opening curly brace should be on a separate line for functions. We
do follow the LKML coding style in LTP, see:

https://www.kernel.org/doc/html/v4.10/process/coding-style.html

You can also use checkpatch script for identifying common problems:

https://github.com/torvalds/linux/blob/master/scripts/checkpatch.pl

> -		/* 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");
>  	}

I would be a bit more pedantic with the return value, i.e. check that
the failure returns exactly -1 as described in the manual page.

> -	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
> +};
> -- 
> 2.22.1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp
diff mbox series

Patch

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 <mdoucha@suse.cz>
  */
 
 /*
- * 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:  <for command-line>
- *  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 <unistd.h>
 #include <errno.h>
-#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
+};