serialize.c 10 KB

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