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 <sys/types.h>
31 #include <CUnit/CUnit.h>
33 #include "pcep_msg_encoding.h"
34 #include "pcep_session_logic.h"
35 #include "pcep_session_logic_internals.h"
36 #include "pcep_timers.h"
37 #include "pcep_utils_ordered_list.h"
38 #include "pcep_utils_memory.h"
39 #include "pcep_session_logic_loop_test.h"
42 extern pcep_session_logic_handle
*session_logic_handle_
;
43 extern pcep_event_queue
*session_logic_event_queue_
;
46 * Test suite setup and teardown called before AND after the test suite.
49 int pcep_session_logic_loop_test_suite_setup(void)
51 pceplib_memory_reset();
55 int pcep_session_logic_loop_test_suite_teardown(void)
58 pceplib_memory_dump();
64 * Test case setup and teardown called before AND after each test.
67 void pcep_session_logic_loop_test_setup()
69 /* We need to setup the session_logic_handle_ without starting the
71 session_logic_handle_
= pceplib_malloc(
72 PCEPLIB_INFRA
, sizeof(pcep_session_logic_handle
));
73 memset(session_logic_handle_
, 0, sizeof(pcep_session_logic_handle
));
74 session_logic_handle_
->active
= true;
75 session_logic_handle_
->session_list
=
76 ordered_list_initialize(pointer_compare_function
);
77 session_logic_handle_
->session_event_queue
= queue_initialize();
78 pthread_cond_init(&(session_logic_handle_
->session_logic_cond_var
),
80 pthread_mutex_init(&(session_logic_handle_
->session_logic_mutex
), NULL
);
81 pthread_mutex_init(&(session_logic_handle_
->session_list_mutex
), NULL
);
83 pthread_mutex_lock(&(session_logic_handle_
->session_logic_mutex
));
84 session_logic_handle_
->session_logic_condition
= true;
85 pthread_cond_signal(&(session_logic_handle_
->session_logic_cond_var
));
86 pthread_mutex_unlock(&(session_logic_handle_
->session_logic_mutex
));
88 session_logic_event_queue_
=
89 pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_event_queue
));
90 memset(session_logic_event_queue_
, 0, sizeof(pcep_event_queue
));
91 session_logic_event_queue_
->event_queue
= queue_initialize();
95 void pcep_session_logic_loop_test_teardown()
97 ordered_list_destroy(session_logic_handle_
->session_list
);
98 queue_destroy(session_logic_handle_
->session_event_queue
);
99 pthread_mutex_unlock(&(session_logic_handle_
->session_logic_mutex
));
100 pthread_mutex_destroy(&(session_logic_handle_
->session_logic_mutex
));
101 pthread_mutex_destroy(&(session_logic_handle_
->session_list_mutex
));
102 pceplib_free(PCEPLIB_INFRA
, session_logic_handle_
);
103 session_logic_handle_
= NULL
;
105 queue_destroy(session_logic_event_queue_
->event_queue
);
106 pceplib_free(PCEPLIB_INFRA
, session_logic_event_queue_
);
107 session_logic_event_queue_
= NULL
;
115 void test_session_logic_loop_null_data()
117 /* Just testing that it does not core dump */
118 session_logic_loop(NULL
);
122 void test_session_logic_loop_inactive()
124 session_logic_handle_
->active
= false;
126 session_logic_loop(session_logic_handle_
);
130 void test_session_logic_msg_ready_handler()
132 /* Just testing that it does not core dump */
133 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(NULL
, 0), -1);
135 /* Read from an empty file should return 0, thus
136 * session_logic_msg_ready_handler returns -1 */
138 oldumask
= umask(S_IXUSR
|S_IXGRP
|S_IWOTH
|S_IROTH
|S_IXOTH
);
139 /* Set umask before anything for security */
141 char tmpfile
[] = "/tmp/pceplib_XXXXXX";
142 int fd
= mkstemp(tmpfile
);
145 CU_ASSERT_TRUE(fd
>=0);
148 pcep_session session
;
149 memset(&session
, 0, sizeof(pcep_session
));
150 session
.session_id
= 100;
151 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(&session
, fd
), 0);
152 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
154 pcep_event
*e
= queue_dequeue(session_logic_event_queue_
->event_queue
);
155 CU_ASSERT_EQUAL(PCE_CLOSED_SOCKET
, e
->event_type
);
156 pceplib_free(PCEPLIB_INFRA
, e
);
157 pcep_session_event
*socket_event
= (pcep_session_event
*)queue_dequeue(
158 session_logic_handle_
->session_event_queue
);
159 CU_ASSERT_PTR_NOT_NULL(socket_event
);
160 CU_ASSERT_TRUE(socket_event
->socket_closed
);
161 pceplib_free(PCEPLIB_INFRA
, socket_event
);
163 /* A pcep_session_event should be created */
164 struct pcep_versioning
*versioning
= create_default_pcep_versioning();
165 struct pcep_message
*keep_alive_msg
= pcep_msg_create_keepalive();
166 pcep_encode_message(keep_alive_msg
, versioning
);
167 int retval
= write(fd
, (char *)keep_alive_msg
->encoded_message
,
168 keep_alive_msg
->encoded_message_length
);
169 CU_ASSERT_TRUE(retval
> 0);
170 lseek(fd
, 0, SEEK_SET
);
171 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(&session
, fd
),
172 keep_alive_msg
->encoded_message_length
);
173 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
175 socket_event
= (pcep_session_event
*)queue_dequeue(
176 session_logic_handle_
->session_event_queue
);
177 CU_ASSERT_PTR_NOT_NULL(socket_event
);
178 CU_ASSERT_FALSE(socket_event
->socket_closed
);
179 CU_ASSERT_PTR_EQUAL(socket_event
->session
, &session
);
180 CU_ASSERT_EQUAL(socket_event
->expired_timer_id
, TIMER_ID_NOT_SET
);
181 CU_ASSERT_PTR_NOT_NULL(socket_event
->received_msg_list
);
182 pcep_msg_free_message_list(socket_event
->received_msg_list
);
183 pcep_msg_free_message(keep_alive_msg
);
184 destroy_pcep_versioning(versioning
);
185 pceplib_free(PCEPLIB_INFRA
, socket_event
);
190 void test_session_logic_conn_except_notifier()
192 /* Just testing that it does not core dump */
193 session_logic_conn_except_notifier(NULL
, 1);
195 /* A pcep_session_event should be created */
196 pcep_session session
;
197 memset(&session
, 0, sizeof(pcep_session
));
198 session
.session_id
= 100;
199 session_logic_conn_except_notifier(&session
, 10);
200 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
202 pcep_session_event
*socket_event
= (pcep_session_event
*)queue_dequeue(
203 session_logic_handle_
->session_event_queue
);
204 CU_ASSERT_PTR_NOT_NULL_FATAL(socket_event
);
205 CU_ASSERT_TRUE(socket_event
->socket_closed
);
206 CU_ASSERT_PTR_EQUAL(socket_event
->session
, &session
);
207 CU_ASSERT_EQUAL(socket_event
->expired_timer_id
, TIMER_ID_NOT_SET
);
208 CU_ASSERT_PTR_NULL(socket_event
->received_msg_list
);
210 pceplib_free(PCEPLIB_INFRA
, socket_event
);
214 void test_session_logic_timer_expire_handler()
216 /* Just testing that it does not core dump */
217 session_logic_timer_expire_handler(NULL
, 42);
219 /* A pcep_session_event should be created */
220 pcep_session session
;
221 memset(&session
, 0, sizeof(pcep_session
));
222 session
.session_id
= 100;
223 session_logic_timer_expire_handler(&session
, 42);
224 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
226 pcep_session_event
*socket_event
= (pcep_session_event
*)queue_dequeue(
227 session_logic_handle_
->session_event_queue
);
228 CU_ASSERT_PTR_NOT_NULL_FATAL(socket_event
);
229 CU_ASSERT_FALSE(socket_event
->socket_closed
);
230 CU_ASSERT_PTR_EQUAL(socket_event
->session
, &session
);
231 CU_ASSERT_EQUAL(socket_event
->expired_timer_id
, 42);
232 CU_ASSERT_PTR_NULL(socket_event
->received_msg_list
);
234 pceplib_free(PCEPLIB_INFRA
, socket_event
);