serialize.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. lib - common 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"
  20. #include "tests/mctest.h"
  21. #include "lib/strutil.h"
  22. #include "lib/serialize.h"
  23. static GError *error = NULL;
  24. static const char *deserialize_input_value1
  25. = "g6:group1p6:param1v10:some valuep6:param2v11:some value "
  26. "g6:group2p6:param1v4:truep6:param2v6:123456"
  27. "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n"
  28. "g6:group4p6:param1v5:falsep6:param2v6:654321";
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* @Before */
  31. static void
  32. setup (void)
  33. {
  34. str_init_strings (NULL);
  35. error = NULL;
  36. }
  37. /* --------------------------------------------------------------------------------------------- */
  38. /* @After */
  39. static void
  40. teardown (void)
  41. {
  42. g_clear_error (&error);
  43. str_uninit_strings ();
  44. }
  45. /* --------------------------------------------------------------------------------------------- */
  46. /* @DataSource("test_serialize_ds") */
  47. /* *INDENT-OFF* */
  48. static const struct test_serialize_ds
  49. {
  50. const char input_char_prefix;
  51. const char *input_string;
  52. const char *expected_result;
  53. } test_serialize_ds[] = {
  54. { 's', "some test string", "s16:some test string" },
  55. { 'a', "some test test test string", "a26:some test test test string" },
  56. };
  57. /* *INDENT-ON* */
  58. /* @Test(dataSource = "test_serialize_ds") */
  59. /* *INDENT-OFF* */
  60. START_PARAMETRIZED_TEST (test_serialize, test_serialize_ds)
  61. /* *INDENT-ON* */
  62. {
  63. /* given */
  64. char *actual_result;
  65. /* when */
  66. actual_result = mc_serialize_str (data->input_char_prefix, data->input_string, &error);
  67. /* then */
  68. mctest_assert_str_eq (actual_result, data->expected_result);
  69. g_free (actual_result);
  70. if (error != NULL)
  71. g_error_free (error);
  72. }
  73. /* *INDENT-OFF* */
  74. END_PARAMETRIZED_TEST
  75. /* *INDENT-ON* */
  76. /* --------------------------------------------------------------------------------------------- */
  77. /* @DataSource("test_deserialize_incorrect_ds") */
  78. /* *INDENT-OFF* */
  79. static const struct test_deserialize_incorrect_ds
  80. {
  81. const char input_char_prefix;
  82. const char *input_string;
  83. const int expected_error_code;
  84. const char *expected_error_string;
  85. } test_deserialize_incorrect_ds[] = {
  86. { 's', NULL, 0, /* FIXME, TODO */
  87. "mc_serialize_str(): Input data is NULL or empty." },
  88. { 's', "incorrect string", 0, /* FIXME, TODO */
  89. "mc_serialize_str(): String prefix doesn't equal to 's'" },
  90. { 's', "s12345string without delimiter", 0, /* FIXME, TODO */
  91. "mc_serialize_str(): Length delimiter ':' doesn't exists" },
  92. { 's', "s1234567890123456789012345678901234567890123456789012345678901234567890:too big number",
  93. 0, /* FIXME, TODO */
  94. "mc_serialize_str(): Too big string length" },
  95. { 's', "s500:actual string length less that specified length", 0, /* FIXME, TODO */
  96. "mc_serialize_str(): Specified data length (500) is greater than actual data length (47)" },
  97. };
  98. /* *INDENT-ON* */
  99. /* @Test(dataSource = "test_deserialize_incorrect_ds") */
  100. /* *INDENT-OFF* */
  101. START_PARAMETRIZED_TEST (test_deserialize_incorrect, test_deserialize_incorrect_ds)
  102. /* *INDENT-ON* */
  103. {
  104. /* given */
  105. char *actual_result;
  106. /* when */
  107. actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);
  108. /* then */
  109. mctest_assert_null (actual_result);
  110. ck_assert_int_eq (error->code, data->expected_error_code);
  111. mctest_assert_str_eq (error->message, data->expected_error_string);
  112. }
  113. /* *INDENT-OFF* */
  114. END_PARAMETRIZED_TEST
  115. /* *INDENT-ON* */
  116. /* --------------------------------------------------------------------------------------------- */
  117. /* @DataSource("test_deserialize_ds") */
  118. /* *INDENT-OFF* */
  119. static const struct test_deserialize_ds
  120. {
  121. const char input_char_prefix;
  122. const char *input_string;
  123. const char *expected_result;
  124. } test_deserialize_ds[] = {
  125. { 's', "s10:actual string length great that specified length", "actual str" },
  126. { 'r', "r21:The right test string", "The right test string" },
  127. };
  128. /* *INDENT-ON* */
  129. /* @Test(dataSource = "test_deserialize_ds") */
  130. /* *INDENT-OFF* */
  131. START_PARAMETRIZED_TEST (test_deserialize, test_deserialize_ds)
  132. /* *INDENT-ON* */
  133. {
  134. /* given */
  135. char *actual_result;
  136. /* when */
  137. actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);
  138. /* then */
  139. mctest_assert_str_eq (actual_result, data->expected_result);
  140. g_free (actual_result);
  141. }
  142. /* *INDENT-OFF* */
  143. END_PARAMETRIZED_TEST
  144. /* *INDENT-ON* */
  145. /* --------------------------------------------------------------------------------------------- */
  146. /* *INDENT-OFF* */
  147. START_TEST (test_serialize_config)
  148. /* *INDENT-ON* */
  149. {
  150. /* given */
  151. mc_config_t *test_data;
  152. char *actual;
  153. const char *expected_result
  154. = "g6:group1p6:param1v10:some valuep6:param2v11:some value "
  155. "g6:group2p6:param1v4:truep6:param2v6:123456"
  156. "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n"
  157. "g6:group4p6:param1v5:falsep6:param2v6:654321";
  158. test_data = mc_config_init (NULL, FALSE);
  159. mc_config_set_string_raw (test_data, "group1", "param1", "some value");
  160. mc_config_set_string (test_data, "group1", "param2", "some value ");
  161. mc_config_set_bool (test_data, "group2", "param1", TRUE);
  162. mc_config_set_int (test_data, "group2", "param2", 123456);
  163. mc_config_set_string_raw (test_data, "group3", "param1", "::bla-bla::");
  164. mc_config_set_string (test_data, "group3", "param2", "bla-:p1:w:v2:12:g3:123:bla-bla\n");
  165. mc_config_set_bool (test_data, "group4", "param1", FALSE);
  166. mc_config_set_int (test_data, "group4", "param2", 654321);
  167. /* when */
  168. actual = mc_serialize_config (test_data, &error);
  169. mc_config_deinit (test_data);
  170. /* then */
  171. mctest_assert_not_null (actual);
  172. mctest_assert_str_eq (actual, expected_result);
  173. g_free (actual);
  174. }
  175. /* *INDENT-OFF* */
  176. END_TEST
  177. /* *INDENT-ON* */
  178. /* --------------------------------------------------------------------------------------------- */
  179. /* @DataSource("test_deserialize_config_incorrect_ds") */
  180. /* *INDENT-OFF* */
  181. static const struct test_deserialize_config_incorrect_ds
  182. {
  183. const char *input_string;
  184. const int expected_error_code;
  185. const char *expected_error_string;
  186. } test_deserialize_config_incorrect_ds[] = {
  187. { "g123error in group name", 0, /* FIXME, TODO */
  188. "mc_deserialize_config() at 1: mc_serialize_str(): Length delimiter ':' doesn't exists" },
  189. { "p6:param1v10:some valuep6:param2v11:some value ", 0, /* FIXME, TODO */
  190. "mc_deserialize_config() at 1: mc_serialize_str(): String prefix doesn't equal to 'g'" },
  191. { "g6:group1v10:some valuep6:param2v11:some value ", 0, /* FIXME, TODO */
  192. "mc_deserialize_config() at 10: mc_serialize_str(): String prefix doesn't equal to 'p'" },
  193. { "g6:group1p6000:param2v11:some value ", 0, /* FIXME, TODO */
  194. "mc_deserialize_config() at 10: mc_serialize_str(): Specified data length (6000) is greater "
  195. "than actual data length (21)" },
  196. };
  197. /* *INDENT-ON* */
  198. /* @Test(dataSource = "test_deserialize_config_incorrect_ds") */
  199. /* *INDENT-OFF* */
  200. START_PARAMETRIZED_TEST (test_deserialize_config_incorrect, test_deserialize_config_incorrect_ds)
  201. /* *INDENT-ON* */
  202. {
  203. /* given */
  204. mc_config_t *actual_result;
  205. /* when */
  206. actual_result = mc_deserialize_config (data->input_string, &error);
  207. /* then */
  208. mctest_assert_null (actual_result);
  209. ck_assert_int_eq (error->code, data->expected_error_code);
  210. mctest_assert_str_eq (error->message, data->expected_error_string);
  211. }
  212. /* *INDENT-OFF* */
  213. END_PARAMETRIZED_TEST
  214. /* *INDENT-ON* */
  215. /* --------------------------------------------------------------------------------------------- */
  216. /* *INDENT-OFF* */
  217. START_TEST (test_deserialize_config)
  218. /* *INDENT-ON* */
  219. {
  220. /* given */
  221. mc_config_t *actual;
  222. char *actual_value;
  223. /* when */
  224. actual = mc_deserialize_config (deserialize_input_value1, &error);
  225. /* then */
  226. mctest_assert_not_null (actual);
  227. actual_value = mc_config_get_string_raw (actual, "group1", "param1", "");
  228. mctest_assert_str_eq (actual_value, "some value");
  229. g_free (actual_value);
  230. actual_value = mc_config_get_string (actual, "group1", "param2", "");
  231. mctest_assert_str_eq (actual_value, "some value ");
  232. g_free (actual_value);
  233. mctest_assert_true (mc_config_get_bool (actual, "group2", "param1", FALSE));
  234. ck_assert_int_eq (mc_config_get_int (actual, "group2", "param2", 0), 123456);
  235. actual_value = mc_config_get_string_raw (actual, "group3", "param1", "");
  236. mctest_assert_str_eq (actual_value, "::bla-bla::");
  237. g_free (actual_value);
  238. actual_value = mc_config_get_string (actual, "group3", "param2", "");
  239. mctest_assert_str_eq (actual_value, "bla-:p1:w:v2:12:g3:123:bla-bla\n");
  240. g_free (actual_value);
  241. mctest_assert_false (mc_config_get_bool (actual, "group4", "param1", TRUE));
  242. ck_assert_int_eq (mc_config_get_int (actual, "group4", "param2", 0), 654321);
  243. mc_config_deinit (actual);
  244. }
  245. /* *INDENT-OFF* */
  246. END_TEST
  247. /* *INDENT-ON* */
  248. #undef input_value
  249. /* --------------------------------------------------------------------------------------------- */
  250. int
  251. main (void)
  252. {
  253. TCase *tc_core;
  254. tc_core = tcase_create ("Core");
  255. tcase_add_checked_fixture (tc_core, setup, teardown);
  256. /* Add new tests here: *************** */
  257. mctest_add_parameterized_test (tc_core, test_serialize, test_serialize_ds);
  258. mctest_add_parameterized_test (tc_core, test_deserialize_incorrect,
  259. test_deserialize_incorrect_ds);
  260. mctest_add_parameterized_test (tc_core, test_deserialize, test_deserialize_ds);
  261. tcase_add_test (tc_core, test_serialize_config);
  262. mctest_add_parameterized_test (tc_core, test_deserialize_config_incorrect,
  263. test_deserialize_config_incorrect_ds);
  264. tcase_add_test (tc_core, test_deserialize_config);
  265. /* *********************************** */
  266. return mctest_run_all (tc_core);
  267. }
  268. /* --------------------------------------------------------------------------------------------- */