swanctl: Pass optional connection name to --initiate/install/uninstall
[strongswan.git] / src / charon-tkm / tests / kernel_sad_tests.c
1 /*
2 * Copyright (C) 2012-2014 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <tests/test_suite.h>
18
19 #include "tkm_kernel_sad.h"
20
21 START_TEST(test_sad_creation)
22 {
23 tkm_kernel_sad_t *sad = NULL;
24
25 sad = tkm_kernel_sad_create();
26 fail_if(!sad, "Error creating tkm kernel SAD");
27
28 sad->destroy(sad);
29 }
30 END_TEST
31
32 START_TEST(test_insert)
33 {
34 host_t *addr = host_create_from_string("127.0.0.1", 1024);
35 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
36
37 fail_unless(sad->insert(sad, 1, 2, addr, addr, 27, 42, 50),
38 "Error inserting SAD entry");
39
40 sad->destroy(sad);
41 addr->destroy(addr);
42 }
43 END_TEST
44
45 START_TEST(test_insert_duplicate)
46 {
47 host_t *addr = host_create_from_string("127.0.0.1", 1024);
48 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
49
50 fail_unless(sad->insert(sad, 1, 2, addr, addr, 27, 42, 50),
51 "Error inserting SAD entry");
52 fail_if(sad->insert(sad, 1, 2, addr, addr, 27, 42, 50),
53 "Expected error inserting duplicate entry");
54
55 sad->destroy(sad);
56 addr->destroy(addr);
57 }
58 END_TEST
59
60 START_TEST(test_get_esa_id)
61 {
62 host_t *addr = host_create_from_string("127.0.0.1", 1024);
63 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
64 fail_unless(sad->insert(sad, 23, 54, addr, addr, 27, 42, 50),
65 "Error inserting SAD entry");
66 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50) == 23,
67 "Error getting esa id");
68 sad->destroy(sad);
69 addr->destroy(addr);
70 }
71 END_TEST
72
73 START_TEST(test_get_esa_id_nonexistent)
74 {
75 host_t *addr = host_create_from_string("127.0.0.1", 1024);
76 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
77 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50) == 0,
78 "Got esa id for nonexistent SAD entry");
79 sad->destroy(sad);
80 addr->destroy(addr);
81 }
82 END_TEST
83
84 START_TEST(test_get_other_esa_id)
85 {
86 host_t *addr = host_create_from_string("127.0.0.1", 1024);
87 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
88 fail_unless(sad->insert(sad, 23, 54, addr, addr, 27, 42, 50),
89 "Error inserting SAD entry");
90 fail_unless(sad->insert(sad, 24, 54, addr, addr, 27, 42, 50),
91 "Error inserting SAD entry");
92 fail_unless(sad->get_other_esa_id(sad, 23) == 24,
93 "Error getting other esa id");
94 sad->destroy(sad);
95 addr->destroy(addr);
96 }
97 END_TEST
98
99 START_TEST(test_get_other_esa_id_nonexistent)
100 {
101 host_t *addr = host_create_from_string("127.0.0.1", 1024);
102 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
103 fail_unless(sad->get_other_esa_id(sad, 1) == 0,
104 "Got other esa id for nonexistent SAD entry");
105 fail_unless(sad->insert(sad, 23, 54, addr, addr, 27, 42, 50),
106 "Error inserting SAD entry");
107 fail_unless(sad->get_other_esa_id(sad, 23) == 0,
108 "Got own esa id");
109
110 sad->destroy(sad);
111 addr->destroy(addr);
112 }
113 END_TEST
114
115 START_TEST(test_get_dst_host)
116 {
117 host_t *addr = host_create_from_string("127.0.0.1", 1024);
118 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
119 fail_unless(sad->insert(sad, 23, 54, addr, addr, 27, 42, 50),
120 "Error inserting SAD entry");
121
122 host_t *dst = sad->get_dst_host(sad, 54, 42, 50);
123 fail_unless(addr->equals(addr, dst), "Error getting dst host");
124 sad->destroy(sad);
125 addr->destroy(addr);
126 }
127 END_TEST
128
129 START_TEST(test_get_dst_host_nonexistent)
130 {
131 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
132 fail_unless(sad->get_dst_host(sad, 1, 12, 50) == NULL,
133 "Got dst for nonexistent SAD entry");
134 sad->destroy(sad);
135 }
136 END_TEST
137
138 START_TEST(test_remove)
139 {
140 host_t *addr = host_create_from_string("127.0.0.1", 1024);
141 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
142 fail_unless(sad->insert(sad, 23, 54, addr, addr, 27, 42, 50),
143 "Error inserting SAD entry");
144 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50) == 23,
145 "Error getting esa id");
146 fail_unless(sad->remove(sad, 23),
147 "Error removing SAD entry");
148 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50) == 0,
149 "Got esa id for removed SAD entry");
150 sad->destroy(sad);
151 addr->destroy(addr);
152 }
153 END_TEST
154
155 START_TEST(test_remove_nonexistent)
156 {
157 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
158 fail_if(sad->remove(sad, 1),
159 "Expected error removing nonexistent SAD entry");
160 sad->destroy(sad);
161 }
162 END_TEST
163
164 Suite *make_kernel_sad_tests()
165 {
166 Suite *s;
167 TCase *tc;
168
169 s = suite_create("kernel SAD tests");
170
171 tc = tcase_create("creation");
172 tcase_add_test(tc, test_sad_creation);
173 suite_add_tcase(s, tc);
174
175 tc = tcase_create("insert");
176 tcase_add_test(tc, test_insert);
177 tcase_add_test(tc, test_insert_duplicate);
178 suite_add_tcase(s, tc);
179
180 tc = tcase_create("get_esa_id");
181 tcase_add_test(tc, test_get_esa_id);
182 tcase_add_test(tc, test_get_esa_id_nonexistent);
183 suite_add_tcase(s, tc);
184
185 tc = tcase_create("get_other_esa_id");
186 tcase_add_test(tc, test_get_other_esa_id);
187 tcase_add_test(tc, test_get_other_esa_id_nonexistent);
188 suite_add_tcase(s, tc);
189
190 tc = tcase_create("get_dst_host");
191 tcase_add_test(tc, test_get_dst_host);
192 tcase_add_test(tc, test_get_dst_host_nonexistent);
193 suite_add_tcase(s, tc);
194
195 tc = tcase_create("remove");
196 tcase_add_test(tc, test_remove);
197 tcase_add_test(tc, test_remove_nonexistent);
198 suite_add_tcase(s, tc);
199
200 return s;
201 }