diff mbox series

syscalls/fchmodat_01: Convert to new API

Message ID 20211119071723.19836-1-tangmeng@uniontech.com
State Changes Requested
Headers show
Series syscalls/fchmodat_01: Convert to new API | expand

Commit Message

Meng Tang Nov. 19, 2021, 7:17 a.m. UTC
Signed-off-by: tangmeng <tangmeng@uniontech.com>
---
 .../kernel/syscalls/fchmodat/fchmodat01.c     | 140 ++++++------------
 1 file changed, 44 insertions(+), 96 deletions(-)

--
2.20.1

Comments

Cyril Hrubis Jan. 28, 2022, 3:50 p.m. UTC | #1
Hi!
>  char pathname[256];
>  char testfile[256];
>  char testfile2[256];
>  char testfile3[256];
> -int fds[TEST_CASES];
> -char *filenames[TEST_CASES];
> -int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0, 0 };
> +int expected_errno[6] = { 0, 0, ENOTDIR, EBADF, 0, 0 };
> +int fds[ARRAY_SIZE(expected_errno)];
> +char *filenames[ARRAY_SIZE(expected_errno)];

We usually pack the syscall parameters into a tcase structure, which is
much easier to read and modify:

...
static int dir_at_fdcwd = AT_FDCWD;
static int file_fd;
static char testfile1[PATH_MAX];

static struct tcase {
	int *dir_fd;
	char filename;
	mode_t mode;
	int exp_err;
} tcases[] = {
	...
	{&file_fd, testfile1, 0600, ENOTDIR},
	...
};

>  int myfchmodat(int dirfd, const char *filename, mode_t mode)
>  {
> -	return ltp_syscall(__NR_fchmodat, dirfd, filename, mode);
> +	return tst_syscall(__NR_fchmodat, dirfd, filename, mode);
>  }

This function is now unused.

