vfs_path_string_convert.c 11 KB

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