ltp.lists.linux.it archive mirror
 help / color / mirror / Atom feed
* [LTP] [PATCH] syscalls/openat01: Convert into new api
@ 2022-08-16  7:57 Liao Huangjie
  2022-08-16 12:38 ` Avinesh Kumar
  0 siblings, 1 reply; 7+ messages in thread
From: Liao Huangjie @ 2022-08-16  7:57 UTC (permalink / raw)
  To: ltp

From: Huangjie Liao <liaohj.jy@fujitsu.com>

Signed-off-by: Huangjie Liao <liaohj.jy@fujitsu.com>
---
 testcases/kernel/syscalls/openat/openat01.c | 145 ++++++++++------------------
 1 file changed, 51 insertions(+), 94 deletions(-)

diff --git a/testcases/kernel/syscalls/openat/openat01.c b/testcases/kernel/syscalls/openat/openat01.c
index daed419..25b3f83 100644
--- a/testcases/kernel/syscalls/openat/openat01.c
+++ b/testcases/kernel/syscalls/openat/openat01.c
@@ -1,55 +1,30 @@
-/******************************************************************************
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022 FUJITSU LIMITED. ALL Rights Reserved.
+ * Author: Huangjie Liao <liaohj.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
  *
- * Copyright (c) International Business Machines  Corp., 2006
- *  Author: Yi Yang <yyangcdl@cn.ibm.com>
- * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
- *
- * 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
- *
- * DESCRIPTION
- *  This test case will verify basic function of openat
- *  added by kernel 2.6.16 or up.
- *
- *****************************************************************************/
-
+ * This test case will verify basic function of openat
+ */
 #define _GNU_SOURCE
-
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
-#include <signal.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
-#include "openat.h"
-
-static void setup(void);
-static void cleanup(void);
+#include <stdio.h>
+#include "tst_test.h"
+#include "tst_safe_macros.h"
 
-char *TCID = "openat01";
+#define TEST_FILE "test_file"
+#define TEST_DIR "test_dir/"
 
 static int dir_fd, fd;
 static int fd_invalid = 100;
 static int fd_atcwd = AT_FDCWD;
-
-#define TEST_FILE "test_file"
-#define TEST_DIR "test_dir/"
-
 static char glob_path[256];
 
 static struct test_case {
@@ -65,80 +40,62 @@ static struct test_case {
 	{&fd_atcwd, TEST_DIR TEST_FILE, 0, 0}
 };
 
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-static void verify_openat(struct test_case *test)
+static void verify_openat(unsigned int n)
 {
-	TEST(openat(*test->dir_fd, test->pathname, O_RDWR, 0600));
-
-	if ((test->exp_ret == -1 && TEST_RETURN != -1) ||
-	    (test->exp_ret == 0 && TEST_RETURN < 0)) {
-		tst_resm(TFAIL | TTERRNO,
-		         "openat() returned %ldl, expected %d",
-			 TEST_RETURN, test->exp_ret);
+	struct test_case *tc = &test_cases[n];
+
+	TEST(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600));
+	
+	if ((tc->exp_ret == -1 && TST_RET != -1) ||
+	    (tc->exp_ret == 0 && TST_RET < 0)) {
+		tst_res(TFAIL | TTERRNO,
+		         "openat() returned %ld, expected %d",
+			 TST_RET, tc->exp_ret);
 		return;
 	}
+	
+	if (TST_RET > 0)
+		SAFE_CLOSE(TST_RET);
 
-	if (TEST_RETURN > 0)
-		SAFE_CLOSE(cleanup, TEST_RETURN);
-
-	if (TEST_ERRNO != test->exp_errno) {
-		tst_resm(TFAIL | TTERRNO,
+	if (TST_ERR != tc->exp_errno) {
+		tst_res(TFAIL | TTERRNO,
 		         "openat() returned wrong errno, expected %s(%d)",
-			 tst_strerrno(test->exp_errno), test->exp_errno);
+			 tst_strerrno(tc->exp_errno), tc->exp_errno);
 		return;
 	}
 
-	tst_resm(TPASS | TTERRNO, "openat() returned %ld", TEST_RETURN);
-}
-
-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;
-
-		for (i = 0; i < TST_TOTAL; i++)
-			verify_openat(test_cases + i);
-	}
-
-	cleanup();
-	tst_exit();
+	if (tc->exp_ret)
+		tst_res(TPASS | TTERRNO, "openat failed as expected");
+	else
+		tst_res(TPASS, "openat succeeded as expected");
 }
 
 static void setup(void)
 {
 	char *tmpdir;
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_tmpdir();
-
-	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
-	dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
-	fd = SAFE_OPEN(cleanup, TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
+	SAFE_MKDIR(TEST_DIR, 0700);
+	dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
+	fd = SAFE_OPEN(TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
 
 	tmpdir = tst_get_tmpdir();
 	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE,
-	         tmpdir);
+				tmpdir);
 	free(tmpdir);
-
-	TEST_PAUSE;
 }
 
 static void cleanup(void)
 {
-	if (fd > 0 && close(fd))
-		tst_resm(TWARN | TERRNO, "close(fd) failed");
-
-	if (dir_fd > 0 && close(dir_fd))
-		tst_resm(TWARN | TERRNO, "close(dir_fd) failed");
-
-	tst_rmdir();
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+	if (dir_fd > 0)
+		SAFE_CLOSE(dir_fd);
 }
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_openat,
+	.tcnt = ARRAY_SIZE(test_cases),
+	.needs_tmpdir = 1,
+};
-- 
1.8.3.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH] syscalls/openat01: Convert into new api
  2022-08-16  7:57 [LTP] [PATCH] syscalls/openat01: Convert into new api Liao Huangjie
@ 2022-08-16 12:38 ` Avinesh Kumar
  2022-08-17  8:35   ` liaohj.jy
  2022-08-17  9:32   ` [LTP] [PATCH v2] syscalls/openat01:Convert " Liao Huangjie
  0 siblings, 2 replies; 7+ messages in thread
