path_serialize.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. lib/vfs - vfs_path_t serialize/deserialize functions
  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.h"
  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. 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. static void
  59. teardown (void)
  60. {
  61. #ifdef HAVE_CHARSET
  62. free_codepages_list ();
  63. #endif
  64. vfs_shut ();
  65. str_uninit_strings ();
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. #ifdef HAVE_CHARSET
  69. #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"
  70. #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"
  71. #define ETALON_SERIALIZED_PATH \
  72. "g14:path-element-0" \
  73. "p4:pathv12:/local/path/" \
  74. "p10:class-namev7:localfs" \
  75. "g14:path-element-1" \
  76. "p4:pathv18:bla-bla/some/path/" \
  77. "p10:class-namev7:testfs1" \
  78. "p10:vfs_prefixv5:test1" \
  79. "p4:userv4:user" \
  80. "p8:passwordv4:pass" \
  81. "p4:hostv9:some.host" \
  82. "p4:portv5:12345" \
  83. "g14:path-element-2" \
  84. "p4:pathv17:bla-bla/some/path" \
  85. "p10:class-namev7:testfs2" \
  86. "p8:encodingv6:KOI8-R" \
  87. "p10:vfs_prefixv5:test2" \
  88. "g14:path-element-3" \
  89. "p4:pathv9:111/22/33" \
  90. "p10:class-namev7:testfs3" \
  91. "p10:vfs_prefixv5:test3"
  92. #else
  93. #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"
  94. #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"
  95. #define ETALON_SERIALIZED_PATH \
  96. "g14:path-element-0" \
  97. "p4:pathv12:/local/path/" \
  98. "p10:class-namev7:localfs" \
  99. "g14:path-element-1" \
  100. "p4:pathv18:bla-bla/some/path/" \
  101. "p10:class-namev7:testfs1" \
  102. "p10:vfs_prefixv5:test1" \
  103. "p4:userv4:user" \
  104. "p8:passwordv4:pass" \
  105. "p4:hostv9:some.host" \
  106. "p4:portv5:12345" \
  107. "g14:path-element-2" \
  108. "p4:pathv17:bla-bla/some/path" \
  109. "p10:class-namev7:testfs2" \
  110. "p10:vfs_prefixv5:test2" \
  111. "g14:path-element-3" \
  112. "p4:pathv9:111/22/33" \
  113. "p10:class-namev7:testfs3" \
  114. "p10:vfs_prefixv5:test3"
  115. #endif
  116. START_TEST (test_path_serialize_deserialize)
  117. {
  118. vfs_path_t *vpath;
  119. char *serialized_vpath;
  120. GError *error = NULL;
  121. vpath = vfs_path_from_str_flags (ETALON_PATH_STR, VPF_USE_DEPRECATED_PARSER);
  122. serialized_vpath = vfs_path_serialize (vpath, &error);
  123. vfs_path_free (vpath);
  124. if (serialized_vpath == NULL)
  125. {
  126. fail ("serialized_vpath is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
  127. g_clear_error (&error);
  128. return;
  129. }
  130. fail_unless (
  131. strcmp (serialized_vpath, ETALON_SERIALIZED_PATH ) == 0,
  132. "\nserialized_vpath (%s)\nnot equal to etalon (%s)", serialized_vpath, ETALON_SERIALIZED_PATH
  133. );
  134. vpath = vfs_path_deserialize (serialized_vpath, &error);
  135. g_free (serialized_vpath);
  136. if (vpath == NULL)
  137. {
  138. fail ("vpath is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
  139. g_clear_error (&error);
  140. return;
  141. }
  142. serialized_vpath = vfs_path_to_str (vpath);
  143. fail_unless (
  144. strcmp (serialized_vpath, ETALON_PATH_URL_STR) == 0,
  145. "\ndeserialized path (%s)\nnot equal to etalon (%s)", serialized_vpath, ETALON_PATH_URL_STR
  146. );
  147. vfs_path_free(vpath);
  148. g_free(serialized_vpath);
  149. }
  150. END_TEST
  151. /* --------------------------------------------------------------------------------------------- */
  152. int
  153. main (void)
  154. {
  155. int number_failed;
  156. Suite *s = suite_create (TEST_SUITE_NAME);
  157. TCase *tc_core = tcase_create ("Core");
  158. SRunner *sr;
  159. tcase_add_checked_fixture (tc_core, setup, teardown);
  160. /* Add new tests here: *************** */
  161. tcase_add_test (tc_core, test_path_serialize_deserialize);
  162. /* *********************************** */
  163. suite_add_tcase (s, tc_core);
  164. sr = srunner_create (s);
  165. srunner_set_log (sr, "path_serialize.log");
  166. srunner_run_all (sr, CK_NORMAL);
  167. number_failed = srunner_ntests_failed (sr);
  168. srunner_free (sr);
  169. return (number_failed == 0) ? 0 : 1;
  170. }
  171. /* --------------------------------------------------------------------------------------------- */