path_manipulations.c 10 KB

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