1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * This file is part of the PCEPlib, a PCEP protocol library.
5 * Copyright (C) 2020 Volta Networks https://voltanet.io/
7 * Author : Brady Johnson <brady@voltanet.io>
17 #include <netinet/in.h>
19 #include <CUnit/CUnit.h>
21 #include "pcep_socket_comm.h"
22 #include "pcep_socket_comm_internals.h"
23 #include "pcep_socket_comm_test.h"
25 extern pcep_socket_comm_handle
*socket_comm_handle_
;
27 static pcep_socket_comm_session
*test_session
= NULL
;
28 static struct in_addr test_host_ip
;
29 static struct in_addr test_src_ip
;
30 static struct in6_addr test_host_ipv6
;
31 static struct in6_addr test_src_ipv6
;
32 static short test_port
= 4789;
33 static short test_src_port
= 4999;
34 static uint32_t connect_timeout_millis
= 500;
37 * Unit Test Basic pcep_socket_comm API usage.
38 * Testing sending messages, etc via sockets should be done
39 * with integration tests, not unit tests.
43 * Different socket_comm handler test implementations
45 static void test_message_received_handler(void *session_data
,
46 const char *message_data
,
47 unsigned int message_length
)
54 static int test_message_ready_to_read_handler(void *session_data
, int socket_fd
)
61 static void test_message_sent_handler(void *session_data
, int socket_fd
)
68 static void test_connection_except_notifier(void *session_data
, int socket_fd
)
76 * Test case setup and teardown called before AND after each test.
78 void pcep_socket_comm_test_setup()
80 inet_pton(AF_INET
, "127.0.0.1", &(test_host_ip
));
81 inet_pton(AF_INET
, "127.0.0.1", &(test_src_ip
));
82 inet_pton(AF_INET6
, "::1", &(test_host_ipv6
));
83 inet_pton(AF_INET6
, "::1", &(test_src_ipv6
));
86 void pcep_socket_comm_test_teardown()
88 socket_comm_session_teardown(test_session
);
97 void test_pcep_socket_comm_initialize()
99 test_session
= socket_comm_session_initialize(
100 test_message_received_handler
, NULL
, NULL
,
101 test_connection_except_notifier
, &test_host_ip
, test_port
,
102 connect_timeout_millis
, NULL
, false, NULL
);
103 CU_ASSERT_PTR_NOT_NULL(test_session
);
104 assert(test_session
!= NULL
);
105 CU_ASSERT_FALSE(test_session
->is_ipv6
);
109 void test_pcep_socket_comm_initialize_ipv6()
111 test_session
= socket_comm_session_initialize_ipv6(
112 test_message_received_handler
, NULL
, NULL
,
113 test_connection_except_notifier
, &test_host_ipv6
, test_port
,
114 connect_timeout_millis
, NULL
, false, NULL
);
115 CU_ASSERT_PTR_NOT_NULL(test_session
);
116 assert(test_session
!= NULL
);
117 CU_ASSERT_TRUE(test_session
->is_ipv6
);
121 void test_pcep_socket_comm_initialize_with_src()
123 /* Test that INADDR_ANY will be used when src_ip is NULL */
124 test_session
= socket_comm_session_initialize_with_src(
125 test_message_received_handler
, NULL
, NULL
,
126 test_connection_except_notifier
, NULL
, 0, &test_host_ip
,
127 test_port
, connect_timeout_millis
, NULL
, false, NULL
);
128 CU_ASSERT_PTR_NOT_NULL(test_session
);
129 assert(test_session
!= NULL
);
131 test_session
->src_sock_addr
.src_sock_addr_ipv4
.sin_addr
.s_addr
,
133 CU_ASSERT_FALSE(test_session
->is_ipv6
);
135 socket_comm_session_teardown(test_session
);
136 test_session
= socket_comm_session_initialize_with_src(
137 test_message_received_handler
, NULL
, NULL
,
138 test_connection_except_notifier
, &test_src_ip
, test_src_port
,
139 &test_host_ip
, test_port
, connect_timeout_millis
, NULL
, false,
141 CU_ASSERT_PTR_NOT_NULL(test_session
);
142 assert(test_session
!= NULL
);
144 test_session
->src_sock_addr
.src_sock_addr_ipv4
.sin_addr
.s_addr
,
146 CU_ASSERT_EQUAL(test_session
->src_sock_addr
.src_sock_addr_ipv4
.sin_port
,
147 ntohs(test_src_port
));
148 CU_ASSERT_FALSE(test_session
->is_ipv6
);
152 void test_pcep_socket_comm_initialize_with_src_ipv6()
154 /* Test that INADDR6_ANY will be used when src_ip is NULL */
155 test_session
= socket_comm_session_initialize_with_src_ipv6(
156 test_message_received_handler
, NULL
, NULL
,
157 test_connection_except_notifier
, NULL
, 0, &test_host_ipv6
,
158 test_port
, connect_timeout_millis
, NULL
, false, NULL
);
159 CU_ASSERT_PTR_NOT_NULL(test_session
);
160 assert(test_session
!= NULL
);
161 CU_ASSERT_EQUAL(memcmp(&test_session
->src_sock_addr
.src_sock_addr_ipv6
163 &in6addr_any
, sizeof(struct in6_addr
)),
165 CU_ASSERT_TRUE(test_session
->is_ipv6
);
167 socket_comm_session_teardown(test_session
);
168 test_session
= socket_comm_session_initialize_with_src_ipv6(
169 test_message_received_handler
, NULL
, NULL
,
170 test_connection_except_notifier
, &test_src_ipv6
, test_src_port
,
171 &test_host_ipv6
, test_port
, connect_timeout_millis
, NULL
, false,
173 CU_ASSERT_PTR_NOT_NULL(test_session
);
174 assert(test_session
!= NULL
);
175 CU_ASSERT_EQUAL(memcmp(&test_session
->src_sock_addr
.src_sock_addr_ipv6
177 &test_src_ipv6
, sizeof(struct in6_addr
)),
180 test_session
->src_sock_addr
.src_sock_addr_ipv6
.sin6_port
,
181 ntohs(test_src_port
));
182 CU_ASSERT_TRUE(test_session
->is_ipv6
);
186 void test_pcep_socket_comm_initialize_tcpmd5()
188 char tcp_md5_str
[] = "hello";
189 int tcp_md5_strlen
= strlen(tcp_md5_str
);
191 test_session
= socket_comm_session_initialize(
192 test_message_received_handler
, NULL
, NULL
,
193 test_connection_except_notifier
, &test_host_ip
, test_port
, 1,
194 tcp_md5_str
, true, NULL
);
195 CU_ASSERT_PTR_NOT_NULL(test_session
);
196 assert(test_session
!= NULL
);
197 CU_ASSERT_EQUAL(0, strncmp(tcp_md5_str
,
198 test_session
->tcp_authentication_str
,
200 CU_ASSERT_TRUE(test_session
->is_tcp_auth_md5
);
201 CU_ASSERT_FALSE(socket_comm_session_connect_tcp(test_session
));
202 /* This call does not work, it returns errno=92, Protocol not available
203 getsockopt(test_session->socket_fd, SOL_SOCKET, TCP_MD5SIG, &sig,
206 socket_comm_session_teardown(test_session
);
207 test_session
= socket_comm_session_initialize(
208 test_message_received_handler
, NULL
, NULL
,
209 test_connection_except_notifier
, &test_host_ip
, test_port
, 1,
210 tcp_md5_str
, false, NULL
);
211 CU_ASSERT_PTR_NOT_NULL(test_session
);
212 assert(test_session
!= NULL
);
213 CU_ASSERT_EQUAL(0, strncmp(tcp_md5_str
,
214 test_session
->tcp_authentication_str
,
216 CU_ASSERT_FALSE(test_session
->is_tcp_auth_md5
);
217 CU_ASSERT_FALSE(socket_comm_session_connect_tcp(test_session
));
221 void test_pcep_socket_comm_initialize_ipv6_tcpmd5()
223 char tcp_md5_str
[] = "hello";
224 int tcp_md5_strlen
= strlen(tcp_md5_str
);
226 test_session
= socket_comm_session_initialize_ipv6(
227 test_message_received_handler
, NULL
, NULL
,
228 test_connection_except_notifier
, &test_host_ipv6
, test_port
, 1,
229 tcp_md5_str
, true, NULL
);
230 CU_ASSERT_PTR_NOT_NULL(test_session
);
231 assert(test_session
!= NULL
);
232 CU_ASSERT_EQUAL(0, strncmp(tcp_md5_str
,
233 test_session
->tcp_authentication_str
,
235 CU_ASSERT_TRUE(test_session
->is_tcp_auth_md5
);
236 CU_ASSERT_FALSE(socket_comm_session_connect_tcp(test_session
));
237 /* This call does not work, it returns errno=92, Protocol not available
238 getsockopt(test_session->socket_fd, SOL_SOCKET, TCP_MD5SIG, &sig,
241 socket_comm_session_teardown(test_session
);
242 test_session
= socket_comm_session_initialize_ipv6(
243 test_message_received_handler
, NULL
, NULL
,
244 test_connection_except_notifier
, &test_host_ipv6
, test_port
, 1,
245 tcp_md5_str
, false, NULL
);
246 CU_ASSERT_PTR_NOT_NULL(test_session
);
247 assert(test_session
!= NULL
);
248 CU_ASSERT_EQUAL(0, strncmp(tcp_md5_str
,
249 test_session
->tcp_authentication_str
,
251 CU_ASSERT_FALSE(test_session
->is_tcp_auth_md5
);
252 CU_ASSERT_FALSE(socket_comm_session_connect_tcp(test_session
));
256 void test_pcep_socket_comm_initialize_handlers()
258 /* Verify incorrect handler usage is correctly handled */
260 /* Both receive handlers cannot be NULL */
261 test_session
= socket_comm_session_initialize(
262 NULL
, NULL
, NULL
, test_connection_except_notifier
,
263 &test_host_ip
, test_port
, connect_timeout_millis
, NULL
, false,
265 CU_ASSERT_PTR_NULL(test_session
);
267 /* Both receive handlers cannot be set */
268 test_session
= socket_comm_session_initialize(
269 test_message_received_handler
,
270 test_message_ready_to_read_handler
, test_message_sent_handler
,
271 test_connection_except_notifier
, &test_host_ip
, test_port
,
272 connect_timeout_millis
, NULL
, false, NULL
);
273 CU_ASSERT_PTR_NULL(test_session
);
275 /* Only one receive handler can be set */
276 test_session
= socket_comm_session_initialize(
277 NULL
, test_message_ready_to_read_handler
,
278 test_message_sent_handler
, test_connection_except_notifier
,
279 &test_host_ip
, test_port
, connect_timeout_millis
, NULL
, false,
281 CU_ASSERT_PTR_NOT_NULL(test_session
);
285 void test_pcep_socket_comm_session_not_initialized()
287 CU_ASSERT_FALSE(socket_comm_session_connect_tcp(NULL
));
288 CU_ASSERT_FALSE(socket_comm_session_close_tcp(NULL
));
289 CU_ASSERT_FALSE(socket_comm_session_close_tcp_after_write(NULL
));
290 socket_comm_session_send_message(NULL
, NULL
, 0, true);
291 CU_ASSERT_FALSE(socket_comm_session_teardown(NULL
));
295 void test_pcep_socket_comm_session_destroy()
297 test_session
= socket_comm_session_initialize(
298 test_message_received_handler
, NULL
, test_message_sent_handler
,
299 test_connection_except_notifier
, &test_host_ip
, test_port
,
300 connect_timeout_millis
, NULL
, false, NULL
);
301 CU_ASSERT_PTR_NOT_NULL(test_session
);
302 assert(test_session
!= NULL
);
303 CU_ASSERT_PTR_NOT_NULL(socket_comm_handle_
);
304 assert(socket_comm_handle_
!= NULL
);
305 CU_ASSERT_EQUAL(socket_comm_handle_
->num_active_sessions
, 1);
307 CU_ASSERT_TRUE(socket_comm_session_teardown(test_session
));
309 CU_ASSERT_PTR_NOT_NULL(socket_comm_handle_
);
311 CU_ASSERT_TRUE(destroy_socket_comm_loop());
312 CU_ASSERT_PTR_NULL(socket_comm_handle_
);