diff mbox series

[v2,2/2] Refactor and merge symlink04/05 using new LTP API

Message ID 1697794677-14892-2-git-send-email-xuyang2018.jy@fujitsu.com
State Changes Requested
Headers show
Series [v2,1/2] syscalls/symlink02: Convert to new API | expand

Commit Message

Yang Xu Oct. 20, 2023, 9:37 a.m. UTC
symlink04 and symlink05 has been merged together, testing
the results of symlink(2) for the existence or non-existence
of a file

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 runtest/syscalls                              |   1 -
 testcases/kernel/syscalls/symlink/.gitignore  |   1 -
 testcases/kernel/syscalls/symlink/symlink04.c | 218 ++++--------------
 testcases/kernel/syscalls/symlink/symlink05.c | 180 ---------------
 4 files changed, 49 insertions(+), 351 deletions(-)
 delete mode 100644 testcases/kernel/syscalls/symlink/symlink05.c

Comments

Petr Vorel Oct. 25, 2023, 11:09 p.m. UTC | #1
Hi Xu,

> symlink04 and symlink05 has been merged together, testing
> the results of symlink(2) for the existence or non-existence
> of a file
> + * Copyright (c) International Business Machines  Corp., 2001
> + * Copyright (c) Linux Test Project, 2003-2023
nit: IMHO LTP copyright was from 2001.

> + * Author: 07/2001 John George
>   */

...
> +	TST_EXP_PASS(symlink(*tc->srcfile, SYMFILE), "symlink(%s, %s)",
> +		     *tc->srcfile, SYMFILE);

nit: I would use just:
TST_EXP_PASS(symlink(*tc->srcfile, SYMFILE));

this:
symlink04.c:45: TFAIL: symlink(testfile, slink_file) failed: EFAULT (14)

is not much more informative than this:
symlink04.c:47: TFAIL: symlink(*tc->srcfile, SYMFILE) failed: EFAULT (14)

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Petr
Yang Xu Oct. 26, 2023, 6:13 a.m. UTC | #2
Hi Petr,

>Hi Xu,

>> symlink04 and symlink05 has been merged together, testing
>> the results of symlink(2) for the existence or non-existence
>> of a file
>> + * Copyright (c) International Business Machines  Corp., 2001
>> + * Copyright (c) Linux Test Project, 2003-2023
>nit: IMHO LTP copyright was from 2001.

OK.

>> + * Author: 07/2001 John George
>>   */

>...
>> +     TST_EXP_PASS(symlink(*tc->srcfile, SYMFILE), "symlink(%s, %s)",
>> +                  *tc->srcfile, SYMFILE);

>nit: I would use just:
>TST_EXP_PASS(symlink(*tc->srcfile, SYMFILE));

>this:
>symlink04.c:45: TFAIL: symlink(testfile, slink_file) failed: EFAULT (14)

>is not much more informative than this:
>symlink04.c:47: TFAIL: symlink(*tc->srcfile, SYMFILE) failed: EFAULT (14)

>Reviewed-by: Petr Vorel <pvorel@suse.cz>

Yes.

Best Regards
Yang Xu

>Kind regards,
>Petr
diff mbox series

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index 1851752cf..7e2f793f6 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1559,7 +1559,6 @@  symlink01 symlink01
 symlink02 symlink02
 symlink03 symlink03
 symlink04 symlink04
-symlink05 symlink05
 
 #symlinkat test cases
 symlinkat01 symlinkat01
diff --git a/testcases/kernel/syscalls/symlink/.gitignore b/testcases/kernel/syscalls/symlink/.gitignore
index d1497e680..6ea587ff3 100644
--- a/testcases/kernel/syscalls/symlink/.gitignore
+++ b/testcases/kernel/syscalls/symlink/.gitignore
@@ -2,4 +2,3 @@ 
 /symlink02
 /symlink03
 /symlink04
-/symlink05
diff --git a/testcases/kernel/syscalls/symlink/symlink04.c b/testcases/kernel/syscalls/symlink/symlink04.c
index 2190b3b1b..5022d4f13 100644
--- a/testcases/kernel/syscalls/symlink/symlink04.c
+++ b/testcases/kernel/syscalls/symlink/symlink04.c
@@ -1,193 +1,73 @@ 
+// 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
+ * Copyright (c) Linux Test Project, 2003-2023
+ * Author: 07/2001 John George
  */
 
