vfs_split.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. lib/vfs - test vfs_split() functionality
  3. Copyright (C) 2011
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011
  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 <config.h>
  21. #include <check.h>
  22. #include "lib/global.h"
  23. #include "lib/strutil.h"
  24. #include "lib/vfs/xdirentry.h"
  25. #include "lib/vfs/path.c" /* for testing static methods */
  26. #include "src/vfs/local/local.c"
  27. struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
  28. struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  29. static void
  30. setup (void)
  31. {
  32. str_init_strings (NULL);
  33. vfs_init ();
  34. init_localfs ();
  35. vfs_setup_work_dir ();
  36. test_subclass1.flags = VFS_S_REMOTE;
  37. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  38. vfs_test_ops1.name = "testfs1";
  39. vfs_test_ops1.flags = VFSF_NOLINKS;
  40. vfs_test_ops1.prefix = "test1:";
  41. vfs_register_class (&vfs_test_ops1);
  42. vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
  43. vfs_test_ops2.name = "testfs2";
  44. vfs_test_ops2.prefix = "test2:";
  45. vfs_register_class (&vfs_test_ops2);
  46. vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
  47. vfs_test_ops3.name = "testfs3";
  48. vfs_test_ops3.prefix = "test3:";
  49. vfs_register_class (&vfs_test_ops3);
  50. }
  51. static void
  52. teardown (void)
  53. {
  54. vfs_shut ();
  55. str_uninit_strings ();
  56. }
  57. /* --------------------------------------------------------------------------------------------- */
  58. START_TEST (test_vfs_split)
  59. {
  60. char *path;
  61. const char *local, *op, *etalon_path, *etalon_local, *etalon_op;
  62. struct vfs_class *result;
  63. path = g_strdup("#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2/#test3:/qqq/www/eee.rr");
  64. etalon_path = "#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2/";
  65. etalon_local = "qqq/www/eee.rr";
  66. etalon_op = "test3:";
  67. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  68. fail_unless(result == &vfs_test_ops3, "Result(%p) doesn't match to vfs_test_ops3(%p)", result, &vfs_test_ops3);
  69. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  70. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  71. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  72. etalon_path = "#test1:/bla-bla/some/path/";
  73. etalon_local = "bla-bla/some/path2/";
  74. etalon_op = "test2:";
  75. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  76. fail_unless(result == &vfs_test_ops2, "Result(%p) doesn't match to vfs_test_ops2(%p)", result, &vfs_test_ops2);
  77. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  78. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  79. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  80. etalon_path = "";
  81. etalon_local = "bla-bla/some/path/";
  82. etalon_op = "test1:";
  83. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  84. fail_unless(result == &vfs_test_ops1, "Result(%p) doesn't match to vfs_test_ops1(%p)", result, &vfs_test_ops2);
  85. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  86. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  87. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  88. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  89. fail_unless(result == NULL, "Result(%p) doesn't match to vfs_test_ops1(NULL)", result);
  90. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  91. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  92. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  93. g_free(path);
  94. }
  95. END_TEST
  96. /* --------------------------------------------------------------------------------------------- */
  97. START_TEST (test_vfs_split_with_local)
  98. {
  99. char *path;
  100. const char *local, *op, *etalon_path, *etalon_local, *etalon_op;
  101. struct vfs_class *result;
  102. path = g_strdup("/local/path/#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2#test3:/qqq/www/eee.rr");
  103. etalon_path = "/local/path/#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2";
  104. etalon_local = "qqq/www/eee.rr";
  105. etalon_op = "test3:";
  106. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  107. fail_unless(result == &vfs_test_ops3, "Result(%p) doesn't match to vfs_test_ops3(%p)", result, &vfs_test_ops3);
  108. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  109. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  110. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  111. etalon_path = "/local/path/#test1:/bla-bla/some/path/";
  112. etalon_local = "bla-bla/some/path2";
  113. etalon_op = "test2:";
  114. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  115. fail_unless(result == &vfs_test_ops2, "Result(%p) doesn't match to vfs_test_ops2(%p)", result, &vfs_test_ops2);
  116. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  117. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  118. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  119. etalon_path = "/local/path/";
  120. etalon_local = "bla-bla/some/path/";
  121. etalon_op = "test1:";
  122. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  123. fail_unless(result == &vfs_test_ops1, "Result(%p) doesn't match to vfs_test_ops1(%p)", result, &vfs_test_ops2);
  124. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  125. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  126. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  127. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  128. fail_unless(result == NULL, "Result(%p) doesn't match to vfs_test_ops1(NULL)", result);
  129. g_free(path);
  130. }
  131. END_TEST
  132. /* --------------------------------------------------------------------------------------------- */
  133. START_TEST (test_vfs_split_url)
  134. {
  135. char *path;
  136. const char *local, *op, *etalon_path, *etalon_local, *etalon_op;
  137. struct vfs_class *result;
  138. path = g_strdup("#test2:username:passwd@somehost.net/bla-bla/some/path2");
  139. etalon_path = "";
  140. etalon_local = "bla-bla/some/path2";
  141. etalon_op = "test2:username:passwd@somehost.net";
  142. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  143. fail_unless(result == &vfs_test_ops2, "Result(%p) doesn't match to vfs_test_ops2(%p)", result, &vfs_test_ops2);
  144. fail_unless(path != NULL && strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  145. fail_unless(local != NULL && strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  146. fail_unless(op != NULL && strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  147. g_free(path);
  148. }
  149. END_TEST
  150. /* --------------------------------------------------------------------------------------------- */
  151. START_TEST (test_vfs_split_url_with_semi)
  152. {
  153. char *path;
  154. const char *local, *op, *etalon_path, *etalon_local, *etalon_op;
  155. struct vfs_class *result;
  156. path = g_strdup("/local/path/#test1:/bla-bla/some/path/#test2:username:p!a@s#s$w%d@somehost.net/bla-bla/some/path2");
  157. etalon_path = "/local/path/#test1:/bla-bla/some/path/";
  158. etalon_local = "bla-bla/some/path2";
  159. etalon_op = "test2:username:p!a@s#s$w%d@somehost.net";
  160. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  161. fail_unless(result == &vfs_test_ops2, "Result(%p) doesn't match to vfs_test_ops2(%p)", result, &vfs_test_ops2);
  162. fail_unless(path != NULL && strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  163. fail_unless(local != NULL && strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  164. fail_unless(op != NULL && strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  165. g_free(path);
  166. }
  167. END_TEST
  168. /* --------------------------------------------------------------------------------------------- */
  169. START_TEST (test_vfs_split_with_semi_in_path)
  170. {
  171. char *path;
  172. const char *local, *op, *etalon_path, *etalon_local, *etalon_op;
  173. struct vfs_class *result;
  174. path = g_strdup("#test2:/bl#a-bl#a/so#me/pa#th2");
  175. etalon_path = "";
  176. etalon_local = "bl#a-bl#a/so#me/pa#th2";
  177. etalon_op = "test2:";
  178. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  179. fail_unless(result == &vfs_test_ops2, "Result(%p) doesn't match to vfs_test_ops2(%p)", result, &vfs_test_ops2);
  180. fail_unless(path != NULL && strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  181. fail_unless(local != NULL && strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  182. fail_unless(op != NULL && strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  183. g_free(path);
  184. }
  185. END_TEST
  186. /* --------------------------------------------------------------------------------------------- */
  187. int
  188. main (void)
  189. {
  190. int number_failed;
  191. Suite *s = suite_create (TEST_SUITE_NAME);
  192. TCase *tc_core = tcase_create ("Core");
  193. SRunner *sr;
  194. tcase_add_checked_fixture (tc_core, setup, teardown);
  195. /* Add new tests here: *************** */
  196. tcase_add_test (tc_core, test_vfs_split);
  197. tcase_add_test (tc_core, test_vfs_split_with_local);
  198. tcase_add_test (tc_core, test_vfs_split_url);
  199. tcase_add_test (tc_core, test_vfs_split_url_with_semi);
  200. tcase_add_test (tc_core, test_vfs_split_with_semi_in_path);
  201. /* *********************************** */
  202. suite_add_tcase (s, tc_core);
  203. sr = srunner_create (s);
  204. srunner_set_log (sr, "vfs_split.log");
  205. srunner_run_all (sr, CK_NORMAL);
  206. number_failed = srunner_ntests_failed (sr);
  207. srunner_free (sr);
  208. return (number_failed == 0) ? 0 : 1;
  209. }
  210. /* --------------------------------------------------------------------------------------------- */