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>
31 #include <CUnit/CUnit.h>
33 #include "pcep_socket_comm_internals.h"
34 #include "pcep_socket_comm_loop.h"
35 #include "pcep_socket_comm_loop_test.h"
36 #include "pcep_socket_comm.h"
37 #include "pcep_utils_memory.h"
39 void test_loop_conn_except_notifier(void *session_data
, int socket_fd
);
42 * Functions to be tested, implemented in pcep_socket_comm_loop.c
45 typedef struct ready_to_read_handler_info_
{
47 bool except_handler_called
;
52 } ready_to_read_handler_info
;
54 static ready_to_read_handler_info read_handler_info
;
55 static pcep_socket_comm_session
*test_comm_session
;
56 static pcep_socket_comm_handle
*test_socket_comm_handle
= NULL
;
58 static int test_loop_message_ready_to_read_handler(void *session_data
,
61 read_handler_info
.handler_called
= true;
62 read_handler_info
.data
= session_data
;
63 read_handler_info
.socket_fd
= socket_fd
;
65 return read_handler_info
.bytes_read
;
69 void test_loop_conn_except_notifier(void *session_data
, int socket_fd
)
73 read_handler_info
.except_handler_called
= true;
78 * Test case setup and teardown called before AND after each test.
80 void pcep_socket_comm_loop_test_setup()
82 test_socket_comm_handle
=
83 pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_socket_comm_handle
));
84 memset(test_socket_comm_handle
, 0, sizeof(pcep_socket_comm_handle
));
85 test_socket_comm_handle
->active
= false;
86 test_socket_comm_handle
->read_list
=
87 ordered_list_initialize(socket_fd_node_compare
);
88 test_socket_comm_handle
->write_list
=
89 ordered_list_initialize(socket_fd_node_compare
);
90 test_socket_comm_handle
->session_list
=
91 ordered_list_initialize(pointer_compare_function
);
92 pthread_mutex_init(&test_socket_comm_handle
->socket_comm_mutex
, NULL
);
93 test_socket_comm_handle
->num_active_sessions
= 0;
96 pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_socket_comm_session
));
97 memset(test_comm_session
, 0, sizeof(pcep_socket_comm_session
));
98 test_comm_session
->message_ready_to_read_handler
=
99 test_loop_message_ready_to_read_handler
;
100 ordered_list_add_node(test_socket_comm_handle
->session_list
,
103 read_handler_info
.handler_called
= false;
104 read_handler_info
.except_handler_called
= false;
105 read_handler_info
.data
= NULL
;
106 read_handler_info
.socket_fd
= -1;
107 read_handler_info
.bytes_read
= 0;
111 void pcep_socket_comm_loop_test_teardown()
113 pthread_mutex_destroy(&test_socket_comm_handle
->socket_comm_mutex
);
114 ordered_list_destroy(test_socket_comm_handle
->read_list
);
115 ordered_list_destroy(test_socket_comm_handle
->write_list
);
116 ordered_list_destroy(test_socket_comm_handle
->session_list
);
117 pceplib_free(PCEPLIB_INFRA
, test_socket_comm_handle
);
118 test_socket_comm_handle
= NULL
;
120 if (test_comm_session
!= NULL
) {
121 pceplib_free(PCEPLIB_INFRA
, test_comm_session
);
122 test_comm_session
= NULL
;
131 void test_socket_comm_loop_null_handle()
133 /* Verify that socket_comm_loop() correctly handles a NULL
135 socket_comm_loop(NULL
);
139 void test_socket_comm_loop_not_active()
141 /* Verify that event_loop() correctly handles an inactive flag */
142 pcep_socket_comm_handle handle
;
143 handle
.active
= false;
144 socket_comm_loop(&handle
);
148 void test_handle_reads_no_read()
150 CU_ASSERT_PTR_NULL(test_socket_comm_handle
->read_list
->head
);
152 handle_reads(test_socket_comm_handle
);
154 CU_ASSERT_FALSE(read_handler_info
.handler_called
);
155 CU_ASSERT_FALSE(read_handler_info
.except_handler_called
);
156 CU_ASSERT_PTR_NULL(test_socket_comm_handle
->read_list
->head
);
160 void test_handle_reads_read_message()
162 /* Setup the comm session so that it can read.
163 * It should read 100 bytes, which simulates a successful read */
164 test_comm_session
->socket_fd
= 10;
165 read_handler_info
.bytes_read
= 100;
166 FD_SET(test_comm_session
->socket_fd
,
167 &test_socket_comm_handle
->read_master_set
);
168 ordered_list_add_node(test_socket_comm_handle
->read_list
,
171 handle_reads(test_socket_comm_handle
);
173 CU_ASSERT_TRUE(read_handler_info
.handler_called
);
174 CU_ASSERT_FALSE(read_handler_info
.except_handler_called
);
175 CU_ASSERT_EQUAL(test_comm_session
->received_bytes
,
176 read_handler_info
.bytes_read
);
180 void test_handle_reads_read_message_close()
182 /* Setup the comm session so that it can read.
183 * It should read 0 bytes, which simulates that the socket closed */
184 test_comm_session
->socket_fd
= 11;
185 read_handler_info
.bytes_read
= 0;
186 FD_SET(test_comm_session
->socket_fd
,
187 &test_socket_comm_handle
->read_master_set
);
188 ordered_list_add_node(test_socket_comm_handle
->read_list
,
191 handle_reads(test_socket_comm_handle
);
193 CU_ASSERT_TRUE(read_handler_info
.handler_called
);
194 CU_ASSERT_FALSE(read_handler_info
.except_handler_called
);
195 CU_ASSERT_EQUAL(test_comm_session
->received_bytes
,
196 read_handler_info
.bytes_read
);
197 CU_ASSERT_PTR_NULL(test_socket_comm_handle
->read_list
->head
);