serialize.c 9.6 KB

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