serialize.c 11 KB

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