2 * This file is part of the PCEPlib, a PCEP protocol library.
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
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.
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.
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/>.
19 * Author : Brady Johnson <brady@voltanet.io>
28 #include <CUnit/CUnit.h>
30 #include "pcep_socket_comm_mock.h"
31 #include "pcep_session_logic.h"
32 #include "pcep_session_logic_test.h"
35 * Test suite setup and teardown called before AND after the test suite.
38 int pcep_session_logic_test_suite_setup(void)
40 pceplib_memory_reset();
44 int pcep_session_logic_test_suite_teardown(void)
47 pceplib_memory_dump();
52 * Test case setup and teardown called before AND after each test.
55 void pcep_session_logic_test_setup()
57 setup_mock_socket_comm_info();
61 void pcep_session_logic_test_teardown()
64 teardown_mock_socket_comm_info();
72 void test_run_stop_session_logic()
74 CU_ASSERT_TRUE(run_session_logic());
75 CU_ASSERT_TRUE(stop_session_logic());
79 void test_run_session_logic_twice()
81 CU_ASSERT_TRUE(run_session_logic());
82 CU_ASSERT_FALSE(run_session_logic());
86 void test_session_logic_without_run()
88 /* Verify the functions that depend on run_session_logic() being called
90 CU_ASSERT_FALSE(stop_session_logic());
94 void test_create_pcep_session_null_params()
96 pcep_configuration config
;
97 struct in_addr pce_ip
;
99 CU_ASSERT_PTR_NULL(create_pcep_session(NULL
, NULL
));
100 CU_ASSERT_PTR_NULL(create_pcep_session(NULL
, &pce_ip
));
101 CU_ASSERT_PTR_NULL(create_pcep_session(&config
, NULL
));
105 void test_create_destroy_pcep_session()
107 pcep_session
*session
;
108 pcep_configuration config
;
109 struct in_addr pce_ip
;
113 memset(&config
, 0, sizeof(pcep_configuration
));
114 config
.keep_alive_seconds
= 5;
115 config
.dead_timer_seconds
= 5;
116 config
.request_time_seconds
= 5;
117 config
.max_unknown_messages
= 5;
118 config
.max_unknown_requests
= 5;
119 inet_pton(AF_INET
, "127.0.0.1", &(pce_ip
));
121 mock_socket_comm_info
*mock_info
= get_mock_socket_comm_info();
122 mock_info
->send_message_save_message
= true;
123 session
= create_pcep_session(&config
, &pce_ip
);
124 CU_ASSERT_PTR_NOT_NULL(session
);
125 /* What gets saved in the mock is the msg byte buffer. The msg struct
126 * was deleted when it was sent. Instead of inspecting the msg byte
127 * buffer, lets just decode it. */
128 uint8_t *encoded_msg
=
129 dll_delete_first_node(mock_info
->sent_message_list
);
130 CU_ASSERT_PTR_NOT_NULL(encoded_msg
);
131 struct pcep_message
*open_msg
= pcep_decode_message(encoded_msg
);
132 CU_ASSERT_PTR_NOT_NULL(open_msg
);
133 /* Should be an Open, with no TLVs: length = 12 */
134 CU_ASSERT_EQUAL(open_msg
->msg_header
->type
, PCEP_TYPE_OPEN
);
135 CU_ASSERT_EQUAL(open_msg
->encoded_message_length
, 12);
136 destroy_pcep_session(session
);
137 pcep_msg_free_message(open_msg
);
138 pceplib_free(PCEPLIB_MESSAGES
, encoded_msg
);
140 stop_session_logic();
144 void test_create_destroy_pcep_session_ipv6()
146 pcep_session
*session
;
147 pcep_configuration config
;
148 struct in6_addr pce_ip
;
152 memset(&config
, 0, sizeof(pcep_configuration
));
153 config
.keep_alive_seconds
= 5;
154 config
.dead_timer_seconds
= 5;
155 config
.request_time_seconds
= 5;
156 config
.max_unknown_messages
= 5;
157 config
.max_unknown_requests
= 5;
158 config
.is_src_ipv6
= true;
159 inet_pton(AF_INET6
, "::1", &pce_ip
);
161 mock_socket_comm_info
*mock_info
= get_mock_socket_comm_info();
162 mock_info
->send_message_save_message
= true;
163 session
= create_pcep_session_ipv6(&config
, &pce_ip
);
164 CU_ASSERT_PTR_NOT_NULL(session
);
165 CU_ASSERT_TRUE(session
->socket_comm_session
->is_ipv6
);
166 /* What gets saved in the mock is the msg byte buffer. The msg struct
167 * was deleted when it was sent. Instead of inspecting the msg byte
168 * buffer, lets just decode it. */
169 uint8_t *encoded_msg
=
170 dll_delete_first_node(mock_info
->sent_message_list
);
171 CU_ASSERT_PTR_NOT_NULL(encoded_msg
);
172 struct pcep_message
*open_msg
= pcep_decode_message(encoded_msg
);
173 CU_ASSERT_PTR_NOT_NULL(open_msg
);
174 /* Should be an Open, with no TLVs: length = 12 */
175 CU_ASSERT_EQUAL(open_msg
->msg_header
->type
, PCEP_TYPE_OPEN
);
176 CU_ASSERT_EQUAL(open_msg
->encoded_message_length
, 12);
177 destroy_pcep_session(session
);
178 pcep_msg_free_message(open_msg
);
179 pceplib_free(PCEPLIB_MESSAGES
, encoded_msg
);
181 stop_session_logic();
185 void test_create_pcep_session_open_tlvs()
187 pcep_session
*session
;
188 struct in_addr pce_ip
;
189 struct pcep_message
*open_msg
;
190 struct pcep_object_header
*open_obj
;
191 pcep_configuration config
;
192 memset(&config
, 0, sizeof(pcep_configuration
));
193 config
.pcep_msg_versioning
= create_default_pcep_versioning();
194 inet_pton(AF_INET
, "127.0.0.1", &(pce_ip
));
198 /* Verify the created Open message only has 1 TLV:
199 * pcep_tlv_create_stateful_pce_capability() */
200 mock_socket_comm_info
*mock_info
= get_mock_socket_comm_info();
201 mock_info
->send_message_save_message
= true;
202 config
.support_stateful_pce_lsp_update
= true;
203 config
.pcep_msg_versioning
->draft_ietf_pce_segment_routing_07
= false;
204 config
.support_sr_te_pst
= false;
206 session
= create_pcep_session(&config
, &pce_ip
);
207 CU_ASSERT_PTR_NOT_NULL(session
);
208 /* Get and verify the Open Message */
209 uint8_t *encoded_msg
=
210 dll_delete_first_node(mock_info
->sent_message_list
);
211 CU_ASSERT_PTR_NOT_NULL(encoded_msg
);
212 open_msg
= pcep_decode_message(encoded_msg
);
213 CU_ASSERT_PTR_NOT_NULL(open_msg
);
214 /* Get and verify the Open Message objects */
215 CU_ASSERT_PTR_NOT_NULL(open_msg
->obj_list
);
216 CU_ASSERT_TRUE(open_msg
->obj_list
->num_entries
> 0);
217 /* Get and verify the Open object */
218 open_obj
= pcep_obj_get(open_msg
->obj_list
, PCEP_OBJ_CLASS_OPEN
);
219 CU_ASSERT_PTR_NOT_NULL(open_obj
);
220 /* Get and verify the Open object TLVs */
221 CU_ASSERT_PTR_NOT_NULL(open_obj
->tlv_list
);
222 CU_ASSERT_EQUAL(open_obj
->tlv_list
->num_entries
, 1);
223 CU_ASSERT_EQUAL(((struct pcep_object_tlv_header
*)
224 open_obj
->tlv_list
->head
->data
)
226 PCEP_OBJ_TLV_TYPE_STATEFUL_PCE_CAPABILITY
);
228 destroy_pcep_session(session
);
229 pcep_msg_free_message(open_msg
);
230 pceplib_free(PCEPLIB_MESSAGES
, encoded_msg
);
232 /* Verify the created Open message only has 2 TLVs:
233 * pcep_tlv_create_stateful_pce_capability()
234 * pcep_tlv_create_lsp_db_version() */
235 reset_mock_socket_comm_info();
236 mock_info
->send_message_save_message
= true;
237 config
.support_include_db_version
= true;
238 config
.lsp_db_version
= 100;
240 session
= create_pcep_session(&config
, &pce_ip
);
241 CU_ASSERT_PTR_NOT_NULL(session
);
242 /* Get and verify the Open Message */
243 encoded_msg
= dll_delete_first_node(mock_info
->sent_message_list
);
244 CU_ASSERT_PTR_NOT_NULL(encoded_msg
);
245 open_msg
= pcep_decode_message(encoded_msg
);
246 CU_ASSERT_PTR_NOT_NULL(open_msg
);
247 /* Get and verify the Open Message objects */
248 CU_ASSERT_PTR_NOT_NULL(open_msg
->obj_list
);
249 CU_ASSERT_TRUE(open_msg
->obj_list
->num_entries
> 0);
250 /* Get and verify the Open object */
251 open_obj
= pcep_obj_get(open_msg
->obj_list
, PCEP_OBJ_CLASS_OPEN
);
252 CU_ASSERT_PTR_NOT_NULL(open_obj
);
253 /* Get and verify the Open object TLVs */
254 CU_ASSERT_PTR_NOT_NULL(open_obj
->tlv_list
);
255 CU_ASSERT_EQUAL(open_obj
->tlv_list
->num_entries
, 2);
256 CU_ASSERT_EQUAL(((struct pcep_object_tlv_header
*)
257 open_obj
->tlv_list
->head
->data
)
259 PCEP_OBJ_TLV_TYPE_STATEFUL_PCE_CAPABILITY
);
260 CU_ASSERT_EQUAL(((struct pcep_object_tlv_header
*)
261 open_obj
->tlv_list
->head
->next_node
->data
)
263 PCEP_OBJ_TLV_TYPE_LSP_DB_VERSION
);
265 destroy_pcep_session(session
);
266 pcep_msg_free_message(open_msg
);
267 pceplib_free(PCEPLIB_MESSAGES
, encoded_msg
);
270 /* Verify the created Open message only has 4 TLVs:
271 * pcep_tlv_create_stateful_pce_capability()
272 * pcep_tlv_create_lsp_db_version()
273 * pcep_tlv_create_sr_pce_capability()
274 * pcep_tlv_create_path_setup_type_capability() */
275 reset_mock_socket_comm_info();
276 mock_info
->send_message_save_message
= true;
277 config
.support_sr_te_pst
= true;
279 session
= create_pcep_session(&config
, &pce_ip
);
280 CU_ASSERT_PTR_NOT_NULL(session
);
281 /* Get and verify the Open Message */
282 encoded_msg
= dll_delete_first_node(mock_info
->sent_message_list
);
283 CU_ASSERT_PTR_NOT_NULL(encoded_msg
);
284 open_msg
= pcep_decode_message(encoded_msg
);
285 CU_ASSERT_PTR_NOT_NULL(open_msg
);
286 /* Get and verify the Open Message objects */
287 CU_ASSERT_PTR_NOT_NULL(open_msg
->obj_list
);
288 CU_ASSERT_TRUE(open_msg
->obj_list
->num_entries
> 0);
289 /* Get and verify the Open object */
290 open_obj
= pcep_obj_get(open_msg
->obj_list
, PCEP_OBJ_CLASS_OPEN
);
291 CU_ASSERT_PTR_NOT_NULL(open_obj
);
292 /* Get and verify the Open object TLVs */
293 CU_ASSERT_PTR_NOT_NULL(open_obj
->tlv_list
);
294 CU_ASSERT_EQUAL(open_obj
->tlv_list
->num_entries
, 3);
295 double_linked_list_node
*tlv_node
= open_obj
->tlv_list
->head
;
296 CU_ASSERT_EQUAL(((struct pcep_object_tlv_header
*)tlv_node
->data
)->type
,
297 PCEP_OBJ_TLV_TYPE_STATEFUL_PCE_CAPABILITY
);
298 tlv_node
= tlv_node
->next_node
;
299 CU_ASSERT_EQUAL(((struct pcep_object_tlv_header
*)tlv_node
->data
)->type
,
300 PCEP_OBJ_TLV_TYPE_LSP_DB_VERSION
);
301 tlv_node
= tlv_node
->next_node
;
302 CU_ASSERT_EQUAL(((struct pcep_object_tlv_header
*)tlv_node
->data
)->type
,
303 PCEP_OBJ_TLV_TYPE_PATH_SETUP_TYPE_CAPABILITY
);
305 destroy_pcep_session(session
);
306 pcep_msg_free_message(open_msg
);
307 pceplib_free(PCEPLIB_MESSAGES
, encoded_msg
);
309 /* Verify the created Open message only has 4 TLVs:
310 * pcep_tlv_create_stateful_pce_capability()
311 * pcep_tlv_create_lsp_db_version()
312 * pcep_tlv_create_sr_pce_capability()
313 * pcep_tlv_create_path_setup_type_capability() */
314 reset_mock_socket_comm_info();
315 mock_info
->send_message_save_message
= true;
316 config
.pcep_msg_versioning
->draft_ietf_pce_segment_routing_07
= true;
318 session
= create_pcep_session(&config
, &pce_ip
);
319 CU_ASSERT_PTR_NOT_NULL(session
);
320 /* Get and verify the Open Message */
321 encoded_msg
= dll_delete_first_node(mock_info
->sent_message_list
);
322 CU_ASSERT_PTR_NOT_NULL(encoded_msg
);
323 open_msg
= pcep_decode_message(encoded_msg
);
324 CU_ASSERT_PTR_NOT_NULL(open_msg
);
325 /* Get and verify the Open Message objects */
326 CU_ASSERT_PTR_NOT_NULL(open_msg
->obj_list
);
327 CU_ASSERT_TRUE(open_msg
->obj_list
->num_entries
> 0);
328 /* Get and verify the Open object */
329 open_obj
= pcep_obj_get(open_msg
->obj_list
, PCEP_OBJ_CLASS_OPEN
);
330 CU_ASSERT_PTR_NOT_NULL(open_obj
);
331 /* Get and verify the Open object TLVs */
332 CU_ASSERT_PTR_NOT_NULL(open_obj
->tlv_list
);
333 CU_ASSERT_EQUAL(open_obj
->tlv_list
->num_entries
, 4);
334 tlv_node
= open_obj
->tlv_list
->head
;
335 CU_ASSERT_EQUAL(((struct pcep_object_tlv_header
*)tlv_node
->data
)->type
,
336 PCEP_OBJ_TLV_TYPE_STATEFUL_PCE_CAPABILITY
);
337 tlv_node
= tlv_node
->next_node
;
338 CU_ASSERT_EQUAL(((struct pcep_object_tlv_header
*)tlv_node
->data
)->type
,
339 PCEP_OBJ_TLV_TYPE_LSP_DB_VERSION
);
340 tlv_node
= tlv_node
->next_node
;
341 CU_ASSERT_EQUAL(((struct pcep_object_tlv_header
*)tlv_node
->data
)->type
,
342 PCEP_OBJ_TLV_TYPE_SR_PCE_CAPABILITY
);
343 tlv_node
= tlv_node
->next_node
;
344 CU_ASSERT_EQUAL(((struct pcep_object_tlv_header
*)tlv_node
->data
)->type
,
345 PCEP_OBJ_TLV_TYPE_PATH_SETUP_TYPE_CAPABILITY
);
347 destroy_pcep_versioning(config
.pcep_msg_versioning
);
348 destroy_pcep_session(session
);
349 pcep_msg_free_message(open_msg
);
350 pceplib_free(PCEPLIB_MESSAGES
, encoded_msg
);
352 stop_session_logic();
356 void test_destroy_pcep_session_null_session()
358 /* Just testing that it does not core dump */
359 destroy_pcep_session(NULL
);