path_serialize.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* lib/vfs - vfs_path_t serialize/deserialize functions
  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.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. static void
  31. setup (void)
  32. {
  33. str_init_strings (NULL);
  34. vfs_init ();
  35. init_localfs ();
  36. vfs_setup_work_dir ();
  37. test_subclass1.flags = VFS_S_REMOTE;
  38. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  39. vfs_test_ops1.name = "testfs1";
  40. vfs_test_ops1.flags = VFSF_NOLINKS;
  41. vfs_test_ops1.prefix = "test1";
  42. vfs_register_class (&vfs_test_ops1);
  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. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  52. load_codepages_list ();
  53. }
  54. static void
  55. teardown (void)
  56. {
  57. free_codepages_list ();
  58. vfs_shut ();
  59. str_uninit_strings ();
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  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. START_TEST (test_path_serialize_deserialize)
  86. {
  87. vfs_path_t *vpath;
  88. char *serialized_vpath;
  89. GError *error = NULL;
  90. vpath = vfs_path_from_str_flags (ETALON_PATH_STR, VPF_USE_DEPRECATED_PARSER);
  91. serialized_vpath = vfs_path_serialize (vpath, &error);
  92. vfs_path_free (vpath);
  93. if (serialized_vpath == NULL)
  94. {
  95. fail ("serialized_vpath is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
  96. g_clear_error (&error);
  97. return;
  98. }
  99. fail_unless (
  100. strcmp (serialized_vpath, ETALON_SERIALIZED_PATH ) == 0,
  101. "\nserialized_vpath (%s)\nnot equal to etalon (%s)", serialized_vpath, ETALON_SERIALIZED_PATH
  102. );
  103. vpath = vfs_path_deserialize (serialized_vpath, &error);
  104. g_free (serialized_vpath);
  105. if (vpath == NULL)
  106. {
  107. fail ("vpath is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
  108. g_clear_error (&error);
  109. return;
  110. }
  111. serialized_vpath = vfs_path_to_str (vpath);
  112. fail_unless (
  113. strcmp (serialized_vpath, ETALON_PATH_URL_STR) == 0,
  114. "\ndeserialized path (%s)\nnot equal to etalon (%s)", serialized_vpath, ETALON_PATH_URL_STR
  115. );
  116. vfs_path_free(vpath);
  117. g_free(serialized_vpath);
  118. }
  119. END_TEST
  120. /* --------------------------------------------------------------------------------------------- */
  121. int
  122. main (void)
  123. {
  124. int number_failed;
  125. Suite *s = suite_create (TEST_SUITE_NAME);
  126. TCase *tc_core = tcase_create ("Core");
  127. SRunner *sr;
  128. tcase_add_checked_fixture (tc_core, setup, teardown);
  129. /* Add new tests here: *************** */
  130. tcase_add_test (tc_core, test_path_serialize_deserialize);
  131. /* *********************************** */
  132. suite_add_tcase (s, tc_core);
  133. sr = srunner_create (s);
  134. srunner_set_log (sr, "path_serialize.log");
  135. srunner_run_all (sr, CK_NORMAL);
  136. number_failed = srunner_ntests_failed (sr);
  137. srunner_free (sr);
  138. return (number_failed == 0) ? 0 : 1;
  139. }
  140. /* --------------------------------------------------------------------------------------------- */