From: Frédéric Bérat Date: Mon, 9 Sep 2024 12:43:36 +0000 (+0200) Subject: libio: Add test case for fflush X-Git-Tag: glibc-2.42~312 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=11e188659db264f6b101d8eb65824ffa5bc91a0a;p=thirdparty%2Fglibc.git libio: Add test case for fflush Since one path uses _IO_SYNC and the other _IO_OVERFLOW, the newly added test cases verifies that `fflush (FILE)` and `fflush (NULL)` are semantically equivalent from the FILE perspective. Reviewed-by: Joseph Myers --- diff --git a/libio/Makefile b/libio/Makefile index e143ccdb2c..f020f8ec4d 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -101,6 +101,8 @@ tests = \ tst-fclose-unopened \ tst-fclose-unopened2 \ tst-fdopen-seek-failure \ + tst-fflush \ + tst-fflush-NULL \ tst-fgetc-after-eof \ tst-fgetwc \ tst-fgetws \ diff --git a/libio/tst-fflush-NULL.c b/libio/tst-fflush-NULL.c new file mode 100644 index 0000000000..4c8fe7cb87 --- /dev/null +++ b/libio/tst-fflush-NULL.c @@ -0,0 +1,24 @@ +/* Test that fflush (FILE) and fflush (NULL) are semantically equivalent. + This is the `fflush (NULL)` part. + + 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 + . */ + +#define FILE_FLUSH_TYPE 0 +#define S_FLUSH_TYPE "NULL" + +#include "tst-fflush-skeleton.c" diff --git a/libio/tst-fflush-skeleton.c b/libio/tst-fflush-skeleton.c new file mode 100644 index 0000000000..6341d3d079 --- /dev/null +++ b/libio/tst-fflush-skeleton.c @@ -0,0 +1,158 @@ +/* Test that fflush (FILE) and fflush (NULL) are semantically equivalent. + + 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 + . */ + +/* A success on this test doesn't imply the effectiveness of fflush as + we can't ensure that the file wasn't already in the expected state + before the call of the function. It only ensures that, if the test + fails, fflush is broken. */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#define CONTENT_SZ_MAX 32 +#define TEST_FILE_COUNT 10 + +struct file_tracking +{ + FILE *file; + char *name; + int fd; + char *mfile; +} files[TEST_FILE_COUNT]; + +static void +file_init (int file) +{ + int fd = -1; + + assert (file < TEST_FILE_COUNT); + + files[file] = (struct file_tracking) { .fd = -1, }; + + xclose (create_temp_file ("tst-fflush", &files[file].name)); + + fd = xopen (files[file].name, O_RDONLY, 0); + files[file].mfile = xmmap (NULL, CONTENT_SZ_MAX, PROT_READ, MAP_SHARED, fd); + xclose (fd); +} + +static void +file_cleanup (int file) +{ + free (files[file].name); + xmunmap (files[file].mfile, CONTENT_SZ_MAX); + files[file] = (struct file_tracking) { .fd = -1, }; +} + +static void +file_changed (int to_check, const char *mode) +{ + struct stat stats = { }; + char expected[CONTENT_SZ_MAX] = { }; + + verbose_printf ("Check that %s (%d) exactly contains the data we put in\n", + files[to_check].name, to_check); + + /* File should contain "N:M" where both N and M are one digit exactly. */ + snprintf (expected, sizeof (expected), "%d:%d", FILE_FLUSH_TYPE, to_check); + TEST_COMPARE_BLOB (files[to_check].mfile, sizeof (expected), + expected, sizeof (expected)); + + TEST_VERIFY (fstat (files[to_check].fd, &stats) >= 0); + TEST_VERIFY (stats.st_size == 3); + /* In read mode we expect to be at position 1, in write mode at position 3 */ + TEST_COMPARE (lseek (files[to_check].fd, 0, SEEK_CUR), + mode[0] == 'r' ? 1 : 3); + + if (support_record_failure_is_failed ()) + FAIL_EXIT1 ("exiting due to previous failure"); + + /* Not reached if the data doesn't match. */ +} + +static void +file_flush (const char *mode) +{ + for (int i = 0; i < TEST_FILE_COUNT; i++) + { + files[i].file = xfopen (files[i].name, mode); + files[i].fd = fileno (files[i].file); + } + + /* Print a unique identifier in each file, that is not too long nor contain + new line to not trigger _IO_OVERFLOW/_IO_SYNC. */ + for (int i = 0; i < TEST_FILE_COUNT; i++) + { + if (mode[0] == 'r') + fgetc (files[i].file); + else + fprintf (files[i].file, "%d:%d", FILE_FLUSH_TYPE, i); + } + + if (!FILE_FLUSH_TYPE) + TEST_VERIFY (fflush (NULL) == 0); + else + for (int i = 0; i < TEST_FILE_COUNT; i++) + TEST_VERIFY (fflush (files[i].file) == 0); + + for (int i = 0; i < TEST_FILE_COUNT; i++) + { + verbose_printf ("Check that file %s has been modified after fflush\n", + files[i].name); + file_changed (i, mode); + } + + for (int i = 0; i < TEST_FILE_COUNT; i++) + xfclose (files[i].file); +} + +static int +do_test (void) +{ + for (int i = 0; i < TEST_FILE_COUNT; i++) + file_init (i); + + verbose_printf ("Checking fflush(" S_FLUSH_TYPE "), WRITE mode\n"); + file_flush ("w"); + + verbose_printf ("Checking fflush(" S_FLUSH_TYPE "), READWRITE mode\n"); + file_flush ("r+"); + + for (int i = 0; i < TEST_FILE_COUNT; i++) + file_cleanup (i); + + return 0; +} + +#include diff --git a/libio/tst-fflush.c b/libio/tst-fflush.c new file mode 100644 index 0000000000..d3a4b78f43 --- /dev/null +++ b/libio/tst-fflush.c @@ -0,0 +1,24 @@ +/* Test that fflush (FILE) and fflush (NULL) are semantically equivalent. + This is the `fflush (FILE)` part. + + 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 + . */ + +#define FILE_FLUSH_TYPE 1 +#define S_FLUSH_TYPE "FILE" + +#include "tst-fflush-skeleton.c"