tst-fflush-skeleton.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Test that fflush (FILE) and fflush (NULL) are semantically equivalent.
  2. Copyright (C) 2024-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* A success on this test doesn't imply the effectiveness of fflush as
  16. we can't ensure that the file wasn't already in the expected state
  17. before the call of the function. It only ensures that, if the test
  18. fails, fflush is broken. */
  19. #include <assert.h>
  20. #include <fcntl.h>
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <sys/stat.h>
  27. #include <sys/mman.h>
  28. #include <support/check.h>
  29. #include <support/support.h>
  30. #include <support/temp_file.h>
  31. #include <support/test-driver.h>
  32. #include <support/xstdio.h>
  33. #include <support/xunistd.h>
  34. #define CONTENT_SZ_MAX 32
  35. #define TEST_FILE_COUNT 10
  36. struct file_tracking
  37. {
  38. FILE *file;
  39. char *name;
  40. int fd;
  41. char *mfile;
  42. } files[TEST_FILE_COUNT];
  43. static void
  44. file_init (int file)
  45. {
  46. int fd = -1;
  47. assert (file < TEST_FILE_COUNT);
  48. files[file] = (struct file_tracking) { .fd = -1, };
  49. xclose (create_temp_file ("tst-fflush", &files[file].name));
  50. fd = xopen (files[file].name, O_RDONLY, 0);
  51. files[file].mfile = xmmap (NULL, CONTENT_SZ_MAX, PROT_READ, MAP_SHARED, fd);
  52. xclose (fd);
  53. }
  54. static void
  55. file_cleanup (int file)
  56. {
  57. free (files[file].name);
  58. xmunmap (files[file].mfile, CONTENT_SZ_MAX);
  59. files[file] = (struct file_tracking) { .fd = -1, };
  60. }
  61. static void
  62. file_changed (int to_check, const char *mode)
  63. {
  64. struct stat stats = { };
  65. char expected[CONTENT_SZ_MAX] = { };
  66. verbose_printf ("Check that %s (%d) exactly contains the data we put in\n",
  67. files[to_check].name, to_check);
  68. /* File should contain "N:M" where both N and M are one digit exactly. */
  69. snprintf (expected, sizeof (expected), "%d:%d", FILE_FLUSH_TYPE, to_check);
  70. TEST_COMPARE_BLOB (files[to_check].mfile, sizeof (expected),
  71. expected, sizeof (expected));
  72. TEST_VERIFY (fstat (files[to_check].fd, &stats) >= 0);
  73. TEST_VERIFY (stats.st_size == 3);
  74. /* In read mode we expect to be at position 1, in write mode at position 3 */
  75. TEST_COMPARE (lseek (files[to_check].fd, 0, SEEK_CUR),
  76. mode[0] == 'r' ? 1 : 3);
  77. if (support_record_failure_is_failed ())
  78. FAIL_EXIT1 ("exiting due to previous failure");
  79. /* Not reached if the data doesn't match. */
  80. }
  81. static void
  82. file_flush (const char *mode)
  83. {
  84. for (int i = 0; i < TEST_FILE_COUNT; i++)
  85. {
  86. files[i].file = xfopen (files[i].name, mode);
  87. files[i].fd = fileno (files[i].file);
  88. }
  89. /* Print a unique identifier in each file, that is not too long nor contain
  90. new line to not trigger _IO_OVERFLOW/_IO_SYNC. */
  91. for (int i = 0; i < TEST_FILE_COUNT; i++)
  92. {
  93. if (mode[0] == 'r')
  94. fgetc (files[i].file);
  95. else
  96. fprintf (files[i].file, "%d:%d", FILE_FLUSH_TYPE, i);
  97. }
  98. if (!FILE_FLUSH_TYPE)
  99. TEST_VERIFY (fflush (NULL) == 0);
  100. else
  101. for (int i = 0; i < TEST_FILE_COUNT; i++)
  102. TEST_VERIFY (fflush (files[i].file) == 0);
  103. for (int i = 0; i < TEST_FILE_COUNT; i++)
  104. {
  105. verbose_printf ("Check that file %s has been modified after fflush\n",
  106. files[i].name);
  107. file_changed (i, mode);
  108. }
  109. for (int i = 0; i < TEST_FILE_COUNT; i++)
  110. xfclose (files[i].file);
  111. }
  112. static int
  113. do_test (void)
  114. {
  115. for (int i = 0; i < TEST_FILE_COUNT; i++)
  116. file_init (i);
  117. verbose_printf ("Checking fflush(" S_FLUSH_TYPE "), WRITE mode\n");
  118. file_flush ("w");
  119. verbose_printf ("Checking fflush(" S_FLUSH_TYPE "), READWRITE mode\n");
  120. file_flush ("r+");
  121. for (int i = 0; i < TEST_FILE_COUNT; i++)
  122. file_cleanup (i);
  123. return 0;
  124. }
  125. #include <support/test-driver.c>