Skip to contentSkip to conversation

selftests: openat2: migrate to kselftest harness

PATCH Applied Aleksa Sarai 2 participants 6 messages Apr 1 – Apr 9

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover selftests: openat2: migrate to kselftest harness

These tests were written in the early days of selftests' TAP support, the more modern kselftest harness is much easier to follow and maintain. The actual contents of the tests are unchanged by this change.

---
Changes in v2:
- move openat2 tests to selftests/filesystems [Christian]
- v1: <https://lore.kernel.org/r/[email protected]>

---
Aleksa Sarai (4):
selftests: move openat2 tests to selftests/filesystems/
selftests: openat2: move helpers to header
selftests: openat2: switch from custom ARRAY_LEN to ARRAY_SIZE
selftests: openat2: migrate to kselftest harness

 .../selftests/{ => filesystems}/openat2/.gitignore |   0
 .../selftests/{ => filesystems}/openat2/Makefile   |   5 +-
 .../selftests/filesystems/openat2/helpers.h        | 168 ++++++++++
 .../{ => filesystems}/openat2/openat2_test.c       | 223 ++++++-------
 .../filesystems/openat2/rename_attack_test.c       | 159 +++++++++
 .../{ => filesystems}/openat2/resolve_test.c       | 368 ++++++++++++---------
 tools/testing/selftests/openat2/helpers.c          | 109 ------
 tools/testing/selftests/openat2/helpers.h          | 108 ------
 .../testing/selftests/openat2/rename_attack_test.c | 160 ---------
 9 files changed, 631 insertions(+), 669 deletions(-)

---
base-commit: 84f524dca4b8bc9e9aca8ea1dbbd2aa3a239d998
change-id: 20260317-openat2-selftests-kunit-6939af23ee27

Kind regards,

Signature · 2 lines
Aleksa Sarai
https://www.cyphar.com/
#1 lore raw

PATCH v2 1/4 selftests: move openat2 tests to selftests/filesystems/

These tests really should've always belonged there, doubly so now that they include a lot of other generic filesystem-related tests.

---
 tools/testing/selftests/{ => filesystems}/openat2/.gitignore           | 0
 tools/testing/selftests/{ => filesystems}/openat2/Makefile             | 3 ++-
 tools/testing/selftests/{ => filesystems}/openat2/helpers.c            | 0
 tools/testing/selftests/{ => filesystems}/openat2/helpers.h            | 0
 tools/testing/selftests/{ => filesystems}/openat2/openat2_test.c       | 0
 tools/testing/selftests/{ => filesystems}/openat2/rename_attack_test.c | 0
 tools/testing/selftests/{ => filesystems}/openat2/resolve_test.c       | 0
 7 files changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/filesystems/openat2/.gitignore renamed from tools/testing/selftests/openat2/.gitignore +0 -0
similarity index 100%
rename from tools/testing/selftests/openat2/.gitignore
rename to tools/testing/selftests/filesystems/openat2/.gitignore
tools/testing/selftests/filesystems/openat2/Makefile renamed from tools/testing/selftests/openat2/Makefile +2 -1
similarity index 91%
rename from tools/testing/selftests/openat2/Makefile
rename to tools/testing/selftests/filesystems/openat2/Makefile
index 185dc76ebb5f..f36dedccedb6 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
+CFLAGS += $(KHDR_INCLUDES)
 CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined
 TEST_GEN_PROGS := openat2_test resolve_test rename_attack_test
 
@@ -13,6 +14,6 @@ endif
 
 LOCAL_HDRS += helpers.h
 
-include ../lib.mk
+include ../../lib.mk
 
 $(TEST_GEN_PROGS): helpers.c
