vfs_split.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* lib/vfs - test vfs_split() functionality
  2. Copyright (C) 2011 Free Software Foundation, Inc.
  3. Written by:
  4. Slava Zanko <slavazanko@gmail.com>, 2011
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License
  7. as published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #define TEST_SUITE_NAME "/lib/vfs"
  18. #include <check.h>
  19. #include "lib/global.h"
  20. #include "lib/strutil.h"
  21. #include "lib/vfs/xdirentry.h"
  22. #include "lib/vfs/path.c" /* for testing static methods */
  23. #include "src/vfs/local/local.c"
  24. struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
  25. struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  26. static void
  27. setup (void)
  28. {
  29. str_init_strings (NULL);
  30. vfs_init ();
  31. init_localfs ();
  32. vfs_setup_work_dir ();
  33. test_subclass1.flags = VFS_S_REMOTE;
  34. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  35. vfs_test_ops1.name = "testfs1";
  36. vfs_test_ops1.flags = VFSF_NOLINKS;
  37. vfs_test_ops1.prefix = "test1:";
  38. vfs_register_class (&vfs_test_ops1);
  39. vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
  40. vfs_test_ops2.name = "testfs2";
  41. vfs_test_ops2.prefix = "test2:";
  42. vfs_register_class (&vfs_test_ops2);
  43. vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
  44. vfs_test_ops3.name = "testfs3";
  45. vfs_test_ops3.prefix = "test3:";
  46. vfs_register_class (&vfs_test_ops3);
  47. }
  48. static void
  49. teardown (void)
  50. {
  51. vfs_shut ();
  52. str_uninit_strings ();
  53. }
  54. /* --------------------------------------------------------------------------------------------- */
  55. START_TEST (test_vfs_split)
  56. {
  57. char *path;
  58. const char *local, *op, *etalon_path, *etalon_local, *etalon_op;
  59. struct vfs_class *result;
  60. path = g_strdup("#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2/#test3:/qqq/www/eee.rr");
  61. etalon_path = "#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2/";
  62. etalon_local = "qqq/www/eee.rr";
  63. etalon_op = "test3:";
  64. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  65. fail_unless(result == &vfs_test_ops3, "Result(%p) doesn't match to vfs_test_ops3(%p)", result, &vfs_test_ops3);
  66. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  67. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  68. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  69. etalon_path = "#test1:/bla-bla/some/path/";
  70. etalon_local = "bla-bla/some/path2/";
  71. etalon_op = "test2:";
  72. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  73. fail_unless(result == &vfs_test_ops2, "Result(%p) doesn't match to vfs_test_ops2(%p)", result, &vfs_test_ops2);
  74. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  75. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  76. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  77. etalon_path = "";
  78. etalon_local = "bla-bla/some/path/";
  79. etalon_op = "test1:";
  80. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  81. fail_unless(result == &vfs_test_ops1, "Result(%p) doesn't match to vfs_test_ops1(%p)", result, &vfs_test_ops2);
  82. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  83. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  84. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  85. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  86. fail_unless(result == NULL, "Result(%p) doesn't match to vfs_test_ops1(NULL)", result);
  87. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  88. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  89. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  90. g_free(path);
  91. }
  92. END_TEST
  93. /* --------------------------------------------------------------------------------------------- */
  94. START_TEST (test_vfs_split_with_local)
  95. {
  96. char *path;
  97. const char *local, *op, *etalon_path, *etalon_local, *etalon_op;
  98. struct vfs_class *result;
  99. path = g_strdup("/local/path/#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2#test3:/qqq/www/eee.rr");
  100. etalon_path = "/local/path/#test1:/bla-bla/some/path/#test2:/bla-bla/some/path2";
  101. etalon_local = "qqq/www/eee.rr";
  102. etalon_op = "test3:";
  103. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  104. fail_unless(result == &vfs_test_ops3, "Result(%p) doesn't match to vfs_test_ops3(%p)", result, &vfs_test_ops3);
  105. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  106. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  107. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  108. etalon_path = "/local/path/#test1:/bla-bla/some/path/";
  109. etalon_local = "bla-bla/some/path2";
  110. etalon_op = "test2:";
  111. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  112. fail_unless(result == &vfs_test_ops2, "Result(%p) doesn't match to vfs_test_ops2(%p)", result, &vfs_test_ops2);
  113. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  114. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  115. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  116. etalon_path = "/local/path/";
  117. etalon_local = "bla-bla/some/path/";
  118. etalon_op = "test1:";
  119. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  120. fail_unless(result == &vfs_test_ops1, "Result(%p) doesn't match to vfs_test_ops1(%p)", result, &vfs_test_ops2);
  121. fail_unless(strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  122. fail_unless(strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  123. fail_unless(strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  124. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  125. fail_unless(result == NULL, "Result(%p) doesn't match to vfs_test_ops1(NULL)", result);
  126. g_free(path);
  127. }
  128. END_TEST
  129. /* --------------------------------------------------------------------------------------------- */
  130. START_TEST (test_vfs_split_url)
  131. {
  132. char *path;
  133. const char *local, *op, *etalon_path, *etalon_local, *etalon_op;
  134. struct vfs_class *result;
  135. path = g_strdup("#test2:username:passwd@somehost.net/bla-bla/some/path2");
  136. etalon_path = "";
  137. etalon_local = "bla-bla/some/path2";
  138. etalon_op = "test2:username:passwd@somehost.net";
  139. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  140. fail_unless(result == &vfs_test_ops2, "Result(%p) doesn't match to vfs_test_ops2(%p)", result, &vfs_test_ops2);
  141. fail_unless(path != NULL && strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  142. fail_unless(local != NULL && strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  143. fail_unless(op != NULL && strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  144. g_free(path);
  145. }
  146. END_TEST
  147. /* --------------------------------------------------------------------------------------------- */
  148. START_TEST (test_vfs_split_url_with_semi)
  149. {
  150. char *path;
  151. const char *local, *op, *etalon_path, *etalon_local, *etalon_op;
  152. struct vfs_class *result;
  153. path = g_strdup("/local/path/#test1:/bla-bla/some/path/#test2:username:p!a@s#s$w%d@somehost.net/bla-bla/some/path2");
  154. etalon_path = "/local/path/#test1:/bla-bla/some/path/";
  155. etalon_local = "bla-bla/some/path2";
  156. etalon_op = "test2:username:p!a@s#s$w%d@somehost.net";
  157. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  158. fail_unless(result == &vfs_test_ops2, "Result(%p) doesn't match to vfs_test_ops2(%p)", result, &vfs_test_ops2);
  159. fail_unless(path != NULL && strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  160. fail_unless(local != NULL && strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  161. fail_unless(op != NULL && strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  162. g_free(path);
  163. }
  164. END_TEST
  165. /* --------------------------------------------------------------------------------------------- */
  166. START_TEST (test_vfs_split_with_semi_in_path)
  167. {
  168. char *path;
  169. const char *local, *op, *etalon_path, *etalon_local, *etalon_op;
  170. struct vfs_class *result;
  171. path = g_strdup("#test2:/bl#a-bl#a/so#me/pa#th2");
  172. etalon_path = "";
  173. etalon_local = "bl#a-bl#a/so#me/pa#th2";
  174. etalon_op = "test2:";
  175. result = _vfs_split_with_semi_skip_count (path, &local, &op, 0);
  176. fail_unless(result == &vfs_test_ops2, "Result(%p) doesn't match to vfs_test_ops2(%p)", result, &vfs_test_ops2);
  177. fail_unless(path != NULL && strcmp (path, etalon_path) == 0, "path('%s') doesn't match to '%s'", path, etalon_path);
  178. fail_unless(local != NULL && strcmp (local, etalon_local) == 0, "parsed local path('%s') doesn't match to '%s'", local, etalon_local);
  179. fail_unless(op != NULL && strcmp (op, etalon_op) == 0, "parsed VFS name ('%s') doesn't match to '%s'", op, etalon_op);
  180. g_free(path);
  181. }
  182. END_TEST
  183. /* --------------------------------------------------------------------------------------------- */
  184. int
  185. main (void)
  186. {
  187. int number_failed;
  188. Suite *s = suite_create (TEST_SUITE_NAME);
  189. TCase *tc_core = tcase_create ("Core");
  190. SRunner *sr;
  191. tcase_add_checked_fixture (tc_core, setup, teardown);
  192. /* Add new tests here: *************** */
  193. tcase_add_test (tc_core, test_vfs_split);
  194. tcase_add_test (tc_core, test_vfs_split_with_local);
  195. tcase_add_test (tc_core, test_vfs_split_url);
  196. tcase_add_test (tc_core, test_vfs_split_url_with_semi);
  197. tcase_add_test (tc_core, test_vfs_split_with_semi_in_path);
  198. /* *********************************** */
  199. suite_add_tcase (s, tc_core);
  200. sr = srunner_create (s);
  201. srunner_set_log (sr, "vfs_split.log");
  202. srunner_run_all (sr, CK_NORMAL);
  203. number_failed = srunner_ntests_failed (sr);
  204. srunner_free (sr);
  205. return (number_failed == 0) ? 0 : 1;
  206. }
  207. /* --------------------------------------------------------------------------------------------- */