1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* This file is part of the PCEPlib, a PCEP protocol library.
*
* Copyright (C) 2020 Volta Networks https://voltanet.io/
*
* Author : Brady Johnson <brady@voltanet.io>
*
*/
/*
* This module is built into a separate library, and is used by several
* other modules for unit testing, so that real sockets dont have to be
* created.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <netinet/in.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <CUnit/CUnit.h>
#include "pcep_socket_comm.h"
#include "pcep_socket_comm_mock.h"
#include "pcep_utils_queue.h"
/* reset_mock_socket_comm_info() should be used before each test */
mock_socket_comm_info mock_socket_metadata;
void setup_mock_socket_comm_info(void)
{
mock_socket_metadata.socket_comm_session_initialize_times_called = 0;
mock_socket_metadata.socket_comm_session_initialize_src_times_called =
0;
mock_socket_metadata.socket_comm_session_teardown_times_called = 0;
mock_socket_metadata.socket_comm_session_connect_tcp_times_called = 0;
mock_socket_metadata.socket_comm_session_send_message_times_called = 0;
mock_socket_metadata
.socket_comm_session_close_tcp_after_write_times_called = 0;
mock_socket_metadata.socket_comm_session_close_tcp_times_called = 0;
mock_socket_metadata.destroy_socket_comm_loop_times_called = 0;
mock_socket_metadata.send_message_save_message = false;
mock_socket_metadata.sent_message_list = dll_initialize();
}
void teardown_mock_socket_comm_info(void)
{
dll_destroy(mock_socket_metadata.sent_message_list);
}
void reset_mock_socket_comm_info(void)
{
teardown_mock_socket_comm_info();
setup_mock_socket_comm_info();
}
mock_socket_comm_info *get_mock_socket_comm_info(void)
{
return &mock_socket_metadata;
}
void verify_socket_comm_times_called(int initialized, int teardown, int connect,
int send_message,
int close_tcp_after_write, int close_tcp,
int destroy)
{
CU_ASSERT_EQUAL(initialized,
mock_socket_metadata
.socket_comm_session_initialize_times_called);
CU_ASSERT_EQUAL(
teardown,
mock_socket_metadata.socket_comm_session_teardown_times_called);
CU_ASSERT_EQUAL(connect,
mock_socket_metadata
.socket_comm_session_connect_tcp_times_called);
CU_ASSERT_EQUAL(send_message,
mock_socket_metadata
.socket_comm_session_send_message_times_called);
CU_ASSERT_EQUAL(
close_tcp_after_write,
mock_socket_metadata
.socket_comm_session_close_tcp_after_write_times_called);
CU_ASSERT_EQUAL(close_tcp,
mock_socket_metadata
.socket_comm_session_close_tcp_times_called);
CU_ASSERT_EQUAL(
destroy,
mock_socket_metadata.destroy_socket_comm_loop_times_called);
}
/*
* Mock the socket_comm functions used by session_logic for Unit Testing
*/
bool initialize_socket_comm_external_infra(
void *external_infra_data, ext_socket_read socket_read_cb,
ext_socket_write socket_write_cb,
ext_socket_pthread_create_callback thread_create_func)
{
(void)external_infra_data;
(void)socket_read_cb;
(void)socket_write_cb;
(void)thread_create_func;
mock_socket_metadata
.socket_comm_initialize_external_infra_times_called++;
return true;
}
bool destroy_socket_comm_loop(void)
{
mock_socket_metadata.destroy_socket_comm_loop_times_called++;
return false;
}
pcep_socket_comm_session *
socket_comm_session_initialize(message_received_handler msg_rcv_handler,
message_ready_to_read_handler msg_ready_handler,
message_sent_notifier msg_sent_notifier,
connection_except_notifier notifier,
struct in_addr *dst_ip, short dst_port,
uint32_t connect_timeout_millis,
const char *tcp_authentication_str,
bool is_tcp_auth_md5, void *session_data)
{
(void)msg_sent_notifier;
(void)tcp_authentication_str;
(void)is_tcp_auth_md5;
mock_socket_metadata.socket_comm_session_initialize_times_called++;
pcep_socket_comm_session *comm_session =
malloc(sizeof(pcep_socket_comm_session));
memset(comm_session, 0, sizeof(pcep_socket_comm_session));
comm_session->message_handler = msg_rcv_handler;
comm_session->message_ready_to_read_handler = msg_ready_handler;
comm_session->conn_except_notifier = notifier;
comm_session->message_queue = queue_initialize();
comm_session->session_data = session_data;
comm_session->close_after_write = false;
comm_session->connect_timeout_millis = connect_timeout_millis;
comm_session->is_ipv6 = false;
comm_session->dest_sock_addr.dest_sock_addr_ipv4.sin_family = AF_INET;
comm_session->dest_sock_addr.dest_sock_addr_ipv4.sin_port =
htons(dst_port);
comm_session->dest_sock_addr.dest_sock_addr_ipv4.sin_addr.s_addr =
dst_ip->s_addr;
return comm_session;
}
pcep_socket_comm_session *socket_comm_session_initialize_ipv6(
message_received_handler msg_rcv_handler,
message_ready_to_read_handler msg_ready_handler,
message_sent_notifier msg_sent_notifier,
connection_except_notifier notifier, struct in6_addr *dst_ip,
short dst_port, uint32_t connect_timeout_millis,
const char *tcp_authentication_str, bool is_tcp_auth_md5,
void *session_data)
{
(void)msg_sent_notifier;
(void)tcp_authentication_str;
(void)is_tcp_auth_md5;
mock_socket_metadata.socket_comm_session_initialize_times_called++;
pcep_socket_comm_session *comm_session =
malloc(sizeof(pcep_socket_comm_session));
memset(comm_session, 0, sizeof(pcep_socket_comm_session));
comm_session->message_handler = msg_rcv_handler;
comm_session->message_ready_to_read_handler = msg_ready_handler;
comm_session->conn_except_notifier = notifier;
comm_session->message_queue = queue_initialize();
comm_session->session_data = session_data;
comm_session->close_after_write = false;
comm_session->connect_timeout_millis = connect_timeout_millis;
comm_session->is_ipv6 = true;
comm_session->dest_sock_addr.dest_sock_addr_ipv6.sin6_family = AF_INET6;
comm_session->dest_sock_addr.dest_sock_addr_ipv6.sin6_port =
htons(dst_port);
memcpy(&comm_session->dest_sock_addr.dest_sock_addr_ipv6.sin6_addr,
dst_ip, sizeof(struct in6_addr));
return comm_session;
}
pcep_socket_comm_session *socket_comm_session_initialize_with_src(
message_received_handler msg_rcv_handler,
message_ready_to_read_handler msg_ready_handler,
message_sent_notifier msg_sent_notifier,
connection_except_notifier notifier, struct in_addr *src_ip,
short src_port, struct in_addr *dst_ip, short dst_port,
uint32_t connect_timeout_millis, const char *tcp_authentication_str,
bool is_tcp_auth_md5, void *session_data)
{
(void)msg_sent_notifier;
(void)tcp_authentication_str;
(void)is_tcp_auth_md5;
mock_socket_metadata.socket_comm_session_initialize_src_times_called++;
pcep_socket_comm_session *comm_session =
malloc(sizeof(pcep_socket_comm_session));
memset(comm_session, 0, sizeof(pcep_socket_comm_session));
comm_session->message_handler = msg_rcv_handler;
comm_session->message_ready_to_read_handler = msg_ready_handler;
comm_session->conn_except_notifier = notifier;
comm_session->message_queue = queue_initialize();
comm_session->session_data = session_data;
comm_session->close_after_write = false;
comm_session->connect_timeout_millis = connect_timeout_millis;
comm_session->is_ipv6 = false;
comm_session->src_sock_addr.src_sock_addr_ipv4.sin_family = AF_INET;
comm_session->src_sock_addr.src_sock_addr_ipv4.sin_port =
htons(src_port);
comm_session->src_sock_addr.src_sock_addr_ipv4.sin_addr.s_addr =
((src_ip == NULL) ? INADDR_ANY : src_ip->s_addr);
comm_session->dest_sock_addr.dest_sock_addr_ipv4.sin_family = AF_INET;
comm_session->dest_sock_addr.dest_sock_addr_ipv4.sin_port =
htons(dst_port);
comm_session->dest_sock_addr.dest_sock_addr_ipv4.sin_addr.s_addr =
dst_ip->s_addr;
return comm_session;
}
pcep_socket_comm_session *socket_comm_session_initialize_with_src_ipv6(
message_received_handler msg_rcv_handler,
message_ready_to_read_handler msg_ready_handler,
message_sent_notifier msg_sent_notifier,
connection_except_notifier notifier, struct in6_addr *src_ip,
short src_port, struct in6_addr *dst_ip, short dst_port,
uint32_t connect_timeout_millis, const char *tcp_authentication_str,
bool is_tcp_auth_md5, void *session_data)
{
(void)msg_sent_notifier;
(void)tcp_authentication_str;
(void)is_tcp_auth_md5;
mock_socket_metadata.socket_comm_session_initialize_src_times_called++;
pcep_socket_comm_session *comm_session =
malloc(sizeof(pcep_socket_comm_session));
memset(comm_session, 0, sizeof(pcep_socket_comm_session));
comm_session->message_handler = msg_rcv_handler;
comm_session->message_ready_to_read_handler = msg_ready_handler;
comm_session->conn_except_notifier = notifier;
comm_session->message_queue = queue_initialize();
comm_session->session_data = session_data;
comm_session->close_after_write = false;
comm_session->connect_timeout_millis = connect_timeout_millis;
comm_session->is_ipv6 = true;
comm_session->src_sock_addr.src_sock_addr_ipv6.sin6_family = AF_INET6;
comm_session->src_sock_addr.src_sock_addr_ipv6.sin6_port =
htons(src_port);
if (src_ip == NULL) {
comm_session->src_sock_addr.src_sock_addr_ipv6.sin6_addr =
in6addr_any;
} else {
memcpy(&comm_session->src_sock_addr.src_sock_addr_ipv6
.sin6_addr,
src_ip, sizeof(struct in6_addr));
}
comm_session->dest_sock_addr.dest_sock_addr_ipv6.sin6_family = AF_INET6;
comm_session->dest_sock_addr.dest_sock_addr_ipv6.sin6_port =
htons(dst_port);
memcpy(&comm_session->dest_sock_addr.dest_sock_addr_ipv6.sin6_addr,
dst_ip, sizeof(struct in6_addr));
return comm_session;
}
bool socket_comm_session_teardown(pcep_socket_comm_session *socket_comm_session)
{
mock_socket_metadata.socket_comm_session_teardown_times_called++;
if (socket_comm_session != NULL) {
queue_destroy(socket_comm_session->message_queue);
free(socket_comm_session);
}
return true;
}
bool socket_comm_session_connect_tcp(
pcep_socket_comm_session *socket_comm_session)
{
(void)socket_comm_session;
mock_socket_metadata.socket_comm_session_connect_tcp_times_called++;
return true;
}
void socket_comm_session_send_message(
pcep_socket_comm_session *socket_comm_session,
const char *encoded_message, unsigned int msg_length,
bool delete_after_send)
{
(void)socket_comm_session;
(void)msg_length;
mock_socket_metadata.socket_comm_session_send_message_times_called++;
if (mock_socket_metadata.send_message_save_message == true) {
/* the caller/test case is responsible for freeing the message
*/
dll_append(mock_socket_metadata.sent_message_list,
(char *)encoded_message);
} else {
if (delete_after_send == true) {
free((void *)encoded_message);
}
}
return;
}
bool socket_comm_session_close_tcp_after_write(
pcep_socket_comm_session *socket_comm_session)
{
(void)socket_comm_session;
mock_socket_metadata
.socket_comm_session_close_tcp_after_write_times_called++;
return true;
}
bool socket_comm_session_close_tcp(
pcep_socket_comm_session *socket_comm_session)
{
(void)socket_comm_session;
mock_socket_metadata.socket_comm_session_close_tcp_times_called++;
return true;
}
|