9bbc064f73bd217828e312a997e7a4a50fa5ed8b
[strongswan.git] / src / libcharon / tests / suites / test_ike_cfg.c
1 /*
2 * Copyright (C) 2015 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
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 <config/ike_cfg.h>
19
20 static void assert_family(int expected, char *addr, bool local)
21 {
22 ike_cfg_t *cfg;
23 int family;
24
25 cfg = ike_cfg_create(IKEV2, FALSE, FALSE, local ? addr : "%any", 500,
26 local ? "%any" : addr, 500, FRAGMENTATION_NO, 0);
27 family = ike_cfg_get_family(cfg, local);
28 ck_assert_msg(expected == family, "expected family %d != %d (addr: '%s')",
29 expected, family, addr);
30 cfg->destroy(cfg);
31 }
32
33 START_TEST(test_get_address_family_empty)
34 {
35 assert_family(AF_UNSPEC, "", _i);
36 }
37 END_TEST
38
39 START_TEST(test_get_address_family_addr)
40 {
41 assert_family(AF_INET, "192.168.1.1", _i);
42 assert_family(AF_INET6, "fec::1", _i);
43 }
44 END_TEST
45
46 START_TEST(test_get_address_family_multi)
47 {
48 assert_family(AF_INET, "192.168.1.1,192.168.2.2", _i);
49 assert_family(AF_INET6, "fec::1,fec::2", _i);
50
51 assert_family(AF_UNSPEC, "192.168.1.1,fec::1", _i);
52 assert_family(AF_UNSPEC, "fec::1,192.168.1.1", _i);
53 }
54 END_TEST
55
56 START_TEST(test_get_address_family_any)
57 {
58 assert_family(AF_UNSPEC, "%any", _i);
59
60 assert_family(AF_INET, "%any4", _i);
61 assert_family(AF_INET, "0.0.0.0", _i);
62
63 assert_family(AF_INET6, "%any6", _i);
64 assert_family(AF_INET6, "::", _i);
65
66 assert_family(AF_INET, "192.168.1.1,%any", _i);
67 assert_family(AF_INET, "192.168.1.1,%any4", _i);
68 assert_family(AF_UNSPEC, "192.168.1.1,%any6", _i);
69
70 assert_family(AF_INET6, "fec::1,%any", _i);
71 assert_family(AF_UNSPEC, "fec::1,%any4", _i);
72 assert_family(AF_INET6, "fec::1,%any6", _i);
73 }
74 END_TEST
75
76 START_TEST(test_get_address_family_other)
77 {
78 assert_family(AF_INET, "192.168.1.0", _i);
79 assert_family(AF_UNSPEC, "192.168.1.0/24", _i);
80 assert_family(AF_UNSPEC, "192.168.1.0-192.168.1.10", _i);
81
82 assert_family(AF_INET, "192.168.1.0/24,192.168.2.1", _i);
83 assert_family(AF_INET, "192.168.1.0-192.168.1.10,192.168.2.1", _i);
84 assert_family(AF_INET6, "192.168.1.0/24,fec::1", _i);
85 assert_family(AF_INET6, "192.168.1.0-192.168.1.10,fec::1", _i);
86
87 assert_family(AF_INET6, "fec::", _i);
88 assert_family(AF_UNSPEC, "fec::/64", _i);
89 assert_family(AF_UNSPEC, "fec::1-fec::10", _i);
90
91 assert_family(AF_INET6, "fec::/64,fed::1", _i);
92 assert_family(AF_INET6, "fec::1-fec::10,fec::1", _i);
93 assert_family(AF_INET, "fec::/64,192.168.1.1", _i);
94 assert_family(AF_INET, "fec::1-fec::10,192.168.1.1", _i);
95
96 assert_family(AF_UNSPEC, "strongswan.org", _i);
97 assert_family(AF_INET, "192.168.1.0,strongswan.org", _i);
98 assert_family(AF_INET6, "fec::1,strongswan.org", _i);
99 }
100 END_TEST
101
102 Suite *ike_cfg_suite_create()
103 {
104 Suite *s;
105 TCase *tc;
106
107 s = suite_create("ike_cfg");
108
109 tc = tcase_create("ike_cfg_get_address_family");
110 tcase_add_loop_test(tc, test_get_address_family_empty, 0, 2);
111 tcase_add_loop_test(tc, test_get_address_family_addr, 0, 2);
112 tcase_add_loop_test(tc, test_get_address_family_multi, 0, 2);
113 tcase_add_loop_test(tc, test_get_address_family_any, 0, 2);
114 tcase_add_loop_test(tc, test_get_address_family_other, 0, 2);
115 suite_add_tcase(s, tc);
116
117 return s;
118 }