2a30b8f4b2
Upstream commit: dcaf51b41e259387602774829c45222d0507f90a - elf: Change ldconfig auxcache magic number (bug 32231) - Make tst-strtod-underflow type-generic - Add crt1-2.0.o for glibc 2.0 compatibility tests - Add tests of more strtod special cases - Add more tests of strtod end pointer - Make tst-strtod2 and tst-strtod5 type-generic - powerpc64le: Build new strtod tests with long double ABI flags (bug 32145) - Do not set errno for overflowing NaN payload in strtod/nan (bug 32045) - Improve NaN payload testing - Make __strtod_internal tests type-generic - Fix strtod subnormal rounding (bug 30220) - More thoroughly test underflow / errno in tst-strtod-round - Test errno setting on strtod overflow in tst-strtod-round - Add tests of fread - stdio-common: Add new test for fdopen - libio: Attempt wide backup free only for non-legacy code - debug: Fix read error handling in pcprofiledump - elf: Fix tst-dlopen-tlsreinit1.out test dependency - elf: Avoid re-initializing already allocated TLS in dlopen (bug 31717) - elf: Clarify and invert second argument of _dl_allocate_tls_init - elf: Support recursive use of dynamic TLS in interposed malloc - nptl: Use <support/check.h> facilities in tst-setuid3 - posix: Use <support/check.h> facilities in tst-truncate and tst-truncate64 - ungetc: Fix backup buffer leak on program exit [BZ #27821] - ungetc: Fix uninitialized read when putting into unused streams [BZ #27821] - Make tst-ungetc use libsupport - stdio-common: Add test for vfscanf with matches longer than INT_MAX [BZ #27650] - support: Add FAIL test failure helper - string: strerror, strsignal cannot use buffer after dlmopen (bug 32026) - Define __libc_initial for the static libc - x86: Fix bug in strchrnul-evex512 [BZ #32078] - Adjust check-local-headers test for libaudit 4.0 - x32/cet: Support shadow stack during startup for Linux 6.10 - x86-64: Remove sysdeps/x86_64/x32/dl-machine.h - support: Add options list terminator to the test driver - manual/stdio: Further clarify putc, putwc, getc, and getwc - Fix name space violation in fortify wrappers (bug 32052) - resolv: Fix tst-resolv-short-response for older GCC (bug 32042) - Add mremap tests - mremap: Update manual entry - linux: Update the mremap C implementation [BZ #31968] - Enhanced test coverage for strncmp, wcsncmp - Enhance test coverage for strnlen, wcsnlen Resolves: RHEL-57776 Resolves: RHEL-57777 Resolves: RHEL-61392
276 lines
6.1 KiB
Diff
276 lines
6.1 KiB
Diff
commit 373aab3e52e1c764cf8d86ae14bc4b14e064d623
|
|
Author: Sergey Kolosov <skolosov@redhat.com>
|
|
Date: Wed Sep 25 15:51:23 2024 +0200
|
|
|
|
stdio-common: Add new test for fdopen
|
|
|
|
This commit adds fdopen test with all modes.
|
|
Reviewed-by: DJ Delorie <dj@redhat.com>
|
|
|
|
(cherry picked from commit 1d72fa3cfa046f7293421a7e58f2a272474ea901)
|
|
|
|
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
|
|
index b9c38ce6b3b2f43a..5f3bd4662340eee9 100644
|
|
--- a/stdio-common/Makefile
|
|
+++ b/stdio-common/Makefile
|
|
@@ -206,6 +206,7 @@ tests := \
|
|
tst-cookie \
|
|
tst-dprintf-length \
|
|
tst-fdopen \
|
|
+ tst-fdopen2 \
|
|
tst-ferror \
|
|
tst-fgets \
|
|
tst-fileno \
|
|
diff --git a/stdio-common/tst-fdopen2.c b/stdio-common/tst-fdopen2.c
|
|
new file mode 100644
|
|
index 0000000000000000..0c6625f25853aed5
|
|
--- /dev/null
|
|
+++ b/stdio-common/tst-fdopen2.c
|
|
@@ -0,0 +1,246 @@
|
|
+/* Test the fdopen function.
|
|
+ Copyright (C) 2024 Free Software Foundation, Inc.
|
|
+ This file is part of the GNU C Library.
|
|
+
|
|
+ The GNU C Library is free software; you can redistribute it and/or
|
|
+ modify it under the terms of the GNU Lesser General Public
|
|
+ License as published by the Free Software Foundation; either
|
|
+ version 2.1 of the License, or (at your option) any later version.
|
|
+
|
|
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
+ Lesser General Public License for more details.
|
|
+
|
|
+ You should have received a copy of the GNU Lesser General Public
|
|
+ License along with the GNU C Library; if not, see
|
|
+ <https://www.gnu.org/licenses/>. */
|
|
+
|
|
+#include <stdio.h>
|
|
+#include <errno.h>
|
|
+#include <fcntl.h>
|
|
+#include <support/check.h>
|
|
+#include <support/support.h>
|
|
+#include <support/xunistd.h>
|
|
+#include <support/temp_file.h>
|
|
+
|
|
+char *tmp_dir;
|
|
+char *path_to_file;
|
|
+
|
|
+void
|
|
+prepare_tmp_dir (void)
|
|
+{
|
|
+ tmp_dir = support_create_temp_directory ("tst-fdopen2");
|
|
+ path_to_file = xasprintf ("%s/tst-fdopen2.txt", tmp_dir);
|
|
+}
|
|
+
|
|
+/* open temp file descriptor with mode. */
|
|
+int
|
|
+open_tmp_fd (int mode)
|
|
+{
|
|
+ int fd = xopen (path_to_file, mode, 0644);
|
|
+ return fd;
|
|
+}
|
|
+
|
|
+
|
|
+/* close and remove temp file with close. */
|
|
+void
|
|
+close_tmp_fd (int fd)
|
|
+{
|
|
+ xclose (fd);
|
|
+ xunlink (path_to_file);
|
|
+}
|
|
+
|
|
+/* close and remove temp file with fclose. */
|
|
+void
|
|
+close_tmp_fp (FILE *fp)
|
|
+{
|
|
+ fclose (fp);
|
|
+ xunlink (path_to_file);
|
|
+}
|
|
+
|
|
+/* test "w" fdopen mode. */
|
|
+void
|
|
+do_test_fdopen_w (void)
|
|
+{
|
|
+ int fd, ret;
|
|
+ FILE *fp;
|
|
+ fd = open_tmp_fd (O_WRONLY | O_CREAT | O_TRUNC);
|
|
+
|
|
+ /* test mode mismatch. */
|
|
+ fp = fdopen (fd, "r");
|
|
+ if (fp != NULL || errno != EINVAL)
|
|
+ {
|
|
+ close_tmp_fd (fd);
|
|
+ FAIL_EXIT1 ("fdopen (%d, r) should fail with EINVAL: %m", fd);
|
|
+ }
|
|
+
|
|
+ fp = fdopen (fd, "w");
|
|
+ if (fp == NULL)
|
|
+ {
|
|
+ close_tmp_fd (fd);
|
|
+ FAIL_EXIT1 ("fdopen (%d, w): %m", fd);
|
|
+ }
|
|
+
|
|
+ const void *buf = "AAAA";
|
|
+ ret = fwrite (buf, 1, 4, fp);
|
|
+ if (ret != 4)
|
|
+ {
|
|
+ close_tmp_fp (fp);
|
|
+ FAIL_EXIT1 ("fwrite (): %m");
|
|
+ }
|
|
+
|
|
+ unsigned char buf2[4];
|
|
+ rewind (fp);
|
|
+ clearerr (fp);
|
|
+ /* fread should fail in "w" mode */
|
|
+ ret = fread (buf2, 1, 4, fp);
|
|
+ if (ret != 0 || ferror (fp) == 0)
|
|
+ {
|
|
+ close_tmp_fp (fp);
|
|
+ FAIL_EXIT1 ("fread should fail in \"w\" mode");
|
|
+ }
|
|
+
|
|
+ fclose (fp);
|
|
+}
|
|
+
|
|
+/* test "r" fdopen mode. */
|
|
+void
|
|
+do_test_fdopen_r (void)
|
|
+{
|
|
+ int fd, ret;
|
|
+ FILE *fp;
|
|
+ fd = open_tmp_fd (O_RDONLY);
|
|
+
|
|
+ /* test mode mismatch. */
|
|
+ fp = fdopen (fd, "w");
|
|
+ if (fp != NULL || errno != EINVAL)
|
|
+ {
|
|
+ close_tmp_fd (fd);
|
|
+ FAIL_EXIT1 ("fdopen (%d, w) should fail with EINVAL: %m", fd);
|
|
+ }
|
|
+
|
|
+ fp = fdopen (fd, "r");
|
|
+ if (fp == NULL)
|
|
+ {
|
|
+ close_tmp_fd (fd);
|
|
+ FAIL_EXIT1 ("fdopen (%d, w): %m", fd);
|
|
+ }
|
|
+
|
|
+ const void *buf = "BBBB";
|
|
+ /* fwrite should fail in "r" mode. */
|
|
+ ret = fwrite (buf, 1, 4, fp);
|
|
+ if (ret != 0 || ferror (fp) == 0)
|
|
+ {
|
|
+ close_tmp_fp (fp);
|
|
+ FAIL_EXIT1 ("fwrite should fail in \"r\" mode");
|
|
+ }
|
|
+
|
|
+ unsigned char buf2[4];
|
|
+ ret = fread (buf2, 1, 4, fp);
|
|
+ if (ret != 4)
|
|
+ {
|
|
+ close_tmp_fp (fp);
|
|
+ FAIL_EXIT1 ("fread (): %m");
|
|
+ }
|
|
+
|
|
+ fclose (fp);
|
|
+}
|
|
+
|
|
+/* test "a" fdopen mode. */
|
|
+void
|
|
+do_test_fdopen_a (void)
|
|
+{
|
|
+ int fd, ret;
|
|
+ FILE *fp;
|
|
+ fd = open_tmp_fd (O_WRONLY | O_CREAT | O_APPEND);
|
|
+
|
|
+ /* test mode mismatch. */
|
|
+ fp = fdopen (fd, "r+");
|
|
+ if (fp != NULL || errno != EINVAL)
|
|
+ {
|
|
+ close_tmp_fd (fd);
|
|
+ FAIL_EXIT1 ("fdopen (%d, \"r+\") should fail with EINVAL: %m", fd);
|
|
+ }
|
|
+
|
|
+ fp = fdopen (fd, "a");
|
|
+ if (fp == NULL)
|
|
+ {
|
|
+ close_tmp_fd (fd);
|
|
+ FAIL_EXIT1 ("fdopen (%d, w): %m", fd);
|
|
+ }
|
|
+
|
|
+ const void *buf = "CCCC";
|
|
+ ret = fwrite (buf, 1, 4, fp);
|
|
+ if (ret != 4)
|
|
+ {
|
|
+ close_tmp_fp (fp);
|
|
+ FAIL_EXIT1 ("fwrite (): %m");
|
|
+ }
|
|
+
|
|
+ /* fread should fail in "a" mode. */
|
|
+ unsigned char buf2[4];
|
|
+ clearerr (fp);
|
|
+ ret = fread (buf2, 1, 4, fp);
|
|
+ if (ret != 0 || ferror (fp) == 0)
|
|
+ {
|
|
+ close_tmp_fp (fp);
|
|
+ FAIL_EXIT1 ("fread should fail \"a\" mode");
|
|
+ }
|
|
+
|
|
+ fclose (fp);
|
|
+}
|
|
+
|
|
+void
|
|
+do_test_fdopen_mode (int mode, const char *fmode)
|
|
+{
|
|
+ int fd, ret;
|
|
+ FILE *fp;
|
|
+ fd = open_tmp_fd (mode);
|
|
+
|
|
+ fp = fdopen (fd, fmode);
|
|
+ if (fp == NULL)
|
|
+ {
|
|
+ close_tmp_fd (fd);
|
|
+ FAIL_EXIT1 ("fdopen (%d, %s): %m", fd, fmode);
|
|
+ }
|
|
+
|
|
+ const void *buf = "EEEE";
|
|
+ ret = fwrite (buf, 1, 4, fp);
|
|
+ if (ret != 4)
|
|
+ {
|
|
+ close_tmp_fp (fp);
|
|
+ FAIL_EXIT1 ("fwrite () in mode:%s returns %d: %m", fmode, ret);
|
|
+ }
|
|
+
|
|
+ rewind (fp);
|
|
+ unsigned char buf2[4];
|
|
+ ret = fread (buf2, 1, 4, fp);
|
|
+ if (ret != 4)
|
|
+ {
|
|
+ close_tmp_fp (fp);
|
|
+ FAIL_EXIT1 ("fread () in mode:%s returns %d: %m", fmode, ret);
|
|
+ }
|
|
+
|
|
+ fclose (fp);
|
|
+}
|
|
+
|
|
+static int
|
|
+do_test (void)
|
|
+{
|
|
+
|
|
+ prepare_tmp_dir ();
|
|
+
|
|
+ do_test_fdopen_w ();
|
|
+ do_test_fdopen_r ();
|
|
+ do_test_fdopen_a ();
|
|
+
|
|
+ /* test r+ w+ a+ fdopen modes. */
|
|
+ do_test_fdopen_mode (O_RDWR, "r+");
|
|
+ do_test_fdopen_mode (O_RDWR | O_CREAT | O_TRUNC, "w+");
|
|
+ do_test_fdopen_mode (O_RDWR | O_CREAT | O_APPEND, "a+");
|
|
+ xunlink (path_to_file);
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+#include <support/test-driver.c>
|