From: Avinesh Kumar @ 2022-08-16 12:38 UTC (permalink / raw)
  To: ltp

Hi,

On Tuesday, August 16, 2022 1:27:02 PM IST Liao Huangjie wrote:
> From: Huangjie Liao <liaohj.jy@fujitsu.com>
> 
> Signed-off-by: Huangjie Liao <liaohj.jy@fujitsu.com>
> ---
>  testcases/kernel/syscalls/openat/openat01.c | 145 ++++++++++------------------
>  1 file changed, 51 insertions(+), 94 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/openat/openat01.c b/testcases/kernel/syscalls/openat/openat01.c
> index daed419..25b3f83 100644
> --- a/testcases/kernel/syscalls/openat/openat01.c
> +++ b/testcases/kernel/syscalls/openat/openat01.c
> @@ -1,55 +1,30 @@
> -/******************************************************************************
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2022 FUJITSU LIMITED. ALL Rights Reserved.
> + * Author: Huangjie Liao <liaohj.jy@fujitsu.com>
> + */
> +
> +/*\
> + * [Description]
>   *
> - * Copyright (c) International Business Machines  Corp., 2006
> - *  Author: Yi Yang <yyangcdl@cn.ibm.com>
> - * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
Old copyrights should not be removed.

> - *
> - * 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
> - *
> - * DESCRIPTION
> - *  This test case will verify basic function of openat
> - *  added by kernel 2.6.16 or up.
> - *
> - *****************************************************************************/
> -
> + * This test case will verify basic function of openat
> + */
>  #define _GNU_SOURCE
> -
>  #include <sys/types.h>
>  #include <sys/stat.h>
>  #include <stdlib.h>
>  #include <errno.h>
>  #include <string.h>
> -#include <signal.h>
> -
> -#include "test.h"
> -#include "safe_macros.h"
> -#include "lapi/fcntl.h"
> -#include "openat.h"
> -
> -static void setup(void);
> -static void cleanup(void);
> +#include <stdio.h>
> +#include "tst_test.h"
> +#include "tst_safe_macros.h"
we can get rid of more headers, which are either unnecessary or
get included recursively from tst_test.h

>  
> -char *TCID = "openat01";
> +#define TEST_FILE "test_file"
> +#define TEST_DIR "test_dir/"
>  
>  static int dir_fd, fd;
>  static int fd_invalid = 100;
>  static int fd_atcwd = AT_FDCWD;
> -
> -#define TEST_FILE "test_file"
> -#define TEST_DIR "test_dir/"
> -
>  static char glob_path[256];
>  
>  static struct test_case {
> @@ -65,80 +40,62 @@ static struct test_case {
>  	{&fd_atcwd, TEST_DIR TEST_FILE, 0, 0}
>  };
>  
> -int TST_TOTAL = ARRAY_SIZE(test_cases);
> -
> -static void verify_openat(struct test_case *test)
> +static void verify_openat(unsigned int n)
>  {
> -	TEST(openat(*test->dir_fd, test->pathname, O_RDWR, 0600));
> -
> -	if ((test->exp_ret == -1 && TEST_RETURN != -1) ||
> -	    (test->exp_ret == 0 && TEST_RETURN < 0)) {
> -		tst_resm(TFAIL | TTERRNO,
> -		         "openat() returned %ldl, expected %d",
> -			 TEST_RETURN, test->exp_ret);
> +	struct test_case *tc = &test_cases[n];
> +
> +	TEST(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600));
> +	
> +	if ((tc->exp_ret == -1 && TST_RET != -1) ||
> +	    (tc->exp_ret == 0 && TST_RET < 0)) {
> +		tst_res(TFAIL | TTERRNO,
> +		         "openat() returned %ld, expected %d",
> +			 TST_RET, tc->exp_ret);
>  		return;
>  	}
Test scenarios in this function should be changed to use TST_EXP_*() macro.
See examples at: https://github.com/linux-test-project/ltp/wiki/C-Test-API
> +	
> +	if (TST_RET > 0)
> +		SAFE_CLOSE(TST_RET);
>  
> -	if (TEST_RETURN > 0)
> -		SAFE_CLOSE(cleanup, TEST_RETURN);
> -
> -	if (TEST_ERRNO != test->exp_errno) {
> -		tst_resm(TFAIL | TTERRNO,
> +	if (TST_ERR != tc->exp_errno) {
> +		tst_res(TFAIL | TTERRNO,
>  		         "openat() returned wrong errno, expected %s(%d)",
> -			 tst_strerrno(test->exp_errno), test->exp_errno);
> +			 tst_strerrno(tc->exp_errno), tc->exp_errno);
>  		return;
>  	}
>  
> -	tst_resm(TPASS | TTERRNO, "openat() returned %ld", TEST_RETURN);
> -}
> -
> -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;
> -
> -		for (i = 0; i < TST_TOTAL; i++)
> -			verify_openat(test_cases + i);
> -	}
> -
> -	cleanup();
> -	tst_exit();
> +	if (tc->exp_ret)
> +		tst_res(TPASS | TTERRNO, "openat failed as expected");
> +	else
> +		tst_res(TPASS, "openat succeeded as expected");
>  }
>  
>  static void setup(void)
>  {
>  	char *tmpdir;
>  
> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> -	tst_tmpdir();
> -
> -	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
> -	dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
> -	fd = SAFE_OPEN(cleanup, TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
> +	SAFE_MKDIR(TEST_DIR, 0700);
> +	dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
> +	fd = SAFE_OPEN(TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
>  
>  	tmpdir = tst_get_tmpdir();
this also should be removed along with tst_tmpdir().
From the above documentation link:
"If .needs_tmpdir is set to 1 in the struct tst_test unique test temporary
is created and it’s set as the test working directory."
>  	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE,
> -	         tmpdir);
> +				tmpdir);
>  	free(tmpdir);
> -
> -	TEST_PAUSE;
>  }
>  
>  static void cleanup(void)
>  {
> -	if (fd > 0 && close(fd))
> -		tst_resm(TWARN | TERRNO, "close(fd) failed");
> -
> -	if (dir_fd > 0 && close(dir_fd))
> -		tst_resm(TWARN | TERRNO, "close(dir_fd) failed");
> -
> -	tst_rmdir();
> +	if (fd > 0)
> +		SAFE_CLOSE(fd);
> +	if (dir_fd > 0)
> +		SAFE_CLOSE(dir_fd);
>  }
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test = verify_openat,
> +	.tcnt = ARRAY_SIZE(test_cases),
> +	.needs_tmpdir = 1,
> +};
> 

