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