> -int main(int ac, char **av)
> +static void verify_fchmodat(unsigned int i)
>  {
> -	int lc;
> -	int i;
> -
> -	/* Disable test if the version of the kernel is less than 2.6.16 */
> -	if ((tst_kvercmp(2, 6, 16)) < 0) {
> -		tst_resm(TWARN, "This test can only run on kernels that are ");
> -		tst_resm(TWARN, "2.6.16 and higher");
> -		exit(0);
> -	}
> -
> -	tst_parse_opts(ac, av, NULL, NULL);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -		tst_count = 0;
> -
> -		for (i = 0; i < TST_TOTAL; i++) {
> -			TEST(myfchmodat(fds[i], filenames[i], 0600));
> -
> -			if (TEST_ERRNO == expected_errno[i]) {
> -				tst_resm(TPASS,
> -					 "fchmodat() returned the expected  errno %d: %s",
> -					 TEST_ERRNO, strerror(TEST_ERRNO));
> -			} else {
> -				tst_resm(TFAIL,
> -					 "fchmodat() Failed, errno=%d : %s",
> -					 TEST_ERRNO, strerror(TEST_ERRNO));
> -			}
> -		}
> +	TEST(tst_syscall(__NR_fchmodat, fds[i], filenames[i], 0600));
> +
> +	if (TST_ERR == expected_errno[i]) {
> +		tst_res(TPASS,
> +			 "fchmodat() returned the expected  errno %d: %s",
> +			 TST_ERR, strerror(TST_ERR));
> +	} else {
> +		tst_res(TFAIL,
> +			 "fchmodat() Failed, errno=%d : %s",
> +			 TST_ERR, strerror(TST_ERR));
>  	}

Ideally the test should be split into two testcases. One for the error
tests and one for the functionality test so that we can use
TST_EXP_PASS() and TST_EXP_FAIL().

> -	cleanup();
> -	tst_exit();
>  }
> 
> -void setup(void)
> +static void setup(void)
>  {
> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> -	tst_tmpdir();
> -
>  	/* Initialize test dir and file names */
>  	char *abs_path = tst_get_tmpdir();
>  	int p = getpid();
> @@ -122,31 +65,36 @@ void setup(void)
> 
>  	free(abs_path);
> 
> -	SAFE_MKDIR(cleanup, pathname, 0700);
> +	SAFE_MKDIR(pathname, 0700);
> 
> -	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
> +	fds[0] = SAFE_OPEN(pathname, O_DIRECTORY);
>  	fds[1] = fds[4] = fds[0];
> 
> -	SAFE_FILE_PRINTF(cleanup, testfile, "%s", testfile);
> -	SAFE_FILE_PRINTF(cleanup, testfile2, "%s", testfile2);
> +	SAFE_FILE_PRINTF(testfile, "%s", testfile);
> +	SAFE_FILE_PRINTF(testfile2, "%s", testfile2);
> 
> -	fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
> +	fds[2] = SAFE_OPEN(testfile3, O_CREAT | O_RDWR, 0600);
>  	fds[3] = 100;
>  	fds[5] = AT_FDCWD;
> 
>  	filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
>  	filenames[1] = testfile2;
>  	filenames[5] = testfile3;
> -
> -	TEST_PAUSE;
>  }
> 
> -void cleanup(void)
> +static void cleanup(void)
>  {
>  	if (fds[0] > 0)
>  		close(fds[0]);
>  	if (fds[2] > 0)
>  		close(fds[2]);
> -
> -	tst_rmdir();
>  }
> +
> +static struct tst_test test = {
> +	.min_kver = "2.6.16",
> +	.tcnt = ARRAY_SIZE(expected_errno),
> +	.test = verify_fchmodat,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.needs_tmpdir = 1,
> +};
> --
> 2.20.1
> 
> 
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp
Meng Tang Feb. 16, 2022, 7:49 a.m. UTC | #2
On 2022/1/28 23:50, Cyril Hrubis wrote:
> Hi!
> 
> We usually pack the syscall parameters into a tcase structure, which is
> much easier to read and modify:
> 
> ...
> static int dir_at_fdcwd = AT_FDCWD;
> static int file_fd;
> static char testfile1[PATH_MAX];
> 
> static struct tcase {
> 	int *dir_fd;
> 	char filename;
> 	mode_t mode;
> 	int exp_err;
> } tcases[] = {
> 	...
> 	{&file_fd, testfile1, 0600, ENOTDIR},
> 	...
> };
> 
>>   int myfchmodat(int dirfd, const char *filename, mode_t mode)
>>   {
>> -	return ltp_syscall(__NR_fchmodat, dirfd, filename, mode);
>> +	return tst_syscall(__NR_fchmodat, dirfd, filename, mode);
>>   }
> 
> This function is now unused.
> 
>> -int main(int ac, char **av)
>> +static void verify_fchmodat(unsigned int i)
>>   {
>> -	int lc;
>> -	int i;
>> -
>> -	/* Disable test if the version of the kernel is less than 2.6.16 */
>> -	if ((tst_kvercmp(2, 6, 16)) < 0) {
>> -		tst_resm(TWARN, "This test can only run on kernels that are ");
>> -		tst_resm(TWARN, "2.6.16 and higher");
>> -		exit(0);
>> -	}
>> -
>> -	tst_parse_opts(ac, av, NULL, NULL);
>> -
>> -	setup();
>> -
>> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
>> -		tst_count = 0;
>> -
>> -		for (i = 0; i < TST_TOTAL; i++) {
>> -			TEST(myfchmodat(fds[i], filenames[i], 0600));
>> -
>> -			if (TEST_ERRNO == expected_errno[i]) {
>> -				tst_resm(TPASS,
>> -					 "fchmodat() returned the expected  errno %d: %s",
>> -					 TEST_ERRNO, strerror(TEST_ERRNO));
>> -			} else {
>> -				tst_resm(TFAIL,
>> -					 "fchmodat() Failed, errno=%d : %s",
>> -					 TEST_ERRNO, strerror(TEST_ERRNO));
>> -			}
>> -		}
>> +	TEST(tst_syscall(__NR_fchmodat, fds[i], filenames[i], 0600));
>> +
>> +	if (TST_ERR == expected_errno[i]) {
>> +		tst_res(TPASS,
>> +			 "fchmodat() returned the expected  errno %d: %s",
>> +			 TST_ERR, strerror(TST_ERR));
>> +	} else {
>> +		tst_res(TFAIL,
>> +			 "fchmodat() Failed, errno=%d : %s",
>> +			 TST_ERR, strerror(TST_ERR));
>>   	}
> 
> Ideally the test should be split into two testcases. One for the error
> tests and one for the functionality test so that we can use
> TST_EXP_PASS() and TST_EXP_FAIL().
> 
>> -	cleanup();
>> -	tst_exit();
>>   }
>>
>> -void setup(void)
>> +static void setup(void)
>>   {
>> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
>> -
>> -	tst_tmpdir();
>> -
>>   	/* Initialize test dir and file names */
>>   	char *abs_path = tst_get_tmpdir();
>>   	int p = getpid();
>> @@ -122,31 +65,36 @@ void setup(void)
>>
>>   	free(abs_path);
>>
>> -	SAFE_MKDIR(cleanup, pathname, 0700);
>> +	SAFE_MKDIR(pathname, 0700);
>>
>> -	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
>> +	fds[0] = SAFE_OPEN(pathname, O_DIRECTORY);
>>   	fds[1] = fds[4] = fds[0];
>>
>> -	SAFE_FILE_PRINTF(cleanup, testfile, "%s", testfile);
>> -	SAFE_FILE_PRINTF(cleanup, testfile2, "%s", testfile2);
>> +	SAFE_FILE_PRINTF(testfile, "%s", testfile);
>> +	SAFE_FILE_PRINTF(testfile2, "%s", testfile2);
>>
>> -	fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
>> +	fds[2] = SAFE_OPEN(testfile3, O_CREAT | O_RDWR, 0600);
>>   	fds[3] = 100;
>>   	fds[5] = AT_FDCWD;
>>
>>   	filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
>>   	filenames[1] = testfile2;
>>   	filenames[5] = testfile3;
>> -
>> -	TEST_PAUSE;
>>   }
>>
>> -void cleanup(void)
>> +static void cleanup(void)
>>   {
>>   	if (fds[0] > 0)
>>   		close(fds[0]);
>>   	if (fds[2] > 0)
>>   		close(fds[2]);
>> -
>> -	tst_rmdir();
>>   }
>> +
>> +static struct tst_test test = {
>> +	.min_kver = "2.6.16",
>> +	.tcnt = ARRAY_SIZE(expected_errno),
>> +	.test = verify_fchmodat,
>> +	.setup = setup,
>> +	.cleanup = cleanup,
>> +	.needs_tmpdir = 1,
>> +};
>> --
>> 2.20.1
>>
>>
>>
>>
>> -- 
>> Mailing list info: https://lists.linux.it/listinfo/ltp
> 
Thanks for the heads up! I have handled it in patch v2.
Best regards
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/fchmodat/fchmodat01.c b/testcases/kernel/syscalls/fchmodat/fchmodat01.c
index 369f80eb2..555535174 100644
--- a/testcases/kernel/syscalls/fchmodat/fchmodat01.c
+++ b/testcases/kernel/syscalls/fchmodat/fchmodat01.c
@@ -1,116 +1,59 @@ 
-/******************************************************************************
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines  Corp., 2006
  *
- *   Copyright (c) International Business Machines  Corp., 2006
- *
- *   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
- *
- * NAME
- *      fchmodat01.c
- *
- * DESCRIPTION
- *	This test case will verify basic function of fchmodat
- *	added by kernel 2.6.16 or up.
- *
- * Author
- *	Yi Yang <yyangcdl@cn.ibm.com>
- *
- * History
- *      08/28/2006      Created first by Yi Yang <yyangcdl@cn.ibm.com>
+ * 08/28/2006 AUTHOR: Yi Yang <yyangcdl@cn.ibm.com>
+ */
+
+/*\
+ * [Description]
  *
- *****************************************************************************/
+ * This test case will verify basic function of fchmodat.
+ */

 #define _GNU_SOURCE

