public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/2] renameat/renameat01.c: cleanup
@ 2014-04-21 11:17 gux.fnst
  2014-04-21 11:17 ` [LTP] [PATCH 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests gux.fnst
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: gux.fnst @ 2014-04-21 11:17 UTC (permalink / raw)
  To: ltp-list@lists.sourceforge.net

Add renameat.h to encapsulate renameat syscall.
Delete some useless comments.
Use some SAFE_* macros.
Move the test body from main() to (*testfunc[])().

Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
 configure.ac                                    |   1 +
 include/lapi/fcntl.h                            |   4 +
 m4/ltp-renameat.m4                              |  25 ++
 testcases/kernel/syscalls/renameat/renameat.h   |  37 +++
 testcases/kernel/syscalls/renameat/renameat01.c | 348 +++++++++++-------------
 5 files changed, 232 insertions(+), 183 deletions(-)
 create mode 100644 m4/ltp-renameat.m4
 create mode 100644 testcases/kernel/syscalls/renameat/renameat.h

diff --git a/configure.ac b/configure.ac
index 9f397e7..bf888c6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -178,5 +178,6 @@ LTP_CHECK_FCHOWNAT
 LTP_CHECK_MKNODAT
 LTP_CHECK_FALLOCATE
 LTP_CHECK_SYSCALL_FCNTL
+LTP_CHECK_RENAMEAT
 
 AC_OUTPUT
diff --git a/include/lapi/fcntl.h b/include/lapi/fcntl.h
index 85188a0..ca086b2 100644
--- a/include/lapi/fcntl.h
+++ b/include/lapi/fcntl.h
@@ -59,4 +59,8 @@
 # define FALLOC_FL_KEEP_SIZE 1
 #endif
 
+#ifndef AT_REMOVEDIR
+# define AT_REMOVEDIR 0x200
+#endif
+
 #endif /* __LAPI_FCNTL_H__ */
diff --git a/m4/ltp-renameat.m4 b/m4/ltp-renameat.m4
new file mode 100644
index 0000000..f40c58e
--- /dev/null
+++ b/m4/ltp-renameat.m4
@@ -0,0 +1,25 @@
+dnl
+dnl Copyright (c) Linux Test Project, 2014
+dnl
+dnl This program is free software;  you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation; either version 2 of the License, or
+dnl (at your option) any later version.
+dnl
+dnl This program is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY;  without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+dnl the GNU General Public License for more details.
+dnl
+dnl You should have received a copy of the GNU General Public License
+dnl along with this program;  if not, write to the Free Software Foundation,
+dnl Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+dnl
+
+dnl
+dnl LTP_CHECK_RENAMEAT
+dnl ----------------------------
+dnl
+AC_DEFUN([LTP_CHECK_RENAMEAT],[
+AC_CHECK_FUNCS(renameat,,)
+])
diff --git a/testcases/kernel/syscalls/renameat/renameat.h b/testcases/kernel/syscalls/renameat/renameat.h
new file mode 100644
index 0000000..fa1e2e5
--- /dev/null
+++ b/testcases/kernel/syscalls/renameat/renameat.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) International Business Machines  Corp., 2007
+ * Copyright (c) 2014 Fujitsu Ltd.
+ *
+ * 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef RENAMEAT_H
+#define RENAMEAT_H
+
+#include <sys/types.h>
+#include "config.h"
+#include "linux_syscall_numbers.h"
+
+#if !defined(HAVE_RENAMEAT)
+int renameat(int olddirfd, const char *oldpath, int newdirfd,
+			const char *newpath)
+{
+	return ltp_syscall(__NR_renameat, olddirfd, oldpath, newdirfd,
+					newpath);
+}
+#endif
+
+#endif /* RENAMEAT_H */
diff --git a/testcases/kernel/syscalls/renameat/renameat01.c b/testcases/kernel/syscalls/renameat/renameat01.c
index e27e344..bc08477 100644
--- a/testcases/kernel/syscalls/renameat/renameat01.c
+++ b/testcases/kernel/syscalls/renameat/renameat01.c
@@ -1,46 +1,31 @@
-/******************************************************************************
+/*
+ * Copyright (c) International Business Machines  Corp., 2006
+ * 08/24/2006      Created first by Yi Yang <yyangcdl@cn.ibm.com>
  *
- *   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 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.
  *
- *   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
- *      renameat01.c
- *
- * DESCRIPTION
- *	This test case will verify basic function of renameat
- *	added by kernel 2.6.16 or up.
- *
- * USAGE:  <for command-line>
- * renameat01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p]
- * where:
- *      -c n : Run n copies simultaneously.
- *      -e   : Turn on errno logging.
- *      -i n : Execute test n times.
- *      -I x : Execute test for x seconds.
- *      -p   : Pause for SIGUSR1 before starting
- *      -P x : Pause for x seconds between iterations.
- *      -t   : Turn on syscall timing.
- *
- * Author
- *	Yi Yang <yyangcdl@cn.ibm.com>
- *
- * History
- *      08/24/2006      Created first by Yi Yang <yyangcdl@cn.ibm.com>
- *
- *****************************************************************************/
+ * 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:
+ *   Verify that,
+ *   1) renameat(2) returns -1 and sets errno to ENOTDIR if oldpath
+ *      is relative and olddirfd is a file descriptor referring to
+ *      a file other than a directory, or similar for newpath and
+ *      newdirfd.
+ *   2) renameat(2) returns -1 and sets errno to EBADF if olddirfd
+ *      or newdirfd is not a valid file descriptor.
+ */
 
 #define _GNU_SOURCE
 
@@ -53,193 +38,190 @@
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
+
 #include "test.h"
 #include "usctest.h"
+#include "safe_macros.h"
 #include "linux_syscall_numbers.h"
-
-#define TEST_CASES 5
-#ifndef AT_FDCWD
-#define AT_FDCWD -100
-#endif
-#ifndef AT_REMOVEDIR
-#define AT_REMOVEDIR 0x200
-#endif
-
-void setup();
-void cleanup();
-void setup_every_copy();
+#include "lapi/fcntl.h"
+#include "renameat.h"
+
+#define DIR_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
+#define FILE_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
+
+#define TESTDIR "testdir"
+#define NEW_TESTDIR "newtestdir"
+#define TESTFILE "testfile"
+#define NEW_TESTFILE "newtestfile"
+#define TESTFILE2 "testdir/testfile"
+#define NEW_TESTFILE2 "newtestdir/newtestfile"
+#define TESTFILE3 "/tmp/testfile3"
+#define NEW_TESTFILE3 "/tmp/newtestfile3"
+
+static void setup(void);
+static void cleanup(void);
+static void test_success1(void);
+static void test_success2(void);
+static void test_success3(void);
+static void test_enotdir(void);
+static void test_ebadf(void);
+static void check_and_print(int expected_errno);
+
+static void (*testfunc[])(void) = { test_success1, test_success2,
+				test_success3, test_enotdir, test_ebadf };
 
 char *TCID = "renameat01";
-int TST_TOTAL = TEST_CASES;
-char pathname[256];
-char dpathname[256];
-char testfile[256];
-char dtestfile[256];
-char testfile2[256];
-char dtestfile2[256];
-char testfile3[256];
-char dtestfile3[256];
-int olddirfd, newdirfd, fd, ret;
-int oldfds[TEST_CASES], newfds[TEST_CASES];
-char *oldfilenames[TEST_CASES], *newfilenames[TEST_CASES];
-int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0 };
-
-int myrenameat(int olddirfd, const char *oldfilename, int newdirfd,
-	       const char *newfilename)
-{
-	return ltp_syscall(__NR_renameat, olddirfd, oldfilename, newdirfd,
-		       newfilename);
-}
+int TST_TOTAL = ARRAY_SIZE(testfunc);
+static int exp_enos[] = { ENOTDIR, EBADF, 0 };
 
 int main(int ac, char **av)
 {
-	int lc;
 	char *msg;
-	int i;
-
-	/* Disable test if the version of the kernel is less than 2.6.16 */
-	if ((tst_kvercmp(2, 6, 16)) < 0) {
-		tst_brkm(TCONF, NULL,
-			 "This test can only run on kernels that are 2.6.16 and higher");
-	}
+	int i, lc;
 
-	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
+	msg = parse_opts(ac, av, NULL, NULL);
+	if (msg != NULL)
 		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
 
 	setup();
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		setup_every_copy();
+	TEST_EXP_ENOS(exp_enos);
 
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
 		tst_count = 0;
 
-		/*
-		 * Call renameat
-		 */
-		for (i = 0; i < TST_TOTAL; i++) {
-			TEST(myrenameat
-			     (oldfds[i], oldfilenames[i], newfds[i],
-			      newfilenames[i]));
-
-			/* check return code */
-			if (TEST_ERRNO == expected_errno[i]) {
-
-				if (STD_FUNCTIONAL_TEST) {
-					/* No Verification test, yet... */
-					tst_resm(TPASS | TTERRNO,
-						 "renameat failed as expected");
-				}
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "renameat failed unexpectedly");
-			}
-		}
-
+		for (i = 0; i < TST_TOTAL; i++)
+			(*testfunc[i])();
 	}
 
 	cleanup();
-
 	tst_exit();
 }
 
-void setup_every_copy(void)
+static void setup(void)
 {
-	/* Initialize test dir and file names */
-	sprintf(pathname, "renameattestdir%d", getpid());
-	sprintf(dpathname, "drenameattestdir%d", getpid());
-	sprintf(testfile, "renameattestfile%d.txt", getpid());
-	sprintf(dtestfile, "drenameattestfile%d.txt", getpid());
-	sprintf(testfile2, "renameattestdir%d/renameattestfile%d.txt", getpid(),
-		getpid());
-	sprintf(dtestfile2, "drenameattestdir%d/drenameattestfile%d.txt",
-		getpid(), getpid());
-	sprintf(testfile3, "/tmp/renameattestfile%d.txt", getpid());
-	sprintf(dtestfile3, "/tmp/drenameattestfile%d.txt", getpid());
-
-	ret = mkdir(pathname, 0700);
-	if (ret < 0) {
-		perror("mkdir: ");
-		exit(-1);
+	if ((tst_kvercmp(2, 6, 16)) < 0) {
+		tst_brkm(TCONF, NULL,
+			"This test can only run on kernels that are "
+			"2.6.16 and higher");
 	}
 
-	ret = mkdir(dpathname, 0700);
-	if (ret < 0) {
-		perror("mkdir: ");
-		exit(-1);
-	}
+	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
-	olddirfd = open(pathname, O_DIRECTORY);
-	if (olddirfd < 0) {
-		perror("open: ");
-		exit(-1);
-	}
+	tst_tmpdir();
 
-	newdirfd = open(dpathname, O_DIRECTORY);
-	if (newdirfd < 0) {
-		perror("open: ");
-		exit(-1);
-	}
+	TEST_PAUSE;
 
-	fd = open(testfile, O_CREAT | O_RDWR, 0600);
-	if (fd < 0) {
-		perror("open: ");
-		exit(-1);
-	}
+	SAFE_TOUCH(cleanup, TESTFILE, FILE_MODE, NULL);
+
+	SAFE_MKDIR(cleanup, TESTDIR, DIR_MODE);
+	SAFE_TOUCH(cleanup, TESTFILE2, FILE_MODE, NULL);
+	SAFE_MKDIR(cleanup, NEW_TESTDIR, DIR_MODE);
 
-	fd = open(testfile2, O_CREAT | O_RDWR, 0600);
-	if (fd < 0) {
-		perror("open: ");
-		exit(-1);
+	SAFE_TOUCH(cleanup, TESTFILE3, FILE_MODE, NULL);
+}
+
+static void test_success1(void)
+{
+	TEST(renameat(AT_FDCWD, TESTFILE, AT_FDCWD, NEW_TESTFILE));
+	check_and_print(0);
+
+	if (TEST_RETURN == 0) {
+		if (renameat(AT_FDCWD, NEW_TESTFILE, AT_FDCWD,
+				TESTFILE) == -1) {
+			tst_brkm(TBROK | TERRNO, cleanup, "restore test "
+				"environment: renameat(%d, %s, %d, %s) failed.",
+				AT_FDCWD, NEW_TESTFILE, AT_FDCWD, TESTFILE);
+		}
 	}
+}
+
+static void test_success2(void)
+{
+	int olddirfd, newdirfd;
+
+	olddirfd = SAFE_OPEN(cleanup, TESTDIR, O_DIRECTORY);
+	newdirfd = SAFE_OPEN(cleanup, NEW_TESTDIR, O_DIRECTORY);
 
-	fd = open(testfile3, O_CREAT | O_RDWR, 0600);
-	if (fd < 0) {
-		perror("open: ");
-		exit(-1);
+	TEST(renameat(olddirfd, TESTFILE, newdirfd, NEW_TESTFILE));
+	check_and_print(0);
+
+	if (TEST_RETURN == 0) {
+		if (renameat(newdirfd, NEW_TESTFILE, olddirfd,
+				TESTFILE) == -1) {
+			tst_brkm(TBROK | TERRNO, cleanup, "restore test "
+				"environment: renameat(%d, %s, %d, %s) failed.",
+				newdirfd, NEW_TESTFILE, olddirfd, TESTFILE);
+		}
 	}
 
-	oldfds[0] = oldfds[1] = olddirfd;
-	oldfds[2] = fd;
-	oldfds[3] = 100;
-	oldfds[4] = AT_FDCWD;
+	if (close(olddirfd) == -1)
+		tst_resm(TWARN | TERRNO, "close(%d) failed", olddirfd);
 
-	newfds[0] = newfds[1] = newdirfd;
-	newfds[2] = fd;
-	newfds[3] = 100;
-	newfds[4] = AT_FDCWD;
+	if (close(newdirfd) == -1)
+		tst_resm(TWARN | TERRNO, "close(%d) failed", newdirfd);
+}
 
-	oldfilenames[0] = oldfilenames[2] = oldfilenames[3] = oldfilenames[4] =
-	    testfile;
-	oldfilenames[1] = testfile3;
+static void test_success3(void)
+{
+	int olddirfd, newdirfd;
 
-	newfilenames[0] = newfilenames[2] = newfilenames[3] = newfilenames[4] =
-	    dtestfile;
-	newfilenames[1] = dtestfile3;
+	olddirfd = SAFE_OPEN(cleanup, TESTDIR, O_DIRECTORY);
+	newdirfd = SAFE_OPEN(cleanup, NEW_TESTDIR, O_DIRECTORY);
+
+	TEST(renameat(olddirfd, TESTFILE3, newdirfd, NEW_TESTFILE3));
+	check_and_print(0);
+
+	if (TEST_RETURN == 0) {
+		if (renameat(newdirfd, NEW_TESTFILE3, olddirfd,
+				TESTFILE3) == -1) {
+			tst_brkm(TBROK | TERRNO, cleanup, "restore test "
+				"environment: renameat(%d, %s, %d, %s) failed.",
+				newdirfd, NEW_TESTFILE3, olddirfd, TESTFILE3);
+		}
+	}
+
+	if (close(olddirfd) == -1)
+		tst_resm(TWARN | TERRNO, "close(%d) failed", olddirfd);
+
+	if (close(newdirfd) == -1)
+		tst_resm(TWARN | TERRNO, "close(%d) failed", newdirfd);
 }
 
-void setup(void)
+static void test_enotdir(void)
 {
+	int notdirfd;
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	notdirfd = SAFE_OPEN(cleanup, TESTFILE3, O_RDWR);
 
-	TEST_PAUSE;
+	TEST(renameat(notdirfd, TESTFILE, notdirfd, NEW_TESTFILE));
+	check_and_print(ENOTDIR);
+
+	if (close(notdirfd) == -1)
+		tst_resm(TWARN | TERRNO, "close(%d) failed", notdirfd);
+}
+
+static void test_ebadf(void)
+{
+	TEST(renameat(100, TESTFILE, 100, NEW_TESTFILE));
+	check_and_print(EBADF);
 }
 
-void cleanup(void)
+static void check_and_print(int expected_errno)
+{
+	if (TEST_ERRNO == expected_errno) {
+		tst_resm(TPASS | TTERRNO,
+			"renameat() returned the expected value");
+	} else {
+		tst_resm(TFAIL | TTERRNO,
+			"renameat() got unexpected return value; expected: "
+			"%d - %s", expected_errno, strerror(expected_errno));
+	}
+}
+
+static void cleanup(void)
 {
-	/* Remove them */
-	unlink(testfile2);
-	unlink(dtestfile2);
-	unlink(testfile3);
-	unlink(dtestfile3);
-	unlink(testfile);
-	unlink(dtestfile);
-	rmdir(pathname);
-	rmdir(dpathname);
-
-	/*
-	 * print timing stats if that option was specified.
-	 * print errno log if that option was specified.
-	 */
 	TEST_CLEANUP;
+
+	tst_rmdir();
 }
-- 
1.9.0
------------------------------------------------------------------------------
Start Your Social Network Today - Download eXo Platform
Build your Enterprise Intranet with eXo Platform Software
Java Based Open Source Intranet - Social, Extensible, Cloud Ready
Get Started Now And Turn Your Intranet Into A Collaboration Platform
http://p.sf.net/sfu/ExoPlatform
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests
  2014-04-21 11:17 [LTP] [PATCH 1/2] renameat/renameat01.c: cleanup gux.fnst
@ 2014-04-21 11:17 ` gux.fnst
  2014-05-16  4:35 ` [LTP] [PATCH v2 1/2] renameat/renameat01.c: cleanup Xing Gu
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: gux.fnst @ 2014-04-21 11:17 UTC (permalink / raw)
  To: ltp-list@lists.sourceforge.net

Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
 testcases/kernel/syscalls/renameat/renameat01.c | 115 +++++++++++++++++++++++-
 1 file changed, 113 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/renameat/renameat01.c b/testcases/kernel/syscalls/renameat/renameat01.c
index bc08477..20526c5 100644
--- a/testcases/kernel/syscalls/renameat/renameat01.c
+++ b/testcases/kernel/syscalls/renameat/renameat01.c
@@ -25,6 +25,15 @@
  *      newdirfd.
  *   2) renameat(2) returns -1 and sets errno to EBADF if olddirfd
  *      or newdirfd is not a valid file descriptor.
+ *   3) renameat(2) returns -1 and sets errno to ELOOP if too many
+ *      symbolic links were encountered in resolving oldpath or
+ *      newpath.
+ *   4) renameat(2) returns -1 and sets errno to EROFS if the file
+ *      is on a read-only file system.
+ *   5) renameat(2) returns -1 and sets errno to EMLINK if oldpath
+ *      already has the maximum number of links to it, or it was a
+ *      directory and the directory containing newpath has the maximum
+ *      number of links.
  */
 
 #define _GNU_SOURCE
@@ -38,6 +47,7 @@
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
+#include <sys/mount.h>
 
 #include "test.h"
 #include "usctest.h"
@@ -49,14 +59,24 @@
 #define DIR_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
 #define FILE_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
 
+#define MNTPOINT "mntpoint"
 #define TESTDIR "testdir"
 #define NEW_TESTDIR "newtestdir"
+#define TESTDIR2 "/loopdir"
+#define NEW_TESTDIR2 "newloopdir"
+#define TESTDIR3 "mntpoint/mlinkdir"
+#define NEW_TESTDIR3 "mntpoint/testmlink/newmlinkdir"
 #define TESTFILE "testfile"
 #define NEW_TESTFILE "newtestfile"
 #define TESTFILE2 "testdir/testfile"
 #define NEW_TESTFILE2 "newtestdir/newtestfile"
 #define TESTFILE3 "/tmp/testfile3"
 #define NEW_TESTFILE3 "/tmp/newtestfile3"
+#define TESTFILE4 "mntpoint/rofile"
+#define NEW_TESTFILE4 "mntpoint/newrofile"
+
+static char looppathname[sizeof(TESTDIR2) * 43] = ".";
+static char *device;
 
 static void setup(void);
 static void cleanup(void);
@@ -65,14 +85,18 @@ static void test_success2(void);
 static void test_success3(void);
 static void test_enotdir(void);
 static void test_ebadf(void);
+static void test_eloop(void);
+static void test_erofs(void);
+static void test_emlink(void);
 static void check_and_print(int expected_errno);
 
 static void (*testfunc[])(void) = { test_success1, test_success2,
-				test_success3, test_enotdir, test_ebadf };
+				test_success3, test_enotdir, test_ebadf,
+				test_eloop, test_erofs, test_emlink };
 
 char *TCID = "renameat01";
 int TST_TOTAL = ARRAY_SIZE(testfunc);
-static int exp_enos[] = { ENOTDIR, EBADF, 0 };
+static int exp_enos[] = { ENOTDIR, EBADF, ELOOP, EROFS, EMLINK, 0 };
 
 int main(int ac, char **av)
 {
@@ -100,12 +124,27 @@ int main(int ac, char **av)
 
 static void setup(void)
 {
+	int i, ret;
+	char lname[PATH_MAX];
+	unsigned long long link_count = 0;
+	const char *const opts[] = { "-O", "^dir_index", NULL };
+
 	if ((tst_kvercmp(2, 6, 16)) < 0) {
 		tst_brkm(TCONF, NULL,
 			"This test can only run on kernels that are "
 			"2.6.16 and higher");
 	}
 
+	tst_require_root(NULL);
+
+	device = getenv("LTP_BIG_DEV");
+	if (device == NULL) {
+		tst_brkm(TCONF, NULL,
+			"you must specify a big blockdevice(>500MB)");
+	} else {
+		tst_mkfs(NULL, device, "ext4", opts);
+	}
+
 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
 	tst_tmpdir();
@@ -119,6 +158,46 @@ static void setup(void)
 	SAFE_MKDIR(cleanup, NEW_TESTDIR, DIR_MODE);
 
 	SAFE_TOUCH(cleanup, TESTFILE3, FILE_MODE, NULL);
+
+	/*
+	 * NOTE: the ELOOP test is written based on that the
+	 * consecutive symlinks limit in kernel is hardwired
+	 * to 40.
+	 */
+	SAFE_MKDIR(cleanup, "loopdir", DIR_MODE);
+	SAFE_SYMLINK(cleanup, "../loopdir", "loopdir/loopdir");
+	for (i = 0; i < 43; i++)
+		strcat(looppathname, TESTDIR2);
+
+	SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE);
+
+	if (mount(device, MNTPOINT, "ext4", 0, NULL) < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup,
+			"mount device:%s failed", device);
+	}
+
+	SAFE_TOUCH(cleanup, TESTFILE4, FILE_MODE, NULL);
+
+	SAFE_MKDIR(cleanup, TESTDIR3, DIR_MODE);
+	SAFE_MKDIR(cleanup, "mntpoint/testmlink", DIR_MODE);
+	while (1) {
+		sprintf(lname, "mntpoint/testmlink/mlink%lld", ++link_count);
+		ret = mkdir(lname, DIR_MODE);
+		if (ret == -1) {
+			switch (errno) {
+			case EMLINK:
+				tst_resm(TINFO, "for ext4 the max links is"
+					" %lld", link_count + 1);
+				break;
+			default:
+				tst_brkm(TCONF | TERRNO, cleanup,
+				"we do not hit the maximum number of "
+				"links to dir test_parent_dir in ext4."
+				"Unexpected error:");
+			}
+			break;
+		}
+	}
 }
 
 static void test_success1(void)
@@ -207,6 +286,35 @@ static void test_ebadf(void)
 	check_and_print(EBADF);
 }
 
+static void test_eloop(void)
+{
+	TEST(renameat(AT_FDCWD, looppathname, AT_FDCWD, NEW_TESTDIR2));
+	check_and_print(ELOOP);
+}
+
+static void test_erofs(void)
+{
+	if (mount(device, MNTPOINT, "ext4", MS_REMOUNT | MS_RDONLY,
+			NULL) < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup,
+			"mount device:%s failed", device);
+	}
+
+	TEST(renameat(AT_FDCWD, TESTFILE4, AT_FDCWD, NEW_TESTFILE4));
+	check_and_print(EROFS);
+}
+
+static void test_emlink(void)
+{
+	if (mount(device, MNTPOINT, "ext4", MS_REMOUNT, NULL) < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup,
+			"mount device:%s failed", device);
+	}
+
+	TEST(renameat(AT_FDCWD, TESTDIR3, AT_FDCWD, NEW_TESTDIR3));
+	check_and_print(EMLINK);
+}
+
 static void check_and_print(int expected_errno)
 {
 	if (TEST_ERRNO == expected_errno) {
@@ -223,5 +331,8 @@ static void cleanup(void)
 {
 	TEST_CLEANUP;
 
+	if (umount(MNTPOINT) == -1)
+		tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
+
 	tst_rmdir();
 }
-- 
1.9.0
------------------------------------------------------------------------------
Start Your Social Network Today - Download eXo Platform
Build your Enterprise Intranet with eXo Platform Software
Java Based Open Source Intranet - Social, Extensible, Cloud Ready
Get Started Now And Turn Your Intranet Into A Collaboration Platform
http://p.sf.net/sfu/ExoPlatform
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v2 1/2] renameat/renameat01.c: cleanup
  2014-04-21 11:17 [LTP] [PATCH 1/2] renameat/renameat01.c: cleanup gux.fnst
  2014-04-21 11:17 ` [LTP] [PATCH 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests gux.fnst
@ 2014-05-16  4:35 ` Xing Gu
  2014-05-16  4:35   ` [LTP] [PATCH v2 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests Xing Gu
  2014-05-20 15:42 ` [LTP] [PATCH 1/2] renameat/renameat01.c: cleanup chrubis
  2014-06-17  7:26 ` [LTP] [PATCH v3 " Xing Gu
  3 siblings, 1 reply; 9+ messages in thread
From: Xing Gu @ 2014-05-16  4:35 UTC (permalink / raw)
  To: ltp-list

From: "gux.fnst@cn.fujitsu.com" <gux.fnst@cn.fujitsu.com>

Add renameat.h to encapsulate renameat syscall.
Delete some useless comments.
Use some SAFE_* macros.
Move the test body from main() to (*testfunc[])().

Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
 configure.ac                                    |   1 +
 include/lapi/fcntl.h                            |   4 +
 m4/ltp-renameat.m4                              |  25 ++
 testcases/kernel/syscalls/renameat/renameat.h   |  37 +++
 testcases/kernel/syscalls/renameat/renameat01.c | 348 +++++++++++-------------
 5 files changed, 232 insertions(+), 183 deletions(-)
 create mode 100644 m4/ltp-renameat.m4
 create mode 100644 testcases/kernel/syscalls/renameat/renameat.h

diff --git a/configure.ac b/configure.ac
index 8698c47..327a282 100644
--- a/configure.ac
+++ b/configure.ac
@@ -182,5 +182,6 @@ LTP_CHECK_FALLOCATE
 LTP_CHECK_SYSCALL_FCNTL
 LTP_CHECK_SYSCALL_PERF_EVENT_OPEN
 LTP_CHECK_TIRPC
+LTP_CHECK_RENAMEAT
 
 AC_OUTPUT
diff --git a/include/lapi/fcntl.h b/include/lapi/fcntl.h
index 85188a0..ca086b2 100644
--- a/include/lapi/fcntl.h
+++ b/include/lapi/fcntl.h
@@ -59,4 +59,8 @@
 # define FALLOC_FL_KEEP_SIZE 1
 #endif
 
+#ifndef AT_REMOVEDIR
+# define AT_REMOVEDIR 0x200
+#endif
+
 #endif /* __LAPI_FCNTL_H__ */
diff --git a/m4/ltp-renameat.m4 b/m4/ltp-renameat.m4
new file mode 100644
index 0000000..f40c58e
--- /dev/null
+++ b/m4/ltp-renameat.m4
@@ -0,0 +1,25 @@
+dnl
+dnl Copyright (c) Linux Test Project, 2014
+dnl
+dnl This program is free software;  you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation; either version 2 of the License, or
+dnl (at your option) any later version.
+dnl
+dnl This program is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY;  without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+dnl the GNU General Public License for more details.
+dnl
+dnl You should have received a copy of the GNU General Public License
+dnl along with this program;  if not, write to the Free Software Foundation,
+dnl Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+dnl
+
+dnl
+dnl LTP_CHECK_RENAMEAT
+dnl ----------------------------
+dnl
+AC_DEFUN([LTP_CHECK_RENAMEAT],[
+AC_CHECK_FUNCS(renameat,,)
+])
diff --git a/testcases/kernel/syscalls/renameat/renameat.h b/testcases/kernel/syscalls/renameat/renameat.h
new file mode 100644
index 0000000..fa1e2e5
--- /dev/null
+++ b/testcases/kernel/syscalls/renameat/renameat.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) International Business Machines  Corp., 2007
+ * Copyright (c) 2014 Fujitsu Ltd.
+ *
+ * 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef RENAMEAT_H
+#define RENAMEAT_H
+
+#include <sys/types.h>
+#include "config.h"
+#include "linux_syscall_numbers.h"
+
+#if !defined(HAVE_RENAMEAT)
+int renameat(int olddirfd, const char *oldpath, int newdirfd,
+			const char *newpath)
+{
+	return ltp_syscall(__NR_renameat, olddirfd, oldpath, newdirfd,
+					newpath);
+}
+#endif
+
+#endif /* RENAMEAT_H */
diff --git a/testcases/kernel/syscalls/renameat/renameat01.c b/testcases/kernel/syscalls/renameat/renameat01.c
index e27e344..7601e0c 100644
--- a/testcases/kernel/syscalls/renameat/renameat01.c
+++ b/testcases/kernel/syscalls/renameat/renameat01.c
@@ -1,46 +1,31 @@
-/******************************************************************************
+/*
+ * Copyright (c) International Business Machines  Corp., 2006
+ * 08/24/2006      Created first by Yi Yang <yyangcdl@cn.ibm.com>
  *
- *   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 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.
  *
- *   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
- *      renameat01.c
- *
- * DESCRIPTION
- *	This test case will verify basic function of renameat
- *	added by kernel 2.6.16 or up.
- *
- * USAGE:  <for command-line>
- * renameat01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p]
- * where:
- *      -c n : Run n copies simultaneously.
- *      -e   : Turn on errno logging.
- *      -i n : Execute test n times.
- *      -I x : Execute test for x seconds.
- *      -p   : Pause for SIGUSR1 before starting
- *      -P x : Pause for x seconds between iterations.
- *      -t   : Turn on syscall timing.
- *
- * Author
- *	Yi Yang <yyangcdl@cn.ibm.com>
- *
- * History
- *      08/24/2006      Created first by Yi Yang <yyangcdl@cn.ibm.com>
- *
- *****************************************************************************/
+ * 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:
+ *   Verify that,
+ *   1) renameat(2) returns -1 and sets errno to ENOTDIR if oldpath
+ *      is relative and olddirfd is a file descriptor referring to
+ *      a file other than a directory, or similar for newpath and
+ *      newdirfd.
+ *   2) renameat(2) returns -1 and sets errno to EBADF if olddirfd
+ *      or newdirfd is not a valid file descriptor.
+ */
 
 #define _GNU_SOURCE
 
