vfs_path_string_convert.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. lib/vfs - get vfs_path_t from string
  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.c"
  23. #ifdef HAVE_CHARSET
  24. #include "lib/charsets.h"
  25. #endif
  26. #include "lib/strutil.h"
  27. #include "lib/vfs/xdirentry.h"
  28. #include "lib/vfs/path.c" /* for testing static methods */
  29. #include "src/vfs/local/local.c"
  30. struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
  31. struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  32. static void
  33. setup (void)
  34. {
  35. str_init_strings (NULL);
  36. vfs_init ();
  37. init_localfs ();
  38. vfs_setup_work_dir ();
  39. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  40. vfs_test_ops1.name = "testfs1";
  41. vfs_test_ops1.flags = VFSF_NOLINKS;
  42. vfs_test_ops1.prefix = "test1";
  43. vfs_register_class (&vfs_test_ops1);
  44. test_subclass2.flags = VFS_S_REMOTE;
  45. vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
  46. vfs_test_ops2.name = "testfs2";
  47. vfs_test_ops2.prefix = "test2";
  48. vfs_register_class (&vfs_test_ops2);
  49. vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
  50. vfs_test_ops3.name = "testfs3";
  51. vfs_test_ops3.prefix = "test3";
  52. vfs_register_class (&vfs_test_ops3);
  53. }
  54. static void
  55. teardown (void)
  56. {
  57. vfs_shut ();
  58. str_uninit_strings ();
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. #define ETALON_PATH_STR "/#test1/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
  62. #define ETALON_PATH_URL_STR "/test1://bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
  63. START_TEST (test_vfs_path_from_to_string)
  64. {
  65. vfs_path_t *vpath;
  66. size_t vpath_len;
  67. char *result;
  68. vpath = vfs_path_from_str_flags (ETALON_PATH_STR, VPF_USE_DEPRECATED_PARSER);
  69. vpath_len = vfs_path_elements_count(vpath);
  70. fail_unless(vpath_len == 4, "vpath length should be 4 (actial: %d)",vpath_len);
  71. result = vfs_path_to_str(vpath);
  72. fail_unless(strcmp(ETALON_PATH_URL_STR, result) == 0, "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR, result);
  73. g_free(result);
  74. vfs_path_free(vpath);
  75. }
  76. END_TEST
  77. /* --------------------------------------------------------------------------------------------- */
  78. START_TEST (test_vfs_path_from_to_string2)
  79. {
  80. vfs_path_t *vpath;
  81. size_t vpath_len;
  82. char *result;
  83. const vfs_path_element_t *path_element;
  84. vpath = vfs_path_from_str ("/");
  85. vpath_len = vfs_path_elements_count(vpath);
  86. fail_unless(vpath_len == 1, "vpath length should be 1 (actial: %d)",vpath_len);
  87. result = vfs_path_to_str(vpath);
  88. fail_unless(strcmp("/", result) == 0, "expected(%s) doesn't equal to actual(%s)", "/", result);
  89. g_free(result);
  90. path_element = vfs_path_get_by_index (vpath, -1);
  91. fail_unless(strcmp("/", path_element->path) == 0, "expected(%s) doesn't equal to actual(%s)", "/", path_element->path);
  92. fail_unless(path_element->class == &vfs_local_ops , "actual vfs-class doesn't equal to localfs");
  93. vfs_path_free(vpath);
  94. }
  95. END_TEST
  96. /* --------------------------------------------------------------------------------------------- */
  97. START_TEST (test_vfs_path_from_to_partial_string_by_class)
  98. {
  99. vfs_path_t *vpath;
  100. char *result;
  101. vpath = vfs_path_from_str_flags (ETALON_PATH_STR, VPF_USE_DEPRECATED_PARSER);
  102. result = vfs_path_to_str_elements_count(vpath, -1);
  103. fail_unless(
  104. strcmp("/test1://bla-bla/some/path/test2://bla-bla/some/path", result) == 0,
  105. "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/test2://bla-bla/some/path", result);
  106. g_free(result);
  107. result = vfs_path_to_str_elements_count(vpath, -2);
  108. fail_unless(
  109. strcmp("/test1://bla-bla/some/path/", result) == 0,
  110. "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/", result);
  111. g_free(result);
  112. result = vfs_path_to_str_elements_count(vpath, -3);
  113. fail_unless(
  114. strcmp("/", result) == 0,
  115. "expected(%s) doesn't equal to actual(%s)", "/", result);
  116. g_free(result);
  117. /* index out of bound*/
  118. result = vfs_path_to_str_elements_count(vpath, -4);
  119. fail_unless(
  120. strcmp("", result) == 0,
  121. "expected(%s) doesn't equal to actual(%s)", "", result);
  122. g_free(result);
  123. result = vfs_path_to_str_elements_count(vpath, 1);
  124. fail_unless(
  125. strcmp("/", result) == 0,
  126. "expected(%s) doesn't equal to actual(%s)", "/", result);
  127. g_free(result);
  128. result = vfs_path_to_str_elements_count(vpath, 2);
  129. fail_unless(
  130. strcmp("/test1://bla-bla/some/path/", result) == 0,
  131. "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/", result);
  132. g_free(result);
  133. result = vfs_path_to_str_elements_count(vpath, 3);
  134. fail_unless(
  135. strcmp("/test1://bla-bla/some/path/test2://bla-bla/some/path", result) == 0,
  136. "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/test2://bla-bla/some/path", result);
  137. g_free(result);
  138. result = vfs_path_to_str_elements_count(vpath, 4);
  139. fail_unless(
  140. strcmp(ETALON_PATH_URL_STR, result) == 0,
  141. "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR, result);
  142. g_free(result);
  143. /* index out of bound*/
  144. result = vfs_path_to_str_elements_count(vpath, 5);
  145. fail_unless(
  146. strcmp(ETALON_PATH_URL_STR, result) == 0,
  147. "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR, result);
  148. g_free(result);
  149. vfs_path_free(vpath);
  150. }
  151. END_TEST
  152. /* --------------------------------------------------------------------------------------------- */
  153. #ifdef HAVE_CHARSET
  154. #define encoding_check( input , etalon ) \
  155. { \
  156. vfs_path_t *vpath; \
  157. char *result; \
  158. \
  159. vpath = vfs_path_from_str_flags (input, VPF_USE_DEPRECATED_PARSER); \
  160. result = vfs_path_to_str(vpath); \
  161. fail_unless( result != NULL && strcmp(result, etalon) ==0, \
  162. "\ninput : %s\nactual: %s\netalon: %s", input, result , etalon ); \
  163. \
  164. g_free(result); \
  165. vfs_path_free(vpath); \
  166. }
  167. START_TEST (test_vfs_path_from_to_string_encoding)
  168. {
  169. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  170. load_codepages_list ();
  171. encoding_check (
  172. "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  173. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
  174. );
  175. encoding_check (
  176. "/#test1/bla-bla1/#enc:IBM866/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  177. "/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
  178. );
  179. encoding_check (
  180. "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/#enc:KOI8-R/some/path#test3/111/22/33",
  181. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
  182. );
  183. encoding_check (
  184. "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/some/#enc:KOI8-R/path#test3/111/22/33",
  185. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
  186. );
  187. encoding_check (
  188. "/#test1/bla-bla1/some/path/#test2/#enc:IBM866/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  189. "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
  190. );
  191. encoding_check (
  192. "/#test1/bla-bla1/some/path/#enc:IBM866/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
  193. "/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33"
  194. );
  195. free_codepages_list ();
  196. }
  197. END_TEST
  198. /* --------------------------------------------------------------------------------------------- */
  199. #define ETALON_STR "/path/to/file.ext/test1://#enc:KOI8-R"
  200. START_TEST (test_vfs_path_encoding_at_end)
  201. {
  202. vfs_path_t *vpath;
  203. char *result;
  204. const vfs_path_element_t *element;
  205. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  206. load_codepages_list ();
  207. vpath = vfs_path_from_str_flags ("/path/to/file.ext#test1:/#enc:KOI8-R", VPF_USE_DEPRECATED_PARSER);
  208. result = vfs_path_to_str(vpath);
  209. element = vfs_path_get_by_index(vpath, -1);
  210. fail_unless(*element->path == '\0', "element->path should be empty, but actual value is '%s'",element->path);
  211. fail_unless(element->encoding != NULL && strcmp(element->encoding, "KOI8-R") == 0,
  212. "element->encoding should be 'KOI8-R', but actual value is '%s'",element->encoding);
  213. fail_unless( result != NULL && strcmp(result, ETALON_STR) ==0,
  214. "\nactual: %s\netalon: %s", result , ETALON_STR );
  215. g_free(result);
  216. vfs_path_free(vpath);
  217. free_codepages_list ();
  218. }
  219. END_TEST
  220. #endif /* HAVE_CHARSET */
  221. /* --------------------------------------------------------------------------------------------- */
  222. #undef ETALON_PATH_STR
  223. #define ETALON_PATH_STR "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33"
  224. START_TEST (test_vfs_path_from_to_string_uri)
  225. {
  226. vfs_path_t *vpath;
  227. size_t vpath_len;
  228. char *result;
  229. vpath = vfs_path_from_str (ETALON_PATH_STR);
  230. vpath_len = vfs_path_elements_count(vpath);
  231. fail_unless(vpath_len == 4, "vpath length should be 4 (actial: %d)",vpath_len);
  232. result = vfs_path_to_str(vpath);
  233. fail_unless(strcmp(ETALON_PATH_STR, result) == 0, "\nexpected(%s)\ndoesn't equal to actual(%s)", ETALON_PATH_STR, result);
  234. g_free(result);
  235. vfs_path_free(vpath);
  236. }
  237. END_TEST
  238. /* --------------------------------------------------------------------------------------------- */
  239. int
  240. main (void)
  241. {
  242. int number_failed;
  243. Suite *s = suite_create (TEST_SUITE_NAME);
  244. TCase *tc_core = tcase_create ("Core");
  245. SRunner *sr;
  246. tcase_add_checked_fixture (tc_core, setup, teardown);
  247. /* Add new tests here: *************** */
  248. tcase_add_test (tc_core, test_vfs_path_from_to_string);
  249. tcase_add_test (tc_core, test_vfs_path_from_to_string2);
  250. tcase_add_test (tc_core, test_vfs_path_from_to_partial_string_by_class);
  251. #ifdef HAVE_CHARSET
  252. tcase_add_test (tc_core, test_vfs_path_from_to_string_encoding);
  253. tcase_add_test (tc_core, test_vfs_path_encoding_at_end);
  254. #endif
  255. tcase_add_test (tc_core, test_vfs_path_from_to_string_uri);
  256. /* *********************************** */
  257. suite_add_tcase (s, tc_core);
  258. sr = srunner_create (s);
  259. srunner_set_log (sr, "vfs_path_string_convert.log");
  260. srunner_run_all (sr, CK_NORMAL);
  261. number_failed = srunner_ntests_failed (sr);
  262. srunner_free (sr);
  263. return (number_failed == 0) ? 0 : 1;
  264. }
  265. /* --------------------------------------------------------------------------------------------- */