-/*
- * Test Name : symlink04
- *
- * Test Description :
- *  Verify that, symlink will succeed to creat a symbolic link of an existing
- *  object name path.
- *
- * Expected Result:
- *  symlink() should return value 0 on success and symbolic link of an
- *  existing object should be created.
- *
- * Algorithm:
- *  Setup:
- *   Setup signal handling.
- *   Create temporary directory.
- *   Pause for SIGUSR1 if option specified.
- *
- *  Test:
- *   Loop if the proper options are given.
- *   Execute system call
- *   Check return code, if system call failed (return=-1)
- *   	Log the errno and Issue a FAIL message.
- *   Otherwise,
- *   	Verify the Functionality of system call
- *      if successful,
- *      	Issue Functionality-Pass message.
- *      Otherwise,
- *		Issue Functionality-Fail message.
- *  Cleanup:
- *   Print errno log and/or timing stats if options given
- *   Delete the temporary directory created.
- *
- * Usage:  <for command-line>
- *  symlink04 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
- *	where,  -c n : Run n copies concurrently.
- *		-e   : Turn on errno logging.
- *		-f   : Turn off functionality Testing.
- *		-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 John George
- *		-Ported
- *
- * Restrictions:
- *  None.
+/*\
+ * [Description]
  *
+ * Check that a symbolic link may point to an existing file or
+ * to a nonexistent one.
  */
 
+#include "tst_test.h"
+#include <stdlib.h>
 #include <stdio.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/stat.h>
-#include "test.h"
-#include "safe_macros.h"
 
-#define  TESTFILE	"testfile"
-#define  SYMFILE	"slink_file"
-#define FILE_MODE       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
+#define TESTFILE    "testfile"
+#define NONFILE     "noexistfile"
+#define SYMFILE     "slink_file"
 
-char *TCID = "symlink04";
-int TST_TOTAL = 1;
+static int fd;
+static char *testfile;
+static char *nonfile;
 
-void setup();
-void cleanup();
+static struct tcase {
+	char **srcfile;
+} tcases[] = {
+	{&testfile},
+	{&nonfile},
+};
 
-int main(int ac, char **av)
+static void setup(void)
 {
-	struct stat stat_buf;	/* stat structure buffer */
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/*
-		 * Call symlink(2) to create a symlink of
-		 * testfile.
-		 */
-		TEST(symlink(TESTFILE, SYMFILE));
-
-		if (TEST_RETURN == -1) {
-			tst_resm(TFAIL, "symlink(%s, %s) Failed, errno=%d : %s",
-				 TESTFILE, SYMFILE, TEST_ERRNO,
-				 strerror(TEST_ERRNO));
-		} else {
-			/*
-			 * Get the symlink file status information
-			 * using lstat(2).
-			 */
-			if (lstat(SYMFILE, &stat_buf) < 0) {
-				tst_brkm(TFAIL, cleanup, "lstat(2) of "
-					 "%s failed, error:%d", SYMFILE,
-					 errno);
-			}
-
-			/* Check if the st_mode contains a link  */
-			if (!S_ISLNK(stat_buf.st_mode)) {
-				tst_resm(TFAIL,
-					 "symlink of %s doesn't exist",
-					 TESTFILE);
-			} else {
-				tst_resm(TPASS, "symlink(%s, %s) "
-					 "functionality successful",
-					 TESTFILE, SYMFILE);
-			}
-		}
-
-		/* Unlink the symlink file for next loop */
-		SAFE_UNLINK(cleanup, SYMFILE);
-		tst_count++;	/* incr TEST_LOOP counter */
-	}
-
-	cleanup();
-	tst_exit();
-
+	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, 0644);
 }
 
-/*
- * void
- * setup() - performs all ONE TIME setup for this test.
- *  Create a temporary directory and change directory to it.
- *  Create a test file under temporary directory and close it
- */
-void setup(void)
+static void verify_symlink(unsigned int i)
 {
-	int fd;			/* file handle for testfile */
+	struct tcase *tc = &tcases[i];
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	struct stat stat_buf;
 
-	/* Pause if that option was specified
-	 * TEST_PAUSE contains the code to fork the test with the -i option.
-	 * You want to make sure you do this before you create your temporary
-	 * directory.
-	 */
-	TEST_PAUSE;
+	TST_EXP_PASS(symlink(*tc->srcfile, SYMFILE), "symlink(%s, %s)",
+		     *tc->srcfile, SYMFILE);
 
-	tst_tmpdir();
+	SAFE_LSTAT(SYMFILE, &stat_buf);
 
-	/* creat/open a testfile */
-	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s",
-			 TESTFILE, FILE_MODE, errno, strerror(errno));
-	}
+	if (!S_ISLNK(stat_buf.st_mode))
+		tst_res(TFAIL, "symlink of %s doesn't exist", *tc->srcfile);
 
-	/* Close the temporary file created above */
-	if (close(fd) == -1) {
-		tst_resm(TBROK, "close(%s) Failed, errno=%d : %s",
-			 TESTFILE, errno, strerror(errno));
-	}
+	SAFE_UNLINK(SYMFILE);
 }
 