@@ -53,193 +38,190 @@
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
+
 #include "test.h"
 #include "usctest.h"
+#include "safe_macros.h"
 #include "linux_syscall_numbers.h"
-
-#define TEST_CASES 5
-#ifndef AT_FDCWD
-#define AT_FDCWD -100
-#endif
-#ifndef AT_REMOVEDIR
-#define AT_REMOVEDIR 0x200
-#endif
-
-void setup();
-void cleanup();
-void setup_every_copy();
+#include "lapi/fcntl.h"
+#include "renameat.h"
+
+#define DIR_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
+#define FILE_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
+
+#define TESTDIR "testdir"
+#define NEW_TESTDIR "newtestdir"
+#define TESTFILE "testfile"
+#define NEW_TESTFILE "newtestfile"
+#define TESTFILE2 "testdir/testfile"
+#define NEW_TESTFILE2 "newtestdir/newtestfile"
+#define TESTFILE3 "/tmp/testfile3"
+#define NEW_TESTFILE3 "/tmp/newtestfile3"
+
+static void setup(void);
+static void cleanup(void);
+static void test_success1(void);
+static void test_success2(void);
+static void test_success3(void);
+static void test_enotdir(void);
+static void test_ebadf(void);
+static void check_and_print(int expected_errno);
+
+static void (*testfunc[])(void) = { test_success1, test_success2,
+				test_success3, test_enotdir, test_ebadf };
 
 char *TCID = "renameat01";
