vici: Return default value for get_int() if message value is empty string
[strongswan.git] / src / libcharon / plugins / vici / suites / test_message.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <test_suite.h>
17
18 #include "../vici_message.h"
19 #include "../vici_builder.h"
20
21 #include <unistd.h>
22
23 static char blob[] = {
24 0xd3,0xe5,0xee,0x37,0x7b,0x96,0x2f,0x3e,0x5f,0x3e,0x91,0xea,0x38,0x44,0xba,0x6c,
25 0x75,0xc8,0x42,0x32,0xaf,0x7a,0x66,0x43,0x33,0x92,0xd2,0xef,0x7d,0x91,0x7b,0x59,
26 0x9f,0x9f,0xd1,0x44,0xb6,0x1e,0x8c,0xd1,0xc5,0xa0,0xd9,0xe4,0xf2,0x31,0xfd,0x7b,
27 0x5b,0x56,0xa7,0xfe,0x63,0x0d,0xcb,0x31,0x74,0xd8,0xd6,0x4a,0x42,0x3a,0x88,0xf3,
28 0x79,0xf9,0x41,0xa6,0xc0,0x64,0x53,0x31,0x42,0xe2,0xd4,0x4a,0x22,0x5f,0x3f,0x99,
29 0xe0,0x1a,0xcb,0x93,0x26,0xd0,0xec,0xac,0x90,0x97,0x0a,0x5f,0x69,0x86,0xf1,0xda,
30 0xfc,0xa7,0xac,0xd0,0xd8,0x81,0xcf,0x7d,0x47,0x22,0xbe,0xbf,0x00,0x9b,0x6b,0x86,
31 0x92,0x89,0xbe,0x7f,0x74,0x13,0x53,0xf1,0x4c,0x2b,0xc9,0xe1,0x39,0xd6,0xfc,0x50,
32 0x3f,0x00,0xfb,0x76,0x42,0xa6,0xa4,0x70,0xfc,0x93,0x17,0x4a,0x35,0xce,0x5e,0x78,
33 0x41,0x88,0x24,0x50,0x78,0xf2,0x38,0x08,0xff,0x40,0xef,0x61,0xbb,0xbf,0x16,0xff,
34 0x0b,0xf6,0x33,0x21,0xcb,0x48,0xbd,0x7d,0xd1,0x73,0xfa,0x6d,0xd6,0xab,0xde,0x69,
35 0x63,0x17,0xdb,0x52,0xe2,0x75,0x4b,0xb7,0x1e,0xf0,0x8a,0x55,0x4f,0x70,0x8d,0x18,
36 0xe5,0x38,0x6a,0x9f,0xb8,0x06,0xb5,0x91,0x90,0x2b,0xc5,0x67,0xa9,0x12,0xe5,0xf3,
37 0x48,0x2f,0x80,0x03,0xa1,0xa0,0xfc,0x43,0xe9,0x0f,0x83,0x2b,0xbc,0x7c,0xa8,0x3b,
38 0x6c,0xc1,0xc8,0x72,0x5f,0x87,0x63,0x77,0x93,0x9b,0xe2,0xd7,0x4e,0xe6,0x65,0xa1,
39 0x69,0x00,0xda,0xf8,0xb4,0x61,0xee,0xb7,0x20,0xe7,0x2a,0x35,0x23,0xf0,0x37,0x4b,
40 0x67,0xcf,0x8d,0x85,0x72,0x22,0x6d,0x7a,0xb2,0x96,0xff,0x49,0xf4,0x94,0x3e,0x7e,
41 0x87,0x26,0x5d,0x34,0x05,0x26,0x60,0x9b,0x89,0xfe,0xf9,0x91,0xd3,0x03,0xe7,0x8a,
42 0x03,0xf6,0x4e,0xbf,0x68,0x13,0xc6,0xf2,0x7b,0x9c,0xe6,0x36,0x1b,0xe2,0x22,0x44,
43 0xb1,0x19,0x34,0x5f,0xe8,0x44,0x48,0x3a,0x19,0xe4,0xbd,0xb0,0x4e,0xb5,0x2c,0x40,
44 0x55,0x39,0xe6,0x4c,0xd5,0x68,0x34,0x72,0x6b,0x6d,0x88,0xce,0x7e,0x77,0x95,0x17,
45 0x2e,0x68,0x3f,0x0e,0x9d,0x70,0x9a,0x22,0xfa,0x19,0xcc,0x15,0x9d,0xba,0xaa,0xec,
46 0xb1,0x67,0x19,0x51,0xce,0x60,0x9a,0x38,0xf8,0xa7,0x4e,0xe3,0x25,0x47,0x1e,0x1d,
47 0x30,0x76,0x91,0x8f,0x4d,0x13,0x59,0x06,0x2f,0x01,0x10,0x95,0xdb,0x08,0x7c,0x46,
48 0xed,0x47,0xa1,0x19,0x4c,0x46,0xd1,0x3a,0x3f,0x88,0x7a,0x63,0xae,0x29,0x13,0x42,
49 0xe9,0x17,0xe8,0xa9,0x95,0xfc,0xd1,0xea,0xfa,0x59,0x90,0xfe,0xb7,0xbb,0x7f,0x61,
50 0x1b,0xcb,0x3d,0x12,0x99,0x96,0x3e,0x23,0x23,0xec,0x3a,0x4d,0x86,0x86,0x74,0xef,
51 0x38,0xa6,0xdc,0x3a,0x83,0x85,0xf8,0xb8,0xad,0x5b,0x33,0x94,0x4d,0x0e,0x68,0xbc,
52 0xf2,0xc7,0x6f,0x84,0x18,0x1e,0x5a,0x66,0x1f,0x6c,0x98,0x33,0xda,0xde,0x9e,0xda,
53 0x82,0xd0,0x56,0x44,0x47,0x08,0x0c,0x07,0x81,0x9d,0x8b,0x64,0x16,0x73,0x9d,0x80,
54 0x54,0x9c,0x4c,0x42,0xde,0x27,0x4e,0x97,0xb2,0xcf,0x48,0xaf,0x7e,0x85,0xc1,0xcd,
55 0x6a,0x4d,0x04,0x40,0x89,0xa3,0x9d,0x4e,0x89,0x56,0x60,0x31,0x1f,0x3f,0x49,0x16,
56 };
57
58 typedef struct {
59 vici_type_t type;
60 char *name;
61 chunk_t data;
62 } endecode_test_t;
63
64 static endecode_test_t endecode_test_simple[] = {
65 { VICI_SECTION_START, "section1", {} },
66 { VICI_KEY_VALUE, "key1", { "value1", 6 } },
67 { VICI_KEY_VALUE, "key2", { "value2", 6 } },
68 { VICI_SECTION_END, NULL, {} },
69 { VICI_END, NULL, {} },
70 };
71
72 static endecode_test_t endecode_test_nested[] = {
73 { VICI_SECTION_START, "section1", {} },
74 { VICI_SECTION_START, "section2", {} },
75 { VICI_SECTION_START, "section3", {} },
76 { VICI_KEY_VALUE, "key1", { "value1", 6 } },
77 { VICI_SECTION_START, "section4", {} },
78 { VICI_KEY_VALUE, "key2", { "value2", 6 } },
79 { VICI_SECTION_END, NULL, {} },
80 { VICI_SECTION_END, NULL, {} },
81 { VICI_SECTION_END, NULL, {} },
82 { VICI_KEY_VALUE, "key3", { "value3", 6 } },
83 { VICI_SECTION_END, NULL, {} },
84 { VICI_END, NULL, {} },
85 };
86
87 static endecode_test_t endecode_test_list[] = {
88 { VICI_SECTION_START, "section1", {} },
89 { VICI_LIST_START, "list1", {} },
90 { VICI_LIST_ITEM, NULL, { "item1", 5 } },
91 { VICI_LIST_ITEM, NULL, { "item2", 5 } },
92 { VICI_LIST_END, NULL, {} },
93 { VICI_KEY_VALUE, "key1", { "value1", 6 } },
94 { VICI_SECTION_END, NULL, {} },
95 { VICI_END, NULL, {} },
96 };
97
98 static endecode_test_t endecode_test_blobs[] = {
99 { VICI_KEY_VALUE, "key1", { blob, countof(blob) } },
100 { VICI_SECTION_START, "section1", {} },
101 { VICI_LIST_START, "list1", {} },
102 { VICI_LIST_ITEM, NULL, { blob, countof(blob) } },
103 { VICI_LIST_ITEM, NULL, { blob, countof(blob) } },
104 { VICI_LIST_END, NULL, {} },
105 { VICI_KEY_VALUE, "key2", { blob, countof(blob) } },
106 { VICI_SECTION_END, NULL, {} },
107 { VICI_END, NULL, {} },
108 };
109
110 static endecode_test_t *endecode_tests[] = {
111 endecode_test_simple,
112 endecode_test_nested,
113 endecode_test_list,
114 endecode_test_blobs,
115 };
116
117 typedef struct {
118 enumerator_t public;
119 endecode_test_t *next;
120 } endecode_enum_t;
121
122 static bool endecode_enumerate(endecode_enum_t *this, vici_type_t *type,
123 char **name, chunk_t *data)
124 {
125 if (this->next)
126 {
127 *type = this->next->type;
128 *name = this->next->name;
129 *data = this->next->data;
130 if (this->next->type == VICI_END)
131 {
132 this->next = NULL;
133 }
134 else
135 {
136 this->next++;
137 }
138 return TRUE;
139 }
140 return FALSE;
141 }
142
143 static enumerator_t *endecode_create_enumerator(endecode_test_t *test)
144 {
145 endecode_enum_t *enumerator;
146
147 INIT(enumerator,
148 .public = {
149 .enumerate = (void*)endecode_enumerate,
150 .destroy = (void*)free,
151 },
152 .next = test,
153 );
154
155 return &enumerator->public;
156 }
157
158 static void compare_vici(enumerator_t *parse, enumerator_t *tmpl)
159 {
160 vici_type_t type, ttype;
161 char *name, *tname;
162 chunk_t data, tdata;;
163
164 while (TRUE)
165 {
166 ck_assert(parse->enumerate(parse, &type, &name, &data));
167 ck_assert(tmpl->enumerate(tmpl, &ttype, &tname, &tdata));
168 ck_assert_int_eq(type, ttype);
169 switch (type)
170 {
171 case VICI_END:
172 return;
173 case VICI_SECTION_START:
174 case VICI_LIST_START:
175 ck_assert(streq(name, tname));
176 break;
177 case VICI_LIST_ITEM:
178 ck_assert(chunk_equals(data, tdata));
179 break;
180 case VICI_KEY_VALUE:
181 ck_assert(streq(name, tname));
182 ck_assert(chunk_equals(data, tdata));
183 break;
184 case VICI_SECTION_END:
185 case VICI_LIST_END:
186 break;
187 default:
188 ck_assert(FALSE);
189 break;
190 }
191 }
192 }
193
194 START_TEST(test_endecode)
195 {
196 enumerator_t *parse, *tmpl;
197 vici_message_t *m;
198 chunk_t data;
199
200 tmpl = endecode_create_enumerator(endecode_tests[_i]);
201 m = vici_message_create_from_enumerator(tmpl);
202 ck_assert(m);
203 data = chunk_clone(m->get_encoding(m));
204 tmpl = endecode_create_enumerator(endecode_tests[_i]);
205 parse = m->create_enumerator(m);
206 ck_assert(parse);
207 compare_vici(parse, tmpl);
208 tmpl->destroy(tmpl);
209 parse->destroy(parse);
210 m->destroy(m);
211
212 m = vici_message_create_from_data(data, TRUE);
213 ck_assert(m);
214 tmpl = endecode_create_enumerator(endecode_tests[_i]);
215 parse = m->create_enumerator(m);
216 ck_assert(parse);
217 compare_vici(parse, tmpl);
218 tmpl->destroy(tmpl);
219 parse->destroy(parse);
220 m->destroy(m);
221 }
222 END_TEST
223
224 START_TEST(test_vararg)
225 {
226 enumerator_t *parse, *tmpl;
227 vici_message_t *m;
228
229 m = vici_message_create_from_args(
230 VICI_SECTION_START, "section1",
231 VICI_LIST_START, "list1",
232 VICI_LIST_ITEM, chunk_from_str("item1"),
233 VICI_LIST_ITEM, chunk_from_str("item2"),
234 VICI_LIST_END,
235 VICI_KEY_VALUE, "key1", chunk_from_str("value1"),
236 VICI_SECTION_END,
237 VICI_END);
238 ck_assert(m);
239 tmpl = endecode_create_enumerator(endecode_test_list);
240 parse = m->create_enumerator(m);
241 ck_assert(parse);
242
243 compare_vici(parse, tmpl);
244
245 m->destroy(m);
246 tmpl->destroy(tmpl);
247 parse->destroy(parse);
248 }
249 END_TEST
250
251 START_TEST(test_builder)
252 {
253 enumerator_t *parse, *tmpl;
254 vici_message_t *m;
255 vici_builder_t *b;
256
257 b = vici_builder_create();
258 b->add(b, VICI_SECTION_START, "section1");
259 b->add(b, VICI_LIST_START, "list1");
260 b->add(b, VICI_LIST_ITEM, chunk_from_str("item1"));
261 b->add(b, VICI_LIST_ITEM, chunk_from_str("item2"));
262 b->add(b, VICI_LIST_END);
263 b->add(b, VICI_KEY_VALUE, "key1", chunk_from_str("value1"));
264 b->add(b, VICI_SECTION_END);
265 m = b->finalize(b);
266 ck_assert(m);
267 tmpl = endecode_create_enumerator(endecode_test_list);
268 parse = m->create_enumerator(m);
269 ck_assert(parse);
270
271 compare_vici(parse, tmpl);
272
273 m->destroy(m);
274 tmpl->destroy(tmpl);
275 parse->destroy(parse);
276 }
277 END_TEST
278
279 START_TEST(test_builder_fmt)
280 {
281 enumerator_t *parse, *tmpl;
282 vici_message_t *m;
283 vici_builder_t *b;
284
285 b = vici_builder_create();
286 b->begin_section(b, "section1");
287 b->begin_list(b, "list1");
288 b->add_li(b, "item%u", 1);
289 b->add_li(b, "%s%u", "item", 2);
290 b->end_list(b);
291 b->add_kv(b, "key1", "value%u", 1);
292 b->end_section(b);
293 m = b->finalize(b);
294 ck_assert(m);
295 tmpl = endecode_create_enumerator(endecode_test_list);
296 parse = m->create_enumerator(m);
297 ck_assert(parse);
298
299 compare_vici(parse, tmpl);
300
301 m->destroy(m);
302 tmpl->destroy(tmpl);
303 parse->destroy(parse);
304 }
305 END_TEST
306
307 static vici_message_t* build_getter_msg()
308 {
309 return vici_message_create_from_args(
310 VICI_KEY_VALUE, "key1", chunk_from_str("1"),
311 VICI_SECTION_START, "section1",
312 VICI_KEY_VALUE, "key2", chunk_from_str("0x12"),
313 VICI_SECTION_START, "section2",
314 VICI_KEY_VALUE, "key3", chunk_from_str("-1"),
315 VICI_SECTION_END,
316 VICI_KEY_VALUE, "key4", chunk_from_str("asdf"),
317 VICI_SECTION_END,
318 VICI_KEY_VALUE, "key5", chunk_from_str(""),
319 VICI_END);
320 }
321
322 START_TEST(test_get_str)
323 {
324 vici_message_t *m;
325
326 m = build_getter_msg();
327
328 ck_assert_str_eq(m->get_str(m, "def", "key1"), "1");
329 ck_assert_str_eq(m->get_str(m, "def", "section1.key2"), "0x12");
330 ck_assert_str_eq(m->get_str(m, "def", "section%d.section2.key3", 1), "-1");
331 ck_assert_str_eq(m->get_str(m, "def", "section1.key4"), "asdf");
332 ck_assert_str_eq(m->get_str(m, "def", "key5"), "");
333 ck_assert_str_eq(m->get_str(m, "no", "nonexistent"), "no");
334 ck_assert_str_eq(m->get_str(m, "no", "n.o.n.e.x.i.s.t.e.n.t"), "no");
335
336 m->destroy(m);
337 }
338 END_TEST
339
340 START_TEST(test_get_int)
341 {
342 vici_message_t *m;
343
344 m = build_getter_msg();
345
346 ck_assert_int_eq(m->get_int(m, 2, "key1"), 1);
347 ck_assert_int_eq(m->get_int(m, 2, "section1.key2"), 0x12);
348 ck_assert_int_eq(m->get_int(m, 2, "section1.section2.key3"), -1);
349 ck_assert_int_eq(m->get_int(m, 2, "section1.key4"), 2);
350 ck_assert_int_eq(m->get_int(m, 2, "key5"), 2);
351 ck_assert_int_eq(m->get_int(m, 2, "nonexistent"), 2);
352 ck_assert_int_eq(m->get_int(m, 2, "n.o.n.e.x.i.s.t.e.n.t"), 2);
353
354 m->destroy(m);
355 }
356 END_TEST
357
358 START_TEST(test_get_value)
359 {
360 vici_message_t *m;
361 chunk_t d = chunk_from_chars('d','e','f');
362
363 m = build_getter_msg();
364
365 ck_assert_chunk_eq(m->get_value(m, d, "key1"), chunk_from_str("1"));
366 ck_assert_chunk_eq(m->get_value(m, d, "section1.key2"), chunk_from_str("0x12"));
367 ck_assert_chunk_eq(m->get_value(m, d, "section1.section2.key3"), chunk_from_str("-1"));
368 ck_assert_chunk_eq(m->get_value(m, d, "section1.key4"), chunk_from_str("asdf"));
369 ck_assert_chunk_eq(m->get_value(m, d, "key5"), chunk_empty);
370 ck_assert_chunk_eq(m->get_value(m, d, "nonexistent"), d);
371 ck_assert_chunk_eq(m->get_value(m, d, "n.o.n.e.x.i.s.t.e.n.t"), d);
372
373 m->destroy(m);
374 }
375 END_TEST
376
377 Suite *message_suite_create()
378 {
379 Suite *s;
380 TCase *tc;
381
382 s = suite_create("vici message");
383
384 tc = tcase_create("enumerator en/decode");
385 tcase_add_loop_test(tc, test_endecode, 0, countof(endecode_tests));
386 suite_add_tcase(s, tc);
387
388 tc = tcase_create("vararg encode");
389 tcase_add_test(tc, test_vararg);
390 suite_add_tcase(s, tc);
391
392 tc = tcase_create("builder encode");
393 tcase_add_test(tc, test_builder);
394 suite_add_tcase(s, tc);
395
396 tc = tcase_create("builder format encode");
397 tcase_add_test(tc, test_builder_fmt);
398 suite_add_tcase(s, tc);
399
400 tc = tcase_create("convenience getters");
401 tcase_add_test(tc, test_get_str);
402 tcase_add_test(tc, test_get_int);
403 tcase_add_test(tc, test_get_value);
404 suite_add_tcase(s, tc);
405
406 return s;
407 }