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>
27 #include <CUnit/CUnit.h>
29 #include "pcep_socket_comm_internals.h"
30 #include "pcep_socket_comm_loop.h"
31 #include "pcep_socket_comm_loop_test.h"
32 #include "pcep_socket_comm.h"
33 #include "pcep_utils_memory.h"
35 void test_loop_conn_except_notifier(void *session_data
, int socket_fd
);
38 * Functions to be tested, implemented in pcep_socket_comm_loop.c
41 typedef struct ready_to_read_handler_info_
{
43 bool except_handler_called
;
48 } ready_to_read_handler_info
;
50 static ready_to_read_handler_info read_handler_info
;
51 static pcep_socket_comm_session
*test_comm_session
;
52 static pcep_socket_comm_handle
*test_socket_comm_handle
= NULL
;
54 static int test_loop_message_ready_to_read_handler(void *session_data
,
57 read_handler_info
.handler_called
= true;
58 read_handler_info
.data
= session_data
;
59 read_handler_info
.socket_fd
= socket_fd
;
61 return read_handler_info
.bytes_read
;
65 void test_loop_conn_except_notifier(void *session_data
, int socket_fd
)
69 read_handler_info
.except_handler_called
= true;
74 * Test case setup and teardown called before AND after each test.
76 void pcep_socket_comm_loop_test_setup()
78 test_socket_comm_handle
=
79 pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_socket_comm_handle
));
80 memset(test_socket_comm_handle
, 0, sizeof(pcep_socket_comm_handle
));
81 test_socket_comm_handle
->active
= false;
82 test_socket_comm_handle
->read_list
=
83 ordered_list_initialize(socket_fd_node_compare
);
84 test_socket_comm_handle
->write_list
=
85 ordered_list_initialize(socket_fd_node_compare
);
86 test_socket_comm_handle
->session_list
=
87 ordered_list_initialize(pointer_compare_function
);
88 pthread_mutex_init(&test_socket_comm_handle
->socket_comm_mutex
, NULL
);
89 test_socket_comm_handle
->num_active_sessions
= 0;
92 pceplib_malloc(PCEPLIB_INFRA
, sizeof(pcep_socket_comm_session
));
93 memset(test_comm_session
, 0, sizeof(pcep_socket_comm_session
));
94 test_comm_session
->message_ready_to_read_handler
=
95 test_loop_message_ready_to_read_handler
;
96 ordered_list_add_node(test_socket_comm_handle
->session_list
,
99 read_handler_info
.handler_called
= false;
100 read_handler_info
.except_handler_called
= false;
101 read_handler_info
.data
= NULL
;
102 read_handler_info
.socket_fd
= -1;
103 read_handler_info
.bytes_read
= 0;
107 void pcep_socket_comm_loop_test_teardown()
109 pthread_mutex_destroy(&test_socket_comm_handle
->socket_comm_mutex
);
110 ordered_list_destroy(test_socket_comm_handle
->read_list
);
111 ordered_list_destroy(test_socket_comm_handle
->write_list
);
112 ordered_list_destroy(test_socket_comm_handle
->session_list
);
113 pceplib_free(PCEPLIB_INFRA
, test_socket_comm_handle
);
114 test_socket_comm_handle
= NULL
;
116 if (test_comm_session
!= NULL
) {
117 pceplib_free(PCEPLIB_INFRA
, test_comm_session
);
118 test_comm_session
= NULL
;
127 void test_socket_comm_loop_null_handle()
129 /* Verify that socket_comm_loop() correctly handles a NULL
131 socket_comm_loop(NULL
);
135 void test_socket_comm_loop_not_active()
137 /* Verify that event_loop() correctly handles an inactive flag */
138 pcep_socket_comm_handle handle
;
139 handle
.active
= false;
140 socket_comm_loop(&handle
);
144 void test_handle_reads_no_read()
146 CU_ASSERT_PTR_NULL(test_socket_comm_handle
->read_list
->head
);
148 handle_reads(test_socket_comm_handle
);
150 CU_ASSERT_FALSE(read_handler_info
.handler_called
);
151 CU_ASSERT_FALSE(read_handler_info
.except_handler_called
);
152 CU_ASSERT_PTR_NULL(test_socket_comm_handle
->read_list
->head
);
156 void test_handle_reads_read_message()
158 /* Setup the comm session so that it can read.
159 * It should read 100 bytes, which simulates a successful read */
160 test_comm_session
->socket_fd
= 10;
161 read_handler_info
.bytes_read
= 100;
162 FD_SET(test_comm_session
->socket_fd
,
163 &test_socket_comm_handle
->read_master_set
);
164 ordered_list_add_node(test_socket_comm_handle
->read_list
,
167 handle_reads(test_socket_comm_handle
);
169 CU_ASSERT_TRUE(read_handler_info
.handler_called
);
170 CU_ASSERT_FALSE(read_handler_info
.except_handler_called
);
171 CU_ASSERT_EQUAL(test_comm_session
->received_bytes
,
172 read_handler_info
.bytes_read
);
176 void test_handle_reads_read_message_close()
178 /* Setup the comm session so that it can read.
179 * It should read 0 bytes, which simulates that the socket closed */
180 test_comm_session
->socket_fd
= 11;
181 read_handler_info
.bytes_read
= 0;
182 FD_SET(test_comm_session
->socket_fd
,
183 &test_socket_comm_handle
->read_master_set
);
184 ordered_list_add_node(test_socket_comm_handle
->read_list
,
187 handle_reads(test_socket_comm_handle
);
189 CU_ASSERT_TRUE(read_handler_info
.handler_called
);
190 CU_ASSERT_FALSE(read_handler_info
.except_handler_called
);
191 CU_ASSERT_EQUAL(test_comm_session
->received_bytes
,
192 read_handler_info
.bytes_read
);
193 CU_ASSERT_PTR_NULL(test_socket_comm_handle
->read_list
->head
);