TestRealpath.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <limits.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. static constexpr char TMPDIR_PATTERN[] = "/tmp/overlong_realpath_XXXXXX";
  17. 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";
  18. static constexpr size_t ITERATION_DEPTH = 17;
  19. static void check_result(char const* what, String const& expected, char const* actual)
  20. {
  21. if (expected != actual)
  22. FAIL(String::formatted("Expected {} to be \"{}\" ({} characters)", what, actual, actual ? strlen(actual) : 0));
  23. }
  24. TEST_CASE(overlong_realpath)
  25. {
  26. // We want to construct a path that is over PATH_MAX characters long.
  27. // This cannot be done in a single step.
  28. // First, switch to a known environment:
  29. char tmp_dir[] = "/tmp/overlong_realpath_XXXXXX";
  30. errno = 0;
  31. auto* new_dir = mkdtemp(tmp_dir);
  32. VERIFY(new_dir != nullptr);
  33. VERIFY(errno == 0);
  34. errno = 0;
  35. auto ret = chdir(tmp_dir);
  36. VERIFY(ret >= 0);
  37. VERIFY(errno == 0);
  38. // Then, create a long path.
  39. StringBuilder expected;
  40. expected.append({ tmp_dir, strlen(tmp_dir) });
  41. // But first, demonstrate the functionality at a reasonable depth:
  42. auto expected_str = expected.build();
  43. check_result("getwd", expected_str, getwd(static_cast<char*>(calloc(1, PATH_MAX))));
  44. check_result("getcwd", expected_str, getcwd(nullptr, 0));
  45. check_result("realpath", expected_str, realpath(".", nullptr));
  46. for (size_t i = 0; i < ITERATION_DEPTH; ++i) {
  47. ret = mkdir(PATH_LOREM_250, S_IRWXU);
  48. if (ret < 0) {
  49. perror("mkdir iter");
  50. FAIL(String::formatted("Unable to mkdir the overlong path fragment in iteration {}", i));
  51. return;
  52. }
  53. expected.append('/');
  54. expected.append({ PATH_LOREM_250, strlen(PATH_LOREM_250) });
  55. ret = chdir(PATH_LOREM_250);
  56. if (ret < 0) {
  57. perror("chdir iter");
  58. FAIL(String::formatted("Unable to chdir to the overlong path fragment in iteration {}", i));
  59. return;
  60. }
  61. }
  62. outln("cwd should now be ridiculously large");
  63. // Evaluate
  64. expected_str = expected.build();
  65. check_result("getwd", {}, getwd(static_cast<char*>(calloc(1, PATH_MAX))));
  66. check_result("getcwd", expected_str, getcwd(nullptr, 0));
  67. check_result("realpath", expected_str, realpath(".", nullptr));
  68. VERIFY(strlen(PATH_LOREM_250) == 250);
  69. VERIFY(strlen(TMPDIR_PATTERN) + ITERATION_DEPTH * (1 + strlen(PATH_LOREM_250)) == expected_str.length());
  70. VERIFY(expected_str.length() > PATH_MAX);
  71. }