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/vfs - common serialize/deserialize functions
   3 
   4    Copyright (C) 2011-2019
   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 <http://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 /* *INDENT-OFF* */
  65 static const struct test_serialize_ds
  66 {
  67     const char input_char_prefix;
  68     const char *input_string;
  69     const char *expected_result;
  70 } test_serialize_ds[] =
  71 {
  72     {
  73         's',
  74         "some test string",
  75         "s16:some test string"
  76     },
  77     {
  78         'a',
  79         "some test test test string",
  80         "a26:some test test test string"
  81     },
  82 };
  83 /* *INDENT-ON* */
  84 /* @Test(dataSource = "test_serialize_ds") */
  85 /* *INDENT-OFF* */
  86 START_PARAMETRIZED_TEST (test_serialize, test_serialize_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
  87 /* *INDENT-ON* */
  88 {
  89     /* given */
  90     char *actual_result;
  91 
  92     /* when */
  93     actual_result = mc_serialize_str (data->input_char_prefix, data->input_string, &error);
  94 
  95     /* then */
  96     mctest_assert_str_eq (actual_result, data->expected_result);
  97 
  98     g_free (actual_result);
  99 
 100 }
 101 /* *INDENT-OFF* */
 102 END_PARAMETRIZED_TEST
 103 /* *INDENT-ON* */
 104 
 105 /* --------------------------------------------------------------------------------------------- */
 106 
 107 /* @DataSource("test_deserialize_incorrect_ds") */
 108 /* *INDENT-OFF* */
 109 static const struct test_deserialize_incorrect_ds
 110 {
 111     const char input_char_prefix;
 112     const char *input_string;
 113     const int  expected_error_code;
 114     const char *expected_error_string;
 115 } test_deserialize_incorrect_ds[] =
 116 {
 117     {
 118         's',
 119         NULL,
 120         0,      /* FIXME, TODO */
 121         "mc_serialize_str(): Input data is NULL or empty."
 122     },
 123     {
 124         's',
 125         "incorrect string",
 126         0,      /* FIXME, TODO */
 127         "mc_serialize_str(): String prefix doesn't equal to 's'"
 128     },
 129     {
 130         's',
 131         "s12345string without delimiter",
 132         0,      /* FIXME, TODO */
 133         "mc_serialize_str(): Length delimiter ':' doesn't exists"
 134     },
 135     {
 136         's',
 137         "s1234567890123456789012345678901234567890123456789012345678901234567890:too big number",
 138         0,      /* FIXME, TODO */
 139         "mc_serialize_str(): Too big string length"
 140     },
 141     {
 142         's',
 143         "s500:actual string length less that specified length",
 144         0,      /* FIXME, TODO */
 145         "mc_serialize_str(): Specified data length (500) is greater than actual data length (47)"
 146     },
 147 };
 148 /* *INDENT-ON* */
 149 /* @Test(dataSource = "test_deserialize_incorrect_ds") */
 150 /* *INDENT-OFF* */
 151 START_PARAMETRIZED_TEST (test_deserialize_incorrect, test_deserialize_incorrect_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 152 /* *INDENT-ON* */
 153 {
 154     /* given */
 155     char *actual_result;
 156 
 157     /* when */
 158     actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);
 159 
 160     /* then */
 161     mctest_assert_null (actual_result);
 162 
 163     mctest_assert_int_eq (error->code, data->expected_error_code);
 164     mctest_assert_str_eq (error->message, data->expected_error_string);
 165 }
 166 /* *INDENT-OFF* */
 167 END_PARAMETRIZED_TEST
 168 /* *INDENT-ON* */
 169 
 170 /* --------------------------------------------------------------------------------------------- */
 171 
 172 /* @DataSource("test_deserialize_ds") */
 173 /* *INDENT-OFF* */
 174 static const struct test_deserialize_ds
 175 {
 176     const char input_char_prefix;
 177     const char *input_string;
 178     const char *expected_result;
 179 } test_deserialize_ds[] =
 180 {
 181     {
 182         's',
 183         "s10:actual string length great that specified length",
 184         "actual str"
 185     },
 186     {
 187         'r',
 188         "r21:The right test string",
 189         "The right test string"
 190     },
 191 };
 192 /* *INDENT-ON* */
 193 /* @Test(dataSource = "test_deserialize_ds") */
 194 /* *INDENT-OFF* */
 195 START_PARAMETRIZED_TEST (test_deserialize, test_deserialize_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 196 /* *INDENT-ON* */
 197 {
 198     /* given */
 199     char *actual_result;
 200 
 201     /* when */
 202     actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);
 203 
 204     /* then */
 205     mctest_assert_str_eq (actual_result, data->expected_result);
 206 
 207     g_free (actual_result);
 208 }
 209 /* *INDENT-OFF* */
 210 END_PARAMETRIZED_TEST
 211 /* *INDENT-ON* */
 212 
 213 /* --------------------------------------------------------------------------------------------- */
 214 
 215 /* *INDENT-OFF* */
 216 START_TEST (test_serialize_config)
     /* [previous][next][first][last][top][bottom][index][help]  */
 217 /* *INDENT-ON* */
 218 {
 219     /* given */
 220     mc_config_t *test_data;
 221     char *actual;
 222     const char *expected_result = "g6:group1p6:param1v10:some valuep6:param2v11:some value "
 223         "g6:group2p6:param1v4:truep6:param2v6:123456"
 224         "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n"
 225         "g6:group4p6:param1v5:falsep6:param2v6:654321";
 226 
 227     test_data = mc_config_init (NULL, FALSE);
 228 
 229     mc_config_set_string_raw (test_data, "group1", "param1", "some value");
 230     mc_config_set_string (test_data, "group1", "param2", "some value ");
 231 
 232     mc_config_set_bool (test_data, "group2", "param1", TRUE);
 233     mc_config_set_int (test_data, "group2", "param2", 123456);
 234 
 235     mc_config_set_string_raw (test_data, "group3", "param1", "::bla-bla::");
 236     mc_config_set_string (test_data, "group3", "param2", "bla-:p1:w:v2:12:g3:123:bla-bla\n");
 237 
 238     mc_config_set_bool (test_data, "group4", "param1", FALSE);
 239     mc_config_set_int (test_data, "group4", "param2", 654321);
 240 
 241     /* when */
 242     actual = mc_serialize_config (test_data, &error);
 243     mc_config_deinit (test_data);
 244 
 245     /* then */
 246     mctest_assert_not_null (actual);
 247     mctest_assert_str_eq (actual, expected_result);
 248 
 249     g_free (actual);
 250 }
 251 /* *INDENT-OFF* */
 252 END_TEST
 253 /* *INDENT-ON* */
 254 
 255 /* --------------------------------------------------------------------------------------------- */
 256 /* @DataSource("test_deserialize_config_incorrect_ds") */
 257 /* *INDENT-OFF* */
 258 static const struct test_deserialize_config_incorrect_ds
 259 {
 260     const char *input_string;
 261     const int  expected_error_code;
 262     const char *expected_error_string;
 263 } test_deserialize_config_incorrect_ds[] =
 264 {
 265     {
 266         "g123error in group name",
 267         0,      /* FIXME, TODO */
 268         "mc_deserialize_config() at 1: mc_serialize_str(): Length delimiter ':' doesn't exists"
 269     },
 270     {
 271         "p6:param1v10:some valuep6:param2v11:some value ",
 272         0,      /* FIXME, TODO */
 273         "mc_deserialize_config() at 1: mc_serialize_str(): String prefix doesn't equal to 'g'"
 274     },
 275     {
 276         "g6:group1v10:some valuep6:param2v11:some value ",
 277         0,      /* FIXME, TODO */
 278         "mc_deserialize_config() at 10: mc_serialize_str(): String prefix doesn't equal to 'p'"
 279     },
 280     {
 281         "g6:group1p6000:param2v11:some value ",
 282         0,      /* FIXME, TODO */
 283         "mc_deserialize_config() at 10: mc_serialize_str(): Specified data length (6000) is greater than actual data length (21)"
 284     },
 285 };
 286 /* *INDENT-ON* */
 287 /* @Test(dataSource = "test_deserialize_config_incorrect_ds") */
 288 /* *INDENT-OFF* */
 289 START_PARAMETRIZED_TEST (test_deserialize_config_incorrect, test_deserialize_config_incorrect_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 290 /* *INDENT-ON* */
 291 {
 292     /* given */
 293     mc_config_t *actual_result;
 294 
 295     /* when */
 296     actual_result = mc_deserialize_config (data->input_string, &error);
 297 
 298     /* then */
 299     mctest_assert_null (actual_result);
 300 
 301     mctest_assert_int_eq (error->code, data->expected_error_code);
 302     mctest_assert_str_eq (error->message, data->expected_error_string);
 303 }
 304 /* *INDENT-OFF* */
 305 END_PARAMETRIZED_TEST
 306 /* *INDENT-ON* */
 307 
 308 /* --------------------------------------------------------------------------------------------- */
 309 
 310 /* *INDENT-OFF* */
 311 START_TEST (test_deserialize_config)
     /* [previous][next][first][last][top][bottom][index][help]  */
 312 /* *INDENT-ON* */
 313 {
 314     /* given */
 315     mc_config_t *actual;
 316     char *actual_value;
 317 
 318     /* when */
 319     actual = mc_deserialize_config (deserialize_input_value1, &error);
 320 
 321     /* then */
 322     mctest_assert_not_null (actual);
 323 
 324     actual_value = mc_config_get_string_raw (actual, "group1", "param1", "");
 325     mctest_assert_str_eq (actual_value, "some value");
 326     g_free (actual_value);
 327 
 328     actual_value = mc_config_get_string (actual, "group1", "param2", "");
 329     mctest_assert_str_eq (actual_value, "some value ");
 330     g_free (actual_value);
 331 
 332     mctest_assert_int_eq (mc_config_get_bool (actual, "group2", "param1", FALSE), TRUE);
 333 
 334     mctest_assert_int_eq (mc_config_get_int (actual, "group2", "param2", 0), 123456);
 335 
 336     actual_value = mc_config_get_string_raw (actual, "group3", "param1", "");
 337     mctest_assert_str_eq (actual_value, "::bla-bla::");
 338     g_free (actual_value);
 339 
 340     actual_value = mc_config_get_string (actual, "group3", "param2", "");
 341     mctest_assert_str_eq (actual_value, "bla-:p1:w:v2:12:g3:123:bla-bla\n");
 342     g_free (actual_value);
 343 
 344     mctest_assert_int_eq (mc_config_get_bool (actual, "group4", "param1", TRUE), FALSE);
 345 
 346     mctest_assert_int_eq (mc_config_get_int (actual, "group4", "param2", 0), 654321);
 347 
 348     mc_config_deinit (actual);
 349 }
 350 /* *INDENT-OFF* */
 351 END_TEST
 352 /* *INDENT-ON* */
 353 
 354 #undef input_value
 355 /* --------------------------------------------------------------------------------------------- */
 356 
 357 int
 358 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 359 {
 360     int number_failed;
 361 
 362     Suite *s = suite_create (TEST_SUITE_NAME);
 363     TCase *tc_core = tcase_create ("Core");
 364     SRunner *sr;
 365 
 366     tcase_add_checked_fixture (tc_core, setup, teardown);
 367 
 368     /* Add new tests here: *************** */
 369     mctest_add_parameterized_test (tc_core, test_serialize, test_serialize_ds);
 370 
 371     mctest_add_parameterized_test (tc_core, test_deserialize_incorrect,
 372                                    test_deserialize_incorrect_ds);
 373 
 374     mctest_add_parameterized_test (tc_core, test_deserialize, test_deserialize_ds);
 375 
 376     tcase_add_test (tc_core, test_serialize_config);
 377 
 378     mctest_add_parameterized_test (tc_core, test_deserialize_config_incorrect,
 379                                    test_deserialize_config_incorrect_ds);
 380 
 381     tcase_add_test (tc_core, test_deserialize_config);
 382     /* *********************************** */
 383 
 384     suite_add_tcase (s, tc_core);
 385     sr = srunner_create (s);
 386     srunner_set_log (sr, "serialize.log");
 387     srunner_run_all (sr, CK_ENV);
 388     number_failed = srunner_ntests_failed (sr);
 389     srunner_free (sr);
 390     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 391 }
 392 
 393 /* --------------------------------------------------------------------------------------------- */

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