-int TST_TOTAL = TEST_CASES;
-char pathname[256];
-char dpathname[256];
-char testfile[256];
-char dtestfile[256];
-char testfile2[256];
-char dtestfile2[256];
-char testfile3[256];
-char dtestfile3[256];
-int olddirfd, newdirfd, fd, ret;
-int oldfds[TEST_CASES], newfds[TEST_CASES];
-char *oldfilenames[TEST_CASES], *newfilenames[TEST_CASES];
-int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0 };
-
-int myrenameat(int olddirfd, const char *oldfilename, int newdirfd,
-	       const char *newfilename)
-{
-	return ltp_syscall(__NR_renameat, olddirfd, oldfilename, newdirfd,
-		       newfilename);
-}
+int TST_TOTAL = ARRAY_SIZE(testfunc);
+static int exp_enos[] = { ENOTDIR, EBADF, 0 };
 
 int main(int ac, char **av)
 {
-	int lc;
 	char *msg;
-	int i;
-
-	/* Disable test if the version of the kernel is less than 2.6.16 */
-	if ((tst_kvercmp(2, 6, 16)) < 0) {
-		tst_brkm(TCONF, NULL,
-			 "This test can only run on kernels that are 2.6.16 and higher");
-	}
+	int i, lc;
 
-	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
+	msg = parse_opts(ac, av, NULL, NULL);
+	if (msg != NULL)
 		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
 
 	setup();
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		setup_every_copy();
+	TEST_EXP_ENOS(exp_enos);
 
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
 		tst_count = 0;
 
-		/*
-		 * Call renameat
-		 */
-		for (i = 0; i < TST_TOTAL; i++) {
-			TEST(myrenameat
-			     (oldfds[i], oldfilenames[i], newfds[i],
-			      newfilenames[i]));
-
-			/* check return code */
-			if (TEST_ERRNO == expected_errno[i]) {
-
-				if (STD_FUNCTIONAL_TEST) {
-					/* No Verification test, yet... */
-					tst_resm(TPASS | TTERRNO,
-						 "renameat failed as expected");
-				}
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "renameat failed unexpectedly");
-			}
-		}
-
+		for (i = 0; i < TST_TOTAL; i++)
+			(*testfunc[i])();
 	}
 
 	cleanup();
