serialize.c 11 KB

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