glibc/stdio-common/tst-freopen6-main.c
Joseph Myers e0f3bf10ac Add freopen special-case tests: chroot, EFBIG, stdin/stdout/stderr
Add tests of special cases for freopen that were omitted from the more
general tests of different modes and similar issues.  The special
cases in the three tests here are logically unconnected, it was simply
convenient to put these tests in one patch.

* Test freopen with a NULL path to the new file, in a chroot.  Rather
  than asserting that this fails (logically, failure in this case is
  an implementation detail; it's not required for freopen to rely on
  /proc), verify that either it fails (without memory leaks) or that
  it succeeds and behaves as expected on success.  There is no check
  for file descriptor leaks because the machinery for that also
  depends on /proc, so can't be used in a chroot.

* Test that freopen and freopen64 are genuinely different in
  configurations with 32-bit off_t by checking for an EFBIG trying to
  write past 2GB in a file opened with freopen in such a configuration
  but no error with 64-bit off_t or when opening with freopen64.

* Test freopen of stdin, stdout and stderr.

Tested for x86_64 and x86.
2024-09-20 23:26:31 +00:00

99 lines
2.9 KiB
C

/* Test freopen of stdin / stdout / stderr.
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 <errno.h>
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <support/check.h>
#include <support/file_contents.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/test-driver.h>
#include <support/xstdio.h>
int
do_test (void)
{
mtrace ();
char *temp_dir = support_create_temp_directory ("tst-freopen6");
char *file1 = xasprintf ("%s/file1", temp_dir);
support_write_file_string (file1, "file1");
add_temp_file (file1);
FILE *fp;
int ret;
verbose_printf ("Testing reopening stdin\n");
fp = FREOPEN (file1, "r", stdin);
TEST_VERIFY_EXIT (fp == stdin);
ret = getchar ();
TEST_COMPARE (ret, 'f');
ret = getchar ();
TEST_COMPARE (ret, 'i');
ret = getchar ();
TEST_COMPARE (ret, 'l');
ret = getchar ();
TEST_COMPARE (ret, 'e');
ret = getchar ();
TEST_COMPARE (ret, '1');
ret = getchar ();
TEST_COMPARE (ret, EOF);
xfclose (fp);
verbose_printf ("Testing reopening stderr\n");
fp = FREOPEN (file1, "w+", stderr);
TEST_VERIFY_EXIT (fp == stderr);
errno = EINVAL;
perror ("test");
ret = fseek (fp, 0, SEEK_SET);
TEST_COMPARE (ret, 0);
TEST_COMPARE_FILE_STRING (fp, "test: Invalid argument\n");
xfclose (fp);
verbose_printf ("Testing reopening stdout\n");
/* Defer checks until the old stdout has been restored to make it
more likely any errors are written to the old stdout (rather than
the temporary file used for the redirected stdout). */
int old_stdout = dup (STDOUT_FILENO);
TEST_VERIFY_EXIT (old_stdout != -1);
int ret_fseek = 0;
int ret_compare = 0;
fp = FREOPEN (file1, "w+", stdout);
int fp_eq_stdout = fp == stdout;
if (fp != NULL)
{
printf ("reopened\n");
ret_fseek = fseek (fp, 0, SEEK_SET);
ret_compare = support_compare_file_string (fp, "reopened\n");
}
xfclose (fp);
stdout = fdopen (old_stdout, "w");
TEST_VERIFY (fp_eq_stdout);
TEST_COMPARE (ret_fseek, 0);
TEST_COMPARE (ret_compare, 0);
xfclose (stdout);
free (temp_dir);
free (file1);
return 0;
}
#include <support/test-driver.c>