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