canonicalize_pathname.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. lib - canonicalize path
  3. Copyright (C) 2011-2019
  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 <http://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", VFS_NOLINKS | VFS_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. /* *INDENT-OFF* */
  59. static const struct test_canonicalize_path_ds
  60. {
  61. const char *input_path;
  62. const char *expected_path;
  63. } test_canonicalize_path_ds[] =
  64. {
  65. { /* 0. UNC path */
  66. "//some_server/ww",
  67. "//some_server/ww"
  68. },
  69. { /* 1. join slashes */
  70. "///some_server/////////ww",
  71. "/some_server/ww"
  72. },
  73. { /* 2. Collapse "/./" -> "/" */
  74. "//some_server//.///////ww/./././.",
  75. "//some_server/ww"
  76. },
  77. {/* 3. Remove leading "./" */
  78. "./some_server/ww",
  79. "some_server/ww"
  80. },
  81. { /* 4. some/.. -> . */
  82. "some_server/..",
  83. "."
  84. },
  85. { /* 5. Collapse "/.." with the previous part of path */
  86. "/some_server/ww/some_server/../ww/../some_server/..//ww/some_server/ww",
  87. "/some_server/ww/ww/some_server/ww"
  88. },
  89. { /* 6. URI style */
  90. "/some_server/ww/ftp://user:pass@host.net/path/",
  91. "/some_server/ww/ftp://user:pass@host.net/path"
  92. },
  93. { /* 7. */
  94. "/some_server/ww/ftp://user:pass@host.net/path/../../",
  95. "/some_server/ww"
  96. },
  97. { /* 8. */
  98. "ftp://user:pass@host.net/path/../../",
  99. "."
  100. },
  101. { /* 9. */
  102. "ftp://user/../../",
  103. ".."
  104. },
  105. #ifdef HAVE_CHARSET
  106. { /* 10. Supported encoding */
  107. "/b/#enc:utf-8/../c",
  108. "/c"
  109. },
  110. { /* 11. Unsupported encoding */
  111. "/b/#enc:aaaa/../c",
  112. "/b/c"
  113. },
  114. { /* 12. Supported encoding */
  115. "/b/../#enc:utf-8/c",
  116. "/#enc:utf-8/c"
  117. },
  118. { /* 13. Unsupported encoding */
  119. "/b/../#enc:aaaa/c",
  120. "/#enc:aaaa/c"
  121. },
  122. { /* 14. Supported encoding */
  123. "/b/c/#enc:utf-8/..",
  124. "/b"
  125. },
  126. { /* 15. Unsupported encoding */
  127. "/b/c/#enc:aaaa/..",
  128. "/b/c"
  129. },
  130. { /* 16. Supported encoding */
  131. "/b/c/../#enc:utf-8",
  132. "/b/#enc:utf-8"
  133. },
  134. { /* 17. Unsupported encoding */
  135. "/b/c/../#enc:aaaa",
  136. "/b/#enc:aaaa"
  137. },
  138. #endif /* HAVE_CHARSET */
  139. };
  140. /* *INDENT-ON* */
  141. /* @Test(dataSource = "test_canonicalize_path_ds") */
  142. /* *INDENT-OFF* */
  143. START_PARAMETRIZED_TEST (test_canonicalize_path, test_canonicalize_path_ds)
  144. /* *INDENT-ON* */
  145. {
  146. /* given */
  147. char *actual_path;
  148. actual_path = g_strdup (data->input_path);
  149. /* when */
  150. canonicalize_pathname (actual_path);
  151. /* then */
  152. mctest_assert_str_eq (actual_path, data->expected_path) g_free (actual_path);
  153. }
  154. /* *INDENT-OFF* */
  155. END_PARAMETRIZED_TEST
  156. /* *INDENT-ON* */
  157. /* --------------------------------------------------------------------------------------------- */
  158. int
  159. main (void)
  160. {
  161. int number_failed;
  162. Suite *s = suite_create (TEST_SUITE_NAME);
  163. TCase *tc_core = tcase_create ("Core");
  164. SRunner *sr;
  165. tcase_add_checked_fixture (tc_core, setup, teardown);
  166. /* Add new tests here: *************** */
  167. mctest_add_parameterized_test (tc_core, test_canonicalize_path, test_canonicalize_path_ds);
  168. /* *********************************** */
  169. suite_add_tcase (s, tc_core);
  170. sr = srunner_create (s);
  171. srunner_set_log (sr, "canonicalize_pathname.log");
  172. srunner_run_all (sr, CK_ENV);
  173. number_failed = srunner_ntests_failed (sr);
  174. srunner_free (sr);
  175. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  176. }
  177. /* --------------------------------------------------------------------------------------------- */