Re: [PATCH 3/3] selftests/nolibc: test fopen() errors and update modes

From: Thomas Weißschuh

Date: Thu Oct 29 2026 - 13:47:26 EST


On 2026-10-28 15:07:43-0500, Danish Khateeb wrote:
> 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@xxxxxxxxx>
> ---
> 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;

That's too funky.

I have a series for fcntl() prepared, with which the test could use
fcntl(F_GETFL). It would be much easier to also test other flag combinations:

test_fopen_flags("rb+", O_RDWR);
test_fopen_flags("r", O_RDONLY);
...

> +
> + if (fclose(f) == EOF)
> + return -1;
> +
> + return r ? -1 : 0;
> +}
> +

(...)