-/*
- * void
- * cleanup() - performs all ONE TIME cleanup for this test at
- *             completion or premature exit.
- *  Remove the test directory and testfile created in the setup.
- */
-void cleanup(void)
+static void cleanup(void)
 {
-
-	tst_rmdir();
-
+	if (fd > -1)
+		SAFE_CLOSE(fd);
 }
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_symlink,
+	.bufs = (struct tst_buffers []) {
+		{&testfile, .str = TESTFILE},
+		{&nonfile, .str = NONFILE},
+		{},
+	},
+	.needs_tmpdir = 1,
+};
diff --git a/testcases/kernel/syscalls/symlink/symlink05.c b/testcases/kernel/syscalls/symlink/symlink05.c
deleted file mode 100644
index 83b151f5d..000000000
--- a/testcases/kernel/syscalls/symlink/symlink05.c
+++ /dev/null
@@ -1,180 +0,0 @@ 
-/*
- *
- *   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
- */
-
-/*
- * Test Name : symlink05
- *
- * Test Description :
- *  Verify that, symlink will succeed to creat a symbolic link of an
- *  non-existing object name path.
- *
- * Expected Result:
- *  symlink() should return value 0 on success and symlink of an
- *  non-existing object should be created.
- *
- * Algorithm:
- *  Setup:
- *   Setup signal handling.
- *   Create temporary directory.
- *   Pause for SIGUSR1 if option specified.
- *
- *  Test:
- *   Loop if the proper options are given.
- *   Execute system call
- *   Check return code, if system call failed (return=-1)
- *   	Log the errno and Issue a FAIL message.
- *   Otherwise,
- *   	Verify the Functionality of system call
- *      if successful,
- *      	Issue Functionality-Pass message.
- *      Otherwise,
- *		Issue Functionality-Fail message.
- *  Cleanup:
- *   Print errno log and/or timing stats if options given
- *   Delete the temporary directory created.
- *
- * Usage:  <for command-line>
- *  symlink05 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
- *	where,  -c n : Run n copies concurrently.
- *		-e   : Turn on errno logging.
- *		-f   : Turn off functionality Testing.
- *		-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 John George
- *		-Ported
- *
- * Restrictions:
- *  This test should be run by 'non-super-user' only.
- *
- */
-
-#include <stdio.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/stat.h>
-
-#include "test.h"
-#include "safe_macros.h"
-
-#define  TESTFILE	"testfile"
-#define  SYMFILE	"slink_file"
-
-char *TCID = "symlink05";
-int TST_TOTAL = 1;
-
-void setup();
-void cleanup();
-
-int main(int ac, char **av)
-{
-	struct stat stat_buf;	/* stat structure buffer */
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/*
-		 * Call symlink(2) to create a symlink of
-		 * an non-existing testfile.
-		 */
-		TEST(symlink(TESTFILE, SYMFILE));
-
-		if (TEST_RETURN == -1) {
-			tst_resm(TFAIL,
-				 "symlink(%s, %s) Failed, errno=%d : %s",
-				 TESTFILE, SYMFILE, TEST_ERRNO,
-				 strerror(TEST_ERRNO));
-		} else {
-			/*
-			 * Get the symlink file status information
-			 * using lstat(2).
-			 */
-			if (lstat(SYMFILE, &stat_buf) < 0) {
-				tst_brkm(TFAIL, cleanup, "lstat(2) of "
-					 "%s failed, error:%d",
-					 SYMFILE, errno);
-			}
-
-			/* Check if the st_mode contains a link  */
-			if (!S_ISLNK(stat_buf.st_mode)) {
-				tst_resm(TFAIL,
-					 "symlink of %s doesn't exist",
-					 TESTFILE);
-			} else {
-				tst_resm(TPASS, "symlink(%s, %s) "
-					 "functionality successful",
-					 TESTFILE, SYMFILE);
-			}
-		}
-
-		/* Unlink the symlink file for next loop */
-		SAFE_UNLINK(cleanup, SYMFILE);
-		tst_count++;	/* incr TEST_LOOP counter */
-	}
-
-	cleanup();
-	tst_exit();
-
-}
-
-/*
- * void
- * setup() - performs all ONE TIME setup for this test.
- *  Create a temporary directory and change directory to it.
- */
-void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	/* Pause if that option was specified
-	 * TEST_PAUSE contains the code to fork the test with the -i option.
-	 * You want to make sure you do this before you create your temporary
-	 * directory.
-	 */
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-}
-
-/*
- * void
- * cleanup() - performs all ONE TIME cleanup for this test at
- *             completion or premature exit.
- *  Remove the temporary directory created in the setup.
- */
-void cleanup(void)
-{
-
-	tst_rmdir();
-
-}