Skip to contentSkip to conversation

syscall/renameat2: Add tests for renameat2

PATCH Superseded Cedric Hnyda 2 participants 2 messages Sep 25, 2015 – Sep 30, 2015

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 syscall/renameat2: Add tests for renameat2

Added tests for the syscall renameat2.

---
 configure.ac                                      |   1 +
 include/lapi/fcntl.h                              |  12 ++
 m4/ltp-renameat2.m4                               |  27 ++++
 runtest/syscalls                                  |   3 +
 testcases/kernel/include/arm.in                   |   1 +
 testcases/kernel/include/i386.in                  |   1 +
 testcases/kernel/include/ia64.in                  |   1 +
 testcases/kernel/include/powerpc.in               |   1 +
 testcases/kernel/include/powerpc64.in             |   1 +
 testcases/kernel/include/s390.in                  |   1 +
 testcases/kernel/include/s390x.in                 |   1 +
 testcases/kernel/include/sparc.in                 |   1 +
 testcases/kernel/include/sparc64.in               |   1 +
 testcases/kernel/include/x86_64.in                |   1 +
 testcases/kernel/syscalls/.gitignore              |   2 +
 testcases/kernel/syscalls/renameat2/Makefile      |  24 ++++
 testcases/kernel/syscalls/renameat2/renameat2.h   |  37 +++++
 testcases/kernel/syscalls/renameat2/renameat201.c | 158 ++++++++++++++++++++++
 testcases/kernel/syscalls/renameat2/renameat202.c | 131 ++++++++++++++++++
 19 files changed, 405 insertions(+)
 create mode 100644 m4/ltp-renameat2.m4
 create mode 100644 testcases/kernel/syscalls/renameat2/Makefile
 create mode 100644 testcases/kernel/syscalls/renameat2/renameat2.h
 create mode 100644 testcases/kernel/syscalls/renameat2/renameat201.c
 create mode 100644 testcases/kernel/syscalls/renameat2/renameat202.c
configure.ac +1 -0
index ecea070..76c7628 100644
@@ -165,6 +165,7 @@ LTP_CHECK_MKNODAT
 LTP_CHECK_READLINKAT
 LTP_CHECK_OPENAT
 LTP_CHECK_RENAMEAT
+LTP_CHECK_RENAMEAT2
 LTP_CHECK_FALLOCATE
 LTP_CHECK_SYSCALL_FCNTL
 LTP_CHECK_SYSCALL_PERF_EVENT_OPEN
include/lapi/fcntl.h +12 -0
index 3667b5e..0661651 100644
@@ -67,6 +67,18 @@
 # define FALLOC_FL_KEEP_SIZE 1
 #endif
 
+#ifndef RENAME_NOREPLACE
+# define RENAME_NOREPLACE	(1 << 0)
+#endif
+
+#ifndef RENAME_EXCHANGE
+# define RENAME_EXCHANGE		(1 << 1)
+#endif
+
+#ifndef RENAME_WHITEOUT
+# define RENAME_WHITEOUT		(1 << 2)
+#endif
+
 /* splice, vmsplice, tee */
 
 #ifndef SPLICE_F_NONBLOCK
m4/ltp-renameat2.m4 new file +27 -0
new file mode 100644
index 0000000..2bc7313
@@ -0,0 +1,27 @@
+dnl
+dnl Copyright (c) Linux Test Project, 2015
+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 Author: Cedric Hnyda <[email protected]>
+dnl
+
+dnl
+dnl LTP_CHECK_RENAMEAT2
+dnl ----------------------------
+dnl
+AC_DEFUN([LTP_CHECK_RENAMEAT2],[
+AC_CHECK_FUNCS(renameat2,,)
+])
runtest/syscalls +3 -0
index 25d10ac..0d5fdb4 100644
@@ -863,6 +863,9 @@ rename14 rename14
 #renameat test cases
 renameat01 renameat01
 
+renameat201 renameat201
+renameat202 renameat202
+
 rmdir01 rmdir01
 rmdir02 rmdir02
 rmdir03 rmdir03
testcases/kernel/include/arm.in +1 -0
index 841b064..eb130b7 100644
@@ -333,4 +333,5 @@ accept4 (__NR_SYSCALL_BASE+366)
 fanotify_init (__NR_SYSCALL_BASE+367)
 fanotify_mark (__NR_SYSCALL_BASE+368)
 prlimit64 (__NR_SYSCALL_BASE+369)
+renameat2 (__NR_SYSCALL_BASE+382)
 getrandom (__NR_SYSCALL_BASE+384)
testcases/kernel/include/i386.in +1 -0
index a4585c2..0e39902 100644
@@ -333,4 +333,5 @@ recvmmsg 337
 fanotify_init 338
 fanotify_mark 339
 prlimit64 340
+renameat2 354
 getrandom 355
testcases/kernel/include/ia64.in +1 -0
index 4f8fb74..846141f 100644
@@ -292,4 +292,5 @@ recvmmsg 1322
 fanotify_init 1323
 fanotify_mark 1324
 prlimit64 1325