Regards,
Avinesh





-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH] syscalls/openat01: Convert into new api
  2022-08-16 12:38 ` Avinesh Kumar
@ 2022-08-17  8:35   ` liaohj.jy
  2022-08-17  9:32   ` [LTP] [PATCH v2] syscalls/openat01:Convert " Liao Huangjie
  1 sibling, 0 replies; 7+ messages in thread
From: liaohj.jy @ 2022-08-17  8:35 UTC (permalink / raw)
  To: akumar; +Cc: ltp

Hi Avinesh

> Hi,
> 
> On Tuesday, August 16, 2022 1:27:02 PM IST Liao Huangjie wrote:
>> From: Huangjie Liao <liaohj.jy@fujitsu.com>
>>
>> Signed-off-by: Huangjie Liao <liaohj.jy@fujitsu.com>
>> ---
>>   testcases/kernel/syscalls/openat/openat01.c | 145 ++++++++++------------------
>>   1 file changed, 51 insertions(+), 94 deletions(-)
>>
>> diff --git a/testcases/kernel/syscalls/openat/openat01.c b/testcases/kernel/syscalls/openat/openat01.c
>> index daed419..25b3f83 100644
>> --- a/testcases/kernel/syscalls/openat/openat01.c
>> +++ b/testcases/kernel/syscalls/openat/openat01.c
>> @@ -1,55 +1,30 @@
>> -/******************************************************************************
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2022 FUJITSU LIMITED. ALL Rights Reserved.
>> + * Author: Huangjie Liao <liaohj.jy@fujitsu.com>
>> + */
>> +
>> +/*\
>> + * [Description]
>>    *
>> - * Copyright (c) International Business Machines  Corp., 2006
>> - *  Author: Yi Yang <yyangcdl@cn.ibm.com>
>> - * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
> Old copyrights should not be removed.

OK.
> 
>> - *
>> - * 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
>> - *
>> - * DESCRIPTION
>> - *  This test case will verify basic function of openat
>> - *  added by kernel 2.6.16 or up.
>> - *
>> - *****************************************************************************/
>> -
>> + * This test case will verify basic function of openat
>> + */
>>   #define _GNU_SOURCE
>> -
>>   #include <sys/types.h>
>>   #include <sys/stat.h>
>>   #include <stdlib.h>
>>   #include <errno.h>
>>   #include <string.h>
>> -#include <signal.h>
>> -
>> -#include "test.h"
>> -#include "safe_macros.h"
>> -#include "lapi/fcntl.h"
>> -#include "openat.h"
>> -
>> -static void setup(void);
>> -static void cleanup(void);
>> +#include <stdio.h>
>> +#include "tst_test.h"
>> +#include "tst_safe_macros.h"
> we can get rid of more headers, which are either unnecessary or
> get included recursively from tst_test.h

Will remove.
> 
>>   
>> -char *TCID = "openat01";
>> +#define TEST_FILE "test_file"
>> +#define TEST_DIR "test_dir/"
>>   
>>   static int dir_fd, fd;
>>   static int fd_invalid = 100;
>>   static int fd_atcwd = AT_FDCWD;
>> -
>> -#define TEST_FILE "test_file"
>> -#define TEST_DIR "test_dir/"
>> -
>>   static char glob_path[256];
>>   
>>   static struct test_case {
>> @@ -65,80 +40,62 @@ static struct test_case {
>>   	{&fd_atcwd, TEST_DIR TEST_FILE, 0, 0}
>>   };
>>   
>> -int TST_TOTAL = ARRAY_SIZE(test_cases);
>> -
>> -static void verify_openat(struct test_case *test)
>> +static void verify_openat(unsigned int n)
>>   {
>> -	TEST(openat(*test->dir_fd, test->pathname, O_RDWR, 0600));
>> -
>> -	if ((test->exp_ret == -1 && TEST_RETURN != -1) ||
>> -	    (test->exp_ret == 0 && TEST_RETURN < 0)) {
>> -		tst_resm(TFAIL | TTERRNO,
>> -		         "openat() returned %ldl, expected %d",
>> -			 TEST_RETURN, test->exp_ret);
>> +	struct test_case *tc = &test_cases[n];
>> +
>> +	TEST(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600));
>> +	
>> +	if ((tc->exp_ret == -1 && TST_RET != -1) ||
>> +	    (tc->exp_ret == 0 && TST_RET < 0)) {
>> +		tst_res(TFAIL | TTERRNO,
>> +		         "openat() returned %ld, expected %d",
>> +			 TST_RET, tc->exp_ret);
>>   		return;
>>   	}
> Test scenarios in this function should be changed to use TST_EXP_*() macro.
> See examples at: https://github.com/linux-test-project/ltp/wiki/C-Test-API

Will use TST_EXP_*() macro.
>> +	
>> +	if (TST_RET > 0)
>> +		SAFE_CLOSE(TST_RET);
>>   
>> -	if (TEST_RETURN > 0)
>> -		SAFE_CLOSE(cleanup, TEST_RETURN);
>> -
>> -	if (TEST_ERRNO != test->exp_errno) {
>> -		tst_resm(TFAIL | TTERRNO,
>> +	if (TST_ERR != tc->exp_errno) {
>> +		tst_res(TFAIL | TTERRNO,
>>   		         "openat() returned wrong errno, expected %s(%d)",
>> -			 tst_strerrno(test->exp_errno), test->exp_errno);
>> +			 tst_strerrno(tc->exp_errno), tc->exp_errno);
>>   		return;
>>   	}
>>   
>> -	tst_resm(TPASS | TTERRNO, "openat() returned %ld", TEST_RETURN);
>> -}
>> -
>> -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;
>> -
>> -		for (i = 0; i < TST_TOTAL; i++)
>> -			verify_openat(test_cases + i);
>> -	}
>> -
>> -	cleanup();
>> -	tst_exit();
>> +	if (tc->exp_ret)
>> +		tst_res(TPASS | TTERRNO, "openat failed as expected");
>> +	else
>> +		tst_res(TPASS, "openat succeeded as expected");
>>   }
>>   
>>   static void setup(void)
>>   {
>>   	char *tmpdir;
>>   
>> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
>> -
>> -	tst_tmpdir();
>> -
>> -	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
>> -	dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
>> -	fd = SAFE_OPEN(cleanup, TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
>> +	SAFE_MKDIR(TEST_DIR, 0700);
>> +	dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
>> +	fd = SAFE_OPEN(TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
>>   
>>   	tmpdir = tst_get_tmpdir();
> this also should be removed along with tst_tmpdir().
>  From the above documentation link:
> "If .needs_tmpdir is set to 1 in the struct tst_test unique test temporary
> is created and it’s set as the test working directory."

OK, will use SAFE_GETCWD.

Best Regards
Huangjie
>>   	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE,
>> -	         tmpdir);
>> +				tmpdir);
>>   	free(tmpdir);
>> -
>> -	TEST_PAUSE;
>>   }
>>   
>>   static void cleanup(void)
>>   {
>> -	if (fd > 0 && close(fd))
>> -		tst_resm(TWARN | TERRNO, "close(fd) failed");
>> -
>> -	if (dir_fd > 0 && close(dir_fd))
>> -		tst_resm(TWARN | TERRNO, "close(dir_fd) failed");
>> -
>> -	tst_rmdir();
>> +	if (fd > 0)
>> +		SAFE_CLOSE(fd);
>> +	if (dir_fd > 0)
>> +		SAFE_CLOSE(dir_fd);
>>   }
>> +
>> +static struct tst_test test = {
>> +	.setup = setup,
>> +	.cleanup = cleanup,
>> +	.test = verify_openat,
>> +	.tcnt = ARRAY_SIZE(test_cases),
>> +	.needs_tmpdir = 1,
>> +};
>>
> 
> Regards,
> Avinesh
> 
> 
> 
> 

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [LTP] [PATCH v2] syscalls/openat01:Convert into new api
  2022-08-16 12:38 ` Avinesh Kumar
  2022-08-17  8:35   ` liaohj.jy
@ 2022-08-17  9:32   ` Liao Huangjie
  2022-08-19  9:32     ` xuyang2018.jy
  1 sibling, 1 reply; 7+ messages in thread
From: Liao Huangjie @ 2022-08-17  9:32 UTC (permalink / raw)
  To: ltp

From: Huangjie Liao <liaohj.jy@fujitsu.com>

Signed-off-by: Huangjie Liao <liaohj.jy@fujitsu.com>
---
 testcases/kernel/syscalls/openat/openat01.c | 148 +++++++++-------------------
 1 file changed, 47 insertions(+), 101 deletions(-)

diff --git a/testcases/kernel/syscalls/openat/openat01.c b/testcases/kernel/syscalls/openat/openat01.c
index daed419..eb7f70c 100644
--- a/testcases/kernel/syscalls/openat/openat01.c
+++ b/testcases/kernel/syscalls/openat/openat01.c
@@ -1,55 +1,31 @@
-/******************************************************************************
- *
- * Copyright (c) International Business Machines  Corp., 2006
- *  Author: Yi Yang <yyangcdl@cn.ibm.com>
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) Internstional Business Machines  Corp., 2006
+ * Author: Yi Yang <yyangcdl@cn.ibm.com>
  * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
+ */
+
+/*\
+ * [Description]
  *
- * 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
- *
- * DESCRIPTION
- *  This test case will verify basic function of openat
- *  added by kernel 2.6.16 or up.
- *
- *****************************************************************************/
+ * This test case will verify basic function of openat
+ */
 
 #define _GNU_SOURCE
-
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
-#include <signal.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
-#include "openat.h"
-
-static void setup(void);
-static void cleanup(void);
+#include <stdio.h>
+#include "tst_test.h"
 
-char *TCID = "openat01";
+#define TEST_FILE "test_file"
+#define TEST_DIR "test_dir/"
 
 static int dir_fd, fd;
 static int fd_invalid = 100;
 static int fd_atcwd = AT_FDCWD;
-
-#define TEST_FILE "test_file"
-#define TEST_DIR "test_dir/"
-
 static char glob_path[256];
 
 static struct test_case {
@@ -65,80 +41,50 @@ static struct test_case {
 	{&fd_atcwd, TEST_DIR TEST_FILE, 0, 0}
 };
 
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-static void verify_openat(struct test_case *test)
-{
-	TEST(openat(*test->dir_fd, test->pathname, O_RDWR, 0600));
-
-	if ((test->exp_ret == -1 && TEST_RETURN != -1) ||
-	    (test->exp_ret == 0 && TEST_RETURN < 0)) {
-		tst_resm(TFAIL | TTERRNO,
-		         "openat() returned %ldl, expected %d",
-			 TEST_RETURN, test->exp_ret);
-		return;
-	}
-
-	if (TEST_RETURN > 0)
-		SAFE_CLOSE(cleanup, TEST_RETURN);
-
-	if (TEST_ERRNO != test->exp_errno) {
-		tst_resm(TFAIL | TTERRNO,
-		         "openat() returned wrong errno, expected %s(%d)",
-			 tst_strerrno(test->exp_errno), test->exp_errno);
-		return;
-	}
-
-	tst_resm(TPASS | TTERRNO, "openat() returned %ld", TEST_RETURN);
-}
-
-int main(int ac, char **av)
+static void verify_openat(unsigned int n)
 {
-	int lc;
-	int i;
-
-	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++)
-			verify_openat(test_cases + i);
+	struct test_case *tc = &test_cases[n];
+
+	if (tc->exp_ret) {
+		if (tc->exp_errno == ENOTDIR) {
+			TST_EXP_FAIL2(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600),
+				ENOTDIR, "openat with a filefd instead of dirfd");
+		} else {
+			TST_EXP_FAIL2(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600),
+				EBADF, "openat with invalid fd");
+		}
+	} else {
+		TST_EXP_FD(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600));
 	}
 
-	cleanup();
-	tst_exit();
+	if (TST_RET > 0)
+		SAFE_CLOSE(TST_RET);
 }
 
 static void setup(void)
 {
-	char *tmpdir;
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_tmpdir();
-
-	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
-	dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
-	fd = SAFE_OPEN(cleanup, TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
+	char buf[PATH_MAX];
 
-	tmpdir = tst_get_tmpdir();
-	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE,
-	         tmpdir);
-	free(tmpdir);
+	SAFE_GETCWD(buf, PATH_MAX);
+	SAFE_MKDIR(TEST_DIR, 0700);
+	dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
+	fd = SAFE_OPEN(TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
 
-	TEST_PAUSE;
+	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE, buf);
 }
 
 static void cleanup(void)
 {
-	if (fd > 0 && close(fd))
-		tst_resm(TWARN | TERRNO, "close(fd) failed");
-
-	if (dir_fd > 0 && close(dir_fd))
-		tst_resm(TWARN | TERRNO, "close(dir_fd) failed");
-
-	tst_rmdir();
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+	if (dir_fd > 0)
+		SAFE_CLOSE(dir_fd);
 }
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_openat,
+	.tcnt = ARRAY_SIZE(test_cases),
+	.needs_tmpdir = 1,
+};
-- 
1.8.3.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH v2] syscalls/openat01:Convert into new api
  2022-08-17  9:32   ` [LTP] [PATCH v2] syscalls/openat01:Convert " Liao Huangjie
