This source file includes following definitions.
- setup
- teardown
- START_PARAMETRIZED_TEST
- START_PARAMETRIZED_TEST
- START_PARAMETRIZED_TEST
- START_TEST
- START_PARAMETRIZED_TEST
- START_TEST
- main
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 #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
44 static void
45 setup (void)
46 {
47 str_init_strings (NULL);
48 error = NULL;
49 }
50
51
52
53
54 static void
55 teardown (void)
56 {
57 g_clear_error (&error);
58 str_uninit_strings ();
59 }
60
61
62
63
64
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
84
85
86 START_PARAMETRIZED_TEST (test_serialize, test_serialize_ds)
87
88 {
89
90 char *actual_result;
91
92
93 actual_result = mc_serialize_str (data->input_char_prefix, data->input_string, &error);
94
95
96 mctest_assert_str_eq (actual_result, data->expected_result);
97
98 g_free (actual_result);
99
100 if (error != NULL)
101 g_error_free (error);
102 }
103
104 END_PARAMETRIZED_TEST
105
106
107
108
109
110
111 static const struct test_deserialize_incorrect_ds
112 {
113 const char input_char_prefix;
114 const char *input_string;
115 const int expected_error_code;
116 const char *expected_error_string;
117 } test_deserialize_incorrect_ds[] =
118 {
119 {
120 's',
121 NULL,
122 0,
123 "mc_serialize_str(): Input data is NULL or empty."
124 },
125 {
126 's',
127 "incorrect string",
128 0,
129 "mc_serialize_str(): String prefix doesn't equal to 's'"
130 },
131 {
132 's',
133 "s12345string without delimiter",
134 0,
135 "mc_serialize_str(): Length delimiter ':' doesn't exists"
136 },
137 {
138 's',
139 "s1234567890123456789012345678901234567890123456789012345678901234567890:too big number",
140 0,
141 "mc_serialize_str(): Too big string length"
142 },
143 {
144 's',
145 "s500:actual string length less that specified length",
146 0,
147 "mc_serialize_str(): Specified data length (500) is greater than actual data length (47)"
148 },
149 };
150
151
152
153 START_PARAMETRIZED_TEST (test_deserialize_incorrect, test_deserialize_incorrect_ds)
154
155 {
156
157 char *actual_result;
158
159
160 actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);
161
162
163 mctest_assert_null (actual_result);
164
165 ck_assert_int_eq (error->code, data->expected_error_code);
166 mctest_assert_str_eq (error->message, data->expected_error_string);
167 }
168
169 END_PARAMETRIZED_TEST
170
171
172
173
174
175
176 static const struct test_deserialize_ds
177 {
178 const char input_char_prefix;
179 const char *input_string;
180 const char *expected_result;
181 } test_deserialize_ds[] =
182 {
183 {
184 's',
185 "s10:actual string length great that specified length",
186 "actual str"
187 },
188 {
189 'r',
190 "r21:The right test string",
191 "The right test string"
192 },
193 };
194
195
196
197 START_PARAMETRIZED_TEST (test_deserialize, test_deserialize_ds)
198
199 {
200
201 char *actual_result;
202
203
204 actual_result = mc_deserialize_str (data->input_char_prefix, data->input_string, &error);
205
206
207 mctest_assert_str_eq (actual_result, data->expected_result);
208
209 g_free (actual_result);
210 }
211
212 END_PARAMETRIZED_TEST
213
214
215
216
217
218 START_TEST (test_serialize_config)
219
220 {
221
222 mc_config_t *test_data;
223 char *actual;
224 const char *expected_result = "g6:group1p6:param1v10:some valuep6:param2v11:some value "
225 "g6:group2p6:param1v4:truep6:param2v6:123456"
226 "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n"
227 "g6:group4p6:param1v5:falsep6:param2v6:654321";
228
229 test_data = mc_config_init (NULL, FALSE);
230
231 mc_config_set_string_raw (test_data, "group1", "param1", "some value");
232 mc_config_set_string (test_data, "group1", "param2", "some value ");
233
234 mc_config_set_bool (test_data, "group2", "param1", TRUE);
235 mc_config_set_int (test_data, "group2", "param2", 123456);
236
237 mc_config_set_string_raw (test_data, "group3", "param1", "::bla-bla::");
238 mc_config_set_string (test_data, "group3", "param2", "bla-:p1:w:v2:12:g3:123:bla-bla\n");
239
240 mc_config_set_bool (test_data, "group4", "param1", FALSE);
241 mc_config_set_int (test_data, "group4", "param2", 654321);
242
243
244 actual = mc_serialize_config (test_data, &error);
245 mc_config_deinit (test_data);
246
247
248 mctest_assert_not_null (actual);
249 mctest_assert_str_eq (actual, expected_result);
250
251 g_free (actual);
252 }
253
254 END_TEST
255
256
257
258
259
260 static const struct test_deserialize_config_incorrect_ds
261 {
262 const char *input_string;
263 const int expected_error_code;
264 const char *expected_error_string;
265 } test_deserialize_config_incorrect_ds[] =
266 {
267 {
268 "g123error in group name",
269 0,
270 "mc_deserialize_config() at 1: mc_serialize_str(): Length delimiter ':' doesn't exists"
271 },
272 {
273 "p6:param1v10:some valuep6:param2v11:some value ",
274 0,
275 "mc_deserialize_config() at 1: mc_serialize_str(): String prefix doesn't equal to 'g'"
276 },
277 {
278 "g6:group1v10:some valuep6:param2v11:some value ",
279 0,
280 "mc_deserialize_config() at 10: mc_serialize_str(): String prefix doesn't equal to 'p'"
281 },
282 {
283 "g6:group1p6000:param2v11:some value ",
284 0,
285 "mc_deserialize_config() at 10: mc_serialize_str(): Specified data length (6000) is greater than actual data length (21)"
286 },
287 };
288
289
290
291 START_PARAMETRIZED_TEST (test_deserialize_config_incorrect, test_deserialize_config_incorrect_ds)
292
293 {
294
295 mc_config_t *actual_result;
296
297
298 actual_result = mc_deserialize_config (data->input_string, &error);
299
300
301 mctest_assert_null (actual_result);
302
303 ck_assert_int_eq (error->code, data->expected_error_code);
304 mctest_assert_str_eq (error->message, data->expected_error_string);
305 }
306
307 END_PARAMETRIZED_TEST
308
309
310
311
312
313 START_TEST (test_deserialize_config)
314
315 {
316
317 mc_config_t *actual;
318 char *actual_value;
319
320
321 actual = mc_deserialize_config (deserialize_input_value1, &error);
322
323
324 mctest_assert_not_null (actual);
325
326 actual_value = mc_config_get_string_raw (actual, "group1", "param1", "");
327 mctest_assert_str_eq (actual_value, "some value");
328 g_free (actual_value);
329
330 actual_value = mc_config_get_string (actual, "group1", "param2", "");
331 mctest_assert_str_eq (actual_value, "some value ");
332 g_free (actual_value);
333
334 mctest_assert_true (mc_config_get_bool (actual, "group2", "param1", FALSE));
335
336 ck_assert_int_eq (mc_config_get_int (actual, "group2", "param2", 0), 123456);
337
338 actual_value = mc_config_get_string_raw (actual, "group3", "param1", "");
339 mctest_assert_str_eq (actual_value, "::bla-bla::");
340 g_free (actual_value);
341
342 actual_value = mc_config_get_string (actual, "group3", "param2", "");
343 mctest_assert_str_eq (actual_value, "bla-:p1:w:v2:12:g3:123:bla-bla\n");
344 g_free (actual_value);
345
346 mctest_assert_false (mc_config_get_bool (actual, "group4", "param1", TRUE));
347
348 ck_assert_int_eq (mc_config_get_int (actual, "group4", "param2", 0), 654321);
349
350 mc_config_deinit (actual);
351 }
352
353 END_TEST
354
355
356 #undef input_value
357
358
359 int
360 main (void)
361 {
362 TCase *tc_core;
363
364 tc_core = tcase_create ("Core");
365
366 tcase_add_checked_fixture (tc_core, setup, teardown);
367
368
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 return mctest_run_all (tc_core);
385 }
386
387