+renameat2 1338
 getrandom 1339
testcases/kernel/include/powerpc.in +1 -0
index 4025436..ef7469f 100644
@@ -340,4 +340,5 @@ recvmsg 342
 recvmmsg 343
 accept4 344
 syscalls 345
+renameat2 357
 getrandom 359
testcases/kernel/include/powerpc64.in +1 -0
index 4025436..ef7469f 100644
@@ -340,4 +340,5 @@ recvmsg 342
 recvmmsg 343
 accept4 344
 syscalls 345
+renameat2 357
 getrandom 359
testcases/kernel/include/s390.in +1 -0
index 9c7cfa1..c97caa5 100644
@@ -324,4 +324,5 @@ setgid 214
 setfsuid 215
 setfsgid 216
 newfstatat 293
+renameat2 347
 getrandom 349
testcases/kernel/include/s390x.in +1 -0
index 9c7cfa1..c97caa5 100644
@@ -324,4 +324,5 @@ setgid 214
 setfsuid 215
 setfsgid 216
 newfstatat 293
+renameat2 347
 getrandom 349
testcases/kernel/include/sparc.in +1 -0
index 697378f..0c394f8 100644
@@ -331,3 +331,4 @@ recvmmsg 328
 fanotify_init 329
 fanotify_mark 330
 prlimit64 331
+renameat2 345
testcases/kernel/include/sparc64.in +1 -0
index 83c1792..a70540a 100644
@@ -307,3 +307,4 @@ recvmmsg 328
 fanotify_init 329
 fanotify_mark 330
 prlimit64 331
+renameat2 345
testcases/kernel/include/x86_64.in +1 -0
index 860e726..726b76f 100644
@@ -300,4 +300,5 @@ recvmmsg 299
 fanotify_init 300
 fanotify_mark 301
 prlimit64 302
+renameat2 316
 getrandom 318
testcases/kernel/syscalls/.gitignore +2 -0
index f3a18ab..2553b2c 100644
@@ -713,6 +713,8 @@
 /rename/rename13
 /rename/rename14
 /renameat/renameat01
+/renameat2/renameat201
+/renameat2/renameat202
 /rmdir/rmdir01
 /rmdir/rmdir02
 /rmdir/rmdir03
testcases/kernel/syscalls/renameat2/Makefile new file +24 -0
new file mode 100644
index 0000000..083a071
@@ -0,0 +1,24 @@
+#
+#  Copyright (C) 2015 Cedric Hnyda [email protected]
+#
+#  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 St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/renameat2/renameat2.h new file +37 -0
new file mode 100644
index 0000000..8c69746
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2015 Cedric Hnyda [email protected]
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Further, this software is distributed without any warranty that it is
+ * free of the rightful claim of any third person regarding infringement
+ * or the like.  Any license provided herein, whether implied or
+ * otherwise, applies only to this software file.  Patent licenses, if
+ * any, provided herein do not apply to combinations of this program with
+ * other software, or any other product whatsoever.
+ *
+ */
+
+#ifndef RENAMEAT2_H
+#define RENAMEAT2_H
+
+#include <sys/types.h>
+#include "config.h"
+#include "linux_syscall_numbers.h"
+
+#if !defined(HAVE_RENAMEAT2)
+int renameat2(int olddirfd, const char *oldpath, int newdirfd,
+				const char *newpath, unsigned int flags)
+{
+	return ltp_syscall(__NR_renameat2, olddirfd, oldpath, newdirfd,
+						newpath, flags);
+}
+#endif
+
+#endif /* RENAMEAT2_H */
testcases/kernel/syscalls/renameat2/renameat201.c new file +158 -0

158 lines not on this page · raw message

testcases/kernel/syscalls/renameat2/renameat202.c new file +131 -0

131 lines not on this page · raw message

Signature · 1 line
2.1.4
#1 lore raw

PATCH v2 1/1 syscall/renameat2: Add tests for renameat2

Hi!

