TestRealpath.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. #include <errno.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/stat.h>
  14. #include <unistd.h>
  15. static constexpr char TMPDIR_PATTERN[] = "/tmp/overlong_realpath_XXXXXX";
  16. static constexpr char PATH_LOREM_250[] = "This-is-an-annoyingly-long-name-that-should-take-up-exactly-two-hundred-and-fifty-characters-and-is-surprisingly-difficult-to-fill-with-reasonably-meaningful-text-which-is-necessary-because-that-makes-it-easier-for-my-eyes-to-spot-any-corruption-fast";
  17. static constexpr size_t ITERATION_DEPTH = 17;
  18. static void check_result(const char* what, const String& expected, const char* actual)
  19. {
  20. if (expected != actual)
  21. FAIL(String::formatted("Expected {} to be \"{}\" ({} characters)", what, actual, actual ? strlen(actual) : 0));
  22. }
  23. TEST_CASE(overlong_realpath)
  24. {
  25. // We want to construct a path that is over PATH_MAX characters long.
  26. // This cannot be done in a single step.
  27. // First, switch to a known environment:
  28. char tmp_dir[] = "/tmp/overlong_realpath_XXXXXX";
  29. errno = 0;
  30. auto* new_dir = mkdtemp(tmp_dir);
  31. VERIFY(new_dir != nullptr);
  32. VERIFY(errno == 0);
  33. errno = 0;
  34. auto ret = chdir(tmp_dir);
  35. VERIFY(ret >= 0);
  36. VERIFY(errno == 0);
  37. // Then, create a long path.
  38. StringBuilder expected;
  39. expected.append(tmp_dir);
  40. // But first, demonstrate the functionality at a reasonable depth:
  41. auto expected_str = expected.build();
  42. check_result("getwd", expected_str, getwd(static_cast<char*>(calloc(1, PATH_MAX))));
  43. check_result("getcwd", expected_str, getcwd(nullptr, 0));
  44. check_result("realpath", expected_str, realpath(".", nullptr));
  45. for (size_t i = 0; i < ITERATION_DEPTH; ++i) {
  46. ret = mkdir(PATH_LOREM_250, S_IRWXU);
  47. if (ret < 0) {
  48. perror("mkdir iter");
  49. FAIL(String::formatted("Unable to mkdir the overlong path fragment in iteration {}", i));
  50. return;
  51. }
  52. expected.append('/');
  53. expected.append(PATH_LOREM_250);
  54. ret = chdir(PATH_LOREM_250);
  55. if (ret < 0) {
  56. perror("chdir iter");
  57. FAIL(String::formatted("Unable to chdir to the overlong path fragment in iteration {}", i));
  58. return;
  59. }
  60. }
  61. outln("cwd should now be ridiculously large");
  62. // Evaluate
  63. expected_str = expected.build();
  64. check_result("getwd", {}, getwd(static_cast<char*>(calloc(1, PATH_MAX))));
  65. check_result("getcwd", expected_str, getcwd(nullptr, 0));
  66. check_result("realpath", expected_str, realpath(".", nullptr));
  67. VERIFY(strlen(PATH_LOREM_250) == 250);
  68. VERIFY(strlen(TMPDIR_PATTERN) + ITERATION_DEPTH * (1 + strlen(PATH_LOREM_250)) == expected_str.length());
  69. VERIFY(expected_str.length() > PATH_MAX);
  70. }