path_serialize.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. lib/vfs - vfs_path_t serialize/deserialize functions
  3. Copyright (C) 2011-2024
  4. 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. static struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* @Before */
  31. static void
  32. setup (void)
  33. {
  34. str_init_strings ("UTF-8");
  35. vfs_init ();
  36. vfs_init_localfs ();
  37. vfs_setup_work_dir ();
  38. vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  39. vfs_register_class (&vfs_test_ops1);
  40. vfs_init_class (&vfs_test_ops2, "testfs2", VFSF_UNKNOWN, "test2");
  41. vfs_register_class (&vfs_test_ops2);
  42. vfs_init_class (&vfs_test_ops3, "testfs3", VFSF_UNKNOWN, "test3");
  43. vfs_register_class (&vfs_test_ops3);
  44. #ifdef HAVE_CHARSET
  45. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  46. load_codepages_list ();
  47. #endif
  48. }
  49. /* --------------------------------------------------------------------------------------------- */
  50. /* @After */
  51. static void
  52. teardown (void)
  53. {
  54. #ifdef HAVE_CHARSET
  55. free_codepages_list ();
  56. #endif
  57. vfs_shut ();
  58. str_uninit_strings ();
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. #ifdef HAVE_CHARSET
  62. #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"
  63. #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"
  64. #define ETALON_SERIALIZED_PATH \
  65. "g14:path-element-0" \
  66. "p4:pathv12:/local/path/" \
  67. "p10:class-namev7:localfs" \
  68. "g14:path-element-1" \
  69. "p4:pathv18:bla-bla/some/path/" \
  70. "p10:class-namev7:testfs1" \
  71. "p10:vfs_prefixv5:test1" \
  72. "p4:userv4:user" \
  73. "p8:passwordv4:pass" \
  74. "p4:hostv9:some.host" \
  75. "p4:portv5:12345" \
  76. "g14:path-element-2" \
  77. "p4:pathv17:bla-bla/some/path" \
  78. "p10:class-namev7:testfs2" \
  79. "p8:encodingv6:KOI8-R" \
  80. "p10:vfs_prefixv5:test2" \
  81. "g14:path-element-3" \
  82. "p4:pathv9:111/22/33" \
  83. "p10:class-namev7:testfs3" \
  84. "p10:vfs_prefixv5:test3"
  85. #else
  86. #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"
  87. #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"
  88. #define ETALON_SERIALIZED_PATH \
  89. "g14:path-element-0" \
  90. "p4:pathv12:/local/path/" \
  91. "p10:class-namev7:localfs" \
  92. "g14:path-element-1" \
  93. "p4:pathv18:bla-bla/some/path/" \
  94. "p10:class-namev7:testfs1" \
  95. "p10:vfs_prefixv5:test1" \
  96. "p4:userv4:user" \
  97. "p8:passwordv4:pass" \
  98. "p4:hostv9:some.host" \
  99. "p4:portv5:12345" \
  100. "g14:path-element-2" \
  101. "p4:pathv17:bla-bla/some/path" \
  102. "p10:class-namev7:testfs2" \
  103. "p10:vfs_prefixv5:test2" \
  104. "g14:path-element-3" \
  105. "p4:pathv9:111/22/33" \
  106. "p10:class-namev7:testfs3" \
  107. "p10:vfs_prefixv5:test3"
  108. #endif
  109. /* *INDENT-OFF* */
  110. START_TEST (test_path_serialize)
  111. /* *INDENT-ON* */
  112. {
  113. /* given */
  114. vfs_path_t *vpath;
  115. char *serialized_vpath;
  116. GError *error = NULL;
  117. /* when */
  118. vpath = vfs_path_from_str_flags (ETALON_PATH_STR, VPF_USE_DEPRECATED_PARSER);
  119. serialized_vpath = vfs_path_serialize (vpath, &error);
  120. vfs_path_free (vpath, TRUE);
  121. if (error != NULL)
  122. g_error_free (error);
  123. /* then */
  124. mctest_assert_ptr_ne (serialized_vpath, NULL);
  125. mctest_assert_str_eq (serialized_vpath, ETALON_SERIALIZED_PATH);
  126. }
  127. /* *INDENT-OFF* */
  128. END_TEST
  129. /* *INDENT-ON* */
  130. /* --------------------------------------------------------------------------------------------- */
  131. /* *INDENT-OFF* */
  132. START_TEST (test_path_deserialize)
  133. /* *INDENT-ON* */
  134. {
  135. /* given */
  136. vfs_path_t *vpath;
  137. GError *error = NULL;
  138. /* when */
  139. vpath = vfs_path_deserialize (ETALON_SERIALIZED_PATH, &error);
  140. /* then */
  141. mctest_assert_ptr_ne (vpath, NULL);
  142. mctest_assert_str_eq (vfs_path_as_str (vpath), ETALON_PATH_URL_STR);
  143. vfs_path_free (vpath, TRUE);
  144. }
  145. /* *INDENT-OFF* */
  146. END_TEST
  147. /* *INDENT-ON* */
  148. /* --------------------------------------------------------------------------------------------- */
  149. int
  150. main (void)
  151. {
  152. TCase *tc_core;
  153. tc_core = tcase_create ("Core");
  154. tcase_add_checked_fixture (tc_core, setup, teardown);
  155. /* Add new tests here: *************** */
  156. tcase_add_test (tc_core, test_path_serialize);
  157. tcase_add_test (tc_core, test_path_deserialize);
  158. /* *********************************** */
  159. return mctest_run_all (tc_core);
  160. }
  161. /* --------------------------------------------------------------------------------------------- */