Skip to contentSkip to conversation

syscalls/chdir04: Convert to the new API

PATCH Superseded Shiyang Ruan 2 participants 3 messages Mar 4, 2021 – Mar 11, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 syscalls/chdir04: Convert to the new API

---
 testcases/kernel/syscalls/chdir/chdir04.c | 168 +++-------------------
 1 file changed, 22 insertions(+), 146 deletions(-)
testcases/kernel/syscalls/chdir/chdir04.c +22 -146
index f0420e4c9..d7e6210c3 100644
@@ -1,162 +1,38 @@
+// 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) International Business Machines  Corp., 2001
  */
 
-/*
- * NAME
- * 	chdir04.c
- *
- * DESCRIPTION
- *	Testcase to test whether chdir(2) sets errno correctly.
- *
- * ALGORITHM
- *	1.	Test for ENAMETOOLONG:
- *		Create a bad directory name with length more than
- *
- *		VFS_MAXNAMELEN (Linux kernel variable), and attempt to
- *		chdir(2) to it.
- *
- *	2.	Test for ENOENT:
- *		Attempt to chdir(2) on a non-existent directory
- *
- *	3.	Test for EFAULT:
- *		Pass an address which lies outside the address space of the
- *		process, and expect an EFAULT.
- *
- * USAGE:  <for command-line>
- * chdir04 [-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.
+/*\
+ * [DESCRIPTION]
  *
- * HISTORY
- *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- *	NONE
- */
+ * Testcase to test whether chdir(2) sets errno correctly.
+\*/
 
-#include <stdio.h>
 #include <errno.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include "test.h"
-
-char *TCID = "chdir04";
+#include "tst_test.h"
 
 char bad_dir[] =
     "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
-
 char noexist_dir[] = "/tmp/noexistdir";
-
-struct test_case_t {
-	char *dname;
-	int error;
-} TC[] = {
-	/*
-	 * to test whether chdir() is setting ENAMETOOLONG if the
-	 * directory is more than VFS_MAXNAMELEN
-	 */
-	{
-	bad_dir, ENAMETOOLONG},
-	    /*
-	     * to test whether chdir() is setting ENOENT if the
-	     * directory is not existing.
-	     */
-	{
-	noexist_dir, ENOENT},
-	    /*
-	     * to test whether chdir() is setting EFAULT if the
-	     * directory is an invalid address.
-	     */
-	{
-	(void *)-1, EFAULT}
-};
-
-int TST_TOTAL = ARRAY_SIZE(TC);
-
-int flag;
-#define	FAILED	1
-
-void setup(void);
-void cleanup(void);
-
 char *bad_addr = 0;
 
-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++) {
-
-			TEST(chdir(TC[i].dname));
-
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "call succeeded unexpectedly");
-				continue;
-			}
-
-			if (TEST_ERRNO == TC[i].error)
-				tst_resm(TPASS | TTERRNO, "failed as expected");
-			else {
-				tst_resm(TFAIL | TTERRNO,
-					 "didn't fail as expected (expected %d)",
-					 TC[i].error);
-			}
-		}
-	}
-	cleanup();
-
-	tst_exit();
-
-}
+static struct tcase {
+	char *dir;
+	int exp_errno;
+} tcases [] = {
+	{bad_dir, ENAMETOOLONG},
+	{noexist_dir, ENOENT},
+	{(void *)-1, EFAULT}
+};
 
