canonicalize_pathname.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. lib - canonicalize path
  3. Copyright (C) 2011, 2013
  4. The 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_s_subclass test_subclass;
  29. static struct vfs_class vfs_test_ops;
  30. /* --------------------------------------------------------------------------------------------- */
  31. /* @Before */
  32. static void
  33. setup (void)
  34. {
  35. str_init_strings (NULL);
  36. vfs_init ();
  37. init_localfs ();
  38. vfs_setup_work_dir ();
  39. #ifdef HAVE_CHARSET
  40. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  41. load_codepages_list ();
  42. #endif
  43. vfs_s_init_class (&vfs_test_ops, &test_subclass);
  44. vfs_test_ops.name = "testfs";
  45. vfs_test_ops.flags = VFSF_NOLINKS;
  46. vfs_test_ops.prefix = "ftp";
  47. test_subclass.flags = VFS_S_REMOTE;
  48. vfs_register_class (&vfs_test_ops);
  49. }
  50. /* --------------------------------------------------------------------------------------------- */
  51. /* @After */
  52. static void
  53. teardown (void)
  54. {
  55. #ifdef HAVE_CHARSET
  56. free_codepages_list ();
  57. #endif
  58. vfs_shut ();
  59. str_uninit_strings ();
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  62. /* @DataSource("test_canonicalize_path_ds") */
  63. /* *INDENT-OFF* */
  64. static const struct test_canonicalize_path_ds
  65. {
  66. const char *input_path;
  67. const char *expected_path;
  68. } test_canonicalize_path_ds[] =
  69. {
  70. { /* 0. UNC path */
  71. "//some_server/ww",
  72. "//some_server/ww"
  73. },
  74. { /* 1. join slashes */
  75. "///some_server/////////ww",
  76. "/some_server/ww"
  77. },
  78. { /* 2. Collapse "/./" -> "/" */
  79. "//some_server//.///////ww/./././.",
  80. "//some_server/ww"
  81. },
  82. {/* 3. Remove leading "./" */
  83. "./some_server/ww",
  84. "some_server/ww"
  85. },
  86. { /* 4. some/.. -> . */
  87. "some_server/..",
  88. "."
  89. },
  90. { /* 5. Collapse "/.." with the previous part of path */
  91. "/some_server/ww/some_server/../ww/../some_server/..//ww/some_server/ww",
  92. "/some_server/ww/ww/some_server/ww"
  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. { /* 7. */
  99. "/some_server/ww/ftp://user:pass@host.net/path/../../",
  100. "/some_server/ww"
  101. },
  102. { /* 8. */
  103. "ftp://user:pass@host.net/path/../../",
  104. "."
  105. },
  106. { /* 9. */
  107. "ftp://user/../../",
  108. ".."
  109. },
  110. #ifdef HAVE_CHARSET
  111. { /* 10. Supported encoding */
  112. "/b/#enc:utf-8/../c",
  113. "/c"
  114. },
  115. { /* 11. Unsupported encoding */
  116. "/b/#enc:aaaa/../c",
  117. "/b/c"
  118. },
  119. { /* 12. Supported encoding */
  120. "/b/../#enc:utf-8/c",
  121. "/#enc:utf-8/c"
  122. },
  123. { /* 13. Unsupported encoding */
  124. "/b/../#enc:aaaa/c",
  125. "/#enc:aaaa/c"
  126. },
  127. { /* 14. Supported encoding */
  128. "/b/c/#enc:utf-8/..",
  129. "/b"
  130. },
  131. { /* 15. Unsupported encoding */
  132. "/b/c/#enc:aaaa/..",
  133. "/b/c"
  134. },
  135. { /* 16. Supported encoding */
  136. "/b/c/../#enc:utf-8",
  137. "/b/#enc:utf-8"
  138. },
  139. { /* 17. Unsupported encoding */
  140. "/b/c/../#enc:aaaa",
  141. "/b/#enc:aaaa"
  142. },
  143. #endif /* HAVE_CHARSET */
  144. };
  145. /* *INDENT-ON* */
  146. /* @Test(dataSource = "test_canonicalize_path_ds") */
  147. /* *INDENT-OFF* */
  148. START_PARAMETRIZED_TEST (test_canonicalize_path, test_canonicalize_path_ds)
  149. /* *INDENT-ON* */
  150. {
  151. /* given */
  152. char *actual_path;
  153. actual_path = g_strdup (data->input_path);
  154. /* when */
  155. canonicalize_pathname (actual_path);
  156. /* then */
  157. mctest_assert_str_eq (actual_path, data->expected_path) g_free (actual_path);
  158. }
  159. /* *INDENT-OFF* */
  160. END_PARAMETRIZED_TEST
  161. /* *INDENT-ON* */
  162. /* --------------------------------------------------------------------------------------------- */
  163. int
  164. main (void)
  165. {
  166. int number_failed;
  167. Suite *s = suite_create (TEST_SUITE_NAME);
  168. TCase *tc_core = tcase_create ("Core");
  169. SRunner *sr;
  170. tcase_add_checked_fixture (tc_core, setup, teardown);
  171. /* Add new tests here: *************** */
  172. mctest_add_parameterized_test (tc_core, test_canonicalize_path, test_canonicalize_path_ds);
  173. /* *********************************** */
  174. suite_add_tcase (s, tc_core);
  175. sr = srunner_create (s);
  176. srunner_set_log (sr, "canonicalize_pathname.log");
  177. srunner_run_all (sr, CK_NORMAL);
  178. number_failed = srunner_ntests_failed (sr);
  179. srunner_free (sr);
  180. return (number_failed == 0) ? 0 : 1;
  181. }
  182. /* --------------------------------------------------------------------------------------------- */