@ 2022-08-19  9:32     ` xuyang2018.jy
  0 siblings, 0 replies; 7+ messages in thread
From: xuyang2018.jy @ 2022-08-19  9:32 UTC (permalink / raw)
  To: liaohj.jy, ltp

Hi Liao

I added detailed test description and merged, Thanks!

Best Regards
Yang Xu
> From: Huangjie Liao <liaohj.jy@fujitsu.com>
> 
> Signed-off-by: Huangjie Liao <liaohj.jy@fujitsu.com>
> ---
>   testcases/kernel/syscalls/openat/openat01.c | 148 +++++++++-------------------
>   1 file changed, 47 insertions(+), 101 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/openat/openat01.c b/testcases/kernel/syscalls/openat/openat01.c
> index daed419..eb7f70c 100644
> --- a/testcases/kernel/syscalls/openat/openat01.c
> +++ b/testcases/kernel/syscalls/openat/openat01.c
> @@ -1,55 +1,31 @@
> -/******************************************************************************
> - *
> - * Copyright (c) International Business Machines  Corp., 2006
> - *  Author: Yi Yang <yyangcdl@cn.ibm.com>
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) Internstional Business Machines  Corp., 2006
> + * Author: Yi Yang <yyangcdl@cn.ibm.com>
>    * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
> + */
> +
> +/*\
> + * [Description]
>    *
> - * 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
> - *
> - * DESCRIPTION
> - *  This test case will verify basic function of openat
> - *  added by kernel 2.6.16 or up.
> - *
> - *****************************************************************************/
> + * This test case will verify basic function of openat
> + */
>   
>   #define _GNU_SOURCE
> -
>   #include <sys/types.h>
>   #include <sys/stat.h>
>   #include <stdlib.h>
>   #include <errno.h>
>   #include <string.h>
> -#include <signal.h>
> -
> -#include "test.h"
> -#include "safe_macros.h"
> -#include "lapi/fcntl.h"
> -#include "openat.h"
> -
> -static void setup(void);
> -static void cleanup(void);
> +#include <stdio.h>
> +#include "tst_test.h"
>   
> -char *TCID = "openat01";
> +#define TEST_FILE "test_file"
> +#define TEST_DIR "test_dir/"
>   
>   static int dir_fd, fd;
>   static int fd_invalid = 100;
>   static int fd_atcwd = AT_FDCWD;
> -
> -#define TEST_FILE "test_file"
> -#define TEST_DIR "test_dir/"
> -
>   static char glob_path[256];
>   
>   static struct test_case {
> @@ -65,80 +41,50 @@ static struct test_case {
>   	{&fd_atcwd, TEST_DIR TEST_FILE, 0, 0}
>   };
>   
> -int TST_TOTAL = ARRAY_SIZE(test_cases);
> -
> -static void verify_openat(struct test_case *test)
> -{
> -	TEST(openat(*test->dir_fd, test->pathname, O_RDWR, 0600));
> -
> -	if ((test->exp_ret == -1 && TEST_RETURN != -1) ||
> -	    (test->exp_ret == 0 && TEST_RETURN < 0)) {
> -		tst_resm(TFAIL | TTERRNO,
> -		         "openat() returned %ldl, expected %d",
> -			 TEST_RETURN, test->exp_ret);
> -		return;
> -	}
> -
> -	if (TEST_RETURN > 0)
> -		SAFE_CLOSE(cleanup, TEST_RETURN);
> -
> -	if (TEST_ERRNO != test->exp_errno) {
> -		tst_resm(TFAIL | TTERRNO,
> -		         "openat() returned wrong errno, expected %s(%d)",
> -			 tst_strerrno(test->exp_errno), test->exp_errno);
> -		return;
> -	}
> -
> -	tst_resm(TPASS | TTERRNO, "openat() returned %ld", TEST_RETURN);
> -}
> -
> -int main(int ac, char **av)
> +static void verify_openat(unsigned int n)
>   {
> -	int lc;
> -	int i;
> -
> -	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++)
> -			verify_openat(test_cases + i);
> +	struct test_case *tc = &test_cases[n];
> +
> +	if (tc->exp_ret) {
> +		if (tc->exp_errno == ENOTDIR) {
> +			TST_EXP_FAIL2(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600),
> +				ENOTDIR, "openat with a filefd instead of dirfd");
> +		} else {
> +			TST_EXP_FAIL2(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600),
> +				EBADF, "openat with invalid fd");
> +		}
> +	} else {
> +		TST_EXP_FD(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600));
>   	}
>   
> -	cleanup();
> -	tst_exit();
> +	if (TST_RET > 0)
> +		SAFE_CLOSE(TST_RET);
>   }
>   
>   static void setup(void)
>   {
> -	char *tmpdir;
> -
> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> -	tst_tmpdir();
> -
> -	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
> -	dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
> -	fd = SAFE_OPEN(cleanup, TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
> +	char buf[PATH_MAX];
>   
> -	tmpdir = tst_get_tmpdir();
> -	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE,
> -	         tmpdir);
> -	free(tmpdir);
> +	SAFE_GETCWD(buf, PATH_MAX);
> +	SAFE_MKDIR(TEST_DIR, 0700);
> +	dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
> +	fd = SAFE_OPEN(TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
>   
> -	TEST_PAUSE;
> +	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE, buf);
>   }
>   
>   static void cleanup(void)
>   {
> -	if (fd > 0 && close(fd))
> -		tst_resm(TWARN | TERRNO, "close(fd) failed");
> -
> -	if (dir_fd > 0 && close(dir_fd))
> -		tst_resm(TWARN | TERRNO, "close(dir_fd) failed");
> -
> -	tst_rmdir();
> +	if (fd > 0)
> +		SAFE_CLOSE(fd);
> +	if (dir_fd > 0)
> +		SAFE_CLOSE(dir_fd);
>   }
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test = verify_openat,
> +	.tcnt = ARRAY_SIZE(test_cases),
> +	.needs_tmpdir = 1,
> +};

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH] syscalls/openat01: Convert into new api
  2022-08-17  8:47 [LTP] [PATCH] syscalls/openat01: Convert " Liao Huangjie
@ 2022-08-17  9:34 ` liaohj.jy
  0 siblings, 0 replies; 7+ messages in thread
