path_manipulations.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* lib/vfs - test vfs_path_t manipulation functions
  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.c"
  20. #ifndef HAVE_CHARSET
  21. #define HAVE_CHARSET 1
  22. #endif
  23. #include "lib/charsets.h"
  24. #include "lib/strutil.h"
  25. #include "lib/vfs/xdirentry.h"
  26. #include "lib/vfs/path.h"
  27. #include "src/vfs/local/local.c"
  28. struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
  29. struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  30. static void
  31. setup (void)
  32. {
  33. str_init_strings (NULL);
  34. vfs_init ();
  35. init_localfs ();
  36. vfs_setup_work_dir ();
  37. test_subclass1.flags = VFS_S_REMOTE;
  38. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  39. vfs_test_ops1.name = "testfs1";
  40. vfs_test_ops1.flags = VFSF_NOLINKS;
  41. vfs_test_ops1.prefix = "test1";
  42. vfs_register_class (&vfs_test_ops1);
  43. vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
  44. vfs_test_ops2.name = "testfs2";
  45. vfs_test_ops2.prefix = "test2";
  46. vfs_register_class (&vfs_test_ops2);
  47. vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
  48. vfs_test_ops3.name = "testfs3";
  49. vfs_test_ops3.prefix = "test3";
  50. vfs_test_ops3.flags = VFSF_LOCAL;
  51. vfs_register_class (&vfs_test_ops3);
  52. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  53. load_codepages_list ();
  54. }
  55. static void
  56. teardown (void)
  57. {
  58. free_codepages_list ();
  59. vfs_shut ();
  60. str_uninit_strings ();
  61. }
  62. /* --------------------------------------------------------------------------------------------- */
  63. START_TEST (test_vfs_path_tokens_count)
  64. {
  65. size_t tokens_count;
  66. vfs_path_t *vpath;
  67. vpath = vfs_path_from_str ("/");
  68. tokens_count = vfs_path_tokens_count(vpath);
  69. fail_unless (tokens_count == 0, "actual: %zu; expected: 0\n", tokens_count);
  70. vfs_path_free (vpath);
  71. vpath = vfs_path_from_str ("/path");
  72. tokens_count = vfs_path_tokens_count(vpath);
  73. fail_unless (tokens_count == 1, "actual: %zu; expected: 1\n", tokens_count);
  74. vfs_path_free (vpath);
  75. vpath = vfs_path_from_str ("/path1/path2/path3");
  76. tokens_count = vfs_path_tokens_count(vpath);
  77. fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
  78. vfs_path_free (vpath);
  79. vpath = vfs_path_from_str_flags ("test3://path1/path2/path3/path4", VPF_NO_CANON);
  80. tokens_count = vfs_path_tokens_count(vpath);
  81. fail_unless (tokens_count == 4, "actual: %zu; expected: 4\n", tokens_count);
  82. vfs_path_free (vpath);
  83. vpath = vfs_path_from_str_flags ("path1/path2/path3", VPF_NO_CANON);
  84. tokens_count = vfs_path_tokens_count(vpath);
  85. fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
  86. vfs_path_free (vpath);
  87. vpath = vfs_path_from_str ("/path1/path2/path3/");
  88. tokens_count = vfs_path_tokens_count(vpath);
  89. fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
  90. vfs_path_free (vpath);
  91. vpath = vfs_path_from_str ("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/");
  92. tokens_count = vfs_path_tokens_count(vpath);
  93. fail_unless (tokens_count == 5, "actual: %zu; expected: 5\n", tokens_count);
  94. vfs_path_free (vpath);
  95. vpath = vfs_path_from_str (
  96. "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33"
  97. );
  98. tokens_count = vfs_path_tokens_count(vpath);
  99. fail_unless (tokens_count == 11, "actual: %zu; expected: 11\n", tokens_count);
  100. vfs_path_free (vpath);
  101. }
  102. END_TEST
  103. /* --------------------------------------------------------------------------------------------- */
  104. #define check_invalid_token_str(input, start, length) { \
  105. vpath = vfs_path_from_str (input); \
  106. path_tokens = vfs_path_tokens_get(vpath, start, length); \
  107. fail_unless (path_tokens == NULL, "path_tokens should be NULL!\n"); \
  108. g_free (path_tokens); \
  109. vfs_path_free (vpath); \
  110. }
  111. #define check_token_str(input, start, length, etalon) { \
  112. vpath = vfs_path_from_str_flags (input, VPF_NO_CANON); \
  113. path_tokens = vfs_path_tokens_get(vpath, start, length); \
  114. fail_unless (path_tokens != NULL, "path_tokens shouldn't equal to NULL!\n"); \
  115. if (path_tokens != NULL) \
  116. fail_unless (strcmp(path_tokens, etalon) == 0, "\nactual: '%s'\netalon: '%s'", path_tokens, etalon); \
  117. g_free (path_tokens); \
  118. vfs_path_free (vpath); \
  119. }
  120. START_TEST (test_vfs_path_tokens_get)
  121. {
  122. vfs_path_t *vpath;
  123. char *path_tokens;
  124. /* Invalid start position */
  125. check_invalid_token_str ("/" , 2, 1);
  126. /* Invalid negative position */
  127. check_invalid_token_str ("/path" , -3, 1);
  128. /* Count of tokens is zero. Count should be autocorrected */
  129. check_token_str ("/path", 0, 0, "path");
  130. /* get 'path2/path3' by 1,2 */
  131. check_token_str ("/path1/path2/path3/path4", 1, 2, "path2/path3");
  132. /* get 'path2/path3' by 1,2 from LOCAL VFS */
  133. check_token_str ("test3://path1/path2/path3/path4", 1, 2, "path2/path3");
  134. /* get 'path2/path3' by 1,2 from LOCAL VFS with encoding */
  135. check_token_str ("test3://path1/path2/test3://#enc:KOI8-R/path3/path4", 1, 2, "path2/test3://#enc:KOI8-R/path3");
  136. /* get 'path2/path3' by 1,2 with encoding */
  137. check_token_str ("#enc:KOI8-R/path1/path2/path3/path4", 1, 2, "#enc:KOI8-R/path2/path3");
  138. /* get 'path2/path3' by 1,2 from non-LOCAL VFS */
  139. check_token_str ("test2://path1/path2/path3/path4", 1, 2, "test2://path2/path3");
  140. /* get 'path2/path3' by 1,2 throught non-LOCAL VFS */
  141. check_token_str ("/path1/path2/test1://user:pass@some.host:12345/path3/path4", 1, 2, "path2/test1://user:pass@some.host:12345/path3");
  142. /* get 'path2/path3' by 1,2 from LOCAL VFS */
  143. /* TODO: currently this test don't passed. Probably broken string URI parser */
  144. /* check_token_str ("test3://path1/path2/test2://test3://path3/path4", 1, 2, "path2/path3"); */
  145. /* get 'path2/path3' by 1,2 where path2 it's LOCAL VFS */
  146. check_token_str ("test3://path1/path2/test2://path3/path4", 1, 2, "path2/test2://path3");
  147. /* get 'path2/path3' by 1,2 where path3 it's LOCAL VFS */
  148. check_token_str ("test2://path1/path2/test3://path3/path4", 1, 2, "test2://path2/test3://path3");
  149. /* get 'path4' by -1,1 */
  150. check_token_str ("/path1/path2/path3/path4", -1, 1, "path4");
  151. /* get 'path2/path3/path4' by -3,0 */
  152. check_token_str ("/path1/path2/path3/path4", -3, 0, "path2/path3/path4");
  153. }
  154. END_TEST
  155. /* --------------------------------------------------------------------------------------------- */
  156. START_TEST (test_vfs_path_append_vpath)
  157. {
  158. vfs_path_t *vpath1, *vpath2, *vpath3;
  159. vpath1 = vfs_path_from_str("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33");
  160. vpath2 = vfs_path_from_str("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/");
  161. vpath3 = vfs_path_append_vpath_new (vpath1, vpath2, NULL);
  162. fail_unless (vfs_path_elements_count(vpath3) == 6,
  163. "\nvpath elements count should be %d, actial is %d\n",
  164. 6,
  165. vfs_path_elements_count(vpath3)
  166. );
  167. vfs_path_free (vpath1);
  168. vfs_path_free (vpath2);
  169. vfs_path_free (vpath3);
  170. }
  171. END_TEST
  172. /* --------------------------------------------------------------------------------------------- */
  173. START_TEST (test_vfs_path_relative)
  174. {
  175. vfs_path_t *vpath, *copy_vpath;
  176. char *path_str;
  177. vpath = vfs_path_from_str_flags("../bla-bla", VPF_NO_CANON);
  178. fail_unless (vpath->relative, "relative flag fail!\n");
  179. path_str = vfs_path_to_str (vpath);
  180. fail_unless (strcmp(path_str, "../bla-bla") == 0, "relative fail!\nactual: [%s]\n", path_str);
  181. g_free (path_str);
  182. path_str = (char *) vfs_path_get_last_path_str (vpath);
  183. fail_unless (strcmp(path_str, "../bla-bla") == 0, "relative fail!\nactual: element->path=[%s]\n", path_str);
  184. copy_vpath = vfs_path_clone (vpath);
  185. path_str = vfs_path_to_str (copy_vpath);
  186. fail_unless (strcmp(path_str, "../bla-bla") == 0, "relative fail!\nactual: [%s]\n", path_str);
  187. g_free (path_str);
  188. vfs_path_free (copy_vpath);
  189. vfs_path_free (vpath);
  190. vpath = vfs_path_from_str_flags ("../path/test1://user:pass@some.host:12345/bla-bla/some/path/", VPF_NO_CANON);
  191. path_str = vfs_path_to_str (vpath);
  192. fail_unless (strcmp(path_str, "../path/test1://user:pass@some.host:12345/bla-bla/some/path/") == 0, "relative fail!\nactual: [%s]\n", path_str);
  193. g_free (path_str);
  194. vfs_path_free (vpath);
  195. }
  196. END_TEST
  197. /* --------------------------------------------------------------------------------------------- */
  198. int
  199. main (void)
  200. {
  201. int number_failed;
  202. Suite *s = suite_create (TEST_SUITE_NAME);
  203. TCase *tc_core = tcase_create ("Core");
  204. SRunner *sr;
  205. tcase_add_checked_fixture (tc_core, setup, teardown);
  206. /* Add new tests here: *************** */
  207. tcase_add_test (tc_core, test_vfs_path_tokens_count);
  208. tcase_add_test (tc_core, test_vfs_path_tokens_get);
  209. tcase_add_test (tc_core, test_vfs_path_append_vpath);
  210. tcase_add_test (tc_core, test_vfs_path_relative);
  211. /* *********************************** */
  212. suite_add_tcase (s, tc_core);
  213. sr = srunner_create (s);
  214. srunner_set_log (sr, "path_manipulations.log");
  215. srunner_run_all (sr, CK_NORMAL);
  216. number_failed = srunner_ntests_failed (sr);
  217. srunner_free (sr);
  218. return (number_failed == 0) ? 0 : 1;
  219. }
  220. /* --------------------------------------------------------------------------------------------- */