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. #ifndef HAVE_CHARSET
  24. #define HAVE_CHARSET 1
  25. #endif
  26. #include "lib/charsets.h"
  27. #include "lib/strutil.h"
  28. #include "lib/vfs/xdirentry.h"
  29. #include "lib/vfs/path.c" /* for testing static methods */
  30. #include "src/vfs/local/local.c"
  31. struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
  32. struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  33. static void
  34. setup (void)
  35. {
  36. str_init_strings (NULL);
  37. vfs_init ();
  38. init_localfs ();
  39. vfs_setup_work_dir ();
  40. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  41. vfs_test_ops1.name = "testfs1";
  42. vfs_test_ops1.flags = VFSF_NOLINKS;
  43. vfs_test_ops1.prefix = "test1";
  44. vfs_register_class (&vfs_test_ops1);
  45. test_subclass2.flags = VFS_S_REMOTE;
  46. vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
  47. vfs_test_ops2.name = "testfs2";
  48. vfs_test_ops2.prefix = "test2";
  49. vfs_register_class (&vfs_test_ops2);
  50. vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
  51. vfs_test_ops3.name = "testfs3";
  52. vfs_test_ops3.prefix = "test3";
  53. vfs_register_class (&vfs_test_ops3);
  54. }
  55. static void
  56. teardown (void)
  57. {
  58. vfs_shut ();
  59. str_uninit_strings ();
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  62. #define ETALON_PATH_STR "/#test1/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
  63. #define ETALON_PATH_URL_STR "/test1://bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
  64. START_TEST (test_vfs_path_from_to_string)
  65. {
  66. vfs_path_t *vpath;
  67. size_t vpath_len;
  68. char *result;
  69. vpath = vfs_path_from_str_flags (ETALON_PATH_STR, VPF_USE_DEPRECATED_PARSER);
  70. vpath_len = vfs_path_elements_count(vpath);
  71. fail_unless(vpath_len == 4, "vpath length should be 4 (actial: %d)",vpath_len);
  72. result = vfs_path_to_str(vpath);
  73. fail_unless(strcmp(ETALON_PATH_URL_STR, result) == 0, "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR, result);
  74. g_free(result);
  75. vfs_path_free(vpath);
  76. }
  77. END_TEST
  78. /* --------------------------------------------------------------------------------------------- */
  79. START_TEST (test_vfs_path_from_to_string2)
  80. {
  81. vfs_path_t *vpath;
  82. size_t vpath_len;
  83. char *result;
  84. vfs_path_element_t *path_element;
  85. vpath = vfs_path_from_str ("/");
  86. vpath_len = vfs_path_elements_count(vpath);
  87. fail_unless(vpath_len == 1, "vpath length should be 1 (actial: %d)",vpath_len);
  88. result = vfs_path_to_str(vpath);
  89. fail_unless(strcmp("/", result) == 0, "expected(%s) doesn't equal to actual(%s)", "/", result);
  90. g_free(result);
  91. path_element = vfs_path_get_by_index (vpath, -1);
  92. fail_unless(strcmp("/", path_element->path) == 0, "expected(%s) doesn't equal to actual(%s)", "/", path_element->path);
  93. fail_unless(path_element->class == &vfs_local_ops , "actual vfs-class doesn't equal to localfs");
  94. vfs_path_free(vpath);
  95. }
  96. END_TEST
  97. /* --------------------------------------------------------------------------------------------- */
  98. START_TEST (test_vfs_path_from_to_partial_string_by_class)
  99. {
  100. vfs_path_t *vpath;
  101. char *result;
  102. vpath = vfs_path_from_str_flags (ETALON_PATH_STR, VPF_USE_DEPRECATED_PARSER);
  103. result = vfs_path_to_str_elements_count(vpath, -1);
  104. fail_unless(
  105. strcmp("/test1://bla-bla/some/path/test2://bla-bla/some/path", result) == 0,
  106. "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/test2://bla-bla/some/path", result);
  107. g_free(result);
  108. result = vfs_path_to_str_elements_count(vpath, -2);
  109. fail_unless(
  110. strcmp("/test1://bla-bla/some/path/", result) == 0,
  111. "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/", result);
  112. g_free(result);
  113. result = vfs_path_to_str_elements_count(vpath, -3);
  114. fail_unless(
  115. strcmp("/", result) == 0,
  116. "expected(%s) doesn't equal to actual(%s)", "/", result);
  117. g_free(result);
  118. /* index out of bound*/
  119. result = vfs_path_to_str_elements_count(vpath, -4);
  120. fail_unless(
  121. strcmp("", result) == 0,
  122. "expected(%s) doesn't equal to actual(%s)", "", result);
  123. g_free(result);
  124. result = vfs_path_to_str_elements_count(vpath, 1);
  125. fail_unless(
  126. strcmp("/", result) == 0,
  127. "expected(%s) doesn't equal to actual(%s)", "/", result);
  128. g_free(result);
  129. result = vfs_path_to_str_elements_count(vpath, 2);
  130. fail_unless(
  131. strcmp("/test1://bla-bla/some/path/", result) == 0,
  132. "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/", result);
  133. g_free(result);
  134. result = vfs_path_to_str_elements_count(vpath, 3);
  135. fail_unless(
  136. strcmp("/test1://bla-bla/some/path/test2://bla-bla/some/path", result) == 0,
  137. "expected(%s) doesn't equal to actual(%s)", "/test1://bla-bla/some/path/test2://bla-bla/some/path", result);
  138. g_free(result);
  139. result = vfs_path_to_str_elements_count(vpath, 4);
  140. fail_unless(
  141. strcmp(ETALON_PATH_URL_STR, result) == 0,
  142. "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR, result);
  143. g_free(result);
  144. /* index out of bound*/
  145. result = vfs_path_to_str_elements_count(vpath, 5);
  146. fail_unless(
  147. strcmp(ETALON_PATH_URL_STR, result) == 0,
  148. "expected(%s) doesn't equal to actual(%s)", ETALON_PATH_URL_STR, result);
  149. g_free(result);
  150. vfs_path_free(vpath);
  151. }
  152. END_TEST
  153. /* --------------------------------------------------------------------------------------------- */
  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. 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. /* --------------------------------------------------------------------------------------------- */
  221. #undef ETALON_PATH_STR
  222. #define ETALON_PATH_STR "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33"
  223. START_TEST (test_vfs_path_from_to_string_uri)
  224. {
  225. vfs_path_t *vpath;
  226. size_t vpath_len;
  227. char *result;
  228. vpath = vfs_path_from_str (ETALON_PATH_STR);
  229. vpath_len = vfs_path_elements_count(vpath);
  230. fail_unless(vpath_len == 4, "vpath length should be 4 (actial: %d)",vpath_len);
  231. result = vfs_path_to_str(vpath);
  232. fail_unless(strcmp(ETALON_PATH_STR, result) == 0, "\nexpected(%s)\ndoesn't equal to actual(%s)", ETALON_PATH_STR, result);
  233. g_free(result);
  234. vfs_path_free(vpath);
  235. }
  236. END_TEST
  237. /* --------------------------------------------------------------------------------------------- */
  238. int
  239. main (void)
  240. {
  241. int number_failed;
  242. Suite *s = suite_create (TEST_SUITE_NAME);
  243. TCase *tc_core = tcase_create ("Core");
  244. SRunner *sr;
  245. tcase_add_checked_fixture (tc_core, setup, teardown);
  246. /* Add new tests here: *************** */
  247. tcase_add_test (tc_core, test_vfs_path_from_to_string);
  248. tcase_add_test (tc_core, test_vfs_path_from_to_string2);
  249. tcase_add_test (tc_core, test_vfs_path_from_to_partial_string_by_class);
  250. tcase_add_test (tc_core, test_vfs_path_from_to_string_encoding);
  251. tcase_add_test (tc_core, test_vfs_path_encoding_at_end);
  252. tcase_add_test (tc_core, test_vfs_path_from_to_string_uri);
  253. /* *********************************** */
  254. suite_add_tcase (s, tc_core);
  255. sr = srunner_create (s);
  256. srunner_set_log (sr, "vfs_path_string_convert.log");
  257. srunner_run_all (sr, CK_NORMAL);
  258. number_failed = srunner_ntests_failed (sr);
  259. srunner_free (sr);
  260. return (number_failed == 0) ? 0 : 1;
  261. }
  262. /* --------------------------------------------------------------------------------------------- */