[PATCH 1/3] renameat201.c: Rewrite into new API, split into renameat203.c

Petr Vorel <[email protected]>
Newsgroups gmane.linux.ltp
Message-ID <[email protected]>
Keep only expected errors changes in renameat201.c, move successful
cases into renameat203.c (although RENAME_EXCHANGE is somehow covered by
renameat202.c, lets keep simple RENAME_EXCHANGE test also in new
renameat203.c).

Due cdd1fedf8261c ("btrfs: add support for RENAME_EXCHANGE and
RENAME_WHITEOUT") from v4.7-rc1 which has been backported to 4.4 based
SLES 12-SP3 kernel it's not possible to use TST_EXP_PASS() (which would
further simplify the code) because we cannot raise .min_kver = "4.7".

Other options would be to use tst_kvercmp2(), which is defacto
deprecated (single test is using it).

Simplify, print a description, fix typo implemented.

Signed-off-by: Petr Vorel <[email protected]>
---
 .../kernel/syscalls/renameat2/renameat201.c   | 172 +++++-------------
 .../kernel/syscalls/renameat2/renameat203.c   |  99 ++++++++++
 2 files changed, 148 insertions(+), 123 deletions(-)
 create mode 100644 testcases/kernel/syscalls/renameat2/renameat203.c

