]>
Commit | Line | Data |
---|---|---|
74971473 JG |
1 | /* |
2 | * This file is part of the PCEPlib, a PCEP protocol library. | |
3 | * | |
4 | * Copyright (C) 2020 Volta Networks https://voltanet.io/ | |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public License | |
17 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
18 | * | |
19 | * Author : Brady Johnson <brady@voltanet.io> | |
20 | * | |
21 | */ | |
22 | ||
23 | ||
1f8031f7 DL |
24 | #ifdef HAVE_CONFIG_H |
25 | #include "config.h" | |
26 | #endif | |
27 | ||
fadf00aa | 28 | #include <assert.h> |
74971473 JG |
29 | #include <stdlib.h> |
30 | #include <string.h> | |
31 | #include <time.h> | |
32 | ||
33 | #include <CUnit/CUnit.h> | |
34 | ||
35 | #include "pcep_socket_comm_mock.h" | |
36 | #include "pcep_session_logic.h" | |
37 | #include "pcep_session_logic_test.h" | |
38 | ||
39 | /* | |
40 | * Test suite setup and teardown called before AND after the test suite. | |
41 | */ | |
42 | ||
43 | int pcep_session_logic_test_suite_setup(void) | |
44 | { | |
45 | pceplib_memory_reset(); | |
46 | return 0; | |
47 | } | |
48 | ||
49 | int pcep_session_logic_test_suite_teardown(void) | |
50 | { | |
51 | printf("\n"); | |
52 | pceplib_memory_dump(); | |
53 | return 0; | |
54 | } | |
55 | ||
56 | /* | |
57 | * Test case setup and teardown called before AND after each test. | |
58 | */ | |
59 | ||
60 | void pcep_session_logic_test_setup() | |
61 | { | |
62 | setup_mock_socket_comm_info(); | |
63 | } | |
64 | ||
65 | ||
66 | void pcep_session_logic_test_teardown() | |
67 | { | |
68 | stop_session_logic(); | |
69 | teardown_mock_socket_comm_info(); | |
70 | } | |
71 | ||
72 | ||
73 | /* | |
74 | * Test cases | |
75 | */ | |
76 | ||
77 | void test_run_stop_session_logic() | |
78 | { | |
79 | CU_ASSERT_TRUE(run_session_logic()); | |
80 | CU_ASSERT_TRUE(stop_session_logic()); | |
81 | } | |
82 | ||
83 | ||
84 | void test_run_session_logic_twice() | |
85 | { | |
86 | CU_ASSERT_TRUE(run_session_logic()); | |
87 | CU_ASSERT_FALSE(run_session_logic()); | |
88 | } | |
89 | ||
90 | ||
91 | void test_session_logic_without_run() | |
92 | { | |
93 | /* Verify the functions that depend on run_session_logic() being called | |
94 | */ | |
95 | CU_ASSERT_FALSE(stop_session_logic()); | |
96 | } | |
97 | ||
98 | ||
99 | void test_create_pcep_session_null_params() | |
100 | { | |
101 | pcep_configuration config; | |
102 | struct in_addr pce_ip; | |
103 | ||
104 | CU_ASSERT_PTR_NULL(create_pcep_session(NULL, NULL)); | |
105 | CU_ASSERT_PTR_NULL(create_pcep_session(NULL, &pce_ip)); | |
106 | CU_ASSERT_PTR_NULL(create_pcep_session(&config, NULL)); | |
107 | } | |
108 | ||
109 | ||
110 | void test_create_destroy_pcep_session() | |
111 | { | |
112 | pcep_session *session; | |
113 | pcep_configuration config; | |
114 | struct in_addr pce_ip; | |
115 | ||
116 | run_session_logic(); | |
117 | ||
118 | memset(&config, 0, sizeof(pcep_configuration)); | |
119 | config.keep_alive_seconds = 5; | |
120 | config.dead_timer_seconds = 5; | |
121 | config.request_time_seconds = 5; | |
122 | config.max_unknown_messages = 5; | |
123 | config.max_unknown_requests = 5; | |
124 | inet_pton(AF_INET, "127.0.0.1", &(pce_ip)); | |
125 | ||
126 | mock_socket_comm_info *mock_info = get_mock_socket_comm_info(); | |
127 | mock_info->send_message_save_message = true; | |
128 | session = create_pcep_session(&config, &pce_ip); | |
129 | CU_ASSERT_PTR_NOT_NULL(session); | |
130 | /* What gets saved in the mock is the msg byte buffer. The msg struct | |
131 | * was deleted when it was sent. Instead of inspecting the msg byte | |
132 | * buffer, lets just decode it. */ | |
133 | uint8_t *encoded_msg = | |
134 | dll_delete_first_node(mock_info->sent_message_list); | |
135 | CU_ASSERT_PTR_NOT_NULL(encoded_msg); | |
136 | struct pcep_message *open_msg = pcep_decode_message(encoded_msg); | |
137 | CU_ASSERT_PTR_NOT_NULL(open_msg); | |
fadf00aa | 138 | assert(open_msg != NULL); |
74971473 JG |
139 | /* Should be an Open, with no TLVs: length = 12 */ |
140 | CU_ASSERT_EQUAL(open_msg->msg_header->type, PCEP_TYPE_OPEN); | |
141 | CU_ASSERT_EQUAL(open_msg->encoded_message_length, 12); | |
142 | destroy_pcep_session(session); | |
143 | pcep_msg_free_message(open_msg); | |
144 | pceplib_free(PCEPLIB_MESSAGES, encoded_msg); | |
145 | ||
146 | stop_session_logic(); | |
147 | } | |
148 | ||
149 | ||
150 | void test_create_destroy_pcep_session_ipv6() | |
151 | { | |
152 | pcep_session *session; | |
153 | pcep_configuration config; | |
154 | struct in6_addr pce_ip; | |
155 | ||
156 | run_session_logic(); | |
157 | ||
158 | memset(&config, 0, sizeof(pcep_configuration)); | |
159 | config.keep_alive_seconds = 5; | |
160 | config.dead_timer_seconds = 5; | |
161 | config.request_time_seconds = 5; | |
162 | config.max_unknown_messages = 5; | |
163 | config.max_unknown_requests = 5; | |
164 | config.is_src_ipv6 = true; | |
165 | inet_pton(AF_INET6, "::1", &pce_ip); | |
166 | ||
167 | mock_socket_comm_info *mock_info = get_mock_socket_comm_info(); | |
168 | mock_info->send_message_save_message = true; | |
169 | session = create_pcep_session_ipv6(&config, &pce_ip); | |
170 | CU_ASSERT_PTR_NOT_NULL(session); | |
fadf00aa | 171 | assert(session != NULL); |
74971473 JG |
172 | CU_ASSERT_TRUE(session->socket_comm_session->is_ipv6); |
173 | /* What gets saved in the mock is the msg byte buffer. The msg struct | |
174 | * was deleted when it was sent. Instead of inspecting the msg byte | |
175 | * buffer, lets just decode it. */ | |
176 | uint8_t *encoded_msg = | |
177 | dll_delete_first_node(mock_info->sent_message_list); | |
178 | CU_ASSERT_PTR_NOT_NULL(encoded_msg); | |
179 | struct pcep_message *open_msg = pcep_decode_message(encoded_msg); | |
180 | CU_ASSERT_PTR_NOT_NULL(open_msg); | |
fadf00aa | 181 | assert(open_msg != NULL); |
74971473 JG |
182 | /* Should be an Open, with no TLVs: length = 12 */ |
183 | CU_ASSERT_EQUAL(open_msg->msg_header->type, PCEP_TYPE_OPEN); | |
184 | CU_ASSERT_EQUAL(open_msg->encoded_message_length, 12); | |
185 | destroy_pcep_session(session); | |
186 | pcep_msg_free_message(open_msg); | |
187 | pceplib_free(PCEPLIB_MESSAGES, encoded_msg); | |
188 | ||
189 | stop_session_logic(); | |
190 | } | |
191 | ||
192 | ||
193 | void test_create_pcep_session_open_tlvs() | |
194 | { | |
195 | pcep_session *session; | |
196 | struct in_addr pce_ip; | |
197 | struct pcep_message *open_msg; | |
198 | struct pcep_object_header *open_obj; | |
199 | pcep_configuration config; | |
200 | memset(&config, 0, sizeof(pcep_configuration)); | |
201 | config.pcep_msg_versioning = create_default_pcep_versioning(); | |
202 | inet_pton(AF_INET, "127.0.0.1", &(pce_ip)); | |
203 | ||
204 | run_session_logic(); | |
205 | ||
206 | /* Verify the created Open message only has 1 TLV: | |
207 | * pcep_tlv_create_stateful_pce_capability() */ | |
208 | mock_socket_comm_info *mock_info = get_mock_socket_comm_info(); | |
209 | mock_info->send_message_save_message = true; | |
210 | config.support_stateful_pce_lsp_update = true; | |
211 | config.pcep_msg_versioning->draft_ietf_pce_segment_routing_07 = false; | |
212 | config.support_sr_te_pst = false; | |
213 | ||
214 | session = create_pcep_session(&config, &pce_ip); | |
215 | CU_ASSERT_PTR_NOT_NULL(session); | |
216 | /* Get and verify the Open Message */ | |
217 | uint8_t *encoded_msg = | |
218 | dll_delete_first_node(mock_info->sent_message_list); | |
219 | CU_ASSERT_PTR_NOT_NULL(encoded_msg); | |
220 | open_msg = pcep_decode_message(encoded_msg); | |
221 | CU_ASSERT_PTR_NOT_NULL(open_msg); | |
fadf00aa | 222 | assert(open_msg != NULL); |
74971473 JG |
223 | /* Get and verify the Open Message objects */ |
224 | CU_ASSERT_PTR_NOT_NULL(open_msg->obj_list); | |
fadf00aa | 225 | assert(open_msg->obj_list != NULL); |
74971473 JG |
226 | CU_ASSERT_TRUE(open_msg->obj_list->num_entries > 0); |
227 | /* Get and verify the Open object */ | |
228 | open_obj = pcep_obj_get(open_msg->obj_list, PCEP_OBJ_CLASS_OPEN); | |
229 | CU_ASSERT_PTR_NOT_NULL(open_obj); | |
230 | /* Get and verify the Open object TLVs */ | |
231 | CU_ASSERT_PTR_NOT_NULL(open_obj->tlv_list); | |
fadf00aa | 232 | assert(open_obj->tlv_list != NULL); |
74971473 JG |
233 | CU_ASSERT_EQUAL(open_obj->tlv_list->num_entries, 1); |
234 | CU_ASSERT_EQUAL(((struct pcep_object_tlv_header *) | |
235 | open_obj->tlv_list->head->data) | |
236 | ->type, | |
237 | PCEP_OBJ_TLV_TYPE_STATEFUL_PCE_CAPABILITY); | |
238 | ||
239 | destroy_pcep_session(session); | |
240 | pcep_msg_free_message(open_msg); | |
241 | pceplib_free(PCEPLIB_MESSAGES, encoded_msg); | |
242 | ||
243 | /* Verify the created Open message only has 2 TLVs: | |
244 | * pcep_tlv_create_stateful_pce_capability() | |
245 | * pcep_tlv_create_lsp_db_version() */ | |
246 | reset_mock_socket_comm_info(); | |
247 | mock_info->send_message_save_message = true; | |
248 | config.support_include_db_version = true; | |
249 | config.lsp_db_version = 100; | |
250 | ||
251 | session = create_pcep_session(&config, &pce_ip); | |
252 | CU_ASSERT_PTR_NOT_NULL(session); | |
253 | /* Get and verify the Open Message */ | |
254 | encoded_msg = dll_delete_first_node(mock_info->sent_message_list); | |
255 | CU_ASSERT_PTR_NOT_NULL(encoded_msg); | |
256 | open_msg = pcep_decode_message(encoded_msg); | |
257 | CU_ASSERT_PTR_NOT_NULL(open_msg); | |
258 | /* Get and verify the Open Message objects */ | |
259 | CU_ASSERT_PTR_NOT_NULL(open_msg->obj_list); | |
fadf00aa | 260 | assert(open_msg != NULL); |
74971473 JG |
261 | CU_ASSERT_TRUE(open_msg->obj_list->num_entries > 0); |
262 | /* Get and verify the Open object */ | |
263 | open_obj = pcep_obj_get(open_msg->obj_list, PCEP_OBJ_CLASS_OPEN); | |
264 | CU_ASSERT_PTR_NOT_NULL(open_obj); | |
265 | /* Get and verify the Open object TLVs */ | |
266 | CU_ASSERT_PTR_NOT_NULL(open_obj->tlv_list); | |
fadf00aa | 267 | assert(open_obj->tlv_list != NULL); |
74971473 JG |
268 | CU_ASSERT_EQUAL(open_obj->tlv_list->num_entries, 2); |
269 | CU_ASSERT_EQUAL(((struct pcep_object_tlv_header *) | |
270 | open_obj->tlv_list->head->data) | |
271 | ->type, | |
272 | PCEP_OBJ_TLV_TYPE_STATEFUL_PCE_CAPABILITY); | |
273 | CU_ASSERT_EQUAL(((struct pcep_object_tlv_header *) | |
274 | open_obj->tlv_list->head->next_node->data) | |
275 | ->type, | |
276 | PCEP_OBJ_TLV_TYPE_LSP_DB_VERSION); | |
277 | ||
278 | destroy_pcep_session(session); | |
279 | pcep_msg_free_message(open_msg); | |
280 | pceplib_free(PCEPLIB_MESSAGES, encoded_msg); | |
281 | ||
282 | ||
283 | /* Verify the created Open message only has 4 TLVs: | |
284 | * pcep_tlv_create_stateful_pce_capability() | |
285 | * pcep_tlv_create_lsp_db_version() | |
286 | * pcep_tlv_create_sr_pce_capability() | |
287 | * pcep_tlv_create_path_setup_type_capability() */ | |
288 | reset_mock_socket_comm_info(); | |
289 | mock_info->send_message_save_message = true; | |
290 | config.support_sr_te_pst = true; | |
291 | ||
292 | session = create_pcep_session(&config, &pce_ip); | |
293 | CU_ASSERT_PTR_NOT_NULL(session); | |
294 | /* Get and verify the Open Message */ | |
295 | encoded_msg = dll_delete_first_node(mock_info->sent_message_list); | |
296 | CU_ASSERT_PTR_NOT_NULL(encoded_msg); | |
297 | open_msg = pcep_decode_message(encoded_msg); | |
298 | CU_ASSERT_PTR_NOT_NULL(open_msg); | |
fadf00aa | 299 | assert(open_msg != NULL); |
74971473 JG |
300 | /* Get and verify the Open Message objects */ |
301 | CU_ASSERT_PTR_NOT_NULL(open_msg->obj_list); | |
fadf00aa | 302 | assert(open_msg->obj_list != NULL); |
74971473 JG |
303 | CU_ASSERT_TRUE(open_msg->obj_list->num_entries > 0); |
304 | /* Get and verify the Open object */ | |
305 | open_obj = pcep_obj_get(open_msg->obj_list, PCEP_OBJ_CLASS_OPEN); | |
306 | CU_ASSERT_PTR_NOT_NULL(open_obj); | |
307 | /* Get and verify the Open object TLVs */ | |
308 | CU_ASSERT_PTR_NOT_NULL(open_obj->tlv_list); | |
fadf00aa | 309 | assert(open_obj->tlv_list != NULL); |
74971473 JG |
310 | CU_ASSERT_EQUAL(open_obj->tlv_list->num_entries, 3); |
311 | double_linked_list_node *tlv_node = open_obj->tlv_list->head; | |
312 | CU_ASSERT_EQUAL(((struct pcep_object_tlv_header *)tlv_node->data)->type, | |
313 | PCEP_OBJ_TLV_TYPE_STATEFUL_PCE_CAPABILITY); | |
314 | tlv_node = tlv_node->next_node; | |
315 | CU_ASSERT_EQUAL(((struct pcep_object_tlv_header *)tlv_node->data)->type, | |
316 | PCEP_OBJ_TLV_TYPE_LSP_DB_VERSION); | |
317 | tlv_node = tlv_node->next_node; | |
318 | CU_ASSERT_EQUAL(((struct pcep_object_tlv_header *)tlv_node->data)->type, | |
319 | PCEP_OBJ_TLV_TYPE_PATH_SETUP_TYPE_CAPABILITY); | |
320 | ||
321 | destroy_pcep_session(session); | |
322 | pcep_msg_free_message(open_msg); | |
323 | pceplib_free(PCEPLIB_MESSAGES, encoded_msg); | |
324 | ||
325 | /* Verify the created Open message only has 4 TLVs: | |
326 | * pcep_tlv_create_stateful_pce_capability() | |
327 | * pcep_tlv_create_lsp_db_version() | |
328 | * pcep_tlv_create_sr_pce_capability() | |
329 | * pcep_tlv_create_path_setup_type_capability() */ | |
330 | reset_mock_socket_comm_info(); | |
331 | mock_info->send_message_save_message = true; | |
332 | config.pcep_msg_versioning->draft_ietf_pce_segment_routing_07 = true; | |
333 | ||
334 | session = create_pcep_session(&config, &pce_ip); | |
335 | CU_ASSERT_PTR_NOT_NULL(session); | |
336 | /* Get and verify the Open Message */ | |
337 | encoded_msg = dll_delete_first_node(mock_info->sent_message_list); | |
338 | CU_ASSERT_PTR_NOT_NULL(encoded_msg); | |
fadf00aa | 339 | assert(encoded_msg != NULL); |
74971473 JG |
340 | open_msg = pcep_decode_message(encoded_msg); |
341 | CU_ASSERT_PTR_NOT_NULL(open_msg); | |
fadf00aa | 342 | assert(open_msg != NULL); |
74971473 JG |
343 | /* Get and verify the Open Message objects */ |
344 | CU_ASSERT_PTR_NOT_NULL(open_msg->obj_list); | |
fadf00aa | 345 | assert(open_msg->obj_list != NULL); |
74971473 JG |
346 | CU_ASSERT_TRUE(open_msg->obj_list->num_entries > 0); |
347 | /* Get and verify the Open object */ | |
348 | open_obj = pcep_obj_get(open_msg->obj_list, PCEP_OBJ_CLASS_OPEN); | |
349 | CU_ASSERT_PTR_NOT_NULL(open_obj); | |
fadf00aa | 350 | assert(open_obj != NULL); |
74971473 JG |
351 | /* Get and verify the Open object TLVs */ |
352 | CU_ASSERT_PTR_NOT_NULL(open_obj->tlv_list); | |
fadf00aa | 353 | assert(open_obj->tlv_list != NULL); |
74971473 JG |
354 | CU_ASSERT_EQUAL(open_obj->tlv_list->num_entries, 4); |
355 | tlv_node = open_obj->tlv_list->head; | |
356 | CU_ASSERT_EQUAL(((struct pcep_object_tlv_header *)tlv_node->data)->type, | |
357 | PCEP_OBJ_TLV_TYPE_STATEFUL_PCE_CAPABILITY); | |
358 | tlv_node = tlv_node->next_node; | |
359 | CU_ASSERT_EQUAL(((struct pcep_object_tlv_header *)tlv_node->data)->type, | |
360 | PCEP_OBJ_TLV_TYPE_LSP_DB_VERSION); | |
361 | tlv_node = tlv_node->next_node; | |
362 | CU_ASSERT_EQUAL(((struct pcep_object_tlv_header *)tlv_node->data)->type, | |
363 | PCEP_OBJ_TLV_TYPE_SR_PCE_CAPABILITY); | |
364 | tlv_node = tlv_node->next_node; | |
365 | CU_ASSERT_EQUAL(((struct pcep_object_tlv_header *)tlv_node->data)->type, | |
366 | PCEP_OBJ_TLV_TYPE_PATH_SETUP_TYPE_CAPABILITY); | |
367 | ||
368 | destroy_pcep_versioning(config.pcep_msg_versioning); | |
369 | destroy_pcep_session(session); | |
370 | pcep_msg_free_message(open_msg); | |
371 | pceplib_free(PCEPLIB_MESSAGES, encoded_msg); | |
372 | ||
373 | stop_session_logic(); | |
374 | } | |
375 | ||
376 | ||
377 | void test_destroy_pcep_session_null_session() | |
378 | { | |
379 | /* Just testing that it does not core dump */ | |
380 | destroy_pcep_session(NULL); | |
381 | } |