serialize.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. lib/vfs - common 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"
  20. #include <config.h>
  21. #include <check.h>
  22. #include "lib/global.h"
  23. #include "lib/strutil.h"
  24. #include "lib/serialize.h"
  25. static void
  26. setup (void)
  27. {
  28. str_init_strings (NULL);
  29. }
  30. static void
  31. teardown (void)
  32. {
  33. str_uninit_strings ();
  34. }
  35. /* --------------------------------------------------------------------------------------------- */
  36. #define deserialize_check_incorrect( etalon_code, etalon_str ) { \
  37. if (actual != NULL) \
  38. { \
  39. fail("actual value is '%s', but should be NULL", actual); \
  40. g_free(actual); \
  41. } \
  42. else \
  43. { \
  44. fail_unless (error->code == etalon_code && strcmp(error->message, etalon_str) == 0, \
  45. "\nerror code is %d (should be %d);\nerror message is '%s' (should be '%s')", \
  46. error->code, etalon_code, error->message, etalon_str); \
  47. g_clear_error(&error); \
  48. } \
  49. }
  50. START_TEST (test_serialize_deserialize_str)
  51. {
  52. GError *error = NULL;
  53. char *actual;
  54. actual = mc_serialize_str('s', "some test string", &error);
  55. if (actual == NULL)
  56. {
  57. fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
  58. g_clear_error(&error);
  59. return;
  60. }
  61. fail_unless (strcmp(actual,"s16:some test string") == 0, "Actual value(%s) doesn't equal to etalon(s16:some test string)", actual);
  62. g_free(actual);
  63. actual = mc_deserialize_str('s', NULL, &error);
  64. deserialize_check_incorrect( -1, "mc_serialize_str(): Input data is NULL or empty." );
  65. actual = mc_deserialize_str('s', "incorrect string", &error);
  66. deserialize_check_incorrect( -2, "mc_serialize_str(): String prefix doesn't equal to 's'" );
  67. actual = mc_deserialize_str('s', "s12345string without delimiter", &error);
  68. deserialize_check_incorrect( -3, "mc_serialize_str(): Length delimiter ':' doesn't exists" );
  69. actual = mc_deserialize_str('s', "s1234567890123456789012345678901234567890123456789012345678901234567890:too big number", &error);
  70. deserialize_check_incorrect( -3, "mc_serialize_str(): Too big string length" );
  71. actual = mc_deserialize_str('s', "s500:actual string length less that specified length", &error);
  72. deserialize_check_incorrect( -3, "mc_serialize_str(): Specified data length (500) is greater than actual data length (47)" );
  73. actual = mc_deserialize_str('s', "s10:actual string length great that specified length", &error);
  74. fail_unless (actual != NULL && strcmp(actual, "actual str") == 0, "actual (%s) doesn't equal to etalon(actual str)", actual);
  75. g_free(actual);
  76. actual = mc_deserialize_str('s', "s21:The right test string", &error);
  77. fail_unless (actual != NULL && strcmp(actual, "The right test string") == 0, "actual (%s) doesn't equal to etalon(The right test string)", actual);
  78. g_free(actual);
  79. }
  80. END_TEST
  81. /* --------------------------------------------------------------------------------------------- */
  82. #define etalon_str "g6:group1p6:param1v10:some valuep6:param2v11:some value " \
  83. "g6:group2p6:param1v4:truep6:param2v6:123456" \
  84. "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n" \
  85. "g6:group4p6:param1v5:falsep6:param2v6:654321"
  86. START_TEST (test_serialize_config)
  87. {
  88. mc_config_t *test_data;
  89. GError *error = NULL;
  90. char *actual;
  91. test_data = mc_config_init (NULL);
  92. mc_config_set_string_raw (test_data, "group1", "param1", "some value");
  93. mc_config_set_string (test_data, "group1", "param2", "some value ");
  94. mc_config_set_bool (test_data, "group2", "param1", TRUE);
  95. mc_config_set_int (test_data, "group2", "param2", 123456);
  96. mc_config_set_string_raw (test_data, "group3", "param1", "::bla-bla::");
  97. mc_config_set_string (test_data, "group3", "param2", "bla-:p1:w:v2:12:g3:123:bla-bla\n");
  98. mc_config_set_bool (test_data, "group4", "param1", FALSE);
  99. mc_config_set_int (test_data, "group4", "param2", 654321);
  100. actual = mc_serialize_config (test_data, &error);
  101. mc_config_deinit (test_data);
  102. if (actual == NULL)
  103. {
  104. fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
  105. g_clear_error(&error);
  106. return;
  107. }
  108. fail_unless(strcmp(actual, etalon_str) == 0, "Not equal:\nactual (%s)\netalon (%s)", actual, etalon_str);
  109. g_free(actual);
  110. }
  111. END_TEST
  112. /* --------------------------------------------------------------------------------------------- */
  113. #undef deserialize_check_incorrect
  114. #define deserialize_check_incorrect( etalon_code, etalon_str ) { \
  115. if (actual != NULL) \
  116. { \
  117. fail("actual value but should be NULL", actual); \
  118. mc_config_deinit(actual); \
  119. } \
  120. else \
  121. { \
  122. fail_unless (error->code == etalon_code && strcmp(error->message, etalon_str) == 0, \
  123. "\nerror code is %d (should be %d);\nerror message is '%s' (should be '%s')", \
  124. error->code, etalon_code, error->message, etalon_str); \
  125. g_clear_error(&error); \
  126. } \
  127. }
  128. START_TEST (test_deserialize_config)
  129. {
  130. mc_config_t *actual;
  131. GError *error = NULL;
  132. char *actual_value;
  133. actual = mc_deserialize_config ("g123error in group name", &error);
  134. deserialize_check_incorrect( -3,
  135. "mc_deserialize_config() at 1: mc_serialize_str(): Length delimiter ':' doesn't exists");
  136. actual = mc_deserialize_config ("p6:param1v10:some valuep6:param2v11:some value ", &error);
  137. deserialize_check_incorrect( -2,
  138. "mc_deserialize_config() at 1: mc_serialize_str(): String prefix doesn't equal to 'g'");
  139. actual = mc_deserialize_config ("g6:group1v10:some valuep6:param2v11:some value ", &error);
  140. deserialize_check_incorrect( -2,
  141. "mc_deserialize_config() at 10: mc_serialize_str(): String prefix doesn't equal to 'p'");
  142. actual = mc_deserialize_config ("g6:group1p6000:param2v11:some value ", &error);
  143. deserialize_check_incorrect( -3,
  144. "mc_deserialize_config() at 10: mc_serialize_str(): Specified data length (6000) is greater than actual data length (21)");
  145. actual = mc_deserialize_config (etalon_str, &error);
  146. if (actual == NULL)
  147. {
  148. fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
  149. g_clear_error(&error);
  150. return;
  151. }
  152. actual_value = mc_config_get_string_raw(actual, "group1", "param1", "");
  153. fail_unless( strcmp(actual_value, "some value") == 0,
  154. "group1->param1(%s) should be equal to 'some value'", actual_value);
  155. g_free(actual_value);
  156. actual_value = mc_config_get_string(actual, "group1", "param2", "");
  157. fail_unless( strcmp(actual_value, "some value ") == 0,
  158. "group1->param2(%s) should be equal to 'some value '", actual_value);
  159. g_free(actual_value);
  160. fail_unless( mc_config_get_bool(actual, "group2", "param1", FALSE) == TRUE,
  161. "group2->param1(FALSE) should be equal to TRUE");
  162. fail_unless( mc_config_get_int(actual, "group2", "param2", 0) == 123456,
  163. "group2->param2(%d) should be equal to 123456", mc_config_get_int(actual, "group2", "param2", 0));
  164. actual_value = mc_config_get_string_raw(actual, "group3", "param1", "");
  165. fail_unless( strcmp(actual_value, "::bla-bla::") == 0,
  166. "group3->param1(%s) should be equal to '::bla-bla::'", actual_value);
  167. g_free(actual_value);
  168. actual_value = mc_config_get_string(actual, "group3", "param2", "");
  169. fail_unless( strcmp(actual_value, "bla-:p1:w:v2:12:g3:123:bla-bla\n") == 0,
  170. "group3->param2(%s) should be equal to 'bla-:p1:w:v2:12:g3:123:bla-bla\n'", actual_value);
  171. g_free(actual_value);
  172. fail_unless( mc_config_get_bool(actual, "group4", "param1", TRUE) == FALSE,
  173. "group4->param1(TRUE) should be equal to FALSE");
  174. fail_unless( mc_config_get_int(actual, "group4", "param2", 0) == 654321,
  175. "group4->param2(%d) should be equal to 654321", mc_config_get_int(actual, "group4", "param2", 0));
  176. mc_config_deinit (actual);
  177. }
  178. END_TEST
  179. /* --------------------------------------------------------------------------------------------- */
  180. int
  181. main (void)
  182. {
  183. int number_failed;
  184. Suite *s = suite_create (TEST_SUITE_NAME);
  185. TCase *tc_core = tcase_create ("Core");
  186. SRunner *sr;
  187. tcase_add_checked_fixture (tc_core, setup, teardown);
  188. /* Add new tests here: *************** */
  189. tcase_add_test (tc_core, test_serialize_deserialize_str);
  190. tcase_add_test (tc_core, test_serialize_config);
  191. tcase_add_test (tc_core, test_deserialize_config);
  192. /* *********************************** */
  193. suite_add_tcase (s, tc_core);
  194. sr = srunner_create (s);
  195. srunner_set_log (sr, "serialize.log");
  196. srunner_run_all (sr, CK_NORMAL);
  197. number_failed = srunner_ntests_failed (sr);
  198. srunner_free (sr);
  199. return (number_failed == 0) ? 0 : 1;
  200. }
  201. /* --------------------------------------------------------------------------------------------- */