-void setup(void)
+static void verify_chdir(unsigned int i)
 {
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-#ifdef UCLINUX
-	bad_addr = mmap(0, 1, PROT_NONE,
-			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
-	if (bad_addr == MAP_FAILED)
-		tst_brkm(TBROK | TERRNO, cleanup, "mmap() failed");
-	TC[2].dname = bad_addr;
-#endif
+	TST_EXP_FAIL(chdir(tcases[i].dir), tcases[i].exp_errno);
 }
 
-void cleanup(void)
-{
-	tst_rmdir();
-
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test = verify_chdir,
+	.tcnt = ARRAY_SIZE(tcases),
+};
Signature · 1 line
2.30.1
#1 lore raw

PATCH v2 1/1 syscalls/chdir04: Convert to the new API

Quoted 61 lines · Shiyang Ruan
---
  testcases/kernel/syscalls/chdir/chdir04.c | 168 +++-------------------
  1 file changed, 22 insertions(+), 146 deletions(-)
testcases/kernel/syscalls/chdir/chdir04.c +3 -45
index f0420e4c9..d7e6210c3 100644
@@ -1,162 +1,38 @@
+// 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) International Business Machines  Corp., 2001
   */
 
-/*
- * NAME
- * 	chdir04.c
- *
- * DESCRIPTION
- *	Testcase to test whether chdir(2) sets errno correctly.
- *
- * ALGORITHM
- *	1.	Test for ENAMETOOLONG:
- *		Create a bad directory name with length more than
- *
- *		VFS_MAXNAMELEN (Linux kernel variable), and attempt to
- *		chdir(2) to it.
- *
- *	2.	Test for ENOENT:
- *		Attempt to chdir(2) on a non-existent directory
- *
- *	3.	Test for EFAULT:
- *		Pass an address which lies outside the address space of the
- *		process, and expect an EFAULT.
- *
- * USAGE:<for command-line>
- * chdir04 [-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.
+/*\

testcases/kernel/syscalls/chdir/chdir04.c line 10

Quoted 10 lines
testcases/kernel/syscalls/chdir/chdir04.c +3 -6
+ * [DESCRIPTION]
   *
- * HISTORY
- *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- *	NONE
- */
+ * Testcase to test whether chdir(2) sets errno correctly.
+\*/

Hi Ruan,

Please remove the first '\' as Petr mentioned:
http://lists.linux.it/pipermail/ltp/2021-March/021356.html

Quoted 82 lines
testcases/kernel/syscalls/chdir/chdir04.c +1 -73
-#include<stdio.h>
  #include<errno.h>
-#include<sys/stat.h>
-#include<sys/mman.h>
-#include "test.h"
-
-char *TCID = "chdir04";
+#include "tst_test.h"
 
  char bad_dir[] =
      "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
-
  char noexist_dir[] = "/tmp/noexistdir";
-
-struct test_case_t {
-	char *dname;
-	int error;
-} TC[] = {
-	/*
-	 * to test whether chdir() is setting ENAMETOOLONG if the
-	 * directory is more than VFS_MAXNAMELEN
-	 */
-	{
-	bad_dir, ENAMETOOLONG},
-	    /*
-	     * to test whether chdir() is setting ENOENT if the
-	     * directory is not existing.
-	     */
-	{
-	noexist_dir, ENOENT},
-	    /*
-	     * to test whether chdir() is setting EFAULT if the
-	     * directory is an invalid address.
-	     */
-	{
-	(void *)-1, EFAULT}
-};
-
-int TST_TOTAL = ARRAY_SIZE(TC);
-
-int flag;
-#define	FAILED	1
-
-void setup(void);
-void cleanup(void);
-
  char *bad_addr = 0;
 
-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++) {
-
-			TEST(chdir(TC[i].dname));
-
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "call succeeded unexpectedly");
-				continue;
-			}
-
-			if (TEST_ERRNO == TC[i].error)
-				tst_resm(TPASS | TTERRNO, "failed as expected");
-			else {
-				tst_resm(TFAIL | TTERRNO,
-					 "didn't fail as expected (expected %d)",
-					 TC[i].error);
-			}
-		}
-	}
-	cleanup();
-
-	tst_exit();

testcases/kernel/syscalls/chdir/chdir04.c line 27

Quoted 10 lines
testcases/kernel/syscalls/chdir/chdir04.c +8 -2
-
-}
+static struct tcase {
+	char *dir;
+	int exp_errno;
+} tcases [] = {
+	{bad_dir, ENAMETOOLONG},
+	{noexist_dir, ENOENT},
+	{(void *)-1, EFAULT}
+};

For portability, It is better to test bad address by tst_get_bad_addr() instead of (void *)-1.
See the following explanation:
https://github.com/linux-test-project/ltp/commit/80bed467bc6ab48a6cd88a8ab74ca15d08830cb0

Best Regards,
Xiao Yang

