summaryrefslogtreecommitdiffstats
path: root/pceplib/test/pcep_socket_comm_test.c
blob: 116531f12d0d29d97c6c958f782ba9627d6c28c6 (plain)
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
/*
 * This file is part of the PCEPlib, a PCEP protocol library.
 *
 * Copyright (C) 2020 Volta Networks https://voltanet.io/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Author : Brady Johnson <brady@voltanet.io>
 *
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <netinet/in.h>

#include <CUnit/CUnit.h>

#include "pcep_socket_comm.h"
#include "pcep_socket_comm_internals.h"
#include "pcep_socket_comm_test.h"

extern pcep_socket_comm_handle *socket_comm_handle_;

static pcep_socket_comm_session *test_session = NULL;
static struct in_addr test_host_ip;
static struct in_addr test_src_ip;
static struct in6_addr test_host_ipv6;
static struct in6_addr test_src_ipv6;
static short test_port = 4789;
static short test_src_port = 4999;
static uint32_t connect_timeout_millis = 500;

/*
 * Unit Test Basic pcep_socket_comm API usage.
 * Testing sending messages, etc via sockets should be done
 * with integration tests, not unit tests.
 */

/*
 * Different socket_comm handler test implementations
 */
static void test_message_received_handler(void *session_data,
					  const char *message_data,
					  unsigned int message_length)
{
	(void)session_data;
	(void)message_data;
	(void)message_length;
}

static int test_message_ready_to_read_handler(void *session_data, int socket_fd)
{
	(void)session_data;
	(void)socket_fd;
	return 1;
}

static void test_message_sent_handler(void *session_data, int socket_fd)
{
	(void)session_data;
	(void)socket_fd;
	return;
}

static void test_connection_except_notifier(void *session_data, int socket_fd)
{
	(void)session_data;
	(void)socket_fd;
}


/*
 * Test case setup and teardown called before AND after each test.
 */
void pcep_socket_comm_test_setup()
{
	inet_pton(AF_INET, "127.0.0.1", &(test_host_ip));
	inet_pton(AF_INET, "127.0.0.1", &(test_src_ip));
	inet_pton(AF_INET6, "::1", &(test_host_ipv6));
	inet_pton(AF_INET6, "::1", &(test_src_ipv6));
}

void pcep_socket_comm_test_teardown()
{
	socket_comm_session_teardown(test_session);
	test_session = NULL;
}


/*
 * Test cases
 */

