mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master 1/2] test: self: ramfs: add regression test for recursive opendir
Date: Wed,  8 Jan 2025 10:14:24 +0100	[thread overview]
Message-ID: <20250108091425.911629-1-a.fatoum@pengutronix.de> (raw)

Recursive opendir with parent and child directory having the same name
is currently broken. Add a failing test, which will be fixed in the
follow-up commit.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 test/self/ramfs.c | 54 ++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 51 insertions(+), 3 deletions(-)

diff --git a/test/self/ramfs.c b/test/self/ramfs.c
index 239e3e690740..3fb3812cb629 100644
--- a/test/self/ramfs.c
+++ b/test/self/ramfs.c
@@ -51,6 +51,44 @@ static inline int get_file_count(int i)
 	return i + 1;
 }
 
+struct test_tree_ctx {
+	unsigned nfiles;
+	unsigned ndirs;
+};
+
+static void test_tree(const char *path, struct test_tree_ctx *ctx)
+{
+	DIR *dir;
+	struct dirent *d;
+	char *oldwd;
+
+	dir = opendir(path);
+	if (!dir)
+		return;
+
+	oldwd = pushd(path);
+
+	while ((d = readdir(dir))) {
+		struct stat st;
+
+		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
+			continue;
+
+		if (stat(d->d_name, &st))
+			continue;
+
+		if (S_ISDIR(st.st_mode))
+			ctx->ndirs++;
+		else if (S_ISREG(st.st_mode))
+			ctx->nfiles++;
+
+		test_tree(d->d_name, ctx);
+
+	}
+
+	popd(oldwd);
+}
+
 static void test_ramfs(void)
 {
 	int files[] = { 1, 3, 5, 7, 11, 13, 17 };
@@ -180,24 +218,34 @@ static void test_ramfs(void)
 	expect_success(i == 90 ? 0 : -EINVAL,
 		       "unexpected file count iterating directory: %u", i);
 
-	ret = make_directory("test/a/b/c/d/e/f/g/h/i/j/k/l");
+	ret = make_directory("test/a/b/c/d/d/f/g/h/i/j/k/l");
 	expect_success(ret, "make_directory()");
 
 	if (!ret) {
+		struct test_tree_ctx ctx = {};
 		const char hello[] = "hello";
 		char *buf;
 
-		ret = write_file("test/a/b/c/d/e/f/g/h/i/j/k/l/FILE",
+		ret = write_file("test/a/b/c/d/d/f/g/h/i/j/k/l/FILE",
 				 ARRAY_AND_SIZE(hello));
 		expect_success(ret, "write_file()");
 
-		buf = read_file("test/a/b/c/d/e/f/g/h/i/j/k/l/FILE", NULL);
+		buf = read_file("test/a/b/c/d/d/f/g/h/i/j/k/l/FILE", NULL);
 		if (expect_ptrok(buf, "read_file()")) {
 			expect_success(memcmp(buf, ARRAY_AND_SIZE(hello)),
 				       "read_file() content");
 		}
 
 		free(buf);
+
+		test_tree("test/", &ctx);
+
+		expect_success(ctx.nfiles == 1 ? 0 : -EINVAL,
+			       "wrong recursive opendir file count: %u",
+			       ctx.nfiles);
+		expect_success(ctx.ndirs == 12 ? 0 : -EINVAL,
+			       "wrong recursive opendir directory count: %u",
+			       ctx.ndirs);
 	}
 
 out:
-- 
2.39.5




             reply	other threads:[~2025-01-08  9:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-08  9:14 Ahmad Fatoum [this message]
2025-01-08  9:14 ` [PATCH master 2/2] fs: fix resolving paths when directory name repeats Ahmad Fatoum
2025-01-08 14:17 ` [PATCH master 1/2] test: self: ramfs: add regression test for recursive opendir Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250108091425.911629-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox