path_serialize.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #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.h"
  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. test_subclass1.flags = VFS_S_REMOTE;
  41. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  42. vfs_test_ops1.name = "testfs1";
  43. vfs_test_ops1.flags = VFSF_NOLINKS;
  44. vfs_test_ops1.prefix = "test1";
  45. vfs_register_class (&vfs_test_ops1);
  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. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  55. load_codepages_list ();
  56. }
  57. static void
  58. teardown (void)
  59. {
  60. free_codepages_list ();
  61. vfs_shut ();
  62. str_uninit_strings ();
  63. }
  64. /* --------------------------------------------------------------------------------------------- */
  65. #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"
  66. #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"
  67. #define ETALON_SERIALIZED_PATH \
  68. "g14:path-element-0" \
  69. "p4:pathv12:/local/path/" \
  70. "p10:class-namev7:localfs" \
  71. "g14:path-element-1" \
  72. "p4:pathv18:bla-bla/some/path/" \
  73. "p10:class-namev7:testfs1" \
  74. "p10:vfs_prefixv5:test1" \
  75. "p4:userv4:user" \
  76. "p8:passwordv4:pass" \
  77. "p4:hostv9:some.host" \
  78. "p4:portv5:12345" \
  79. "g14:path-element-2" \
  80. "p4:pathv17:bla-bla/some/path" \
  81. "p10:class-namev7:testfs2" \
  82. "p8:encodingv6:KOI8-R" \
  83. "p10:vfs_prefixv5:test2" \
  84. "g14:path-element-3" \
  85. "p4:pathv9:111/22/33" \
  86. "p10:class-namev7:testfs3" \
  87. "p10:vfs_prefixv5:test3"
  88. START_TEST (test_path_serialize_deserialize)
  89. {
  90. vfs_path_t *vpath;
  91. char *serialized_vpath;
  92. GError *error = NULL;
  93. vpath = vfs_path_from_str_flags (ETALON_PATH_STR, VPF_USE_DEPRECATED_PARSER);
  94. serialized_vpath = vfs_path_serialize (vpath, &error);
  95. vfs_path_free (vpath);
  96. if (serialized_vpath == NULL)
  97. {
  98. fail ("serialized_vpath is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
  99. g_clear_error (&error);
  100. return;
  101. }
  102. fail_unless (
  103. strcmp (serialized_vpath, ETALON_SERIALIZED_PATH ) == 0,
  104. "\nserialized_vpath (%s)\nnot equal to etalon (%s)", serialized_vpath, ETALON_SERIALIZED_PATH
  105. );
  106. vpath = vfs_path_deserialize (serialized_vpath, &error);
  107. g_free (serialized_vpath);
  108. if (vpath == NULL)
  109. {
  110. fail ("vpath is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
  111. g_clear_error (&error);
  112. return;
  113. }
  114. serialized_vpath = vfs_path_to_str (vpath);
  115. fail_unless (
  116. strcmp (serialized_vpath, ETALON_PATH_URL_STR) == 0,
  117. "\ndeserialized path (%s)\nnot equal to etalon (%s)", serialized_vpath, ETALON_PATH_URL_STR
  118. );
  119. vfs_path_free(vpath);
  120. g_free(serialized_vpath);
  121. }
  122. END_TEST
  123. /* --------------------------------------------------------------------------------------------- */
  124. int
  125. main (void)
  126. {
  127. int number_failed;
  128. Suite *s = suite_create (TEST_SUITE_NAME);
  129. TCase *tc_core = tcase_create ("Core");
  130. SRunner *sr;
  131. tcase_add_checked_fixture (tc_core, setup, teardown);
  132. /* Add new tests here: *************** */
  133. tcase_add_test (tc_core, test_path_serialize_deserialize);
  134. /* *********************************** */
  135. suite_add_tcase (s, tc_core);
  136. sr = srunner_create (s);
  137. srunner_set_log (sr, "path_serialize.log");
  138. srunner_run_all (sr, CK_NORMAL);
  139. number_failed = srunner_ntests_failed (sr);
  140. srunner_free (sr);
  141. return (number_failed == 0) ? 0 : 1;
  142. }
  143. /* --------------------------------------------------------------------------------------------- */