Quoted 14 lines
testcases/kernel/syscalls/renameat2/renameat201.c new file +14 -0
+static void renameat2_verify(const struct test_case *test)
+{
+	TEST(renameat2(*(test->olddirfd), test->oldpath,
+			*(test->newdirfd), test->newpath, test->flags));
+
+	if (test->exp_errno && TEST_RETURN != -1) {
+		tst_resm(TFAIL, "renameat2() succeeded unexpectedly");
+		return;
+	}
+
+	if (test->exp_errno == 0 && TEST_RETURN != 0) {
+		tst_resm(TFAIL | TTERRNO, "renameat2() failed unexpectedly");
+		return;
+	}

testcases/kernel/syscalls/renameat2/renameat201.c line 156

Quoted 10 lines
testcases/kernel/syscalls/renameat2/renameat201.c new file +10 -0
+
+	if (test->exp_errno == TEST_ERRNO) {
+		tst_resm(TPASS | TTERRNO,
+		"renameat2() returned the expected value");
+		return;
+	}
+
+	tst_resm(TFAIL | TTERRNO,
+		"renameat2() got unexpected return value: expected: %d - %s",
+			test->exp_errno, strerror(test->exp_errno));

We have tst_strerrno() which produces much nicer output than strerror(), please use it instead. Also printing raw interger errno value is not really usefull once tst_strerrno() is used.

Quoted 66 lines
(quoted diff) +1 -0
+}
testcases/kernel/syscalls/renameat2/renameat202.c new file +59 -0
new file mode 100644
index 0000000..5838359
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2015 Cedric Hnyda <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Further, this software is distributed without any warranty that it is
+ * free of the rightful claim of any third person regarding infringement
+ * or the like.  Any license provided herein, whether implied or
+ * otherwise, applies only to this software file.  Patent licenses, if
+ * any, provided herein do not apply to combinations of this program with
+ * other software, or any other product whatsoever.
+ */
+
+ /* Description:
+ *   Calls renameat2(2) with the flag RENAME_EXCHANGE and check that
+ *   the content was swapped
+ */
+
+#define _GNU_SOURCE
+
+#include "test.h"
+#include "safe_macros.h"
+#include "lapi/fcntl.h"
+#include "renameat2.h"
+
+#define TEST_DIR "test_dir/"
+#define TEST_DIR2 "test_dir2/"
+
+#define TEST_FILE "test_file"
+#define TEST_FILE2 "test_file2"
+
+char *TCID = "renameat202";
+
+static int olddirfd;
+static int newdirfd;
+static int fd = -1;
+
+static const char content[] = "content";
+
+
+int TST_TOTAL = 1;
+
+static void setup(void);
+static void cleanup(void);
+static void renameat2_verify(void);
+
+
+int main(int ac, char **av)
+{
+	int lc;
+
+	tst_parse_opts(ac, av, NULL, NULL);
+

testcases/kernel/syscalls/renameat2/renameat202.c line 69

Quoted 10 lines
testcases/kernel/syscalls/renameat2/renameat202.c new file +10 -0
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+
+		tst_count = 0;
+
+		setup();
+		TEST(renameat2(olddirfd, TEST_FILE,
+				newdirfd, TEST_FILE2, RENAME_EXCHANGE));
+
+		renameat2_verify();
+		cleanup();

Can we please avoid calling setup() here repeatedly? Because that would call tst_tmpdir() over and over...

The most elegant solution I can think of would keep track of which test file currently should contain the content. Then we would just check that the right file contains the "content" and that the second one is empty in the verify() function.

Quoted 51 lines
testcases/kernel/syscalls/renameat2/renameat202.c new file +51 -0
+	}
+
+	tst_exit();
+}
+
+static void setup(void)
+{
+	if ((tst_kvercmp(3, 15, 0)) < 0) {
+		tst_brkm(TCONF, NULL,
+			"This test can only run on kernels that are 3.15. and higher");
+	}
+
+	tst_tmpdir();
+
+	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
+	SAFE_MKDIR(cleanup, TEST_DIR2, 0700);
+
+	SAFE_TOUCH(cleanup, TEST_DIR TEST_FILE, 0600, NULL);
+	SAFE_TOUCH(cleanup, TEST_DIR2 TEST_FILE2, 0600, NULL);
+
+	olddirfd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
+	newdirfd = SAFE_OPEN(cleanup, TEST_DIR2, O_DIRECTORY);
+
+	SAFE_FILE_PRINTF(cleanup, TEST_DIR TEST_FILE, "%s", content);
+
+}
+
+static void cleanup(void)
+{
+	if (olddirfd > 0 && close(olddirfd) < 0)
+		tst_resm(TWARN | TERRNO, "close olddirfd failed");
+
+	if (newdirfd > 0 && close(newdirfd) < 0)
+		tst_resm(TWARN | TERRNO, "close newdirfd failed");
+
+	if (fd > 0 && close(fd) < 0)
+		tst_resm(TWARN | TERRNO, "close fd failed");
+
+	tst_rmdir();
+
+}
+
+static void renameat2_verify(void)
+{
+	char str[sizeof(content)];
+
+	if (TEST_RETURN != 0) {
+		tst_resm(TFAIL, "renameat2() failed unexpectedly");
+		return;
+	}
+

testcases/kernel/syscalls/renameat2/renameat202.c line 130

Quoted 10 lines
testcases/kernel/syscalls/renameat2/renameat202.c new file +10 -0
+	fd = SAFE_OPEN(cleanup, TEST_DIR2 TEST_FILE2, O_RDONLY);
+
+	SAFE_READ(cleanup, 0, fd, str, strlen(content) + 10);
+
+	if (str[strlen(content)] == '\0' && !strcmp(content, str))
+		tst_resm(TPASS,
+			"renameat2() swapped the content of the two files");
+	else
+		tst_resm(TFAIL,
+			"renameat2() didn't swap the content of the two files");

We should really close the fd and set it to 0 here even if it's handled by cleanup().

Signature · 2 lines
Cyril Hrubis
[email protected]