tools/testing/selftests/filesystems/openat2/helpers.c renamed from tools/testing/selftests/openat2/helpers.c +0 -0
similarity index 100%
rename from tools/testing/selftests/openat2/helpers.c
rename to tools/testing/selftests/filesystems/openat2/helpers.c
tools/testing/selftests/filesystems/openat2/helpers.h renamed from tools/testing/selftests/openat2/helpers.h +0 -0
similarity index 100%
rename from tools/testing/selftests/openat2/helpers.h
rename to tools/testing/selftests/filesystems/openat2/helpers.h
tools/testing/selftests/filesystems/openat2/openat2_test.c renamed from tools/testing/selftests/openat2/openat2_test.c +0 -0
similarity index 100%
rename from tools/testing/selftests/openat2/openat2_test.c
rename to tools/testing/selftests/filesystems/openat2/openat2_test.c
tools/testing/selftests/filesystems/openat2/rename_attack_test.c renamed from tools/testing/selftests/openat2/rename_attack_test.c +0 -0
similarity index 100%
rename from tools/testing/selftests/openat2/rename_attack_test.c
rename to tools/testing/selftests/filesystems/openat2/rename_attack_test.c
tools/testing/selftests/filesystems/openat2/resolve_test.c renamed from tools/testing/selftests/openat2/resolve_test.c +0 -0
similarity index 100%
rename from tools/testing/selftests/openat2/resolve_test.c
rename to tools/testing/selftests/filesystems/openat2/resolve_test.c
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/4 selftests: openat2: move helpers to header

In reply to Aleksa Sarai

This is a bit ugly, but in the next patch we will move to using kselftest_harness.h -- which doesn't play well with being included in multiple compilation units due to duplicate function definitions.

Not including kselftest_harness.h would let us avoid this patch, but the helpers will need include kselftest_harness.h in order to switch to TH_LOG.

---
 .../testing/selftests/filesystems/openat2/Makefile |   2 -
 .../selftests/filesystems/openat2/helpers.c        | 109 ------------------
 .../selftests/filesystems/openat2/helpers.h        | 125 ++++++++++++++++++---
 3 files changed, 107 insertions(+), 129 deletions(-)
tools/testing/selftests/filesystems/openat2/Makefile +0 -2
index f36dedccedb6..7736e37b7986 100644
@@ -15,5 +15,3 @@ endif
 LOCAL_HDRS += helpers.h
 
 include ../../lib.mk
-
-$(TEST_GEN_PROGS): helpers.c
tools/testing/selftests/filesystems/openat2/helpers.c deleted +0 -109

109 lines not on this page · raw message

tools/testing/selftests/filesystems/openat2/helpers.h +107 -18

156 lines not on this page · raw message

Signature · 1 line
2.53.0
#3 lore raw

PATCH v2 3/4 selftests: openat2: switch from custom ARRAY_LEN to ARRAY_SIZE

In reply to Aleksa Sarai

For whatever reason, the original version of the tests used a custom version of ARRAY_SIZE, but ARRAY_SIZE works just as well.

---
 tools/testing/selftests/filesystems/openat2/helpers.h      |  1 -
 tools/testing/selftests/filesystems/openat2/openat2_test.c | 12 ++++++------
 tools/testing/selftests/filesystems/openat2/resolve_test.c |  4 ++--
 3 files changed, 8 insertions(+), 9 deletions(-)
tools/testing/selftests/filesystems/openat2/helpers.h +0 -1
index 975de513af86..f56c0c6e3ad1 100644
@@ -17,7 +17,6 @@
 #include <linux/unistd.h>
 #include "kselftest.h"
 