Quoted 31 lines
testcases/kernel/syscalls/chdir/chdir04.c +7 -20
-void setup(void)
+static void verify_chdir(unsigned int i)
  {
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-#ifdef UCLINUX
-	bad_addr = mmap(0, 1, PROT_NONE,
-			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
-	if (bad_addr == MAP_FAILED)
-		tst_brkm(TBROK | TERRNO, cleanup, "mmap() failed");
-	TC[2].dname = bad_addr;
-#endif
+	TST_EXP_FAIL(chdir(tcases[i].dir), tcases[i].exp_errno);
  }
 
-void cleanup(void)
-{
-	tst_rmdir();
-
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test = verify_chdir,
+	.tcnt = ARRAY_SIZE(tcases),
+};
#2 lore raw

PATCH v2 1/1 syscalls/chdir04: Convert to the new API

Quoted 85 lines

-----Original Message-----
From: Yang, Xiao/? ? <[email protected]>
Sent: Thursday, March 11, 2021 9:12 AM
To: Ruan, Shiyang/? ?? <[email protected]>
Cc: [email protected]
Subject: Re: [LTP] [PATCH v2] syscalls/chdir04: Convert to the new API

Quoted 19 lines · Shiyang Ruan
---
  testcases/kernel/syscalls/chdir/chdir04.c | 168 +++-------------------
  1 file changed, 22 insertions(+), 146 deletions(-)
testcases/kernel/syscalls/chdir/chdir04.c +1 -6
index f0420e4c9..d7e6210c3 100644
@@ -1,162 +1,38 @@
+// 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

testcases/kernel/syscalls/chdir/chdir04.c line 17

Quoted 10 lines
testcases/kernel/syscalls/chdir/chdir04.c +0 -10
- *   (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

Quoted 33 lines
testcases/kernel/syscalls/chdir/chdir04.c +2 -29
+ * Copyright (c) International Business Machines  Corp., 2001
   */
 
-/*
- * NAME
- * 	chdir04.c
- *
- * DESCRIPTION
- *	Testcase to test whether chdir(2) sets errno correctly.
- *
- * ALGORITHM
- *	1.	Test for ENAMETOOLONG:
- *		Create a bad directory name with length more than
- *
- *		VFS_MAXNAMELEN (Linux kernel variable), and attempt to
- *		chdir(2) to it.
- *
- *	2.	Test for ENOENT:
- *		Attempt to chdir(2) on a non-existent directory
- *
- *	3.	Test for EFAULT:
- *		Pass an address which lies outside the address space of the
- *		process, and expect an EFAULT.
- *
- * USAGE:<for command-line>
- * chdir04 [-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.
+/*\

testcases/kernel/syscalls/chdir/chdir04.c line 10

Quoted 10 lines
testcases/kernel/syscalls/chdir/chdir04.c +3 -6
+ * [DESCRIPTION]
   *
- * HISTORY
- *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- *	NONE
- */
+ * Testcase to test whether chdir(2) sets errno correctly.
+\*/

Hi Ruan,

Quoted 2 lines

Please remove the first '\' as Petr mentioned:
http://lists.linux.it/pipermail/ltp/2021-March/021356.html

OK.

Quoted 97 lines
Quoted 4 lines
testcases/kernel/syscalls/chdir/chdir04.c +0 -1
-#include<stdio.h>
  #include<errno.h>

testcases/kernel/syscalls/chdir/chdir04.c line 28

Quoted 10 lines
testcases/kernel/syscalls/chdir/chdir04.c +1 -5
-#include<sys/stat.h>
-#include<sys/mman.h>
-#include "test.h"
-
-char *TCID = "chdir04";
+#include "tst_test.h"
 
  char bad_dir[] =
 
 

"abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyza

Quoted 1 line

bcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabc

Quoted 1 line

defghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcde

Quoted 78 lines

fghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";

testcases/kernel/syscalls/chdir/chdir04.c +5 -69
-
  char noexist_dir[] = "/tmp/noexistdir";
-
-struct test_case_t {
-	char *dname;
-	int error;
-} TC[] = {
-	/*
-	 * to test whether chdir() is setting ENAMETOOLONG if the
-	 * directory is more than VFS_MAXNAMELEN
-	 */
-	{
-	bad_dir, ENAMETOOLONG},
-	    /*
-	     * to test whether chdir() is setting ENOENT if the
-	     * directory is not existing.
-	     */
-	{
-	noexist_dir, ENOENT},
-	    /*
-	     * to test whether chdir() is setting EFAULT if the
-	     * directory is an invalid address.
-	     */
-	{
-	(void *)-1, EFAULT}
-};
-
-int TST_TOTAL = ARRAY_SIZE(TC);
-
-int flag;
-#define	FAILED	1
-
-void setup(void);
-void cleanup(void);
-
  char *bad_addr = 0;
 
-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++) {
-
-			TEST(chdir(TC[i].dname));
-
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "call succeeded unexpectedly");
-				continue;
-			}
-
-			if (TEST_ERRNO == TC[i].error)
-				tst_resm(TPASS | TTERRNO, "failed as expected");
-			else {
-				tst_resm(TFAIL | TTERRNO,
-					 "didn't fail as expected (expected %d)",
-					 TC[i].error);
-			}
-		}
-	}
-	cleanup();
-
-	tst_exit();
-
-}
+static struct tcase {
+	char *dir;
+	int exp_errno;
+} tcases [] = {
+	{bad_dir, ENAMETOOLONG},
Quoted 8 lines

testcases/kernel/syscalls/chdir/chdir04.c line 27

Quoted 3 lines
testcases/kernel/syscalls/chdir/chdir04.c +3 -0
+	{noexist_dir, ENOENT},
+	{(void *)-1, EFAULT}
+};

For portability, It is better to test bad address by tst_get_bad_addr() instead of (void *)-1.
See the following explanation:
https://github.com/linux-test-project/ltp/commit/80bed467bc6ab48a6cd88a8a
b74ca15d08830cb0

Thanks for pointing out.

--
Ruan Shiyang.

Quoted 34 lines

Best Regards,
Xiao Yang

Quoted 31 lines
testcases/kernel/syscalls/chdir/chdir04.c +7 -20
-void setup(void)
+static void verify_chdir(unsigned int i)
  {
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-#ifdef UCLINUX
-	bad_addr = mmap(0, 1, PROT_NONE,
-			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
-	if (bad_addr == MAP_FAILED)
-		tst_brkm(TBROK | TERRNO, cleanup, "mmap() failed");
-	TC[2].dname = bad_addr;
-#endif
+	TST_EXP_FAIL(chdir(tcases[i].dir), tcases[i].exp_errno);
  }
 
-void cleanup(void)
-{
-	tst_rmdir();
-
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test = verify_chdir,
+	.tcnt = ARRAY_SIZE(tcases),
+};