diff --git a/testcases/kernel/syscalls/renameat2/renameat201.c b/testcases/kernel/syscalls/renameat2/renameat201.c
index 01e34d6534..a7ec388ab4 100644
--- a/testcases/kernel/syscalls/renameat2/renameat201.c
+++ b/testcases/kernel/syscalls/renameat2/renameat201.c
@@ -1,163 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) 2015 Cedric Hnyda <chnyda-IBi9RG/[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 would 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 the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ * Copyright (c) 2026 Petr Vorel <[email protected]>
  */
 
- /* Description:
- *   Verify that:
- *   1) renameat2(2) returns -1 and sets errno to EEXIST because newpath
- *		already exists and the flag RENAME_NOREPLACE is used.
- *   2) renameat2(2) returns 0.
- *   3) renameat2(2) returns -1 and sets errno to ENOENT because the flag
- *		RENAME_EXCHANGE is used and newpath does not exist.
- *   4) renameat2(2) returns 0 because the flag RENAME_NOREPLACE is used,
- *		both olddirfd and newdirfd are valid and oldpath exists and
- *		newpath does not exist.
- *   5) renameat2(2) returns -1 and sets errno to EINVAL because
- *		RENAME_NOREPLACE and RENAME_EXCHANGE are used together
- *   6) renameat2(2) returns -1 and sets errno to EINVAL because
- *		RENAME_WHITEOUT and RENAME_EXCHANGE are used together
+/*\
+ * Verify that :manpage:`renameat2(2)` returns -1 and sets errno to:
+ *
+ * 1. EEXIST when newpath already exists and the flag RENAME_NOREPLACE is used.
+ * 2. ENOENT when the flag RENAME_EXCHANGE is used and newpath does not exist.
+ * 3. EINVAL when RENAME_NOREPLACE and RENAME_EXCHANGE are used together
+ * 4. EINVAL when RENAME_WHITEOUT and RENAME_EXCHANGE are used together
  */
 
 #define _GNU_SOURCE
 
-#include "test.h"
-#include "tso_safe_macros.h"
 #include "lapi/fcntl.h"
+#include "tst_test.h"
 #include "renameat2.h"
 
 #define TEST_DIR "test_dir/"
 #define TEST_DIR2 "test_dir2/"
 
+#define TEST_PATH TEST_DIR TEST_FILE
+#define TEST_PATH2 TEST_DIR2 TEST_FILE2
+
 #define TEST_FILE "test_file"
 #define TEST_FILE2 "test_file2"
-#define TEST_FILE3 "test_file3"
 #define NON_EXIST "non_exist"
 
-char *TCID = "renameat201";
-
-static int olddirfd;
-static int newdirfd;
-static long fs_type;
+#define FLAGS_ERRNO_DESC(x, y) .flags = x, .exp_errno = y, .desc = "flags: " #x ", errno: " #y
 
-static struct test_case {
-	int *olddirfd;
-	const char *oldpath;
-	int *newdirfd;
+static struct tcase {
 	const char *newpath;
 	int flags;
 	int exp_errno;
-} test_cases[] = {
-	{&olddirfd, TEST_FILE, &newdirfd, TEST_FILE2, RENAME_NOREPLACE, EEXIST},
-	{&olddirfd, TEST_FILE, &newdirfd, TEST_FILE2, RENAME_EXCHANGE, 0},
-	{&olddirfd, TEST_FILE, &newdirfd, NON_EXIST, RENAME_EXCHANGE, ENOENT},
-	{&olddirfd, TEST_FILE, &newdirfd, TEST_FILE3, RENAME_NOREPLACE, 0},
-	{&olddirfd, TEST_FILE, &newdirfd, TEST_FILE2, RENAME_NOREPLACE
-				| RENAME_EXCHANGE, EINVAL},
-	{&olddirfd, TEST_FILE, &newdirfd, TEST_FILE2, RENAME_WHITEOUT
-				| RENAME_EXCHANGE, EINVAL}
+	char *desc;
+} tcases[] = {
+	{TEST_FILE2, FLAGS_ERRNO_DESC(RENAME_NOREPLACE, EEXIST)},
+	{NON_EXIST, FLAGS_ERRNO_DESC(RENAME_EXCHANGE, ENOENT)},
+	{TEST_FILE2, FLAGS_ERRNO_DESC(RENAME_NOREPLACE | RENAME_EXCHANGE, EINVAL)},
+	{TEST_FILE2, FLAGS_ERRNO_DESC(RENAME_WHITEOUT | RENAME_EXCHANGE, EINVAL)}
 };
 
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-static void setup(void);
-static void cleanup(void);
-static void renameat2_verify(const struct test_case *test);
-
-
-int main(int ac, char **av)
-{
-	int i;
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; lc < TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++)
-			renameat2_verify(&test_cases[i]);
-	}
-
-	cleanup();
-	tst_exit();
-}
+static int olddirfd;
+static int newdirfd;
+static long fs_type;
 
 static void setup(void)
 {
-	tst_tmpdir();
-
-	fs_type = tst_fs_type(cleanup, ".");
+	fs_type = tst_fs_type(".");
 
-	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
-	SAFE_MKDIR(cleanup, TEST_DIR2, 0700);
+	SAFE_MKDIR(TEST_DIR, 0700);
+	SAFE_MKDIR(TEST_DIR2, 0700);
 
-	SAFE_TOUCH(cleanup, TEST_DIR TEST_FILE, 0600, NULL);
-	SAFE_TOUCH(cleanup, TEST_DIR2 TEST_FILE2, 0600, NULL);
-	SAFE_TOUCH(cleanup, TEST_DIR TEST_FILE3, 0600, NULL);
+	olddirfd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
+	newdirfd = SAFE_OPEN(TEST_DIR2, O_DIRECTORY);
 
-	olddirfd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
-	newdirfd = SAFE_OPEN(cleanup, TEST_DIR2, O_DIRECTORY);
+	SAFE_TOUCH(TEST_PATH, 0600, NULL);
+	SAFE_TOUCH(TEST_PATH2, 0600, NULL);
 }
 
 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");
-
-	tst_rmdir();
+	if (olddirfd > 0)
+		SAFE_CLOSE(olddirfd);
 
+	if (newdirfd > 0)
+		SAFE_CLOSE(newdirfd);
 }
 
-static void renameat2_verify(const struct test_case *test)
+static void renameat2_verify(unsigned int i)
 {
-	TEST(renameat2(*(test->olddirfd), test->oldpath,
-			*(test->newdirfd), test->newpath, test->flags));
-
-	if ((test->flags & RENAME_EXCHANGE) && EINVAL == TEST_ERRNO
-		&& fs_type == TST_BTRFS_MAGIC) {
-		tst_resm(TCONF,
-			"RENAME_EXCHANGE flag is not implemeted on %s",
-			tst_fs_type_name(fs_type));
-		return;
-	}
-
-	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;
-	}
-
-	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, tst_strerrno(test->exp_errno));
+	struct tcase *tc = &tcases[i];
 