-#define ARRAY_LEN(X) (sizeof (X) / sizeof (*(X)))
 #define BUILD_BUG_ON(e) ((void)(sizeof(struct { int:(-!!(e)); })))
 
 /*
tools/testing/selftests/filesystems/openat2/openat2_test.c +6 -6
index 0e161ef9e9e4..c6c26652ac1b 100644
@@ -83,14 +83,14 @@ void test_openat2_struct(void)
 		  .size = sizeof(struct open_how_ext), .err = -E2BIG },
 	};
 
-	BUILD_BUG_ON(ARRAY_LEN(misalignments) != NUM_OPENAT2_STRUCT_VARIATIONS);
-	BUILD_BUG_ON(ARRAY_LEN(tests) != NUM_OPENAT2_STRUCT_TESTS);
+	BUILD_BUG_ON(ARRAY_SIZE(misalignments) != NUM_OPENAT2_STRUCT_VARIATIONS);
+	BUILD_BUG_ON(ARRAY_SIZE(tests) != NUM_OPENAT2_STRUCT_TESTS);
 
-	for (int i = 0; i < ARRAY_LEN(tests); i++) {
+	for (int i = 0; i < ARRAY_SIZE(tests); i++) {
 		struct struct_test *test = &tests[i];
 		struct open_how_ext how_ext = test->arg;
 
-		for (int j = 0; j < ARRAY_LEN(misalignments); j++) {
+		for (int j = 0; j < ARRAY_SIZE(misalignments); j++) {
 			int fd, misalign = misalignments[j];
 			char *fdpath = NULL;
 			bool failed;
@@ -241,9 +241,9 @@ void test_openat2_flags(void)
 		  .how.resolve = 0, .err = -EINVAL },
 	};
 
-	BUILD_BUG_ON(ARRAY_LEN(tests) != NUM_OPENAT2_FLAG_TESTS);
+	BUILD_BUG_ON(ARRAY_SIZE(tests) != NUM_OPENAT2_FLAG_TESTS);
 
-	for (int i = 0; i < ARRAY_LEN(tests); i++) {
+	for (int i = 0; i < ARRAY_SIZE(tests); i++) {
 		int fd, fdflags = -1;
 		char *path, *fdpath = NULL;
 		bool failed = false;
tools/testing/selftests/filesystems/openat2/resolve_test.c +2 -2
index a76ef15ceb90..f7acb4300641 100644
@@ -436,9 +436,9 @@ void test_openat2_opath_tests(void)
 		  .out.err = -ELOOP,		.pass = false },
 	};
 
-	BUILD_BUG_ON(ARRAY_LEN(tests) != NUM_OPENAT2_OPATH_TESTS);
+	BUILD_BUG_ON(ARRAY_SIZE(tests) != NUM_OPENAT2_OPATH_TESTS);
 
-	for (int i = 0; i < ARRAY_LEN(tests); i++) {
+	for (int i = 0; i < ARRAY_SIZE(tests); i++) {
 		int dfd, fd;
 		char *fdpath = NULL;
 		bool failed;
Signature · 1 line
2.53.0
#4 lore raw

PATCH v2 4/4 selftests: openat2: migrate to kselftest harness

In reply to Aleksa Sarai

These tests were written in the early days of selftests' TAP support, the more modern kselftest harness is much easier to follow and maintain. The actual contents of the tests are unchanged by this change. Most of the diff involves switching from the E_* syscall wrappers we previously used to ASSERT_EQ(fn(...), 0) in tests and helper functions.

The first pass of the migration was done using Claude, followed by a manual rework and review.

Assisted-by Claude:claude-4.6-opus
---
 .../selftests/filesystems/openat2/helpers.h        |  62 +---
 .../selftests/filesystems/openat2/openat2_test.c   | 217 ++++++------
 .../filesystems/openat2/rename_attack_test.c       | 161 +++++----
 .../selftests/filesystems/openat2/resolve_test.c   | 368 ++++++++++++---------
 4 files changed, 396 insertions(+), 412 deletions(-)
tools/testing/selftests/filesystems/openat2/helpers.h +17 -45
index f56c0c6e3ad1..7ca54c718c45 100644
@@ -15,7 +15,7 @@
 #include <limits.h>
 #include <linux/types.h>
 #include <linux/unistd.h>
-#include "kselftest.h"
+#include "kselftest_harness.h"
 
 #define BUILD_BUG_ON(e) ((void)(sizeof(struct { int:(-!!(e)); })))
 
@@ -56,36 +56,6 @@ struct open_how {
 					(similar to chroot(2)). */
 #endif /* RESOLVE_IN_ROOT */
 
