tst-readdir64-compat.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Test readdir64 compatibility symbol.
  2. Copyright (C) 2018-2019 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. <http://www.gnu.org/licenses/>. */
  15. #include <dirent.h>
  16. #include <dlfcn.h>
  17. #include <errno.h>
  18. #include <shlib-compat.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <support/check.h>
  23. /* Copied from <olddirent.h>. */
  24. struct __old_dirent64
  25. {
  26. __ino_t d_ino;
  27. __off64_t d_off;
  28. unsigned short int d_reclen;
  29. unsigned char d_type;
  30. char d_name[256];
  31. };
  32. typedef struct __old_dirent64 *(*compat_readdir64_type) (DIR *);
  33. #if TEST_COMPAT (libc, GLIBC_2_1, GLIBC_2_2)
  34. struct __old_dirent64 *compat_readdir64 (DIR *);
  35. compat_symbol_reference (libc, compat_readdir64, readdir64, GLIBC_2_1);
  36. #endif
  37. static int
  38. do_test (void)
  39. {
  40. #if TEST_COMPAT (libc, GLIBC_2_1, GLIBC_2_2)
  41. /* Directory stream using the non-compat readdir64 symbol. The test
  42. checks against this. */
  43. DIR *dir_reference = opendir (".");
  44. TEST_VERIFY_EXIT (dir_reference != NULL);
  45. DIR *dir_test = opendir (".");
  46. TEST_VERIFY_EXIT (dir_test != NULL);
  47. /* This loop assumes that the enumeration order is consistent for
  48. two different handles. Nothing should write to the current
  49. directory (in the source tree) while this test runs, so there
  50. should not be any difference due to races. */
  51. size_t count = 0;
  52. while (true)
  53. {
  54. errno = 0;
  55. struct dirent64 *entry_reference = readdir64 (dir_reference);
  56. if (entry_reference == NULL && errno != 0)
  57. FAIL_EXIT1 ("readdir64 entry %zu: %m\n", count);
  58. struct __old_dirent64 *entry_test = compat_readdir64 (dir_test);
  59. if (entry_reference == NULL)
  60. {
  61. if (errno == EOVERFLOW)
  62. {
  63. TEST_VERIFY (entry_reference->d_ino
  64. != (__ino_t) entry_reference->d_ino);
  65. printf ("info: inode number overflow at entry %zu\n", count);
  66. break;
  67. }
  68. if (errno != 0)
  69. FAIL_EXIT1 ("compat readdir64 entry %zu: %m\n", count);
  70. }
  71. /* Check that both streams end at the same time. */
  72. if (entry_reference == NULL)
  73. {
  74. TEST_VERIFY (entry_test == NULL);
  75. break;
  76. }
  77. else
  78. TEST_VERIFY_EXIT (entry_test != NULL);
  79. /* d_off is never zero because it is the offset of the next
  80. entry (not the current entry). */
  81. TEST_VERIFY (entry_reference->d_off > 0);
  82. /* Check that the entries are the same. */
  83. TEST_COMPARE_BLOB (entry_reference->d_name,
  84. strlen (entry_reference->d_name),
  85. entry_test->d_name, strlen (entry_test->d_name));
  86. TEST_COMPARE (entry_reference->d_ino, entry_test->d_ino);
  87. TEST_COMPARE (entry_reference->d_off, entry_test->d_off);
  88. TEST_COMPARE (entry_reference->d_type, entry_test->d_type);
  89. TEST_COMPARE (entry_reference->d_reclen, entry_test->d_reclen);
  90. ++count;
  91. }
  92. printf ("info: %zu directory entries found\n", count);
  93. TEST_VERIFY (count >= 3); /* ".", "..", and some source files. */
  94. TEST_COMPARE (closedir (dir_test), 0);
  95. TEST_COMPARE (closedir (dir_reference), 0);
  96. #endif
  97. return 0;
  98. }
  99. #include <support/test-driver.c>