+	tst_res(TINFO, "Testing flag: %s", tc->desc);
+
+	TST_EXP_FAIL(renameat2(olddirfd, TEST_FILE, newdirfd, tc->newpath,
+						   tc->flags), tc->exp_errno);
 }
+
+static struct tst_test test = {
+	.test = renameat2_verify,
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+};
diff --git a/testcases/kernel/syscalls/renameat2/renameat203.c b/testcases/kernel/syscalls/renameat2/renameat203.c
new file mode 100644
index 0000000000..dba12e48b3
--- /dev/null
+++ b/testcases/kernel/syscalls/renameat2/renameat203.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2015 Cedric Hnyda <chnyda-IBi9RG/[email protected]>
+ * Copyright (c) 2026 Petr Vorel <[email protected]>
+ */
+
+/*\
+ * Verify that :manpage:`renameat2(2)` works on these flags:
+ *
+ * 1. RENAME_EXCHANGE
+ * 2. RENAME_NOREPLACE
+ */
+
+#define _GNU_SOURCE
+
+#include "lapi/fcntl.h"
+#include "tst_test.h"
+#include "renameat2.h"
+
+#define TEST_DIR "test_dir/"
+#define TEST_DIR2 "test_dir2/"
+
+#define TEST_FILE "test_file"
+#define TEST_FILE2 "test_file2"
+
+#define TEST_PATH TEST_DIR TEST_FILE
+#define TEST_PATH2 TEST_DIR2 TEST_FILE2
+
+#define FLAGS_DESC(x) .flags = x, .desc = #x
+
+static struct tcase {
+	int flags;
+	char *desc;
+} tcases[] = {
+	{FLAGS_DESC(RENAME_EXCHANGE)},
+	{FLAGS_DESC(RENAME_NOREPLACE)},
+};
+
+static int olddirfd;
+static int newdirfd;
+static long fs_type;
+
+static void setup(void)
+{
+	// TODO
+	fs_type = tst_fs_type(".");
+
+	SAFE_MKDIR(TEST_DIR, 0700);
+	SAFE_MKDIR(TEST_DIR2, 0700);
+
+	olddirfd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
+	newdirfd = SAFE_OPEN(TEST_DIR2, O_DIRECTORY);
+}
+
+static void cleanup(void)
+{
+	if (olddirfd > 0)
+		SAFE_CLOSE(olddirfd);
+
+	if (newdirfd > 0)
+		SAFE_CLOSE(newdirfd);
+}
+
+static void renameat2_verify(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+
+	tst_res(TINFO, "Testing flag: %s", tc->desc);
+
+	SAFE_TOUCH(TEST_PATH, 0600, NULL);
+	if (!i)
+		SAFE_TOUCH(TEST_PATH2, 0600, NULL);
+
+	TEST(renameat2(olddirfd, TEST_FILE, newdirfd, TEST_FILE2, tc->flags));
+	SAFE_UNLINK(TEST_PATH2);
+
+	if (TST_RET == -1) {
+		/*
+		 * cdd1fedf8261c ("btrfs: add support for RENAME_EXCHANGE and
+		 * RENAME_WHITEOUT") from v4.7-rc1 has been backported to 4.4
+		 * based SLES 12-SP3 kernel.
+		 */
+		if (TST_ERR == EINVAL && fs_type == TST_BTRFS_MAGIC) {
+			tst_res(TCONF, "RENAME_EXCHANGE flag is not implemented on %s",
+				tst_fs_type_name(fs_type));
+			return;
+		}
+		tst_brk(TFAIL | TTERRNO, "renameat2() failed");
+	}
+	tst_res(TPASS, "renameat2() passed");
+}
+
+static struct tst_test test = {
+	.test = renameat2_verify,
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+};
-- 
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.