-
 	tst_exit();
 }
 
-void setup_every_copy(void)
+static void setup(void)
 {
-	/* Initialize test dir and file names */
-	sprintf(pathname, "renameattestdir%d", getpid());
-	sprintf(dpathname, "drenameattestdir%d", getpid());
-	sprintf(testfile, "renameattestfile%d.txt", getpid());
-	sprintf(dtestfile, "drenameattestfile%d.txt", getpid());
-	sprintf(testfile2, "renameattestdir%d/renameattestfile%d.txt", getpid(),
-		getpid());
-	sprintf(dtestfile2, "drenameattestdir%d/drenameattestfile%d.txt",
-		getpid(), getpid());
-	sprintf(testfile3, "/tmp/renameattestfile%d.txt", getpid());
-	sprintf(dtestfile3, "/tmp/drenameattestfile%d.txt", getpid());
-
-	ret = mkdir(pathname, 0700);
-	if (ret < 0) {
-		perror("mkdir: ");
-		exit(-1);
+	if ((tst_kvercmp(2, 6, 16)) < 0) {
+		tst_resm(TCONF, "This test can only run on kernels "
+			"that are 2.6.16 and higher");
+		return;
 	}
 
-	ret = mkdir(dpathname, 0700);
-	if (ret < 0) {
-		perror("mkdir: ");
-		exit(-1);
-	}
+	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
-	olddirfd = open(pathname, O_DIRECTORY);
-	if (olddirfd < 0) {
-		perror("open: ");
-		exit(-1);
-	}
+	tst_tmpdir();
 
-	newdirfd = open(dpathname, O_DIRECTORY);
-	if (newdirfd < 0) {
-		perror("open: ");
-		exit(-1);
-	}
+	TEST_PAUSE;
 
-	fd = open(testfile, O_CREAT | O_RDWR, 0600);
-	if (fd < 0) {
-		perror("open: ");
-		exit(-1);
-	}
+	SAFE_TOUCH(cleanup, TESTFILE, FILE_MODE, NULL);
+
+	SAFE_MKDIR(cleanup, TESTDIR, DIR_MODE);
+	SAFE_TOUCH(cleanup, TESTFILE2, FILE_MODE, NULL);
+	SAFE_MKDIR(cleanup, NEW_TESTDIR, DIR_MODE);
 
-	fd = open(testfile2, O_CREAT | O_RDWR, 0600);
-	if (fd < 0) {
-		perror("open: ");
-		exit(-1);
+	SAFE_TOUCH(cleanup, TESTFILE3, FILE_MODE, NULL);
+}
+
+static void test_success1(void)
+{
+	TEST(renameat(AT_FDCWD, TESTFILE, AT_FDCWD, NEW_TESTFILE));
+	check_and_print(0);
+
+	if (TEST_RETURN == 0) {
+		if (renameat(AT_FDCWD, NEW_TESTFILE, AT_FDCWD,
+				TESTFILE) == -1) {
+			tst_brkm(TBROK | TERRNO, cleanup, "restore test "
+				"environment: renameat(%d, %s, %d, %s) failed.",
+				AT_FDCWD, NEW_TESTFILE, AT_FDCWD, TESTFILE);
+		}
 	}
+}
+
+static void test_success2(void)
+{
+	int olddirfd, newdirfd;
+
+	olddirfd = SAFE_OPEN(cleanup, TESTDIR, O_DIRECTORY);
+	newdirfd = SAFE_OPEN(cleanup, NEW_TESTDIR, O_DIRECTORY);
 
-	fd = open(testfile3, O_CREAT | O_RDWR, 0600);
-	if (fd < 0) {
-		perror("open: ");
-		exit(-1);
+	TEST(renameat(olddirfd, TESTFILE, newdirfd, NEW_TESTFILE));
+	check_and_print(0);
+
+	if (TEST_RETURN == 0) {
+		if (renameat(newdirfd, NEW_TESTFILE, olddirfd,
+				TESTFILE) == -1) {
+			tst_brkm(TBROK | TERRNO, cleanup, "restore test "
+				"environment: renameat(%d, %s, %d, %s) failed.",
+				newdirfd, NEW_TESTFILE, olddirfd, TESTFILE);
+		}
 	}
 
-	oldfds[0] = oldfds[1] = olddirfd;
-	oldfds[2] = fd;
-	oldfds[3] = 100;
-	oldfds[4] = AT_FDCWD;
+	if (close(olddirfd) == -1)
+		tst_resm(TWARN | TERRNO, "close(%d) failed", olddirfd);
+
+	if (close(newdirfd) == -1)
+		tst_resm(TWARN | TERRNO, "close(%d) failed", newdirfd);
+}
+
+static void test_success3(void)
+{
+	int olddirfd, newdirfd;
+
+	olddirfd = SAFE_OPEN(cleanup, TESTDIR, O_DIRECTORY);
+	newdirfd = SAFE_OPEN(cleanup, NEW_TESTDIR, O_DIRECTORY);
 
-	newfds[0] = newfds[1] = newdirfd;
-	newfds[2] = fd;
-	newfds[3] = 100;
-	newfds[4] = AT_FDCWD;
+	TEST(renameat(olddirfd, TESTFILE3, newdirfd, NEW_TESTFILE3));
+	check_and_print(0);
 
-	oldfilenames[0] = oldfilenames[2] = oldfilenames[3] = oldfilenames[4] =
-	    testfile;
-	oldfilenames[1] = testfile3;
+	if (TEST_RETURN == 0) {
+		if (renameat(newdirfd, NEW_TESTFILE3, olddirfd,
+				TESTFILE3) == -1) {
+			tst_brkm(TBROK | TERRNO, cleanup, "restore test "
+				"environment: renameat(%d, %s, %d, %s) failed.",
+				newdirfd, NEW_TESTFILE3, olddirfd, TESTFILE3);
+		}
+	}
 
-	newfilenames[0] = newfilenames[2] = newfilenames[3] = newfilenames[4] =
-	    dtestfile;
-	newfilenames[1] = dtestfile3;
+	if (close(olddirfd) == -1)
+		tst_resm(TWARN | TERRNO, "close(%d) failed", olddirfd);
+
+	if (close(newdirfd) == -1)
+		tst_resm(TWARN | TERRNO, "close(%d) failed", newdirfd);
 }
 
-void setup(void)
+static void test_enotdir(void)
 {
+	int notdirfd;
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	notdirfd = SAFE_OPEN(cleanup, TESTFILE3, O_RDWR);
 
-	TEST_PAUSE;
+	TEST(renameat(notdirfd, TESTFILE, notdirfd, NEW_TESTFILE));
+	check_and_print(ENOTDIR);
+
+	if (close(notdirfd) == -1)
+		tst_resm(TWARN | TERRNO, "close(%d) failed", notdirfd);
+}
+
+static void test_ebadf(void)
+{
+	TEST(renameat(100, TESTFILE, 100, NEW_TESTFILE));
+	check_and_print(EBADF);
 }
 
-void cleanup(void)
+static void check_and_print(int expected_errno)
+{
+	if (TEST_ERRNO == expected_errno) {
+		tst_resm(TPASS | TTERRNO,
+			"renameat() returned the expected value");
+	} else {
+		tst_resm(TFAIL | TTERRNO,
+			"renameat() got unexpected return value; expected: "
+			"%d - %s", expected_errno, strerror(expected_errno));
+	}
+}
+
+static void cleanup(void)
 {
-	/* Remove them */
-	unlink(testfile2);
-	unlink(dtestfile2);
-	unlink(testfile3);
-	unlink(dtestfile3);
-	unlink(testfile);
-	unlink(dtestfile);
-	rmdir(pathname);
-	rmdir(dpathname);
-
-	/*
-	 * print timing stats if that option was specified.
-	 * print errno log if that option was specified.
-	 */
 	TEST_CLEANUP;
+
+	tst_rmdir();
 }
-- 
1.9.0


------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v2 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests
  2014-05-16  4:35 ` [LTP] [PATCH v2 1/2] renameat/renameat01.c: cleanup Xing Gu
@ 2014-05-16  4:35   ` Xing Gu
  0 siblings, 0 replies; 9+ messages in thread
From: Xing Gu @ 2014-05-16  4:35 UTC (permalink / raw)
  To: ltp-list

Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
 testcases/kernel/syscalls/renameat/renameat01.c | 116 +++++++++++++++++++++++-
 1 file changed, 114 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/renameat/renameat01.c b/testcases/kernel/syscalls/renameat/renameat01.c
index 7601e0c..8a2773f 100644
--- a/testcases/kernel/syscalls/renameat/renameat01.c
+++ b/testcases/kernel/syscalls/renameat/renameat01.c
@@ -25,6 +25,15 @@
  *      newdirfd.
  *   2) renameat(2) returns -1 and sets errno to EBADF if olddirfd
  *      or newdirfd is not a valid file descriptor.
+ *   3) renameat(2) returns -1 and sets errno to ELOOP if too many
+ *      symbolic links were encountered in resolving oldpath or
+ *      newpath.
+ *   4) renameat(2) returns -1 and sets errno to EROFS if the file
+ *      is on a read-only file system.
+ *   5) renameat(2) returns -1 and sets errno to EMLINK if oldpath
+ *      already has the maximum number of links to it, or it was a
+ *      directory and the directory containing newpath has the maximum
+ *      number of links.
  */
 
 #define _GNU_SOURCE
