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>
29 #include <CUnit/CUnit.h>
31 #include "pcep_msg_encoding.h"
32 #include "pcep_session_logic.h"
33 #include "pcep_session_logic_internals.h"
34 #include "pcep_timers.h"
35 #include "pcep_utils_ordered_list.h"
36 #include "pcep_utils_memory.h"
37 #include "pcep_session_logic_loop_test.h"
40 extern pcep_session_logic_handle
*session_logic_handle_
;
41 extern pcep_event_queue
*session_logic_event_queue_
;
44 * Test suite setup and teardown called before AND after the test suite.
47 int pcep_session_logic_loop_test_suite_setup(void)
49 pceplib_memory_reset();
53 int pcep_session_logic_loop_test_suite_teardown(void)
56 pceplib_memory_dump();
62 * Test case setup and teardown called before AND after each test.
65 void pcep_session_logic_loop_test_setup()
67 /* We need to setup the session_logic_handle_ without starting the
69 session_logic_handle_
= pceplib_malloc(
70 PCEPLIB_INFRA
, sizeof(pcep_session_logic_handle
));
71 memset(session_logic_handle_
, 0, sizeof(pcep_session_logic_handle
));
72 session_logic_handle_
->active
= true;
73 session_logic_handle_
->session_logic_condition
= false;
74 session_logic_handle_
->session_list
=
75 ordered_list_initialize(pointer_compare_function
);
76 session_logic_handle_
->session_event_queue
= queue_initialize();
77 pthread_cond_init(&(session_logic_handle_
->session_logic_cond_var
),
79 pthread_mutex_init(&(session_logic_handle_
->session_logic_mutex
), NULL
);
80 pthread_mutex_init(&(session_logic_handle_
->session_list_mutex
), NULL
);
82 session_logic_event_queue_
=
83 pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_event_queue
));
84 memset(session_logic_event_queue_
, 0, sizeof(pcep_event_queue
));
85 session_logic_event_queue_
->event_queue
= queue_initialize();
89 void pcep_session_logic_loop_test_teardown()
91 ordered_list_destroy(session_logic_handle_
->session_list
);
92 queue_destroy(session_logic_handle_
->session_event_queue
);
93 pthread_mutex_unlock(&(session_logic_handle_
->session_logic_mutex
));
94 pthread_mutex_destroy(&(session_logic_handle_
->session_logic_mutex
));
95 pthread_mutex_destroy(&(session_logic_handle_
->session_list_mutex
));
96 pceplib_free(PCEPLIB_INFRA
, session_logic_handle_
);
97 session_logic_handle_
= NULL
;
99 queue_destroy(session_logic_event_queue_
->event_queue
);
100 pceplib_free(PCEPLIB_INFRA
, session_logic_event_queue_
);
101 session_logic_event_queue_
= NULL
;
109 void test_session_logic_loop_null_data()
111 /* Just testing that it does not core dump */
112 session_logic_loop(NULL
);
116 void test_session_logic_loop_inactive()
118 session_logic_handle_
->active
= false;
120 session_logic_loop(session_logic_handle_
);
124 void test_session_logic_msg_ready_handler()
126 /* Just testing that it does not core dump */
127 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(NULL
, 0), -1);
129 /* Read from an empty file should return 0, thus
130 * session_logic_msg_ready_handler returns -1 */
131 int fd
= fileno(tmpfile());
132 pcep_session session
;
133 memset(&session
, 0, sizeof(pcep_session
));
134 session
.session_id
= 100;
135 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(&session
, fd
), 0);
136 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
138 pcep_event
*e
= queue_dequeue(session_logic_event_queue_
->event_queue
);
139 CU_ASSERT_EQUAL(PCE_CLOSED_SOCKET
, e
->event_type
);
140 pceplib_free(PCEPLIB_INFRA
, e
);
141 pcep_session_event
*socket_event
= (pcep_session_event
*)queue_dequeue(
142 session_logic_handle_
->session_event_queue
);
143 CU_ASSERT_PTR_NOT_NULL(socket_event
);
144 CU_ASSERT_TRUE(socket_event
->socket_closed
);
145 pceplib_free(PCEPLIB_INFRA
, socket_event
);
147 /* A pcep_session_event should be created */
148 struct pcep_versioning
*versioning
= create_default_pcep_versioning();
149 struct pcep_message
*keep_alive_msg
= pcep_msg_create_keepalive();
150 pcep_encode_message(keep_alive_msg
, versioning
);
151 int retval
= write(fd
, (char *)keep_alive_msg
->encoded_message
,
152 keep_alive_msg
->encoded_message_length
);
153 CU_ASSERT_TRUE(retval
> 0);
154 lseek(fd
, 0, SEEK_SET
);
155 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(&session
, fd
),
156 keep_alive_msg
->encoded_message_length
);
157 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
159 socket_event
= (pcep_session_event
*)queue_dequeue(
160 session_logic_handle_
->session_event_queue
);
161 CU_ASSERT_PTR_NOT_NULL(socket_event
);
162 CU_ASSERT_FALSE(socket_event
->socket_closed
);
163 CU_ASSERT_PTR_EQUAL(socket_event
->session
, &session
);
164 CU_ASSERT_EQUAL(socket_event
->expired_timer_id
, TIMER_ID_NOT_SET
);
165 CU_ASSERT_PTR_NOT_NULL(socket_event
->received_msg_list
);
166 pcep_msg_free_message_list(socket_event
->received_msg_list
);
167 pcep_msg_free_message(keep_alive_msg
);
168 destroy_pcep_versioning(versioning
);
169 pceplib_free(PCEPLIB_INFRA
, socket_event
);
174 void test_session_logic_conn_except_notifier()
176 /* Just testing that it does not core dump */
177 session_logic_conn_except_notifier(NULL
, 1);
179 /* A pcep_session_event should be created */
180 pcep_session session
;
181 memset(&session
, 0, sizeof(pcep_session
));
182 session
.session_id
= 100;
183 session_logic_conn_except_notifier(&session
, 10);
184 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
186 pcep_session_event
*socket_event
= (pcep_session_event
*)queue_dequeue(
187 session_logic_handle_
->session_event_queue
);
188 CU_ASSERT_PTR_NOT_NULL_FATAL(socket_event
);
189 CU_ASSERT_TRUE(socket_event
->socket_closed
);
190 CU_ASSERT_PTR_EQUAL(socket_event
->session
, &session
);
191 CU_ASSERT_EQUAL(socket_event
->expired_timer_id
, TIMER_ID_NOT_SET
);
192 CU_ASSERT_PTR_NULL(socket_event
->received_msg_list
);
194 pceplib_free(PCEPLIB_INFRA
, socket_event
);
198 void test_session_logic_timer_expire_handler()
200 /* Just testing that it does not core dump */
201 session_logic_timer_expire_handler(NULL
, 42);
203 /* A pcep_session_event should be created */
204 pcep_session session
;
205 memset(&session
, 0, sizeof(pcep_session
));
206 session
.session_id
= 100;
207 session_logic_timer_expire_handler(&session
, 42);
208 CU_ASSERT_EQUAL(session_logic_handle_
->session_event_queue
->num_entries
,
210 pcep_session_event
*socket_event
= (pcep_session_event
*)queue_dequeue(
211 session_logic_handle_
->session_event_queue
);
212 CU_ASSERT_PTR_NOT_NULL_FATAL(socket_event
);
213 CU_ASSERT_FALSE(socket_event
->socket_closed
);
214 CU_ASSERT_PTR_EQUAL(socket_event
->session
, &session
);
215 CU_ASSERT_EQUAL(socket_event
->expired_timer_id
, 42);
216 CU_ASSERT_PTR_NULL(socket_event
->received_msg_list
);
218 pceplib_free(PCEPLIB_INFRA
, socket_event
);