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>
33 #include <sys/types.h>
36 #include <CUnit/CUnit.h>
38 #include "pcep_msg_encoding.h"
39 #include "pcep_session_logic.h"
40 #include "pcep_session_logic_internals.h"
41 #include "pcep_timers.h"
42 #include "pcep_utils_ordered_list.h"
43 #include "pcep_utils_memory.h"
44 #include "pcep_session_logic_loop_test.h"
47 extern pcep_session_logic_handle
*session_logic_handle_
;
48 extern pcep_event_queue
*session_logic_event_queue_
;
51 * Test suite setup and teardown called before AND after the test suite.
54 int pcep_session_logic_loop_test_suite_setup(void)
56 pceplib_memory_reset();
60 int pcep_session_logic_loop_test_suite_teardown(void)
63 pceplib_memory_dump();
69 * Test case setup and teardown called before AND after each test.
72 void pcep_session_logic_loop_test_setup()
74 /* We need to setup the session_logic_handle_ without starting the
76 session_logic_handle_
= pceplib_malloc(
77 PCEPLIB_INFRA
, sizeof(pcep_session_logic_handle
));
78 memset(session_logic_handle_
, 0, sizeof(pcep_session_logic_handle
));
79 session_logic_handle_
->active
= true;
80 session_logic_handle_
->session_list
=
81 ordered_list_initialize(pointer_compare_function
);
82 session_logic_handle_
->session_event_queue
= queue_initialize();
83 pthread_cond_init(&(session_logic_handle_
->session_logic_cond_var
),
85 pthread_mutex_init(&(session_logic_handle_
->session_logic_mutex
), NULL
);
86 pthread_mutex_init(&(session_logic_handle_
->session_list_mutex
), NULL
);
88 pthread_mutex_lock(&(session_logic_handle_
->session_logic_mutex
));
89 session_logic_handle_
->session_logic_condition
= true;
90 pthread_cond_signal(&(session_logic_handle_
->session_logic_cond_var
));
91 pthread_mutex_unlock(&(session_logic_handle_
->session_logic_mutex
));
93 session_logic_event_queue_
=
94 pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_event_queue
));
95 memset(session_logic_event_queue_
, 0, sizeof(pcep_event_queue
));
96 session_logic_event_queue_
->event_queue
= queue_initialize();
100 void pcep_session_logic_loop_test_teardown()
102 ordered_list_destroy(session_logic_handle_
->session_list
);
103 queue_destroy(session_logic_handle_
->session_event_queue
);
104 pthread_mutex_unlock(&(session_logic_handle_
->session_logic_mutex
));
105 pthread_mutex_destroy(&(session_logic_handle_
->session_logic_mutex
));
106 pthread_mutex_destroy(&(session_logic_handle_
->session_list_mutex
));
107 pceplib_free(PCEPLIB_INFRA
, session_logic_handle_
);
108 session_logic_handle_
= NULL
;
110 queue_destroy(session_logic_event_queue_
->event_queue
);
111 pceplib_free(PCEPLIB_INFRA
, session_logic_event_queue_
);
112 session_logic_event_queue_
= NULL
;
120 void test_session_logic_loop_null_data()
122 /* Just testing that it does not core dump */
123 session_logic_loop(NULL
);
127 void test_session_logic_loop_inactive()
129 session_logic_handle_
->active
= false;
131 session_logic_loop(session_logic_handle_
);
135 void test_session_logic_msg_ready_handler()
137 /* Just testing that it does not core dump */
138 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(NULL
, 0), -1);
140 /* Read from an empty file should return 0, thus
141 * session_logic_msg_ready_handler returns -1 */
143 oldumask
= umask(S_IXUSR
|S_IXGRP
|S_IWOTH
|S_IROTH
|S_IXOTH
);
144 /* Set umask before anything for security */
146 char tmpfile
[] = "/tmp/pceplib_XXXXXX";
147 int fd
= mkstemp(tmpfile
);
150 CU_ASSERT_TRUE(fd
>=0);
153 pcep_session session
;
154 memset(&session
, 0, sizeof(pcep_session
));
155 session
.session_id
= 100;
156 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(&session
, fd
), 0);
157 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
159 pcep_event
*e
= queue_dequeue(session_logic_event_queue_
->event_queue
);
160 CU_ASSERT_EQUAL(PCE_CLOSED_SOCKET
, e
->event_type
);
161 pceplib_free(PCEPLIB_INFRA
, e
);
162 pcep_session_event
*socket_event
= (pcep_session_event
*)queue_dequeue(
163 session_logic_handle_
->session_event_queue
);
164 CU_ASSERT_PTR_NOT_NULL(socket_event
);
165 assert(socket_event
!= NULL
);
166 CU_ASSERT_TRUE(socket_event
->socket_closed
);
167 pceplib_free(PCEPLIB_INFRA
, socket_event
);
169 /* A pcep_session_event should be created */
170 struct pcep_versioning
*versioning
= create_default_pcep_versioning();
171 struct pcep_message
*keep_alive_msg
= pcep_msg_create_keepalive();
172 pcep_encode_message(keep_alive_msg
, versioning
);
173 int retval
= write(fd
, (char *)keep_alive_msg
->encoded_message
,
174 keep_alive_msg
->encoded_message_length
);
175 CU_ASSERT_TRUE(retval
> 0);
176 lseek(fd
, 0, SEEK_SET
);
177 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(&session
, fd
),
178 keep_alive_msg
->encoded_message_length
);
179 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
181 socket_event
= (pcep_session_event
*)queue_dequeue(
182 session_logic_handle_
->session_event_queue
);
183 CU_ASSERT_PTR_NOT_NULL(socket_event
);
184 assert(socket_event
!= NULL
);
185 CU_ASSERT_FALSE(socket_event
->socket_closed
);
186 CU_ASSERT_PTR_EQUAL(socket_event
->session
, &session
);
187 CU_ASSERT_EQUAL(socket_event
->expired_timer_id
, TIMER_ID_NOT_SET
);
188 CU_ASSERT_PTR_NOT_NULL(socket_event
->received_msg_list
);
189 pcep_msg_free_message_list(socket_event
->received_msg_list
);
190 pcep_msg_free_message(keep_alive_msg
);
191 destroy_pcep_versioning(versioning
);
192 pceplib_free(PCEPLIB_INFRA
, socket_event
);
197 void test_session_logic_conn_except_notifier()
199 /* Just testing that it does not core dump */
200 session_logic_conn_except_notifier(NULL
, 1);
202 /* A pcep_session_event should be created */
203 pcep_session session
;
204 memset(&session
, 0, sizeof(pcep_session
));
205 session
.session_id
= 100;
206 session_logic_conn_except_notifier(&session
, 10);
207 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
209 pcep_session_event
*socket_event
= (pcep_session_event
*)queue_dequeue(
210 session_logic_handle_
->session_event_queue
);
211 CU_ASSERT_PTR_NOT_NULL_FATAL(socket_event
);
212 assert(socket_event
!= NULL
);
213 CU_ASSERT_TRUE(socket_event
->socket_closed
);
214 CU_ASSERT_PTR_EQUAL(socket_event
->session
, &session
);
215 CU_ASSERT_EQUAL(socket_event
->expired_timer_id
, TIMER_ID_NOT_SET
);
216 CU_ASSERT_PTR_NULL(socket_event
->received_msg_list
);
218 pceplib_free(PCEPLIB_INFRA
, socket_event
);
222 void test_session_logic_timer_expire_handler()
224 /* Just testing that it does not core dump */
225 session_logic_timer_expire_handler(NULL
, 42);
227 /* A pcep_session_event should be created */
228 pcep_session session
;
229 memset(&session
, 0, sizeof(pcep_session
));
230 session
.session_id
= 100;
231 session_logic_timer_expire_handler(&session
, 42);
232 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
234 pcep_session_event
*socket_event
= (pcep_session_event
*)queue_dequeue(
235 session_logic_handle_
->session_event_queue
);
236 CU_ASSERT_PTR_NOT_NULL_FATAL(socket_event
);
237 assert(socket_event
!= NULL
);
238 CU_ASSERT_FALSE(socket_event
->socket_closed
);
239 CU_ASSERT_PTR_EQUAL(socket_event
->session
, &session
);
240 CU_ASSERT_EQUAL(socket_event
->expired_timer_id
, 42);
241 CU_ASSERT_PTR_NULL(socket_event
->received_msg_list
);
243 pceplib_free(PCEPLIB_INFRA
, socket_event
);