@@ -38,6 +47,7 @@
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
+#include <sys/mount.h>
 
 #include "test.h"
 #include "usctest.h"
@@ -49,14 +59,24 @@
 #define DIR_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
 #define FILE_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
 
+#define MNTPOINT "mntpoint"
 #define TESTDIR "testdir"
 #define NEW_TESTDIR "newtestdir"
+#define TESTDIR2 "/loopdir"
+#define NEW_TESTDIR2 "newloopdir"
+#define TESTDIR3 "mntpoint/mlinkdir"
+#define NEW_TESTDIR3 "mntpoint/testmlink/newmlinkdir"
 #define TESTFILE "testfile"
 #define NEW_TESTFILE "newtestfile"
 #define TESTFILE2 "testdir/testfile"
 #define NEW_TESTFILE2 "newtestdir/newtestfile"
 #define TESTFILE3 "/tmp/testfile3"
 #define NEW_TESTFILE3 "/tmp/newtestfile3"
+#define TESTFILE4 "mntpoint/rofile"
+#define NEW_TESTFILE4 "mntpoint/newrofile"
+
+static char looppathname[sizeof(TESTDIR2) * 43] = ".";
+static char *device;
 
 static void setup(void);
 static void cleanup(void);
@@ -65,14 +85,18 @@ static void test_success2(void);
 static void test_success3(void);
 static void test_enotdir(void);
 static void test_ebadf(void);
+static void test_eloop(void);
+static void test_erofs(void);
+static void test_emlink(void);
 static void check_and_print(int expected_errno);
 
 static void (*testfunc[])(void) = { test_success1, test_success2,
-				test_success3, test_enotdir, test_ebadf };
+				test_success3, test_enotdir, test_ebadf,
+				test_eloop, test_erofs, test_emlink };
 
 char *TCID = "renameat01";
 int TST_TOTAL = ARRAY_SIZE(testfunc);
-static int exp_enos[] = { ENOTDIR, EBADF, 0 };
+static int exp_enos[] = { ENOTDIR, EBADF, ELOOP, EROFS, EMLINK, 0 };
 
 int main(int ac, char **av)
 {
@@ -100,12 +124,28 @@ int main(int ac, char **av)
 
 static void setup(void)
 {
+	int i, ret;
+	char lname[PATH_MAX];
+	unsigned long long link_count = 0;
+	const char *const opts[] = { "-O", "^dir_index", NULL };
+
 	if ((tst_kvercmp(2, 6, 16)) < 0) {
 		tst_resm(TCONF, "This test can only run on kernels "
 			"that are 2.6.16 and higher");
 		return;
 	}
 
+	tst_require_root(NULL);
+
+	device = getenv("LTP_BIG_DEV");
+	if (device == NULL) {
+		tst_resm(TCONF, "you must specify a big"
+			"blockdevice(>500MB)");
+		return;
+	} else {
+		tst_mkfs(NULL, device, "ext4", opts);
+	}
+
 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
 	tst_tmpdir();
@@ -119,6 +159,46 @@ static void setup(void)
 	SAFE_MKDIR(cleanup, NEW_TESTDIR, DIR_MODE);
 
 	SAFE_TOUCH(cleanup, TESTFILE3, FILE_MODE, NULL);
+
+	/*
+	 * NOTE: the ELOOP test is written based on that the
+	 * consecutive symlinks limit in kernel is hardwired
+	 * to 40.
+	 */
+	SAFE_MKDIR(cleanup, "loopdir", DIR_MODE);
+	SAFE_SYMLINK(cleanup, "../loopdir", "loopdir/loopdir");
+	for (i = 0; i < 43; i++)
+		strcat(looppathname, TESTDIR2);
+
+	SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE);
+
+	if (mount(device, MNTPOINT, "ext4", 0, NULL) < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup,
+			"mount device:%s failed", device);
+	}
+
+	SAFE_TOUCH(cleanup, TESTFILE4, FILE_MODE, NULL);
+
+	SAFE_MKDIR(cleanup, TESTDIR3, DIR_MODE);
+	SAFE_MKDIR(cleanup, "mntpoint/testmlink", DIR_MODE);
+	while (1) {
+		sprintf(lname, "mntpoint/testmlink/mlink%lld", ++link_count);
+		ret = mkdir(lname, DIR_MODE);
+		if (ret == -1) {
+			switch (errno) {
+			case EMLINK:
+				tst_resm(TINFO, "for ext4 the max links is"
+					" %lld", link_count + 1);
+				break;
+			default:
+				tst_brkm(TCONF | TERRNO, cleanup,
+				"we do not hit the maximum number of "
+				"links to dir test_parent_dir in ext4."
+				"Unexpected error:");
+			}
+			break;
+		}
+	}
 }
 
 static void test_success1(void)
@@ -207,6 +287,35 @@ static void test_ebadf(void)
 	check_and_print(EBADF);
 }
 
