path_serialize.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. lib/vfs - vfs_path_t serialize/deserialize functions
  3. Copyright (C) 2011, 2013
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  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 "tests/mctest.h"
  21. #ifdef HAVE_CHARSET
  22. #include "lib/charsets.h"
  23. #endif
  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. /* --------------------------------------------------------------------------------------------- */
  31. /* @Before */
  32. static void
  33. setup (void)
  34. {
  35. str_init_strings (NULL);
  36. vfs_init ();
  37. init_localfs ();
  38. vfs_setup_work_dir ();
  39. test_subclass1.flags = VFS_S_REMOTE;
  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. 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. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  54. #ifdef HAVE_CHARSET
  55. load_codepages_list ();
  56. #endif
  57. }
  58. /* --------------------------------------------------------------------------------------------- */
  59. /* @After */
  60. static void
  61. teardown (void)
  62. {
  63. #ifdef HAVE_CHARSET
  64. free_codepages_list ();
  65. #endif
  66. vfs_shut ();
  67. str_uninit_strings ();
  68. }
  69. /* --------------------------------------------------------------------------------------------- */
  70. #ifdef HAVE_CHARSET
  71. #define ETALON_PATH_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"
  72. #define ETALON_PATH_URL_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"
  73. #define ETALON_SERIALIZED_PATH \
  74. "g14:path-element-0" \
  75. "p4:pathv12:/local/path/" \
  76. "p10:class-namev7:localfs" \
  77. "g14:path-element-1" \
  78. "p4:pathv18:bla-bla/some/path/" \
  79. "p10:class-namev7:testfs1" \
  80. "p10:vfs_prefixv5:test1" \
  81. "p4:userv4:user" \
  82. "p8:passwordv4:pass" \
  83. "p4:hostv9:some.host" \
  84. "p4:portv5:12345" \
  85. "g14:path-element-2" \
  86. "p4:pathv17:bla-bla/some/path" \
  87. "p10:class-namev7:testfs2" \
  88. "p8:encodingv6:KOI8-R" \
  89. "p10:vfs_prefixv5:test2" \
  90. "g14:path-element-3" \
  91. "p4:pathv9:111/22/33" \
  92. "p10:class-namev7:testfs3" \
  93. "p10:vfs_prefixv5:test3"
  94. #else
  95. #define ETALON_PATH_STR "/local/path/#test1:user:pass@some.host:12345/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
  96. #define ETALON_PATH_URL_STR "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
  97. #define ETALON_SERIALIZED_PATH \
  98. "g14:path-element-0" \
  99. "p4:pathv12:/local/path/" \
  100. "p10:class-namev7:localfs" \
  101. "g14:path-element-1" \
  102. "p4:pathv18:bla-bla/some/path/" \
  103. "p10:class-namev7:testfs1" \
  104. "p10:vfs_prefixv5:test1" \
  105. "p4:userv4:user" \
  106. "p8:passwordv4:pass" \
  107. "p4:hostv9:some.host" \
  108. "p4:portv5:12345" \
  109. "g14:path-element-2" \
  110. "p4:pathv17:bla-bla/some/path" \
  111. "p10:class-namev7:testfs2" \
  112. "p10:vfs_prefixv5:test2" \
  113. "g14:path-element-3" \
  114. "p4:pathv9:111/22/33" \
  115. "p10:class-namev7:testfs3" \
  116. "p10:vfs_prefixv5:test3"
  117. #endif
  118. /* *INDENT-OFF* */
  119. START_TEST (test_path_serialize)
  120. /* *INDENT-ON* */
  121. {
  122. /* given */
  123. vfs_path_t *vpath;
  124. char *serialized_vpath;
  125. GError *error = NULL;
  126. /* when */
  127. vpath = vfs_path_from_str_flags (ETALON_PATH_STR, VPF_USE_DEPRECATED_PARSER);
  128. serialized_vpath = vfs_path_serialize (vpath, &error);
  129. vfs_path_free (vpath);
  130. /* then */
  131. mctest_assert_ptr_ne (serialized_vpath, NULL);
  132. mctest_assert_str_eq (serialized_vpath, ETALON_SERIALIZED_PATH);
  133. }
  134. /* *INDENT-OFF* */
  135. END_TEST
  136. /* *INDENT-ON* */
  137. /* --------------------------------------------------------------------------------------------- */
  138. /* *INDENT-OFF* */
  139. START_TEST (test_path_deserialize)
  140. /* *INDENT-ON* */
  141. {
  142. /* given */
  143. vfs_path_t *vpath;
  144. GError *error = NULL;
  145. /* when */
  146. vpath = vfs_path_deserialize (ETALON_SERIALIZED_PATH, &error);
  147. /* then */
  148. mctest_assert_ptr_ne (vpath, NULL);
  149. mctest_assert_str_eq (vfs_path_as_str (vpath), ETALON_PATH_URL_STR);
  150. vfs_path_free (vpath);
  151. }
  152. /* *INDENT-OFF* */
  153. END_TEST
  154. /* *INDENT-ON* */
  155. /* --------------------------------------------------------------------------------------------- */
  156. int
  157. main (void)
  158. {
  159. int number_failed;
  160. Suite *s = suite_create (TEST_SUITE_NAME);
  161. TCase *tc_core = tcase_create ("Core");
  162. SRunner *sr;
  163. tcase_add_checked_fixture (tc_core, setup, teardown);
  164. /* Add new tests here: *************** */
  165. tcase_add_test (tc_core, test_path_serialize);
  166. tcase_add_test (tc_core, test_path_deserialize);
  167. /* *********************************** */
  168. suite_add_tcase (s, tc_core);
  169. sr = srunner_create (s);
  170. srunner_set_log (sr, "path_serialize.log");
  171. srunner_run_all (sr, CK_NORMAL);
  172. number_failed = srunner_ntests_failed (sr);
  173. srunner_free (sr);
  174. return (number_failed == 0) ? 0 : 1;
  175. }
  176. /* --------------------------------------------------------------------------------------------- */