From: Danish Khateeb <danishkhateeb03@gmail.com>
To: "Willy Tarreau" <w@1wt.eu>, "Thomas Weißschuh" <linux@weissschuh.net>
Cc: Shuah Khan <shuah@kernel.org>,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
Danish Khateeb <danishkhateeb03@gmail.com>
Subject: [PATCH 3/3] selftests/nolibc: test fopen() errors and update modes
Date: Wed, 28 Oct 2026 15:07:43 -0500 [thread overview]
Message-ID: <20261028200743.154151-4-danishkhateeb03@gmail.com> (raw)
In-Reply-To: <20261028200743.154151-1-danishkhateeb03@gmail.com>
Check that fopen() of a missing file fails with ENOENT, and that a
stream opened in "rb+" or "wb+" mode can be both read and written.
Signed-off-by: Danish Khateeb <danishkhateeb03@gmail.com>
---
tools/testing/selftests/nolibc/nolibc-test.c | 24 ++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index f3d0d27cdbe1..be501b5298d7 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1035,6 +1035,27 @@ int test_file_stream_wsr(void)
return 0;
}
+/* checks that a stream opened with <mode> is readable and writable */
+int test_fopen_rw(const char *mode)
+{
+ FILE *f;
+ char c;
+ int r;
+
+ f = fopen("/dev/null", mode);
+ if (!f)
+ return -1;
+
+ /* reading /dev/null hits end-of-file, so a write may follow */
+ errno = 0;
+ r = fread(&c, 1, 1, f) != 0 || errno || fwrite("x", 1, 1, f) != 1;
+
+ if (fclose(f) == EOF)
+ return -1;
+
+ return r ? -1 : 0;
+}
+
enum fork_type {
FORK_STANDARD,
FORK_VFORK,
@@ -1759,6 +1780,9 @@ int run_syscall(int min, int max)
CASE_TEST(fdopendir_notdir); EXPECT_SYSER(1, (uintptr_t)fdopendir(STDIN_FILENO), (uintptr_t)NULL, ENOTDIR); break;
CASE_TEST(file_stream); EXPECT_SYSZR(1, test_file_stream()); break;
CASE_TEST(file_stream_wsr); EXPECT_SYSZR(1, test_file_stream_wsr()); break;
+ CASE_TEST(fopen_blah); EXPECT_PTRER(1, fopen("/proc/self/blah", "r"), NULL, ENOENT); break;
+ CASE_TEST(fopen_rb_plus); EXPECT_SYSZR(1, test_fopen_rw("rb+")); break;
+ CASE_TEST(fopen_wb_plus); EXPECT_SYSZR(1, test_fopen_rw("wb+")); break;
CASE_TEST(fork); EXPECT_SYSZR(1, test_fork(FORK_STANDARD)); break;
CASE_TEST(ftruncate); EXPECT_SYSZR(1, test_ftruncate()); break;
CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
--
2.56.0
next prev parent reply other threads:[~2026-10-28 20:07 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-28 20:07 [PATCH 0/3] tools/nolibc: fix fopen() errno and "rb+" modes Danish Khateeb
2026-10-28 20:07 ` [PATCH 1/3] tools/nolibc: fix fopen() errno when open() fails Danish Khateeb
2026-10-29 17:38 ` Thomas Weißschuh
2026-10-28 20:07 ` [PATCH 2/3] tools/nolibc: fix fopen() with the "rb+", "wb+" and "ab+" modes Danish Khateeb
2026-10-29 17:41 ` Thomas Weißschuh
2026-10-28 20:07 ` Danish Khateeb [this message]
2026-10-29 17:47 ` [PATCH 3/3] selftests/nolibc: test fopen() errors and update modes Thomas Weißschuh
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=20261028200743.154151-4-danishkhateeb03@gmail.com \
--to=danishkhateeb03@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=shuah@kernel.org \
--cc=w@1wt.eu \
/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