mc_realpath.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. lib - realpath
  3. Copyright (C) 2017-2025
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2017
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/util"
  20. #include "tests/mctest.h"
  21. #include "lib/strutil.h"
  22. #include "lib/vfs/vfs.h" // VFS_ENCODING_PREFIX, vfs_init(), vfs_shut()
  23. #include "src/vfs/local/local.c"
  24. #include "lib/util.h" // mc_realpath()
  25. /* --------------------------------------------------------------------------------------------- */
  26. static char resolved_path[PATH_MAX];
  27. /* --------------------------------------------------------------------------------------------- */
  28. /* @Before */
  29. static void
  30. setup (void)
  31. {
  32. str_init_strings (NULL);
  33. vfs_init ();
  34. vfs_init_localfs ();
  35. vfs_setup_work_dir ();
  36. }
  37. /* @After */
  38. static void
  39. teardown (void)
  40. {
  41. vfs_shut ();
  42. str_uninit_strings ();
  43. }
  44. /* --------------------------------------------------------------------------------------------- */
  45. /* @DataSource("data_source") */
  46. static const struct data_source
  47. {
  48. const char *input_string;
  49. const char *expected_string;
  50. } data_source[] = {
  51. // absolute paths
  52. { "/", "/" },
  53. { "/usr/bin", "/usr/bin" },
  54. #ifdef HAVE_CHARSET
  55. { "/" VFS_ENCODING_PREFIX "UTF-8/", "/" },
  56. { "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/usr/bin" },
  57. #else
  58. { "/" VFS_ENCODING_PREFIX "UTF-8/", "/" VFS_ENCODING_PREFIX "UTF-8/" },
  59. { "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin" },
  60. #endif
  61. // relative paths are relative to /
  62. { "usr/bin", "/usr/bin" },
  63. #ifdef HAVE_CHARSET
  64. { VFS_ENCODING_PREFIX "UTF-8/", "/" },
  65. { VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/usr/bin" },
  66. #else
  67. { VFS_ENCODING_PREFIX "UTF-8/", VFS_ENCODING_PREFIX "UTF-8/" },
  68. { VFS_ENCODING_PREFIX "UTF-8/usr/bin", VFS_ENCODING_PREFIX "UTF-8/usr/bin" },
  69. #endif
  70. };
  71. /* @Test(dataSource = "data_source") */
  72. START_PARAMETRIZED_TEST (realpath_test, data_source)
  73. {
  74. int ret;
  75. /* realpath(3) produces a canonicalized absolute pathname using current directory.
  76. * Change the current directory to produce correct pathname. */
  77. ret = chdir ("/");
  78. // when
  79. if (mc_realpath (data->input_string, resolved_path) == NULL)
  80. resolved_path[0] = '\0';
  81. // then
  82. mctest_assert_str_eq (resolved_path, data->expected_string);
  83. (void) ret;
  84. }
  85. END_PARAMETRIZED_TEST
  86. /* --------------------------------------------------------------------------------------------- */
  87. int
  88. main (void)
  89. {
  90. TCase *tc_core;
  91. char *cwd;
  92. tc_core = tcase_create ("Core");
  93. // writable directory where check creates temporary files
  94. cwd = my_get_current_dir ();
  95. g_setenv ("TEMP", cwd, TRUE);
  96. g_free (cwd);
  97. tcase_add_checked_fixture (tc_core, setup, teardown);
  98. // Add new tests here: ***************
  99. mctest_add_parameterized_test (tc_core, realpath_test, data_source);
  100. // ***********************************
  101. return mctest_run_all (tc_core);
  102. }
  103. /* --------------------------------------------------------------------------------------------- */