-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
 #include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
 #include <string.h>
-#include <signal.h>
-#include "test.h"
-#include "safe_macros.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include "tst_test.h"
 #include "lapi/syscalls.h"

-#define TEST_CASES 6
 #ifndef AT_FDCWD
 #define AT_FDCWD -100
 #endif
-void setup();
-void cleanup();

-char *TCID = "fchmodat01";
-int TST_TOTAL = TEST_CASES;
 char pathname[256];
 char testfile[256];
 char testfile2[256];
 char testfile3[256];
-int fds[TEST_CASES];
-char *filenames[TEST_CASES];
-int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0, 0 };
+int expected_errno[6] = { 0, 0, ENOTDIR, EBADF, 0, 0 };
+int fds[ARRAY_SIZE(expected_errno)];
+char *filenames[ARRAY_SIZE(expected_errno)];

 int myfchmodat(int dirfd, const char *filename, mode_t mode)
 {
-	return ltp_syscall(__NR_fchmodat, dirfd, filename, mode);
+	return tst_syscall(__NR_fchmodat, dirfd, filename, mode);
 }

-int main(int ac, char **av)
+static void verify_fchmodat(unsigned int i)
 {
-	int lc;
-	int i;
-
-	/* Disable test if the version of the kernel is less than 2.6.16 */
-	if ((tst_kvercmp(2, 6, 16)) < 0) {
-		tst_resm(TWARN, "This test can only run on kernels that are ");
-		tst_resm(TWARN, "2.6.16 and higher");
-		exit(0);
-	}
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-			TEST(myfchmodat(fds[i], filenames[i], 0600));
-
-			if (TEST_ERRNO == expected_errno[i]) {
-				tst_resm(TPASS,
-					 "fchmodat() returned the expected  errno %d: %s",
-					 TEST_ERRNO, strerror(TEST_ERRNO));
-			} else {
-				tst_resm(TFAIL,
-					 "fchmodat() Failed, errno=%d : %s",
-					 TEST_ERRNO, strerror(TEST_ERRNO));
-			}
-		}
+	TEST(tst_syscall(__NR_fchmodat, fds[i], filenames[i], 0600));
+
+	if (TST_ERR == expected_errno[i]) {
+		tst_res(TPASS,
+			 "fchmodat() returned the expected  errno %d: %s",
+			 TST_ERR, strerror(TST_ERR));
+	} else {
+		tst_res(TFAIL,
+			 "fchmodat() Failed, errno=%d : %s",
+			 TST_ERR, strerror(TST_ERR));
 	}
