root/tests/lib/serialize.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. setup
  2. teardown
  3. START_PARAMETRIZED_TEST
  4. START_PARAMETRIZED_TEST
  5. START_PARAMETRIZED_TEST
  6. START_TEST
  7. START_PARAMETRIZED_TEST
  8. START_TEST
  9. main

   1 /*
   2    lib - common serialize/deserialize functions
   3 
   4    Copyright (C) 2011-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "/lib"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "lib/strutil.h"
  31 #include "lib/serialize.h"
  32 
  33 static GError *error = NULL;
  34 
  35 static const char *deserialize_input_value1 =
  36     "g6:group1p6:param1v10:some valuep6:param2v11:some value "
  37     "g6:group2p6:param1v4:truep6:param2v6:123456"
  38     "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n"
  39     "g6:group4p6:param1v5:falsep6:param2v6:654321";
  40 
  41 /* --------------------------------------------------------------------------------------------- */
  42 
  43 /* @Before */
  44 static void
  45 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  46 {
  47     str_init_strings (NULL);
  48     error = NULL;
  49 }
  50 
  51 /* --------------------------------------------------------------------------------------------- */
  52 
  53 /* @After */
  54 static void
  55 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  56 {
  57     g_clear_error (&error);
  58     str_uninit_strings ();
  59 }
  60 
  61 /* --------------------------------------------------------------------------------------------- */
  62 
  63 /* @DataSource("test_serialize_ds") */
  64 static const struct test_serialize_ds
  65 {
  66     const char input_char_prefix;
  67     const char *input_string;
  68     const char *expected_result;
  69 } test_serialize_ds[] = {
  70     { 's', "some test string", "s16:some test string" },
  71     { 'a', "some test test test string", "a26:some test test test string" },
  72 };
  73 /* @Test(dataSource = "test_serialize_ds") */
  74 START_PARAMETRIZED_TEST (test_serialize, test_serialize_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
  75 {
  76     // given
  77     char *actual_result;
  78 
  79     // when
  80     actual_result = mc_serialize_str (data->input_char_prefix, data->input_string, &error);
  81 
  82     // then
  83     mctest_assert_str_eq (actual_result, data->expected_result);
  84 
  85     g_free (actual_result);
  86 
  87     if (error != NULL)
  88         g_error_free (error);
  89 }
  90 END_PARAMETRIZED_TEST
  91 
  92 /* --------------------------------------------------------------------------------------------- */
  93 
  94 /* @DataSource("test_deserialize_incorrect_ds") */
  95 static const struct test_deserialize_incorrect_ds
  96 {
  97     const char input_char_prefix;
  98     const char *input_string;
  99     const int expected_error_code;
 100     const char *expected_error_string;
 101 } test_deserialize_incorrect_ds[] = {
 102     {
 103         's',
 104         NULL,
 105         0,  // FIXME, TODO
 106         "mc_serialize_str(): Input data is NULL or empty.",
 107     },
 108     {
 109         's',
 110         "incorrect string",
 111         0,  // FIXME, TODO
 112         "mc_serialize_str(): String prefix doesn't equal to 's'",
 113     },
 114     {
 115         's',
 116         "s12345string without delimiter",
 117         0,  // FIXME, TODO
 118         "mc_serialize_str(): Length delimiter ':' doesn't exists",
 119     },
 120     {
 121         's',
 122         "s1234567890123456789012345678901234567890123456789012345678901234567890:too big number",
 123         0,  // FIXME, TODO
 124         "mc_serialize_str(): Too big string length",
 125     },
 126     {
 127         's',
 128         "s500:actual string length less that specified length",
 129         0,  // FIXME, TODO
 130         "mc_serialize_str(): Specified data length (500) is greater than actual data length (47)",
 131     },
 132 };
 133 /* @Test(dataSource = "test_deserialize_incorrect_ds") */
 134 START_PARAMETRIZED_TEST (test_deserialize_incorrect, test_deserialize_incorrect_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 135 {
 136     // given
 137     char *actual_result;
 138 
 139     // when
 140     actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);
 141 
 142     // then
 143     mctest_assert_null (actual_result);
 144 
 145     ck_assert_int_eq (error->code, data->expected_error_code);
 146     mctest_assert_str_eq (error->message, data->expected_error_string);
 147 }
 148 END_PARAMETRIZED_TEST
 149 
 150 /* --------------------------------------------------------------------------------------------- */
 151 
 152 /* @DataSource("test_deserialize_ds") */
 153 static const struct test_deserialize_ds
 154 {
 155     const char input_char_prefix;
 156     const char *input_string;
 157     const char *expected_result;
 158 } test_deserialize_ds[] = {
 159     {
 160         's',
 161         "s10:actual string length great that specified length",
 162         "actual str",
 163     },
 164     {
 165         'r',
 166         "r21:The right test string",
 167         "The right test string",
 168     },
 169 };
 170 /* @Test(dataSource = "test_deserialize_ds") */
 171 START_PARAMETRIZED_TEST (test_deserialize, test_deserialize_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 172 {
 173     // given
 174     char *actual_result;
 175 
 176     // when
 177     actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);
 178 
 179     // then
 180     mctest_assert_str_eq (actual_result, data->expected_result);
 181 
 182     g_free (actual_result);
 183 }
 184 END_PARAMETRIZED_TEST
 185 
 186 /* --------------------------------------------------------------------------------------------- */
 187 
 188 START_TEST (test_serialize_config)
     /* [previous][next][first][last][top][bottom][index][help]  */
 189 {
 190     // given
 191     mc_config_t *test_data;
 192     char *actual;
 193     const char *expected_result =
 194         "g6:group1p6:param1v10:some valuep6:param2v11:some value "
 195         "g6:group2p6:param1v4:truep6:param2v6:123456"
 196         "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n"
 197         "g6:group4p6:param1v5:falsep6:param2v6:654321";
 198 
 199     test_data = mc_config_init (NULL, FALSE);
 200 
 201     mc_config_set_string_raw (test_data, "group1", "param1", "some value");
 202     mc_config_set_string (test_data, "group1", "param2", "some value ");
 203 
 204     mc_config_set_bool (test_data, "group2", "param1", TRUE);
 205     mc_config_set_int (test_data, "group2", "param2", 123456);
 206 
 207     mc_config_set_string_raw (test_data, "group3", "param1", "::bla-bla::");
 208     mc_config_set_string (test_data, "group3", "param2", "bla-:p1:w:v2:12:g3:123:bla-bla\n");
 209 
 210     mc_config_set_bool (test_data, "group4", "param1", FALSE);
 211     mc_config_set_int (test_data, "group4", "param2", 654321);
 212 
 213     // when
 214     actual = mc_serialize_config (test_data, &error);
 215     mc_config_deinit (test_data);
 216 
 217     // then
 218     mctest_assert_not_null (actual);
 219     mctest_assert_str_eq (actual, expected_result);
 220 
 221     g_free (actual);
 222 }
 223 END_TEST
 224 
 225 /* --------------------------------------------------------------------------------------------- */
 226 /* @DataSource("test_deserialize_config_incorrect_ds") */
 227 static const struct test_deserialize_config_incorrect_ds
 228 {
 229     const char *input_string;
 230     const int expected_error_code;
 231     const char *expected_error_string;
 232 } test_deserialize_config_incorrect_ds[] = {
 233     {
 234         "g123error in group name",
 235         0,  // FIXME, TODO
 236         "mc_deserialize_config() at 1: mc_serialize_str(): Length delimiter ':' doesn't exists",
 237     },
 238     {
 239         "p6:param1v10:some valuep6:param2v11:some value ",
 240         0,  // FIXME, TODO
 241         "mc_deserialize_config() at 1: mc_serialize_str(): String prefix doesn't equal to 'g'",
 242     },
 243     {
 244         "g6:group1v10:some valuep6:param2v11:some value ",
 245         0,  // FIXME, TODO
 246         "mc_deserialize_config() at 10: mc_serialize_str(): String prefix doesn't equal to 'p'",
 247     },
 248     {
 249         "g6:group1p6000:param2v11:some value ",
 250         0,  // FIXME, TODO
 251         "mc_deserialize_config() at 10: mc_serialize_str(): Specified data length (6000) is "
 252         "greater "
 253         "than actual data length (21)",
 254     },
 255 };
 256 /* @Test(dataSource = "test_deserialize_config_incorrect_ds") */
 257 START_PARAMETRIZED_TEST (test_deserialize_config_incorrect, test_deserialize_config_incorrect_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 258 {
 259     // given
 260     mc_config_t *actual_result;
 261 
 262     // when
 263     actual_result = mc_deserialize_config (data->input_string, &error);
 264 
 265     // then
 266     mctest_assert_null (actual_result);
 267 
 268     ck_assert_int_eq (error->code, data->expected_error_code);
 269     mctest_assert_str_eq (error->message, data->expected_error_string);
 270 }
 271 END_PARAMETRIZED_TEST
 272 
 273 /* --------------------------------------------------------------------------------------------- */
 274 
 275 START_TEST (test_deserialize_config)
     /* [previous][next][first][last][top][bottom][index][help]  */
 276 {
 277     // given
 278     mc_config_t *actual;
 279     char *actual_value;
 280 
 281     // when
 282     actual = mc_deserialize_config (deserialize_input_value1, &error);
 283 
 284     // then
 285     mctest_assert_not_null (actual);
 286 
 287     actual_value = mc_config_get_string_raw (actual, "group1", "param1", "");
 288     mctest_assert_str_eq (actual_value, "some value");
 289     g_free (actual_value);
 290 
 291     actual_value = mc_config_get_string (actual, "group1", "param2", "");
 292     mctest_assert_str_eq (actual_value, "some value ");
 293     g_free (actual_value);
 294 
 295     mctest_assert_true (mc_config_get_bool (actual, "group2", "param1", FALSE));
 296 
 297     ck_assert_int_eq (mc_config_get_int (actual, "group2", "param2", 0), 123456);
 298 
 299     actual_value = mc_config_get_string_raw (actual, "group3", "param1", "");
 300     mctest_assert_str_eq (actual_value, "::bla-bla::");
 301     g_free (actual_value);
 302 
 303     actual_value = mc_config_get_string (actual, "group3", "param2", "");
 304     mctest_assert_str_eq (actual_value, "bla-:p1:w:v2:12:g3:123:bla-bla\n");
 305     g_free (actual_value);
 306 
 307     mctest_assert_false (mc_config_get_bool (actual, "group4", "param1", TRUE));
 308 
 309     ck_assert_int_eq (mc_config_get_int (actual, "group4", "param2", 0), 654321);
 310 
 311     mc_config_deinit (actual);
 312 }
 313 END_TEST
 314 
 315 #undef input_value
 316 /* --------------------------------------------------------------------------------------------- */
 317 
 318 int
 319 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 320 {
 321     TCase *tc_core;
 322 
 323     tc_core = tcase_create ("Core");
 324 
 325     tcase_add_checked_fixture (tc_core, setup, teardown);
 326 
 327     // Add new tests here: ***************
 328     mctest_add_parameterized_test (tc_core, test_serialize, test_serialize_ds);
 329 
 330     mctest_add_parameterized_test (tc_core, test_deserialize_incorrect,
 331                                    test_deserialize_incorrect_ds);
 332 
 333     mctest_add_parameterized_test (tc_core, test_deserialize, test_deserialize_ds);
 334 
 335     tcase_add_test (tc_core, test_serialize_config);
 336 
 337     mctest_add_parameterized_test (tc_core, test_deserialize_config_incorrect,
 338                                    test_deserialize_config_incorrect_ds);
 339 
 340     tcase_add_test (tc_core, test_deserialize_config);
 341     // ***********************************
 342 
 343     return mctest_run_all (tc_core);
 344 }
 345 
 346 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */