charon-nm: Set DPD/close action to restart and enable indefinite keying tries
[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 * HSR 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, FALSE) == 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_local)
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->insert(sad, 23, 54, addr, addr, 27, 42, 50),
78 "Error inserting SAD entry");
79 fail_unless(sad->get_esa_id(sad, addr, addr, 27, 50, TRUE) == 23,
80 "Error getting esa id");
81 sad->destroy(sad);
82 addr->destroy(addr);
83 }
84 END_TEST
85
86 START_TEST(test_get_esa_id_nonexistent)
87 {
88 host_t *addr = host_create_from_string("127.0.0.1", 1024);
89 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
90 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50, FALSE) == 0,
91 "Got esa id for nonexistent SAD entry");
92 sad->destroy(sad);
93 addr->destroy(addr);
94 }
95 END_TEST
96
97 START_TEST(test_get_dst_host)
98 {
99 host_t *addr = host_create_from_string("127.0.0.1", 1024);
100 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
101 fail_unless(sad->insert(sad, 23, 54, addr, addr, 27, 42, 50),
102 "Error inserting SAD entry");
103
104 host_t *dst = sad->get_dst_host(sad, 54, 42, 50);
105 fail_unless(addr->equals(addr, dst), "Error getting dst host");
106 sad->destroy(sad);
107 addr->destroy(addr);
108 }
109 END_TEST
110
111 START_TEST(test_get_dst_host_nonexistent)
112 {
113 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
114 fail_unless(sad->get_dst_host(sad, 1, 12, 50) == NULL,
115 "Got dst for nonexistent SAD entry");
116 sad->destroy(sad);
117 }
118 END_TEST
119
120 START_TEST(test_remove)
121 {
122 host_t *addr = host_create_from_string("127.0.0.1", 1024);
123 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
124 fail_unless(sad->insert(sad, 23, 54, addr, addr, 27, 42, 50),
125 "Error inserting SAD entry");
126 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50, FALSE) == 23,
127 "Error getting esa id");
128 fail_unless(sad->remove(sad, 23),
129 "Error removing SAD entry");
130 fail_unless(sad->get_esa_id(sad, addr, addr, 42, 50, FALSE) == 0,
131 "Got esa id for removed SAD entry");
132 sad->destroy(sad);
133 addr->destroy(addr);
134 }
135 END_TEST
136
137 START_TEST(test_remove_nonexistent)
138 {
139 tkm_kernel_sad_t *sad = tkm_kernel_sad_create();
140 fail_if(sad->remove(sad, 1),
141 "Expected error removing nonexistent SAD entry");
142 sad->destroy(sad);
143 }
144 END_TEST
145
146 Suite *make_kernel_sad_tests()
147 {
148 Suite *s;
149 TCase *tc;
150
151 s = suite_create("kernel SAD tests");
152
153 tc = tcase_create("creation");
154 tcase_add_test(tc, test_sad_creation);
155 suite_add_tcase(s, tc);
156
157 tc = tcase_create("insert");
158 tcase_add_test(tc, test_insert);
159 tcase_add_test(tc, test_insert_duplicate);
160 suite_add_tcase(s, tc);
161
162 tc = tcase_create("get_esa_id");
163 tcase_add_test(tc, test_get_esa_id);
164 tcase_add_test(tc, test_get_esa_id_local);
165 tcase_add_test(tc, test_get_esa_id_nonexistent);
166 suite_add_tcase(s, tc);
167
168 tc = tcase_create("get_dst_host");
169 tcase_add_test(tc, test_get_dst_host);
170 tcase_add_test(tc, test_get_dst_host_nonexistent);
171 suite_add_tcase(s, tc);
172
173 tc = tcase_create("remove");
174 tcase_add_test(tc, test_remove);
175 tcase_add_test(tc, test_remove_nonexistent);
176 suite_add_tcase(s, tc);
177
178 return s;
179 }