]> git.proxmox.com Git - mirror_frr.git/blob - pceplib/test/pcep_socket_comm_test.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / pceplib / test / pcep_socket_comm_test.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * This file is part of the PCEPlib, a PCEP protocol library.
4 *
5 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 *
7 * Author : Brady Johnson <brady@voltanet.io>
8 *
9 */
10
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <assert.h>
17 #include <netinet/in.h>
18
19 #include <CUnit/CUnit.h>
20
21 #include "pcep_socket_comm.h"
22 #include "pcep_socket_comm_internals.h"
23 #include "pcep_socket_comm_test.h"
24
25 extern pcep_socket_comm_handle *socket_comm_handle_;
26
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;
35
36 /*
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.
40 */
41
42 /*
43 * Different socket_comm handler test implementations
44 */
45 static void test_message_received_handler(void *session_data,
46 const char *message_data,
47 unsigned int message_length)
48 {
49 (void)session_data;
50 (void)message_data;
51 (void)message_length;
52 }
53
54 static int test_message_ready_to_read_handler(void *session_data, int socket_fd)
55 {
56 (void)session_data;
57 (void)socket_fd;
58 return 1;
59 }
60
61 static void test_message_sent_handler(void *session_data, int socket_fd)
62 {
63 (void)session_data;
64 (void)socket_fd;
65 return;
66 }
67
68 static void test_connection_except_notifier(void *session_data, int socket_fd)
69 {
70 (void)session_data;
71 (void)socket_fd;
72 }
73
74
75 /*
76 * Test case setup and teardown called before AND after each test.
77 */
78 void pcep_socket_comm_test_setup()
79 {
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));
84 }
85
86 void pcep_socket_comm_test_teardown()
87 {
88 socket_comm_session_teardown(test_session);
89 test_session = NULL;
90 }
91
92
93 /*
94 * Test cases
95 */
96
97 void test_pcep_socket_comm_initialize()
98 {
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);
106 }
107
108
109 void test_pcep_socket_comm_initialize_ipv6()
110 {
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);
118 }
119
120
121 void test_pcep_socket_comm_initialize_with_src()
122 {
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);
130 CU_ASSERT_EQUAL(
131 test_session->src_sock_addr.src_sock_addr_ipv4.sin_addr.s_addr,
132 INADDR_ANY);
133 CU_ASSERT_FALSE(test_session->is_ipv6);
134
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,
140 NULL);
141 CU_ASSERT_PTR_NOT_NULL(test_session);
142 assert(test_session != NULL);
143 CU_ASSERT_EQUAL(
144 test_session->src_sock_addr.src_sock_addr_ipv4.sin_addr.s_addr,
145 test_src_ip.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);
149 }
150
151
152 void test_pcep_socket_comm_initialize_with_src_ipv6()
153 {
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
162 .sin6_addr,
163 &in6addr_any, sizeof(struct in6_addr)),
164 0);
165 CU_ASSERT_TRUE(test_session->is_ipv6);
166
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,
172 NULL);
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
176 .sin6_addr,
177 &test_src_ipv6, sizeof(struct in6_addr)),
178 0);
179 CU_ASSERT_EQUAL(
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);
183 }
184
185
186 void test_pcep_socket_comm_initialize_tcpmd5()
187 {
188 char tcp_md5_str[] = "hello";
189 int tcp_md5_strlen = strlen(tcp_md5_str);
190
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,
199 tcp_md5_strlen));
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,
204 &siglen);*/
205
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,
215 tcp_md5_strlen));
216 CU_ASSERT_FALSE(test_session->is_tcp_auth_md5);
217 CU_ASSERT_FALSE(socket_comm_session_connect_tcp(test_session));
218 }
219
220
221 void test_pcep_socket_comm_initialize_ipv6_tcpmd5()
222 {
223 char tcp_md5_str[] = "hello";
224 int tcp_md5_strlen = strlen(tcp_md5_str);
225
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,
234 tcp_md5_strlen));
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,
239 &siglen);*/
240
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,
250 tcp_md5_strlen));
251 CU_ASSERT_FALSE(test_session->is_tcp_auth_md5);
252 CU_ASSERT_FALSE(socket_comm_session_connect_tcp(test_session));
253 }
254
255
256 void test_pcep_socket_comm_initialize_handlers()
257 {
258 /* Verify incorrect handler usage is correctly handled */
259
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,
264 NULL);
265 CU_ASSERT_PTR_NULL(test_session);
266
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);
274
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,
280 NULL);
281 CU_ASSERT_PTR_NOT_NULL(test_session);
282 }
283
284
285 void test_pcep_socket_comm_session_not_initialized()
286 {
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));
292 }
293
294
295 void test_pcep_socket_comm_session_destroy()
296 {
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);
306
307 CU_ASSERT_TRUE(socket_comm_session_teardown(test_session));
308 test_session = NULL;
309 CU_ASSERT_PTR_NOT_NULL(socket_comm_handle_);
310
311 CU_ASSERT_TRUE(destroy_socket_comm_loop());
312 CU_ASSERT_PTR_NULL(socket_comm_handle_);
313 }