+static void test_eloop(void)
+{
+	TEST(renameat(AT_FDCWD, looppathname, AT_FDCWD, NEW_TESTDIR2));
+	check_and_print(ELOOP);
+}
+
+static void test_erofs(void)
+{
+	if (mount(device, MNTPOINT, "ext4", MS_REMOUNT | MS_RDONLY,
+			NULL) < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup,
+			"mount device:%s failed", device);
+	}
+
+	TEST(renameat(AT_FDCWD, TESTFILE4, AT_FDCWD, NEW_TESTFILE4));
+	check_and_print(EROFS);
+}
+
+static void test_emlink(void)
+{
+	if (mount(device, MNTPOINT, "ext4", MS_REMOUNT, NULL) < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup,
+			"mount device:%s failed", device);
+	}
+
+	TEST(renameat(AT_FDCWD, TESTDIR3, AT_FDCWD, NEW_TESTDIR3));
+	check_and_print(EMLINK);
+}
+
 static void check_and_print(int expected_errno)
 {
 	if (TEST_ERRNO == expected_errno) {
@@ -223,5 +332,8 @@ static void cleanup(void)
 {
 	TEST_CLEANUP;
 
+	if (umount(MNTPOINT) == -1)
+		tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
+
 	tst_rmdir();
 }
-- 
1.9.0


------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH 1/2] renameat/renameat01.c: cleanup
  2014-04-21 11:17 [LTP] [PATCH 1/2] renameat/renameat01.c: cleanup gux.fnst
  2014-04-21 11:17 ` [LTP] [PATCH 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests gux.fnst
  2014-05-16  4:35 ` [LTP] [PATCH v2 1/2] renameat/renameat01.c: cleanup Xing Gu
@ 2014-05-20 15:42 ` chrubis
  2014-06-17  7:26 ` [LTP] [PATCH v3 " Xing Gu
  3 siblings, 0 replies; 9+ messages in thread
From: chrubis @ 2014-05-20 15:42 UTC (permalink / raw)
  To: gux.fnst@cn.fujitsu.com; +Cc: ltp-list@lists.sourceforge.net

Hi!
> +#define TESTDIR "testdir"
> +#define NEW_TESTDIR "newtestdir"
> +#define TESTFILE "testfile"
> +#define NEW_TESTFILE "newtestfile"
> +#define TESTFILE2 "testdir/testfile"
> +#define NEW_TESTFILE2 "newtestdir/newtestfile"
> +#define TESTFILE3 "/tmp/testfile3"
> +#define NEW_TESTFILE3 "/tmp/newtestfile3"

The test must not create files outside of the test tmpdir. Have a look
at mkdriat01.c, readlinkat01.c or any *at01.c that I've fixed recently.

You need to construct the absolute path from output of tst_get_tmpdir().

> +static void setup(void);
> +static void cleanup(void);
> +static void test_success1(void);
> +static void test_success2(void);
> +static void test_success3(void);
> +static void test_enotdir(void);
> +static void test_ebadf(void);
> +static void check_and_print(int expected_errno);
> +
> +static void (*testfunc[])(void) = { test_success1, test_success2,
> +				test_success3, test_enotdir, test_ebadf };

I think that the data driven approach (having one structure with all
parameters and single test function) is a bit easier to read, but that
is not a big problem. Have a look at mkdirat01.c.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v3 1/2] renameat/renameat01.c: cleanup
  2014-04-21 11:17 [LTP] [PATCH 1/2] renameat/renameat01.c: cleanup gux.fnst
                   ` (2 preceding siblings ...)
  2014-05-20 15:42 ` [LTP] [PATCH 1/2] renameat/renameat01.c: cleanup chrubis
@ 2014-06-17  7:26 ` Xing Gu
  2014-06-17  7:26   ` [LTP] [PATCH v3 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests Xing Gu
  2014-06-24 14:02   ` [LTP] [PATCH v3 1/2] renameat/renameat01.c: cleanup chrubis
  3 siblings, 2 replies; 9+ messages in thread
From: Xing Gu @ 2014-06-17  7:26 UTC (permalink / raw)
  To: ltp-list

Add renameat.h to encapsulate renameat syscall.
Delete some useless comments.
Use some SAFE_* macros.
Move the test body from main() to renameat_verify().

Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
 configure.ac                                    |   2 +
 include/lapi/fcntl.h                            |   4 +
 include/lapi/renameat.h                         |  36 +++
 m4/ltp-renameat.m4                              |  25 ++
 testcases/kernel/syscalls/renameat/renameat01.c | 312 ++++++++++--------------
 5 files changed, 195 insertions(+), 184 deletions(-)
 create mode 100644 include/lapi/renameat.h
 create mode 100644 m4/ltp-renameat.m4

diff --git a/configure.ac b/configure.ac
index 804a924..70bd1be 100644
--- a/configure.ac
+++ b/configure.ac
@@ -185,4 +185,6 @@ LTP_CHECK_TIRPC
 LTP_CHECK_TEE
 LTP_CHECK_SPLICE
 LTP_CHECK_VMSPLICE
+LTP_CHECK_RENAMEAT
+
 AC_OUTPUT
diff --git a/include/lapi/fcntl.h b/include/lapi/fcntl.h
index cd75fc7..200f574 100644
--- a/include/lapi/fcntl.h
+++ b/include/lapi/fcntl.h
@@ -51,6 +51,10 @@
 # define AT_SYMLINK_NOFOLLOW 0x100
 #endif
 
+#ifndef AT_REMOVEDIR
+# define AT_REMOVEDIR 0x200
+#endif
+
 #ifndef O_NOATIME
 # define O_NOATIME 01000000
 #endif
diff --git a/include/lapi/renameat.h b/include/lapi/renameat.h
new file mode 100644
index 0000000..b0ec5bc
--- /dev/null
+++ b/include/lapi/renameat.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) International Business Machines  Corp., 2007
+ * Copyright (c) 2014 Fujitsu Ltd.
+ *
+ * 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef RENAMEAT_H
+#define RENAMEAT_H
+
+#include <sys/types.h>
+#include "config.h"
+#include "linux_syscall_numbers.h"
+
+#if !defined(HAVE_RENAMEAT)
+int renameat(int olddirfd, const char *oldpath, int newdirfd,
+			const char *newpath)
+{
+	return ltp_syscall(__NR_renameat, olddirfd, oldpath, newdirfd,
+					newpath);
+}
+#endif
+
+#endif /* RENAMEAT_H */
diff --git a/m4/ltp-renameat.m4 b/m4/ltp-renameat.m4
new file mode 100644
index 0000000..f40c58e
--- /dev/null
+++ b/m4/ltp-renameat.m4
@@ -0,0 +1,25 @@
+dnl
+dnl Copyright (c) Linux Test Project, 2014
+dnl
+dnl This program is free software;  you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation; either version 2 of the License, or
+dnl (at your option) any later version.
+dnl
+dnl This program is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY;  without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+dnl the GNU General Public License for more details.
+dnl
+dnl You should have received a copy of the GNU General Public License
+dnl along with this program;  if not, write to the Free Software Foundation,
+dnl Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+dnl
+
+dnl
+dnl LTP_CHECK_RENAMEAT
+dnl ----------------------------
+dnl
+AC_DEFUN([LTP_CHECK_RENAMEAT],[
+AC_CHECK_FUNCS(renameat,,)
+])
diff --git a/testcases/kernel/syscalls/renameat/renameat01.c b/testcases/kernel/syscalls/renameat/renameat01.c
index a1aecc0..8632950 100644
--- a/testcases/kernel/syscalls/renameat/renameat01.c
+++ b/testcases/kernel/syscalls/renameat/renameat01.c
@@ -1,46 +1,30 @@
-/******************************************************************************
+/*
+ * 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 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.
  *
- *   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
- *      renameat01.c
- *
- * DESCRIPTION
- *	This test case will verify basic function of renameat
- *	added by kernel 2.6.16 or up.
- *
- * USAGE:  <for command-line>
- * renameat01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p]
- * where:
- *      -c n : Run n copies simultaneously.
- *      -e   : Turn on errno logging.
- *      -i n : Execute test n times.
- *      -I x : Execute test for x seconds.
- *      -p   : Pause for SIGUSR1 before starting
- *      -P x : Pause for x seconds between iterations.
- *      -t   : Turn on syscall timing.
- *
- * Author
- *	Yi Yang <yyangcdl@cn.ibm.com>
- *
- * History
- *      08/24/2006      Created first by Yi Yang <yyangcdl@cn.ibm.com>
- *
- *****************************************************************************/
+ * 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:
+ *   Verify that,
+ *   1) renameat(2) returns -1 and sets errno to EBADF if olddirfd
+ *      or newdirfd is not a valid file descriptor.
+ *   2) renameat(2) returns -1 and sets errno to ENOTDIR if oldpath
+ *      is relative and olddirfd is a file descriptor referring to
+ *      a file other than a directory, or similar for newpath and
+ *      newdirfd.
+ */
 
 #define _GNU_SOURCE
 
@@ -53,189 +37,149 @@
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
+
 #include "test.h"
 #include "usctest.h"
-#include "linux_syscall_numbers.h"
-
-#define TEST_CASES 5
-#ifndef AT_FDCWD
-#define AT_FDCWD -100
-#endif
-#ifndef AT_REMOVEDIR
-#define AT_REMOVEDIR 0x200
-#endif
-
-void setup();
-void cleanup();
-void setup_every_copy();
+#include "safe_macros.h"
+#include "lapi/fcntl.h"
+#include "lapi/renameat.h"
+
+#define TESTDIR "testdir"
+#define NEW_TESTDIR "new_testdir"
+#define TESTFILE "testfile"
+#define NEW_TESTFILE "new_testfile"
+#define TESTFILE2 "testfile2"
+#define NEW_TESTFILE2 "new_testfile2"
+#define TESTFILE3 "testdir/testfile"
+#define TESTFILE4 "testfile4"
+
+#define DIRMODE (S_IRWXU | S_IRWXG | S_IRWXO)
+#define FILEMODE (S_IRWXU | S_IRWXG | S_IRWXO)
+
+static int curfd = AT_FDCWD;
+static int olddirfd;
+static int newdirfd;
+static int badfd = 100;
+static int filefd;
+static char absoldpath[256];
+static char absnewpath[256];
+
+static struct test_case_t {
+	int *oldfdptr;
+	const char *oldpath;
+	int *newfdptr;
+	const char *newpath;
+	int exp_errno;
+} test_cases[] = {
+	{ &curfd, TESTFILE, &curfd, NEW_TESTFILE, 0 },
+	{ &olddirfd, TESTFILE, &newdirfd, NEW_TESTFILE, 0 },
+	{ &olddirfd, absoldpath, &newdirfd, absnewpath, 0 },
+	{ &badfd, TESTFILE, &badfd, NEW_TESTFILE, EBADF },
+	{ &filefd, TESTFILE, &filefd, NEW_TESTFILE, ENOTDIR },
+};
+
+static void setup(void);
+static void cleanup(void);
+static void renameat_verify(const struct test_case_t *);
 
 char *TCID = "renameat01";
-int TST_TOTAL = TEST_CASES;
-char pathname[256];
-char dpathname[256];
-char testfile[256];
-char dtestfile[256];
-char testfile2[256];
-char dtestfile2[256];
-char testfile3[256];
-char dtestfile3[256];
-int olddirfd, newdirfd, fd, ret;
-int oldfds[TEST_CASES], newfds[TEST_CASES];
-char *oldfilenames[TEST_CASES], *newfilenames[TEST_CASES];
-int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0 };
-
-int myrenameat(int olddirfd, const char *oldfilename, int newdirfd,
-	       const char *newfilename)
-{
-	return ltp_syscall(__NR_renameat, olddirfd, oldfilename, newdirfd,
-		       newfilename);
-}
+int TST_TOTAL = ARRAY_SIZE(test_cases);
+static int exp_enos[] = { EBADF, ENOTDIR, 0 };
 
 int main(int ac, char **av)
 {
-	int lc;
+	int i, lc;
 	const char *msg;
-	int i;
-
-	/* Disable test if the version of the kernel is less than 2.6.16 */
-	if ((tst_kvercmp(2, 6, 16)) < 0) {
-		tst_brkm(TCONF, NULL,
-			 "This test can only run on kernels that are 2.6.16 and higher");
-	}
 
-	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
+	msg = parse_opts(ac, av, NULL, NULL);
+	if (msg != NULL)
 		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
 
 	setup();
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		setup_every_copy();
+	TEST_EXP_ENOS(exp_enos);
 
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
 		tst_count = 0;
 
-		/*
-		 * Call renameat
-		 */
-		for (i = 0; i < TST_TOTAL; i++) {
-			TEST(myrenameat
-			     (oldfds[i], oldfilenames[i], newfds[i],
-			      newfilenames[i]));
-
-			/* check return code */
-			if (TEST_ERRNO == expected_errno[i]) {
-
-				tst_resm(TPASS | TTERRNO,
-					 "renameat failed as expected");
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "renameat failed unexpectedly");
-			}
-		}
-
+		for (i = 0; i < TST_TOTAL; i++)
+			renameat_verify(&test_cases[i]);
 	}
 
 	cleanup();
 	tst_exit();
 }
 
-void setup_every_copy(void)
+static void setup(void)
 {
-	/* Initialize test dir and file names */
-	sprintf(pathname, "renameattestdir%d", getpid());
-	sprintf(dpathname, "drenameattestdir%d", getpid());
-	sprintf(testfile, "renameattestfile%d.txt", getpid());
-	sprintf(dtestfile, "drenameattestfile%d.txt", getpid());
-	sprintf(testfile2, "renameattestdir%d/renameattestfile%d.txt", getpid(),
-		getpid());
-	sprintf(dtestfile2, "drenameattestdir%d/drenameattestfile%d.txt",
-		getpid(), getpid());
-	sprintf(testfile3, "/tmp/renameattestfile%d.txt", getpid());
-	sprintf(dtestfile3, "/tmp/drenameattestfile%d.txt", getpid());
-
-	ret = mkdir(pathname, 0700);
-	if (ret < 0) {
-		perror("mkdir: ");
-		exit(-1);
-	}
+	char *tmpdir;
 
-	ret = mkdir(dpathname, 0700);
-	if (ret < 0) {
-		perror("mkdir: ");
-		exit(-1);
-	}
-
-	olddirfd = open(pathname, O_DIRECTORY);
-	if (olddirfd < 0) {
-		perror("open: ");
-		exit(-1);
+	if ((tst_kvercmp(2, 6, 16)) < 0) {
+		tst_brkm(TCONF, NULL,
+			"This test can only run on kernels that are "
+			"2.6.16 and higher");
 	}
 
-	newdirfd = open(dpathname, O_DIRECTORY);
-	if (newdirfd < 0) {
-		perror("open: ");
-		exit(-1);
-	}
+	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
-	fd = open(testfile, O_CREAT | O_RDWR, 0600);
-	if (fd < 0) {
-		perror("open: ");
-		exit(-1);
-	}
+	tst_tmpdir();
 
-	fd = open(testfile2, O_CREAT | O_RDWR, 0600);
-	if (fd < 0) {
-		perror("open: ");
-		exit(-1);
-	}
+	TEST_PAUSE;
 
-	fd = open(testfile3, O_CREAT | O_RDWR, 0600);
-	if (fd < 0) {
-		perror("open: ");
-		exit(-1);
-	}
+	SAFE_TOUCH(cleanup, TESTFILE, FILEMODE, NULL);
 
-	oldfds[0] = oldfds[1] = olddirfd;
-	oldfds[2] = fd;
-	oldfds[3] = 100;
-	oldfds[4] = AT_FDCWD;
+	SAFE_TOUCH(cleanup, TESTFILE2, FILEMODE, NULL);
+	tmpdir = tst_get_tmpdir();
+	sprintf(absoldpath, "%s/%s", tmpdir, TESTFILE2);
+	sprintf(absnewpath, "%s/%s", tmpdir, NEW_TESTFILE2);
+	free(tmpdir);
 
-	newfds[0] = newfds[1] = newdirfd;
-	newfds[2] = fd;
-	newfds[3] = 100;
-	newfds[4] = AT_FDCWD;
+	SAFE_MKDIR(cleanup, TESTDIR, DIRMODE);
+	SAFE_TOUCH(cleanup, TESTFILE3, FILEMODE, NULL);
+	SAFE_MKDIR(cleanup, NEW_TESTDIR, DIRMODE);
 
-	oldfilenames[0] = oldfilenames[2] = oldfilenames[3] = oldfilenames[4] =
-	    testfile;
-	oldfilenames[1] = testfile3;
+	olddirfd = SAFE_OPEN(cleanup, TESTDIR, O_DIRECTORY);
+	newdirfd = SAFE_OPEN(cleanup, NEW_TESTDIR, O_DIRECTORY);
 
-	newfilenames[0] = newfilenames[2] = newfilenames[3] = newfilenames[4] =
-	    dtestfile;
-	newfilenames[1] = dtestfile3;
+	filefd = SAFE_OPEN(cleanup, TESTFILE4,
+				O_RDWR | O_CREAT, FILEMODE);
 }
 
-void setup(void)
+static void renameat_verify(const struct test_case_t *tc)
 {
+	TEST(renameat(*(tc->oldfdptr), tc->oldpath,
+			*(tc->newfdptr), tc->newpath));
+
+	if (TEST_ERRNO == tc->exp_errno) {
+		tst_resm(TPASS | TTERRNO,
+		"renameat() returned the expected value");
+	} else {
+		tst_resm(TFAIL | TTERRNO,
+			"renameat() got unexpected return value; expected: "
+			"%d - %s", tc->exp_errno,
+			strerror(tc->exp_errno));
+	}
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
+	if (TEST_ERRNO == 0 && renameat(*(tc->newfdptr), tc->newpath,
+		*(tc->oldfdptr), tc->oldpath) < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup, "renameat(%d, %s, "
+			"%d, %s) failed.", *(tc->newfdptr), tc->newpath,
+			*(tc->oldfdptr), tc->oldpath);
+	}
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
-	/* Remove them */
-	unlink(testfile2);
-	unlink(dtestfile2);
-	unlink(testfile3);
-	unlink(dtestfile3);
-	unlink(testfile);
-	unlink(dtestfile);
-	rmdir(pathname);
-	rmdir(dpathname);
-
-	/*
-	 * print timing stats if that option was specified.
-	 * print errno log if that option was specified.
-	 */
 	TEST_CLEANUP;
+
+	if (olddirfd && close(olddirfd) < 0)
+		tst_resm(TWARN | TERRNO, "close olddirfd failed");
+
+	if (newdirfd && close(newdirfd) < 0)
+		tst_resm(TWARN | TERRNO, "close newdirfd failed");
+
+	if (filefd && close(filefd) < 0)
+		tst_resm(TWARN | TERRNO, "close filefd failed");
+
+	tst_rmdir();
 }
-- 
1.9.3


------------------------------------------------------------------------------
HPCC Systems Open Source Big Data Platform from LexisNexis Risk Solutions
Find What Matters Most in Your Big Data with HPCC Systems
Open Source. Fast. Scalable. Simple. Ideal for Dirty Data.
Leverages Graph Analysis for Fast Processing & Easy Data Exploration
http://p.sf.net/sfu/hpccsystems
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v3 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests
  2014-06-17  7:26 ` [LTP] [PATCH v3 " Xing Gu
@ 2014-06-17  7:26   ` Xing Gu
  2014-06-24 14:03     ` chrubis
  2014-06-24 14:02   ` [LTP] [PATCH v3 1/2] renameat/renameat01.c: cleanup chrubis
  1 sibling, 1 reply; 9+ messages in thread
From: Xing Gu @ 2014-06-17  7:26 UTC (permalink / raw)
  To: ltp-list

Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
 runtest/syscalls                                |  2 +-
 testcases/kernel/syscalls/renameat/renameat01.c | 88 ++++++++++++++++++++++++-
 2 files changed, 87 insertions(+), 3 deletions(-)

diff --git a/runtest/syscalls b/runtest/syscalls
index c132a69..abb7906 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -850,7 +850,7 @@ rename13 rename13
 rename14 rename14
 
 #renameat test cases
-renameat01 renameat01
+renameat01 renameat01 -D $LTP_DEV -T $LTP_DEV_FS_TYPE
 
 rmdir01 rmdir01
 rmdir02 rmdir02 -D $LTP_DEV -T $LTP_DEV_FS_TYPE
diff --git a/testcases/kernel/syscalls/renameat/renameat01.c b/testcases/kernel/syscalls/renameat/renameat01.c
index 8632950..ce1e146 100644
--- a/testcases/kernel/syscalls/renameat/renameat01.c
+++ b/testcases/kernel/syscalls/renameat/renameat01.c
@@ -24,6 +24,15 @@
  *      is relative and olddirfd is a file descriptor referring to
  *      a file other than a directory, or similar for newpath and
  *      newdirfd.
+ *   3) renameat(2) returns -1 and sets errno to ELOOP if too many
+ *      symbolic links were encountered in resolving oldpath or
+ *      newpath.
+ *   4) renameat(2) returns -1 and sets errno to EROFS if the file
+ *      is on a read-only file system.
+ *   5) renameat(2) returns -1 and sets errno to EMLINK if oldpath
+ *      already has the maximum number of links to it, or it is a
+ *      directory and the directory containing newpath has the
+ *      maximum number of links.
  */
 
 #define _GNU_SOURCE
@@ -37,6 +46,7 @@
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
+#include <sys/mount.h>
 
 #include "test.h"
 #include "usctest.h"
@@ -44,14 +54,21 @@
 #include "lapi/fcntl.h"
 #include "lapi/renameat.h"
 
+#define MNTPOINT "mntpoint"
 #define TESTDIR "testdir"
 #define NEW_TESTDIR "new_testdir"
+#define TESTDIR2 "/loopdir"
+#define NEW_TESTDIR2 "newloopdir"
+#define TESTDIR3 "emlinkdir"
+#define NEW_TESTDIR3 "testemlinkdir/new_emlinkdir"
 #define TESTFILE "testfile"
 #define NEW_TESTFILE "new_testfile"
 #define TESTFILE2 "testfile2"
 #define NEW_TESTFILE2 "new_testfile2"
 #define TESTFILE3 "testdir/testfile"
 #define TESTFILE4 "testfile4"
+#define TESTFILE5 "mntpoint/rofile"
+#define NEW_TESTFILE5 "mntpoint/newrofile"
 
 #define DIRMODE (S_IRWXU | S_IRWXG | S_IRWXO)
 #define FILEMODE (S_IRWXU | S_IRWXG | S_IRWXO)
@@ -63,6 +80,18 @@ static int badfd = 100;
 static int filefd;
 static char absoldpath[256];
 static char absnewpath[256];
+static char looppathname[sizeof(TESTDIR2) * 43] = ".";
+static int max_subdirs;
+
+static char *fstype = "ext2";
+static char *device;
+static int mount_flag;
+
+static option_t options[] = {
+	{"T:", NULL, &fstype},
+	{"D:", NULL, &device},
+	{NULL, NULL, NULL},
+};
 
 static struct test_case_t {
 	int *oldfdptr;
@@ -76,25 +105,35 @@ static struct test_case_t {
 	{ &olddirfd, absoldpath, &newdirfd, absnewpath, 0 },
 	{ &badfd, TESTFILE, &badfd, NEW_TESTFILE, EBADF },
 	{ &filefd, TESTFILE, &filefd, NEW_TESTFILE, ENOTDIR },
+	{ &curfd, looppathname, &curfd, NEW_TESTDIR2, ELOOP },
+	{ &curfd, TESTFILE5, &curfd, NEW_TESTFILE5, EROFS },
+	{ &curfd, TESTDIR3, &curfd, NEW_TESTDIR3, EMLINK },
 };
 
 static void setup(void);
 static void cleanup(void);
 static void renameat_verify(const struct test_case_t *);
+static void help(void);
 
 char *TCID = "renameat01";
 int TST_TOTAL = ARRAY_SIZE(test_cases);
-static int exp_enos[] = { EBADF, ENOTDIR, 0 };
+static int exp_enos[] = { EBADF, ENOTDIR, ELOOP, EROFS,
+							EMLINK, 0 };
 
 int main(int ac, char **av)
 {
 	int i, lc;
 	const char *msg;
 
-	msg = parse_opts(ac, av, NULL, NULL);
+	msg = parse_opts(ac, av, options, help);
 	if (msg != NULL)
 		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
 
+	if (!device) {
+		tst_brkm(TCONF, NULL, "you must specify the device "
+			"used for mounting with -D option");
+	}
+
 	setup();
 
 	TEST_EXP_ENOS(exp_enos);
@@ -113,6 +152,7 @@ int main(int ac, char **av)
 static void setup(void)
 {
 	char *tmpdir;
+	int i;
 
 	if ((tst_kvercmp(2, 6, 16)) < 0) {
 		tst_brkm(TCONF, NULL,
@@ -120,6 +160,8 @@ static void setup(void)
 			"2.6.16 and higher");
 	}
 
+	tst_require_root(NULL);
+
 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
 	tst_tmpdir();
@@ -143,10 +185,42 @@ static void setup(void)
 
 	filefd = SAFE_OPEN(cleanup, TESTFILE4,
 				O_RDWR | O_CREAT, FILEMODE);
+
+	/*
+	 * NOTE: the ELOOP test is written based on that the
+	 * consecutive symlinks limit in kernel is hardwired
+	 * to 40.
+	 */
+	SAFE_MKDIR(cleanup, "loopdir", DIRMODE);
+	SAFE_SYMLINK(cleanup, "../loopdir", "loopdir/loopdir");
+	for (i = 0; i < 43; i++)
+		strcat(looppathname, TESTDIR2);
+
+	tst_mkfs(NULL, device, fstype, NULL);
+	SAFE_MKDIR(cleanup, MNTPOINT, DIRMODE);
+	if (mount(device, MNTPOINT, fstype, 0, NULL) < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup,
+			"mount device:%s failed", device);
+	}
+	mount_flag = 1;
+	SAFE_TOUCH(cleanup, TESTFILE5, FILEMODE, NULL);
+	if (mount(device, MNTPOINT, fstype,
+			MS_REMOUNT | MS_RDONLY, NULL) < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup,
+			"mount device:%s failed", device);
+	}
+
+	SAFE_MKDIR(cleanup, TESTDIR3, DIRMODE);
+	max_subdirs = tst_fs_fill_subdirs(cleanup, "testemlinkdir");
 }
 
 static void renameat_verify(const struct test_case_t *tc)
 {
+	if (tc->exp_errno == EMLINK && max_subdirs == 0) {
+		tst_resm(TCONF, "EMLINK test is not appropriate");
+		return;
+	}
+
 	TEST(renameat(*(tc->oldfdptr), tc->oldpath,
 			*(tc->newfdptr), tc->newpath));
 
@@ -181,5 +255,15 @@ static void cleanup(void)
 	if (filefd && close(filefd) < 0)
 		tst_resm(TWARN | TERRNO, "close filefd failed");
 
+	if (mount_flag && umount(MNTPOINT) < 0)
+		tst_resm(TWARN | TERRNO, "umount %s failed", MNTPOINT);
+
 	tst_rmdir();
 }
+
+static void help(void)
+{
+	printf("-T type   : specifies the type of filesystem to be mounted. "
+		"Default ext2.\n");
+	printf("-D device : device used for mounting.\n");
+}
-- 
1.9.3


------------------------------------------------------------------------------
HPCC Systems Open Source Big Data Platform from LexisNexis Risk Solutions
Find What Matters Most in Your Big Data with HPCC Systems
Open Source. Fast. Scalable. Simple. Ideal for Dirty Data.
Leverages Graph Analysis for Fast Processing & Easy Data Exploration
http://p.sf.net/sfu/hpccsystems
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v3 1/2] renameat/renameat01.c: cleanup
  2014-06-17  7:26 ` [LTP] [PATCH v3 " Xing Gu
  2014-06-17  7:26   ` [LTP] [PATCH v3 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests Xing Gu
@ 2014-06-24 14:02   ` chrubis
  1 sibling, 0 replies; 9+ messages in thread
From: chrubis @ 2014-06-24 14:02 UTC (permalink / raw)
  To: Xing Gu; +Cc: ltp-list

Hi!
> Add renameat.h to encapsulate renameat syscall.
> Delete some useless comments.
> Use some SAFE_* macros.
> Move the test body from main() to renameat_verify().

Pushed with folling changes, thanks.
(because it's important to check not only the errno but the return value
 as well)

 /*
  * Copyright (c) International Business Machines  Corp., 2006
+ *  Author: Yi Yang <yyangcdl@cn.ibm.com>
  *
  * 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
@@ -150,6 +151,16 @@ static void renameat_verify(const struct test_case_t *tc)
 	TEST(renameat(*(tc->oldfdptr), tc->oldpath,
 			*(tc->newfdptr), tc->newpath));
 
+	if (tc->exp_errno && TEST_RETURN != -1) {
+		tst_resm(TFAIL, "renameat() succeeded unexpectedly");
+		return;
+	}
+
+	if (tc->exp_errno == 0 && TEST_RETURN != 0) {
+		tst_resm(TFAIL | TTERRNO, "renameat() failed unexpectedly");
+		return;
+	}
+
 	if (TEST_ERRNO == tc->exp_errno) {
 		tst_resm(TPASS | TTERRNO,
 		"renameat() returned the expected value");

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v3 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests
  2014-06-17  7:26   ` [LTP] [PATCH v3 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests Xing Gu
@ 2014-06-24 14:03     ` chrubis
  0 siblings, 0 replies; 9+ messages in thread
From: chrubis @ 2014-06-24 14:03 UTC (permalink / raw)
  To: Xing Gu; +Cc: ltp-list

Hi!
> Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
> ---
>  runtest/syscalls                                |  2 +-
>  testcases/kernel/syscalls/renameat/renameat01.c | 88 ++++++++++++++++++++++++-
>  2 files changed, 87 insertions(+), 3 deletions(-)

I've converted the code to use the tst_acquire_device() and pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

end of thread, other threads:[~2014-06-24 14:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-21 11:17 [LTP] [PATCH 1/2] renameat/renameat01.c: cleanup gux.fnst
2014-04-21 11:17 ` [LTP] [PATCH 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests gux.fnst
2014-05-16  4:35 ` [LTP] [PATCH v2 1/2] renameat/renameat01.c: cleanup Xing Gu
2014-05-16  4:35   ` [LTP] [PATCH v2 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests Xing Gu
2014-05-20 15:42 ` [LTP] [PATCH 1/2] renameat/renameat01.c: cleanup chrubis
2014-06-17  7:26 ` [LTP] [PATCH v3 " Xing Gu
2014-06-17  7:26   ` [LTP] [PATCH v3 2/2] renameat/renameat01.c: add ELOOP, EROFS and EMLINK error value tests Xing Gu
2014-06-24 14:03     ` chrubis
2014-06-24 14:02   ` [LTP] [PATCH v3 1/2] renameat/renameat01.c: cleanup chrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox