summaryrefslogtreecommitdiffstats
path: root/tests/lib/serialize.c
blob: e1b355161f525892bc28f5f15f7ab9e7301094df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
   lib - common serialize/deserialize functions

   Copyright (C) 2011-2024
   Free Software Foundation, Inc.

   Written by:
   Slava Zanko <slavazanko@gmail.com>, 2011, 2013

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define TEST_SUITE_NAME "/lib"

#include "tests/mctest.h"

#include "lib/strutil.h"
#include "lib/serialize.h"

static GError *error = NULL;

static const char *deserialize_input_value1 =
    "g6:group1p6:param1v10:some valuep6:param2v11:some value "
    "g6:group2p6:param1v4:truep6:param2v6:123456"
    "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n"
    "g6:group4p6:param1v5:falsep6:param2v6:654321";

/* --------------------------------------------------------------------------------------------- */

/* @Before */
static void
setup (void)
{
    str_init_strings (NULL);
    error = NULL;
}

/* --------------------------------------------------------------------------------------------- */

/* @After */
static void
teardown (void)
{
    g_clear_error (&error);
    str_uninit_strings ();
}

/* --------------------------------------------------------------------------------------------- */

/* @DataSource("test_serialize_ds") */
/* *INDENT-OFF* */
static const struct test_serialize_ds
{
    const char input_char_prefix;
    const char *input_string;
    const char *expected_result;
} test_serialize_ds[] =
{
    {
        's',
        "some test string",
        "s16:some test string"
    },
    {
        'a',
        "some test test test string",
        "a26:some test test test string"
    },
};
/* *INDENT-ON* */
/* @Test(dataSource = "test_serialize_ds") */
/* *INDENT-OFF* */
START_PARAMETRIZED_TEST (test_serialize, test_serialize_ds)
/* *INDENT-ON* */
{
    /* given */
    char *actual_result;

    /* when */
    actual_result = mc_serialize_str (data->input_char_prefix, data->input_string, &error);

    /* then */
    mctest_assert_str_eq (actual_result, data->expected_result);

    g_free (actual_result);

    if (error != NULL)
        g_error_free (error);
}
/* *INDENT-OFF* */
END_PARAMETRIZED_TEST
/* *INDENT-ON* */

/* --------------------------------------------------------------------------------------------- */

/* @DataSource("test_deserialize_incorrect_ds") */
/* *INDENT-OFF* */
static const struct test_deserialize_incorrect_ds
{
    const char input_char_prefix;
    const char *input_string;
    const int  expected_error_code;
    const char *expected_error_string;
} test_deserialize_incorrect_ds[] =
{
    {
        's',
        NULL,
        0,      /* FIXME, TODO */
        "mc_serialize_str(): Input data is NULL or empty."
    },
    {
        's',
        "incorrect string",
        0,      /* FIXME, TODO */
        "mc_serialize_str(): String prefix doesn't equal to 's'"
    },
    {
        's',
        "s12345string without delimiter",
        0,      /* FIXME, TODO */
        "mc_serialize_str(): Length delimiter ':' doesn't exists"
    },
    {
        's',
        "s1234567890123456789012345678901234567890123456789012345678901234567890:too big number",
        0,      /* FIXME, TODO */
        "mc_serialize_str(): Too big string length"
    },
    {
        's',
        "s500:actual string length less that specified length",
        0,      /* FIXME, TODO */
        "mc_serialize_str(): Specified data length (500) is greater than actual data length (47)"
    },
};
/* *INDENT-ON* */
/* @Test(dataSource = "test_deserialize_incorrect_ds") */
/* *INDENT-OFF* */
START_PARAMETRIZED_TEST (test_deserialize_incorrect, test_deserialize_incorrect_ds)
/* *INDENT-ON* */
{
    /* given */
    char *actual_result;

    /* when */
    actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);

    /* then */
    mctest_assert_null (actual_result);

    ck_assert_int_eq (error->code, data->expected_error_code);
    mctest_assert_str_eq (error->message, data->expected_error_string);
}
/* *INDENT-OFF* */
END_PARAMETRIZED_TEST
/* *INDENT-ON* */

/* --------------------------------------------------------------------------------------------- */

/* @DataSource("test_deserialize_ds") */
/* *INDENT-OFF* */
static const struct test_deserialize_ds
{
    const char input_char_prefix;
    const char *input_string;
    const char *expected_result;
} test_deserialize_ds[] =
{
    {
        's',
        "s10:actual string length great that specified length",
        "actual str"
    },
    {
        'r',
        "r21:The right test string",
        "The right test string"
    },
};
/* *INDENT-ON* */
/* @Test(dataSource = "test_deserialize_ds") */
/* *INDENT-OFF* */
START_PARAMETRIZED_TEST (test_deserialize, test_deserialize_ds)
/* *INDENT-ON* */
{
    /* given */
    char *actual_result;

    /* when */
    actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);

    /* then */
    mctest_assert_str_eq (actual_result, data->expected_result);

    g_free (actual_result);
}
/* *INDENT-OFF* */
END_PARAMETRIZED_TEST
/* *INDENT-ON* */

/* --------------------------------------------------------------------------------------------- */

