vfs_split.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. lib/vfs - test vfs_split() functionality
  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. #include "lib/strutil.h"
  22. #include "lib/vfs/xdirentry.h"
  23. #include "lib/vfs/path.c" // for testing static methods
  24. #include "src/vfs/local/local.c"
  25. static struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  26. /* --------------------------------------------------------------------------------------------- */
  27. /* @Before */
  28. static void
  29. setup (void)
  30. {
  31. str_init_strings (NULL);
  32. vfs_init ();
  33. vfs_init_localfs ();
  34. vfs_setup_work_dir ();
  35. vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  36. vfs_register_class (&vfs_test_ops1);
  37. vfs_init_class (&vfs_test_ops2, "testfs2", VFSF_UNKNOWN, "test2");
  38. vfs_register_class (&vfs_test_ops2);
  39. vfs_init_class (&vfs_test_ops3, "testfs3", VFSF_UNKNOWN, "test3");
  40. vfs_register_class (&vfs_test_ops3);
  41. }
  42. /* --------------------------------------------------------------------------------------------- */
  43. /* @After */
  44. static void
  45. teardown (void)
  46. {
  47. vfs_shut ();
  48. str_uninit_strings ();
  49. }
  50. /* --------------------------------------------------------------------------------------------- */
  51. /* @DataSource("test_vfs_split_ds") */
  52. static const struct test_vfs_split_ds
  53. {
  54. const char *input_string;
  55. const char *expected_path;
  56. const char *expected_local;
  57. const char *expected_op;
  58. const struct vfs_class *expected_result;
  59. } test_vfs_split_ds[] = {
  60. {
  61. // 0.
  62. "#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2/#test3:/qqq/www/eee.rr",
  63. "#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2/",
  64. "qqq/www/eee.rr",
  65. "test3:",
  66. &vfs_test_ops3,
  67. },
  68. {
  69. // 1.
  70. "#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2/",
  71. "#test1:/bla-bla/some/path/",
  72. "bla-bla/some/path2/",
  73. "test2:",
  74. &vfs_test_ops2,
  75. },
  76. {
  77. // 2.
  78. "#test1:/bla-bla/some/path/",
  79. "",
  80. "bla-bla/some/path/",
  81. "test1:",
  82. &vfs_test_ops1,
  83. },
  84. {
  85. // 3.
  86. "",
  87. "",
  88. NULL,
  89. NULL,
  90. NULL,
  91. },
  92. {
  93. // 4. split with local
  94. "/local/path/#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2#test3:/qqq/www/eee.rr",
  95. "/local/path/#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2",
  96. "qqq/www/eee.rr",
  97. "test3:",
  98. &vfs_test_ops3,
  99. },
  100. {
  101. // 5. split with local
  102. "/local/path/#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2",
  103. "/local/path/#test1:/bla-bla/some/path/",
  104. "bla-bla/some/path2",
  105. "test2:",
  106. &vfs_test_ops2,
  107. },
  108. {
  109. // 6. split with local
  110. "/local/path/#test1:/bla-bla/some/path/",
  111. "/local/path/",
  112. "bla-bla/some/path/",
  113. "test1:",
  114. &vfs_test_ops1,
  115. },
  116. {
  117. // 7. split with local
  118. "/local/path/",
  119. "/local/path/",
  120. NULL,
  121. NULL,
  122. NULL,
  123. },
  124. {
  125. // 8. split with URL
  126. "#test2:username:passwd@somehost.net/bla-bla/some/path2",
  127. "",
  128. "bla-bla/some/path2",
  129. "test2:username:passwd@somehost.net",
  130. &vfs_test_ops2,
  131. },
  132. {
  133. // 9. split URL with semi
  134. "/local/path/#test1:/bla-bla/some/path/#test2:username:p!a@s#s$w%d@somehost.net/bla-bla/"
  135. "some/"
  136. "path2",
  137. "/local/path/#test1:/bla-bla/some/path/",
  138. "bla-bla/some/path2",
  139. "test2:username:p!a@s#s$w%d@somehost.net",
  140. &vfs_test_ops2,
  141. },
  142. {
  143. // 10. split with semi in path
  144. "#test2:/bl#a-bl#a/so#me/pa#th2",
  145. "",
  146. "bl#a-bl#a/so#me/pa#th2",
  147. "test2:",
  148. &vfs_test_ops2,
  149. }
  150. };
  151. /* @Test(dataSource = "test_vfs_split_ds") */
  152. START_PARAMETRIZED_TEST (test_vfs_split, test_vfs_split_ds)
  153. {
  154. // given
  155. const char *local = NULL, *op = NULL;
  156. struct vfs_class *actual_result;
  157. char *path;
  158. path = g_strdup (data->input_string);
  159. // when
  160. actual_result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  161. // then
  162. mctest_assert_ptr_eq (actual_result, data->expected_result);
  163. mctest_assert_str_eq (path, data->expected_path);
  164. mctest_assert_str_eq (local, data->expected_local);
  165. mctest_assert_str_eq (op, data->expected_op);
  166. g_free (path);
  167. }
  168. END_PARAMETRIZED_TEST
  169. /* --------------------------------------------------------------------------------------------- */
  170. int
  171. main (void)
  172. {
  173. TCase *tc_core;
  174. tc_core = tcase_create ("Core");
  175. tcase_add_checked_fixture (tc_core, setup, teardown);
  176. // Add new tests here: ***************
  177. mctest_add_parameterized_test (tc_core, test_vfs_split, test_vfs_split_ds);
  178. // ***********************************
  179. return mctest_run_all (tc_core);
  180. }
  181. /* --------------------------------------------------------------------------------------------- */