From: liaohj.jy @ 2022-08-17  9:34 UTC (permalink / raw)
  To: ltp

Hi All

Sorry for this noise.


Best Regards
Huangjie

> From: Huangjie Liao <liaohj.jy@fujitsu.com>
> 
> Signed-off-by: Huangjie Liao <liaohj.jy@fujitsu.com>
> ---
>   testcases/kernel/syscalls/openat/openat01.c | 145 ++++++++++------------------
>   1 file changed, 51 insertions(+), 94 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/openat/openat01.c b/testcases/kernel/syscalls/openat/openat01.c
> index daed419..25b3f83 100644
> --- a/testcases/kernel/syscalls/openat/openat01.c
> +++ b/testcases/kernel/syscalls/openat/openat01.c
> @@ -1,55 +1,30 @@
> -/******************************************************************************
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2022 FUJITSU LIMITED. ALL Rights Reserved.
> + * Author: Huangjie Liao <liaohj.jy@fujitsu.com>
> + */
> +
> +/*\
> + * [Description]
>    *
> - * Copyright (c) International Business Machines  Corp., 2006
> - *  Author: Yi Yang <yyangcdl@cn.ibm.com>
> - * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
> - *
> - * 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
> - *
> - * DESCRIPTION
> - *  This test case will verify basic function of openat
> - *  added by kernel 2.6.16 or up.
> - *
> - *****************************************************************************/
> -
> + * This test case will verify basic function of openat
> + */
>   #define _GNU_SOURCE
> -
>   #include <sys/types.h>
>   #include <sys/stat.h>
>   #include <stdlib.h>
>   #include <errno.h>
>   #include <string.h>
> -#include <signal.h>
> -
> -#include "test.h"
> -#include "safe_macros.h"
> -#include "lapi/fcntl.h"
> -#include "openat.h"
> -
> -static void setup(void);
> -static void cleanup(void);
> +#include <stdio.h>
> +#include "tst_test.h"
> +#include "tst_safe_macros.h"
>   
> -char *TCID = "openat01";
> +#define TEST_FILE "test_file"
> +#define TEST_DIR "test_dir/"
>   
>   static int dir_fd, fd;
>   static int fd_invalid = 100;
>   static int fd_atcwd = AT_FDCWD;
> -
> -#define TEST_FILE "test_file"
> -#define TEST_DIR "test_dir/"
> -
>   static char glob_path[256];
>   
>   static struct test_case {
> @@ -65,80 +40,62 @@ static struct test_case {
>   	{&fd_atcwd, TEST_DIR TEST_FILE, 0, 0}
>   };
>   
> -int TST_TOTAL = ARRAY_SIZE(test_cases);
> -
> -static void verify_openat(struct test_case *test)
> +static void verify_openat(unsigned int n)
>   {
> -	TEST(openat(*test->dir_fd, test->pathname, O_RDWR, 0600));
> -
> -	if ((test->exp_ret == -1 && TEST_RETURN != -1) ||
> -	    (test->exp_ret == 0 && TEST_RETURN < 0)) {
> -		tst_resm(TFAIL | TTERRNO,
> -		         "openat() returned %ldl, expected %d",
> -			 TEST_RETURN, test->exp_ret);
> +	struct test_case *tc = &test_cases[n];
> +
> +	TEST(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600));
> +	
> +	if ((tc->exp_ret == -1 && TST_RET != -1) ||
> +	    (tc->exp_ret == 0 && TST_RET < 0)) {
> +		tst_res(TFAIL | TTERRNO,
> +		         "openat() returned %ld, expected %d",
> +			 TST_RET, tc->exp_ret);
>   		return;
>   	}
> +	
> +	if (TST_RET > 0)
> +		SAFE_CLOSE(TST_RET);
>   
> -	if (TEST_RETURN > 0)
> -		SAFE_CLOSE(cleanup, TEST_RETURN);
> -
> -	if (TEST_ERRNO != test->exp_errno) {
> -		tst_resm(TFAIL | TTERRNO,
> +	if (TST_ERR != tc->exp_errno) {
> +		tst_res(TFAIL | TTERRNO,
>   		         "openat() returned wrong errno, expected %s(%d)",
> -			 tst_strerrno(test->exp_errno), test->exp_errno);
> +			 tst_strerrno(tc->exp_errno), tc->exp_errno);
>   		return;
>   	}
>   
> -	tst_resm(TPASS | TTERRNO, "openat() returned %ld", TEST_RETURN);
> -}
> -
> -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;
> -
> -		for (i = 0; i < TST_TOTAL; i++)
> -			verify_openat(test_cases + i);
> -	}
> -
> -	cleanup();
> -	tst_exit();
> +	if (tc->exp_ret)
> +		tst_res(TPASS | TTERRNO, "openat failed as expected");
> +	else
> +		tst_res(TPASS, "openat succeeded as expected");
>   }
>   
>   static void setup(void)
>   {
>   	char *tmpdir;
>   
> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> -	tst_tmpdir();
> -
> -	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
> -	dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
> -	fd = SAFE_OPEN(cleanup, TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
> +	SAFE_MKDIR(TEST_DIR, 0700);
> +	dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
> +	fd = SAFE_OPEN(TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
>   
>   	tmpdir = tst_get_tmpdir();
>   	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE,
> -	         tmpdir);
> +				tmpdir);
>   	free(tmpdir);
> -
> -	TEST_PAUSE;
>   }
>   
>   static void cleanup(void)
>   {
> -	if (fd > 0 && close(fd))
> -		tst_resm(TWARN | TERRNO, "close(fd) failed");
> -
> -	if (dir_fd > 0 && close(dir_fd))
> -		tst_resm(TWARN | TERRNO, "close(dir_fd) failed");
> -
> -	tst_rmdir();
> +	if (fd > 0)
> +		SAFE_CLOSE(fd);
> +	if (dir_fd > 0)
> +		SAFE_CLOSE(dir_fd);
>   }
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test = verify_openat,
> +	.tcnt = ARRAY_SIZE(test_cases),
> +	.needs_tmpdir = 1,
> +};

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [LTP] [PATCH] syscalls/openat01: Convert into new api
@ 2022-08-17  8:47 Liao Huangjie
  2022-08-17  9:34 ` liaohj.jy
  0 siblings, 1 reply; 7+ messages in thread
From: Liao Huangjie @ 2022-08-17  8:47 UTC (permalink / raw)
  To: ltp

From: Huangjie Liao <liaohj.jy@fujitsu.com>

Signed-off-by: Huangjie Liao <liaohj.jy@fujitsu.com>
---
 testcases/kernel/syscalls/openat/openat01.c | 145 ++++++++++------------------
 1 file changed, 51 insertions(+), 94 deletions(-)

diff --git a/testcases/kernel/syscalls/openat/openat01.c b/testcases/kernel/syscalls/openat/openat01.c
index daed419..25b3f83 100644
--- a/testcases/kernel/syscalls/openat/openat01.c
+++ b/testcases/kernel/syscalls/openat/openat01.c
@@ -1,55 +1,30 @@
-/******************************************************************************
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022 FUJITSU LIMITED. ALL Rights Reserved.
+ * Author: Huangjie Liao <liaohj.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
  *
- * Copyright (c) International Business Machines  Corp., 2006
- *  Author: Yi Yang <yyangcdl@cn.ibm.com>
- * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
- *
- * 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
- *
- * DESCRIPTION
- *  This test case will verify basic function of openat
- *  added by kernel 2.6.16 or up.
- *
- *****************************************************************************/
-
+ * This test case will verify basic function of openat
+ */
 #define _GNU_SOURCE
-
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
-#include <signal.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
-#include "openat.h"
-
-static void setup(void);
-static void cleanup(void);
+#include <stdio.h>
+#include "tst_test.h"
+#include "tst_safe_macros.h"
 
-char *TCID = "openat01";
+#define TEST_FILE "test_file"
+#define TEST_DIR "test_dir/"
 
 static int dir_fd, fd;
 static int fd_invalid = 100;
 static int fd_atcwd = AT_FDCWD;
-
-#define TEST_FILE "test_file"
-#define TEST_DIR "test_dir/"
-
 static char glob_path[256];
 
 static struct test_case {
@@ -65,80 +40,62 @@ static struct test_case {
 	{&fd_atcwd, TEST_DIR TEST_FILE, 0, 0}
 };
 
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-static void verify_openat(struct test_case *test)
+static void verify_openat(unsigned int n)
 {
-	TEST(openat(*test->dir_fd, test->pathname, O_RDWR, 0600));
-
-	if ((test->exp_ret == -1 && TEST_RETURN != -1) ||
-	    (test->exp_ret == 0 && TEST_RETURN < 0)) {
-		tst_resm(TFAIL | TTERRNO,
-		         "openat() returned %ldl, expected %d",
-			 TEST_RETURN, test->exp_ret);
+	struct test_case *tc = &test_cases[n];
+
+	TEST(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600));
+	
+	if ((tc->exp_ret == -1 && TST_RET != -1) ||
+	    (tc->exp_ret == 0 && TST_RET < 0)) {
+		tst_res(TFAIL | TTERRNO,
+		         "openat() returned %ld, expected %d",
+			 TST_RET, tc->exp_ret);
 		return;
 	}
+	
+	if (TST_RET > 0)
+		SAFE_CLOSE(TST_RET);
 
-	if (TEST_RETURN > 0)
-		SAFE_CLOSE(cleanup, TEST_RETURN);
-
-	if (TEST_ERRNO != test->exp_errno) {
-		tst_resm(TFAIL | TTERRNO,
+	if (TST_ERR != tc->exp_errno) {
+		tst_res(TFAIL | TTERRNO,
 		         "openat() returned wrong errno, expected %s(%d)",
-			 tst_strerrno(test->exp_errno), test->exp_errno);
+			 tst_strerrno(tc->exp_errno), tc->exp_errno);
 		return;
 	}
 
-	tst_resm(TPASS | TTERRNO, "openat() returned %ld", TEST_RETURN);
-}
-
-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;
-
-		for (i = 0; i < TST_TOTAL; i++)
-			verify_openat(test_cases + i);
-	}
-
-	cleanup();
-	tst_exit();
+	if (tc->exp_ret)
+		tst_res(TPASS | TTERRNO, "openat failed as expected");
+	else
+		tst_res(TPASS, "openat succeeded as expected");
 }
 
 static void setup(void)
 {
 	char *tmpdir;
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_tmpdir();
-
-	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
-	dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
-	fd = SAFE_OPEN(cleanup, TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
+	SAFE_MKDIR(TEST_DIR, 0700);
+	dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
+	fd = SAFE_OPEN(TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
 
 	tmpdir = tst_get_tmpdir();
 	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE,
-	         tmpdir);
+				tmpdir);
 	free(tmpdir);
-
-	TEST_PAUSE;
 }
 
 static void cleanup(void)
 {
-	if (fd > 0 && close(fd))
-		tst_resm(TWARN | TERRNO, "close(fd) failed");
-
-	if (dir_fd > 0 && close(dir_fd))
-		tst_resm(TWARN | TERRNO, "close(dir_fd) failed");
-
-	tst_rmdir();
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+	if (dir_fd > 0)
+		SAFE_CLOSE(dir_fd);
 }
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_openat,
+	.tcnt = ARRAY_SIZE(test_cases),
+	.needs_tmpdir = 1,
+};
-- 
1.8.3.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-08-19  9:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-16  7:57 [LTP] [PATCH] syscalls/openat01: Convert into new api Liao Huangjie
2022-08-16 12:38 ` Avinesh Kumar
2022-08-17  8:35   ` liaohj.jy
2022-08-17  9:32   ` [LTP] [PATCH v2] syscalls/openat01:Convert " Liao Huangjie
2022-08-19  9:32     ` xuyang2018.jy
2022-08-17  8:47 [LTP] [PATCH] syscalls/openat01: Convert " Liao Huangjie
2022-08-17  9:34 ` liaohj.jy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).