/* *INDENT-OFF* */
START_TEST (test_serialize_config)
/* *INDENT-ON* */
{
    /* given */
    mc_config_t *test_data;
    char *actual;
    const char *expected_result = "g6:group1p6:param1v10:some valuep6:param2v11:some value "
        "g6:group2p6:param1v4:truep6:param2v6:123456"
        "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n"
        "g6:group4p6:param1v5:falsep6:param2v6:654321";

    test_data = mc_config_init (NULL, FALSE);

    mc_config_set_string_raw (test_data, "group1", "param1", "some value");
    mc_config_set_string (test_data, "group1", "param2", "some value ");

    mc_config_set_bool (test_data, "group2", "param1", TRUE);
    mc_config_set_int (test_data, "group2", "param2", 123456);

    mc_config_set_string_raw (test_data, "group3", "param1", "::bla-bla::");
    mc_config_set_string (test_data, "group3", "param2", "bla-:p1:w:v2:12:g3:123:bla-bla\n");

    mc_config_set_bool (test_data, "group4", "param1", FALSE);
    mc_config_set_int (test_data, "group4", "param2", 654321);

    /* when */
    actual = mc_serialize_config (test_data, &error);
    mc_config_deinit (test_data);

    /* then */
    mctest_assert_not_null (actual);
    mctest_assert_str_eq (actual, expected_result);

    g_free (actual);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

/* --------------------------------------------------------------------------------------------- */
/* @DataSource("test_deserialize_config_incorrect_ds") */
/* *INDENT-OFF* */
static const struct test_deserialize_config_incorrect_ds
{
    const char *input_string;
    const int  expected_error_code;
    const char *expected_error_string;
} test_deserialize_config_incorrect_ds[] =
{
    {
        "g123error in group name",
        0,      /* FIXME, TODO */
        "mc_deserialize_config() at 1: mc_serialize_str(): Length delimiter ':' doesn't exists"
    },
    {
        "p6:param1v10:some valuep6:param2v11:some value ",
        0,      /* FIXME, TODO */
        "mc_deserialize_config() at 1: mc_serialize_str(): String prefix doesn't equal to 'g'"
    },
    {
        "g6:group1v10:some valuep6:param2v11:some value ",
        0,      /* FIXME, TODO */
        "mc_deserialize_config() at 10: mc_serialize_str(): String prefix doesn't equal to 'p'"
    },
    {
        "g6:group1p6000:param2v11:some value ",
        0,      /* FIXME, TODO */
        "mc_deserialize_config() at 10: mc_serialize_str(): Specified data length (6000) is greater than actual data length (21)"
    },
};
/* *INDENT-ON* */
/* @Test(dataSource = "test_deserialize_config_incorrect_ds") */
/* *INDENT-OFF* */
START_PARAMETRIZED_TEST (test_deserialize_config_incorrect, test_deserialize_config_incorrect_ds)
/* *INDENT-ON* */
{
    /* given */
    mc_config_t *actual_result;

    /* when */
    actual_result = mc_deserialize_config (data->input_string, &error);

    /* then */
    mctest_assert_null (actual_result);

    ck_assert_int_eq (error->code, data->expected_error_code);
    mctest_assert_str_eq (error->message, data->expected_error_string);
}
/* *INDENT-OFF* */
END_PARAMETRIZED_TEST
/* *INDENT-ON* */

/* --------------------------------------------------------------------------------------------- */

/* *INDENT-OFF* */
START_TEST (test_deserialize_config)
/* *INDENT-ON* */
{
    /* given */
    mc_config_t *actual;
    char *actual_value;

    /* when */
    actual = mc_deserialize_config (deserialize_input_value1, &error);

    /* then */
    mctest_assert_not_null (actual);

    actual_value = mc_config_get_string_raw (actual, "group1", "param1", "");
    mctest_assert_str_eq (actual_value, "some value");
    g_free (actual_value);

    actual_value = mc_config_get_string (actual, "group1", "param2", "");
    mctest_assert_str_eq (actual_value, "some value ");
    g_free (actual_value);

    mctest_assert_true (mc_config_get_bool (actual, "group2", "param1", FALSE));

    ck_assert_int_eq (mc_config_get_int (actual, "group2", "param2", 0), 123456);

    actual_value = mc_config_get_string_raw (actual, "group3", "param1", "");
    mctest_assert_str_eq (actual_value, "::bla-bla::");
    g_free (actual_value);

    actual_value = mc_config_get_string (actual, "group3", "param2", "");
    mctest_assert_str_eq (actual_value, "bla-:p1:w:v2:12:g3:123:bla-bla\n");
    g_free (actual_value);

    mctest_assert_false (mc_config_get_bool (actual, "group4", "param1", TRUE));

    ck_assert_int_eq (mc_config_get_int (actual, "group4", "param2", 0), 654321);

    mc_config_deinit (actual);
}
/* *INDENT-OFF* */
END_TEST
/* *INDENT-ON* */

#undef input_value
/* --------------------------------------------------------------------------------------------- */

int
main (void)
{
    TCase *tc_core;

    tc_core = tcase_create ("Core");

    tcase_add_checked_fixture (tc_core, setup, teardown);

    /* Add new tests here: *************** */
    mctest_add_parameterized_test (tc_core, test_serialize, test_serialize_ds);

    mctest_add_parameterized_test (tc_core, test_deserialize_incorrect,
                                   test_deserialize_incorrect_ds);

    mctest_add_parameterized_test (tc_core, test_deserialize, test_deserialize_ds);

    tcase_add_test (tc_core, test_serialize_config);

    mctest_add_parameterized_test (tc_core, test_deserialize_config_incorrect,
                                   test_deserialize_config_incorrect_ds);

    tcase_add_test (tc_core, test_deserialize_config);
    /* *********************************** */

    return mctest_run_all (tc_core);
}

/* --------------------------------------------------------------------------------------------- */