-
-	cleanup();
-	tst_exit();
 }

-void setup(void)
+static void setup(void)
 {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_tmpdir();
-
 	/* Initialize test dir and file names */
 	char *abs_path = tst_get_tmpdir();
 	int p = getpid();
@@ -122,31 +65,36 @@  void setup(void)

 	free(abs_path);

-	SAFE_MKDIR(cleanup, pathname, 0700);
+	SAFE_MKDIR(pathname, 0700);

-	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
+	fds[0] = SAFE_OPEN(pathname, O_DIRECTORY);
 	fds[1] = fds[4] = fds[0];

-	SAFE_FILE_PRINTF(cleanup, testfile, "%s", testfile);
-	SAFE_FILE_PRINTF(cleanup, testfile2, "%s", testfile2);
+	SAFE_FILE_PRINTF(testfile, "%s", testfile);
+	SAFE_FILE_PRINTF(testfile2, "%s", testfile2);

-	fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
+	fds[2] = SAFE_OPEN(testfile3, O_CREAT | O_RDWR, 0600);
 	fds[3] = 100;
 	fds[5] = AT_FDCWD;

 	filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
 	filenames[1] = testfile2;
 	filenames[5] = testfile3;
-
-	TEST_PAUSE;
 }

-void cleanup(void)
+static void cleanup(void)
 {
 	if (fds[0] > 0)
 		close(fds[0]);
 	if (fds[2] > 0)
 		close(fds[2]);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.min_kver = "2.6.16",
+	.tcnt = ARRAY_SIZE(expected_errno),
+	.test = verify_fchmodat,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+};