tst-readdir-long.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Test readdir (+variants) behavior with file names of varying length.
  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. #include <dirent.h>
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <support/check.h>
  21. #include <support/fuse.h>
  22. #include <support/support.h>
  23. #include <support/xdirent.h>
  24. #include <support/readdir.h>
  25. #include <libc-diag.h>
  26. /* If positive, at this length an EMSGSIZE error is injected. */
  27. static _Atomic int inject_error_at_length;
  28. /* Return a file name, LENGTH bytes long. */
  29. static char *
  30. name_of_length (size_t length)
  31. {
  32. char *result = xmalloc (length + 1);
  33. unsigned int prefix = snprintf (result, length + 1, "%zu-", length);
  34. for (size_t i = prefix; i < length; ++i)
  35. result[i] = 'A' + ((length + i) % 26);
  36. result[length] = '\0';
  37. return result;
  38. }
  39. /* Add the directory entry at OFFSET to the stream D. */
  40. static uint64_t
  41. add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset)
  42. {
  43. unsigned int length = offset + 1;
  44. if (length > 1000)
  45. /* Longer than what is possible to produce with 256
  46. UTF-8-encoded Unicode code points. */
  47. return 0;
  48. char *to_free = NULL;
  49. const char *name;
  50. uint64_t ino = 1000 + length; /* Arbitrary value, distinct from 1. */
  51. uint32_t type = DT_REG;
  52. if (offset <= 1)
  53. {
  54. type = DT_DIR;
  55. DIAG_PUSH_NEEDS_COMMENT_CLANG;
  56. DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
  57. name = ".." + !offset; /* "." or "..". */
  58. DIAG_POP_NEEDS_COMMENT_CLANG;
  59. ino = 1;
  60. }
  61. else if (length == 1000)
  62. name = "short";
  63. else
  64. {
  65. to_free = name_of_length (length);
  66. name = to_free;
  67. }
  68. ++offset;
  69. bool added = support_fuse_dirstream_add (d, ino, offset, type, name);
  70. free (to_free);
  71. if (added)
  72. return offset;
  73. else
  74. return 0;
  75. }
  76. /* Set to true if getdents64 should produce only one entry. */
  77. static _Atomic bool one_entry_per_getdents64;
  78. static void
  79. fuse_thread (struct support_fuse *f, void *closure)
  80. {
  81. struct fuse_in_header *inh;
  82. while ((inh = support_fuse_next (f)) != NULL)
  83. {
  84. if (support_fuse_handle_mountpoint (f)
  85. || (inh->nodeid == 1 && support_fuse_handle_directory (f)))
  86. continue;
  87. switch (inh->opcode)
  88. {
  89. case FUSE_READDIR:
  90. if (inh->nodeid == 1)
  91. {
  92. uint64_t offset = support_fuse_cast (READ, inh)->offset;
  93. if (inject_error_at_length == offset + 1)
  94. support_fuse_reply_error (f, EMSGSIZE);
  95. else
  96. {
  97. struct support_fuse_dirstream *d
  98. = support_fuse_prepare_readdir (f);
  99. while (true)
  100. {
  101. offset = add_directory_entry (d, offset);
  102. if (offset == 0 || one_entry_per_getdents64
  103. /* Error will be reported at next READDIR. */
  104. || offset + 1 == inject_error_at_length)
  105. break;
  106. }
  107. support_fuse_reply_prepared (f);
  108. }
  109. }
  110. else
  111. support_fuse_reply_error (f, EIO);
  112. break;
  113. default:
  114. FAIL ("unexpected event %s", support_fuse_opcode (inh->opcode));
  115. support_fuse_reply_error (f, EIO);
  116. }
  117. }
  118. }
  119. /* Run the tests for the specified readdir variant OP. */
  120. static void
  121. run_readdir_tests (struct support_fuse *f, enum support_readdir_op op)
  122. {
  123. printf ("info: testing %s (inject_error=%d unbuffered=%d)\n",
  124. support_readdir_function (op), inject_error_at_length,
  125. (int) one_entry_per_getdents64);
  126. bool testing_r = support_readdir_r_variant (op);
  127. DIR *dir = xopendir (support_fuse_mountpoint (f));
  128. struct support_dirent e = { 0, };
  129. TEST_VERIFY (support_readdir (dir, op, &e));
  130. TEST_COMPARE (e.d_ino, 1);
  131. TEST_COMPARE_STRING (e.d_name, ".");
  132. TEST_VERIFY (support_readdir (dir, op, &e));
  133. TEST_COMPARE (e.d_ino, 1);
  134. TEST_COMPARE_STRING (e.d_name, "..");
  135. for (unsigned int i = 3; i < 1000; ++i)
  136. {
  137. if (i == inject_error_at_length)
  138. /* Error expected below. */
  139. break;
  140. if (i >= sizeof ((struct dirent) { 0, }.d_name) && testing_r)
  141. /* This is a readir_r test. The longer names are not
  142. available because they do not fit into struct dirent. */
  143. break;
  144. char *expected_name = name_of_length (i);
  145. TEST_COMPARE (strlen (expected_name), i);
  146. TEST_VERIFY (support_readdir (dir, op, &e));
  147. TEST_COMPARE (e.d_ino, 1000 + i);
  148. TEST_COMPARE_STRING (e.d_name, expected_name);
  149. free (expected_name);
  150. }
  151. if (inject_error_at_length == 0)
  152. {
  153. /* Check that the ENAMETOOLONG error does not prevent reading a
  154. later short name. */
  155. TEST_VERIFY (support_readdir (dir, op, &e));
  156. TEST_COMPARE (e.d_ino, 2000);
  157. TEST_COMPARE_STRING (e.d_name, "short");
  158. if (testing_r)
  159. /* An earlier name was too long. */
  160. support_readdir_expect_error (dir, op, ENAMETOOLONG);
  161. else
  162. /* Entire directory read without error. */
  163. TEST_VERIFY (!support_readdir (dir, op, &e));
  164. }
  165. else
  166. support_readdir_expect_error (dir, op, EMSGSIZE);
  167. free (e.d_name);
  168. xclosedir (dir);
  169. }
  170. /* Run all readdir variants for both fully-buffered an unbuffered
  171. (one-at-a-time) directory streams. */
  172. static void
  173. run_fully_buffered_and_singleton_buffers (struct support_fuse *f)
  174. {
  175. for (int do_one_entry = 0; do_one_entry < 2; ++do_one_entry)
  176. {
  177. one_entry_per_getdents64 = do_one_entry;
  178. for (enum support_readdir_op op = 0; op <= support_readdir_op_last();
  179. ++op)
  180. run_readdir_tests (f, op);
  181. }
  182. }
  183. static int
  184. do_test (void)
  185. {
  186. /* Smoke test for name_of_length. */
  187. {
  188. char *name = name_of_length (5);
  189. TEST_COMPARE_STRING (name, "5-HIJ");
  190. free (name);
  191. name = name_of_length (6);
  192. TEST_COMPARE_STRING (name, "6-IJKL");
  193. free (name);
  194. }
  195. support_fuse_init ();
  196. struct support_fuse *f = support_fuse_mount (fuse_thread, NULL);
  197. run_fully_buffered_and_singleton_buffers (f);
  198. inject_error_at_length = 100;
  199. run_fully_buffered_and_singleton_buffers (f);
  200. inject_error_at_length = 300;
  201. run_fully_buffered_and_singleton_buffers (f);
  202. support_fuse_unmount (f);
  203. return 0;
  204. }
  205. #include <support/test-driver.c>