void test_pcep_socket_comm_initialize()
{
	test_session = socket_comm_session_initialize(
		test_message_received_handler, NULL, NULL,
		test_connection_except_notifier, &test_host_ip, test_port,
		connect_timeout_millis, NULL, false, NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_FALSE(test_session->is_ipv6);
}


void test_pcep_socket_comm_initialize_ipv6()
{
	test_session = socket_comm_session_initialize_ipv6(
		test_message_received_handler, NULL, NULL,
		test_connection_except_notifier, &test_host_ipv6, test_port,
		connect_timeout_millis, NULL, false, NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_TRUE(test_session->is_ipv6);
}


void test_pcep_socket_comm_initialize_with_src()
{
	/* Test that INADDR_ANY will be used when src_ip is NULL */
	test_session = socket_comm_session_initialize_with_src(
		test_message_received_handler, NULL, NULL,
		test_connection_except_notifier, NULL, 0, &test_host_ip,
		test_port, connect_timeout_millis, NULL, false, NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_EQUAL(
		test_session->src_sock_addr.src_sock_addr_ipv4.sin_addr.s_addr,
		INADDR_ANY);
	CU_ASSERT_FALSE(test_session->is_ipv6);

	socket_comm_session_teardown(test_session);
	test_session = socket_comm_session_initialize_with_src(
		test_message_received_handler, NULL, NULL,
		test_connection_except_notifier, &test_src_ip, test_src_port,
		&test_host_ip, test_port, connect_timeout_millis, NULL, false,
		NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_EQUAL(
		test_session->src_sock_addr.src_sock_addr_ipv4.sin_addr.s_addr,
		test_src_ip.s_addr);
	CU_ASSERT_EQUAL(test_session->src_sock_addr.src_sock_addr_ipv4.sin_port,
			ntohs(test_src_port));
	CU_ASSERT_FALSE(test_session->is_ipv6);
}


void test_pcep_socket_comm_initialize_with_src_ipv6()
{
	/* Test that INADDR6_ANY will be used when src_ip is NULL */
	test_session = socket_comm_session_initialize_with_src_ipv6(
		test_message_received_handler, NULL, NULL,
		test_connection_except_notifier, NULL, 0, &test_host_ipv6,
		test_port, connect_timeout_millis, NULL, false, NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_EQUAL(memcmp(&test_session->src_sock_addr.src_sock_addr_ipv6
					.sin6_addr,
			       &in6addr_any, sizeof(struct in6_addr)),
			0);
	CU_ASSERT_TRUE(test_session->is_ipv6);

	socket_comm_session_teardown(test_session);
	test_session = socket_comm_session_initialize_with_src_ipv6(
		test_message_received_handler, NULL, NULL,
		test_connection_except_notifier, &test_src_ipv6, test_src_port,
		&test_host_ipv6, test_port, connect_timeout_millis, NULL, false,
		NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_EQUAL(memcmp(&test_session->src_sock_addr.src_sock_addr_ipv6
					.sin6_addr,
			       &test_src_ipv6, sizeof(struct in6_addr)),
			0);
	CU_ASSERT_EQUAL(
		test_session->src_sock_addr.src_sock_addr_ipv6.sin6_port,
		ntohs(test_src_port));
	CU_ASSERT_TRUE(test_session->is_ipv6);
}


void test_pcep_socket_comm_initialize_tcpmd5()
{
	char tcp_md5_str[] = "hello";
	int tcp_md5_strlen = strlen(tcp_md5_str);

	test_session = socket_comm_session_initialize(
		test_message_received_handler, NULL, NULL,
		test_connection_except_notifier, &test_host_ip, test_port, 1,
		tcp_md5_str, true, NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_EQUAL(0, strncmp(tcp_md5_str,
				   test_session->tcp_authentication_str,
				   tcp_md5_strlen));
	CU_ASSERT_TRUE(test_session->is_tcp_auth_md5);
	CU_ASSERT_FALSE(socket_comm_session_connect_tcp(test_session));
	/* This call does not work, it returns errno=92, Protocol not available
	getsockopt(test_session->socket_fd, SOL_SOCKET, TCP_MD5SIG, &sig,
	&siglen);*/

	socket_comm_session_teardown(test_session);
	test_session = socket_comm_session_initialize(
		test_message_received_handler, NULL, NULL,
		test_connection_except_notifier, &test_host_ip, test_port, 1,
		tcp_md5_str, false, NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_EQUAL(0, strncmp(tcp_md5_str,
				   test_session->tcp_authentication_str,
				   tcp_md5_strlen));
	CU_ASSERT_FALSE(test_session->is_tcp_auth_md5);
	CU_ASSERT_FALSE(socket_comm_session_connect_tcp(test_session));
}


void test_pcep_socket_comm_initialize_ipv6_tcpmd5()
{
	char tcp_md5_str[] = "hello";
	int tcp_md5_strlen = strlen(tcp_md5_str);

	test_session = socket_comm_session_initialize_ipv6(
		test_message_received_handler, NULL, NULL,
		test_connection_except_notifier, &test_host_ipv6, test_port, 1,
		tcp_md5_str, true, NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_EQUAL(0, strncmp(tcp_md5_str,
				   test_session->tcp_authentication_str,
				   tcp_md5_strlen));
	CU_ASSERT_TRUE(test_session->is_tcp_auth_md5);
	CU_ASSERT_FALSE(socket_comm_session_connect_tcp(test_session));
	/* This call does not work, it returns errno=92, Protocol not available
	getsockopt(test_session->socket_fd, SOL_SOCKET, TCP_MD5SIG, &sig,
	&siglen);*/

	socket_comm_session_teardown(test_session);
	test_session = socket_comm_session_initialize_ipv6(
		test_message_received_handler, NULL, NULL,
		test_connection_except_notifier, &test_host_ipv6, test_port, 1,
		tcp_md5_str, false, NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_EQUAL(0, strncmp(tcp_md5_str,
				   test_session->tcp_authentication_str,
				   tcp_md5_strlen));
	CU_ASSERT_FALSE(test_session->is_tcp_auth_md5);
	CU_ASSERT_FALSE(socket_comm_session_connect_tcp(test_session));
}


void test_pcep_socket_comm_initialize_handlers()
{
	/* Verify incorrect handler usage is correctly handled */

	/* Both receive handlers cannot be NULL */
	test_session = socket_comm_session_initialize(
		NULL, NULL, NULL, test_connection_except_notifier,
		&test_host_ip, test_port, connect_timeout_millis, NULL, false,
		NULL);
	CU_ASSERT_PTR_NULL(test_session);

	/* Both receive handlers cannot be set */
	test_session = socket_comm_session_initialize(
		test_message_received_handler,
		test_message_ready_to_read_handler, test_message_sent_handler,
		test_connection_except_notifier, &test_host_ip, test_port,
		connect_timeout_millis, NULL, false, NULL);
	CU_ASSERT_PTR_NULL(test_session);

	/* Only one receive handler can be set */
	test_session = socket_comm_session_initialize(
		NULL, test_message_ready_to_read_handler,
		test_message_sent_handler, test_connection_except_notifier,
		&test_host_ip, test_port, connect_timeout_millis, NULL, false,
		NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
}


void test_pcep_socket_comm_session_not_initialized()
{
	CU_ASSERT_FALSE(socket_comm_session_connect_tcp(NULL));
	CU_ASSERT_FALSE(socket_comm_session_close_tcp(NULL));
	CU_ASSERT_FALSE(socket_comm_session_close_tcp_after_write(NULL));
	socket_comm_session_send_message(NULL, NULL, 0, true);
	CU_ASSERT_FALSE(socket_comm_session_teardown(NULL));
}


void test_pcep_socket_comm_session_destroy()
{
	test_session = socket_comm_session_initialize(
		test_message_received_handler, NULL, test_message_sent_handler,
		test_connection_except_notifier, &test_host_ip, test_port,
		connect_timeout_millis, NULL, false, NULL);
	CU_ASSERT_PTR_NOT_NULL(test_session);
	assert(test_session != NULL);
	CU_ASSERT_PTR_NOT_NULL(socket_comm_handle_);
	assert(socket_comm_handle_ != NULL);
	CU_ASSERT_EQUAL(socket_comm_handle_->num_active_sessions, 1);

	CU_ASSERT_TRUE(socket_comm_session_teardown(test_session));
	test_session = NULL;
	CU_ASSERT_PTR_NOT_NULL(socket_comm_handle_);

	CU_ASSERT_TRUE(destroy_socket_comm_loop());
	CU_ASSERT_PTR_NULL(socket_comm_handle_);
}