-#define E_func(func, ...)						      \
-	do {								      \
-		errno = 0;						      \
-		if (func(__VA_ARGS__) < 0)				      \
-			ksft_exit_fail_msg("%s:%d %s failed - errno:%d\n",    \
-					   __FILE__, __LINE__, #func, errno); \
-	} while (0)
-
-#define E_asprintf(...)		E_func(asprintf,	__VA_ARGS__)
-#define E_chmod(...)		E_func(chmod,		__VA_ARGS__)
-#define E_dup2(...)		E_func(dup2,		__VA_ARGS__)
-#define E_fchdir(...)		E_func(fchdir,		__VA_ARGS__)
-#define E_fstatat(...)		E_func(fstatat,		__VA_ARGS__)
-#define E_kill(...)		E_func(kill,		__VA_ARGS__)
-#define E_mkdirat(...)		E_func(mkdirat,		__VA_ARGS__)
-#define E_mount(...)		E_func(mount,		__VA_ARGS__)
-#define E_prctl(...)		E_func(prctl,		__VA_ARGS__)
-#define E_readlink(...)		E_func(readlink,	__VA_ARGS__)
-#define E_setresuid(...)	E_func(setresuid,	__VA_ARGS__)
-#define E_symlinkat(...)	E_func(symlinkat,	__VA_ARGS__)
-#define E_touchat(...)		E_func(touchat,		__VA_ARGS__)
-#define E_unshare(...)		E_func(unshare,		__VA_ARGS__)
-
-#define E_assert(expr, msg, ...)					\
-	do {								\
-		if (!(expr))						\
-			ksft_exit_fail_msg("ASSERT(%s:%d) failed (%s): " msg "\n", \
-					   __FILE__, __LINE__, #expr, ##__VA_ARGS__); \
-	} while (0)
-
 __maybe_unused
 static bool needs_openat2(const struct open_how *how)
 {
@@ -135,37 +105,39 @@ static int touchat(int dfd, const char *path)
 }
 
 __maybe_unused
-static char *fdreadlink(int fd)
+static char *fdreadlink(struct __test_metadata *_metadata, int fd)
 {
 	char *target, *tmp;
 
-	E_asprintf(&tmp, "/proc/self/fd/%d", fd);
+	ASSERT_GT(asprintf(&tmp, "/proc/self/fd/%d", fd), 0);
 
 	target = malloc(PATH_MAX);
-	if (!target)
-		ksft_exit_fail_msg("fdreadlink: malloc failed\n");
+	ASSERT_NE(target, NULL);
 	memset(target, 0, PATH_MAX);
 
-	E_readlink(tmp, target, PATH_MAX);
+	ASSERT_GT(readlink(tmp, target, PATH_MAX), 0);
+
 	free(tmp);
 	return target;
 }
 
 __maybe_unused
-static bool fdequal(int fd, int dfd, const char *path)
+static bool fdequal(struct __test_metadata *_metadata, int fd,
+		    int dfd, const char *path)
 {
 	char *fdpath, *dfdpath, *other;
 	bool cmp;
 
-	fdpath = fdreadlink(fd);
-	dfdpath = fdreadlink(dfd);
+	fdpath = fdreadlink(_metadata, fd);
+	dfdpath = fdreadlink(_metadata, dfd);
 
-	if (!path)
-		E_asprintf(&other, "%s", dfdpath);
-	else if (*path == '/')
-		E_asprintf(&other, "%s", path);
-	else
-		E_asprintf(&other, "%s/%s", dfdpath, path);
+	if (!path) {
+		ASSERT_GT(asprintf(&other, "%s", dfdpath), 0);
+	} else if (*path == '/') {
+		ASSERT_GT(asprintf(&other, "%s", path), 0);
+	} else {
+		ASSERT_GT(asprintf(&other, "%s/%s", dfdpath, path), 0);
+	}
 
 	cmp = !strcmp(fdpath, other);
 
tools/testing/selftests/filesystems/openat2/openat2_test.c +96 -121

287 lines not on this page · raw message

tools/testing/selftests/filesystems/openat2/rename_attack_test.c +80 -81

213 lines not on this page · raw message

tools/testing/selftests/filesystems/openat2/resolve_test.c +203 -165

442 lines not on this page · raw message

Signature · 1 line
2.53.0
maintainer Applied
#5 lore raw
In reply to Aleksa Sarai
Quoted 3 lines · Aleksa Sarai

These tests were written in the early days of selftests' TAP support, the more modern kselftest harness is much easier to follow and maintain. The actual contents of the tests are unchanged by this change.

Applied to the vfs-7.2.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.2.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.2.misc

[1/4] selftests: move openat2 tests to selftests/filesystems/

      https://git.kernel.org/vfs/vfs/c/4ac6d5007161
[2/4] selftests: openat2: move helpers to header
      https://git.kernel.org/vfs/vfs/c/4bbc575d97eb
[3/4] selftests: openat2: switch from custom ARRAY_LEN to ARRAY_SIZE
      https://git.kernel.org/vfs/vfs/c/20321927da56
[4/4] selftests: openat2: migrate to kselftest harness
      https://git.kernel.org/vfs/vfs/c/1993b7daaf1f