canonicalize_pathname.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. lib - canonicalize path
  3. Copyright (C) 2011-2025
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  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/vfs"
  20. #include "tests/mctest.h"
  21. #ifdef HAVE_CHARSET
  22. # include "lib/charsets.h"
  23. #endif
  24. #include "lib/strutil.h"
  25. #include "lib/util.h"
  26. #include "lib/vfs/xdirentry.h"
  27. #include "src/vfs/local/local.c"
  28. static struct vfs_class vfs_test_ops;
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* @Before */
  31. static void
  32. setup (void)
  33. {
  34. str_init_strings (NULL);
  35. vfs_init ();
  36. vfs_init_localfs ();
  37. vfs_setup_work_dir ();
  38. #ifdef HAVE_CHARSET
  39. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  40. load_codepages_list ();
  41. #endif
  42. vfs_init_class (&vfs_test_ops, "testfs", VFSF_NOLINKS | VFSF_REMOTE, "ftp");
  43. vfs_register_class (&vfs_test_ops);
  44. }
  45. /* --------------------------------------------------------------------------------------------- */
  46. /* @After */
  47. static void
  48. teardown (void)
  49. {
  50. #ifdef HAVE_CHARSET
  51. free_codepages_list ();
  52. #endif
  53. vfs_shut ();
  54. str_uninit_strings ();
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. /* @DataSource("test_canonicalize_path_ds") */
  58. static const struct test_canonicalize_path_ds
  59. {
  60. const char *input_path;
  61. const char *expected_path;
  62. } test_canonicalize_path_ds[] = {
  63. {
  64. // 0. UNC path
  65. "//some_server/ww",
  66. "//some_server/ww",
  67. },
  68. {
  69. // 1. join slashes
  70. "///some_server/////////ww",
  71. "/some_server/ww",
  72. },
  73. {
  74. // 2. Collapse "/./" -> "/"
  75. "//some_server//.///////ww/./././.",
  76. "//some_server/ww",
  77. },
  78. {
  79. // 3. Remove leading "./"
  80. "./some_server/ww",
  81. "some_server/ww",
  82. },
  83. {
  84. // 4. some/.. -> .
  85. "some_server/..",
  86. ".",
  87. },
  88. {
  89. // 5. Collapse "/.." with the previous part of path
  90. "/some_server/ww/some_server/../ww/../some_server/..//ww/some_server/ww",
  91. "/some_server/ww/ww/some_server/ww",
  92. },
  93. {
  94. // 6. URI style
  95. "/some_server/ww/ftp://user:pass@host.net/path/",
  96. "/some_server/ww/ftp://user:pass@host.net/path",
  97. },
  98. {
  99. // 7.
  100. "/some_server/ww/ftp://user:pass@host.net/path/../../",
  101. "/some_server/ww",
  102. },
  103. {
  104. // 8.
  105. "ftp://user:pass@host.net/path/../../",
  106. ".",
  107. },
  108. {
  109. // 9.
  110. "ftp://user/../../",
  111. "..",
  112. },
  113. #ifdef HAVE_CHARSET
  114. {
  115. // 10. Supported encoding
  116. "/b/#enc:utf-8/../c",
  117. "/c",
  118. },
  119. {
  120. // 11. Unsupported encoding
  121. "/b/#enc:aaaa/../c",
  122. "/b/c",
  123. },
  124. {
  125. // 12. Supported encoding
  126. "/b/../#enc:utf-8/c",
  127. "/#enc:utf-8/c",
  128. },
  129. {
  130. // 13. Unsupported encoding
  131. "/b/../#enc:aaaa/c",
  132. "/#enc:aaaa/c",
  133. },
  134. {
  135. // 14. Supported encoding
  136. "/b/c/#enc:utf-8/..",
  137. "/b",
  138. },
  139. {
  140. // 15. Unsupported encoding
  141. "/b/c/#enc:aaaa/..",
  142. "/b/c",
  143. },
  144. {
  145. // 16. Supported encoding
  146. "/b/c/../#enc:utf-8",
  147. "/b/#enc:utf-8",
  148. },
  149. {
  150. // 17. Unsupported encoding
  151. "/b/c/../#enc:aaaa",
  152. "/b/#enc:aaaa",
  153. },
  154. #endif
  155. };
  156. /* @Test(dataSource = "test_canonicalize_path_ds") */
  157. START_PARAMETRIZED_TEST (test_canonicalize_path, test_canonicalize_path_ds)
  158. {
  159. // given
  160. char *actual_path;
  161. actual_path = g_strdup (data->input_path);
  162. // when
  163. canonicalize_pathname (actual_path);
  164. // then
  165. mctest_assert_str_eq (actual_path, data->expected_path) g_free (actual_path);
  166. }
  167. END_PARAMETRIZED_TEST
  168. /* --------------------------------------------------------------------------------------------- */
  169. int
  170. main (void)
  171. {
  172. TCase *tc_core;
  173. tc_core = tcase_create ("Core");
  174. tcase_add_checked_fixture (tc_core, setup, teardown);
  175. // Add new tests here: ***************
  176. mctest_add_parameterized_test (tc_core, test_canonicalize_path, test_canonicalize_path_ds);
  177. // ***********************************
  178. return mctest_run_all (tc_core);
  179. }
  180. /* --------------------------------------------------------------------------------------------- */