tst-xreadlink.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Test the xreadlink function.
  2. Copyright (C) 2017-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 <errno.h>
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <support/check.h>
  21. #include <support/support.h>
  22. #include <support/temp_file.h>
  23. #include <support/xunistd.h>
  24. static int
  25. do_test (void)
  26. {
  27. char *dir = support_create_temp_directory ("tst-xreadlink-");
  28. char *symlink_name = xasprintf ("%s/symlink", dir);
  29. add_temp_file (symlink_name);
  30. /* The limit 10000 is arbitrary and simply there to prevent an
  31. attempt to exhaust all available disk space. */
  32. for (int size = 1; size < 10000; ++size)
  33. {
  34. char *contents = xmalloc (size + 1);
  35. for (int i = 0; i < size; ++i)
  36. contents[i] = 'a' + (rand () % 26);
  37. contents[size] = '\0';
  38. if (symlink (contents, symlink_name) != 0)
  39. {
  40. if (errno == ENAMETOOLONG)
  41. {
  42. printf ("info: ENAMETOOLONG failure at %d bytes\n", size);
  43. free (contents);
  44. break;
  45. }
  46. FAIL_EXIT1 ("symlink (%d bytes): %m", size);
  47. }
  48. char *readlink_result = xreadlink (symlink_name);
  49. TEST_VERIFY (strcmp (readlink_result, contents) == 0);
  50. free (readlink_result);
  51. xunlink (symlink_name);
  52. free (contents);
  53. }
  54. /* Create an empty file to suppress the temporary file deletion
  55. warning. */
  56. xclose (xopen (symlink_name, O_WRONLY | O_CREAT, 0));
  57. free (symlink_name);
  58. free (dir);
  59. return 0;
  60. }
  61. #include <support/test-driver.c>