]> git.proxmox.com Git - mirror_frr.git/blob - pceplib/test/pcep_socket_comm_loop_test.c
Merge pull request #8577 from volta-networks/fix_ldp_broken_lsps
[mirror_frr.git] / pceplib / test / pcep_socket_comm_loop_test.c
1 /*
2 * This file is part of the PCEPlib, a PCEP protocol library.
3 *
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author : Brady Johnson <brady@voltanet.io>
20 *
21 */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <pthread.h>
29 #include <stdlib.h>
30
31 #include <CUnit/CUnit.h>
32
33 #include "pcep_socket_comm_internals.h"
34 #include "pcep_socket_comm_loop.h"
35 #include "pcep_socket_comm_loop_test.h"
36 #include "pcep_socket_comm.h"
37 #include "pcep_utils_memory.h"
38
39 void test_loop_conn_except_notifier(void *session_data, int socket_fd);
40
41 /*
42 * Functions to be tested, implemented in pcep_socket_comm_loop.c
43 */
44
45 typedef struct ready_to_read_handler_info_ {
46 bool handler_called;
47 bool except_handler_called;
48 void *data;
49 int socket_fd;
50 int bytes_read;
51
52 } ready_to_read_handler_info;
53
54 static ready_to_read_handler_info read_handler_info;
55 static pcep_socket_comm_session *test_comm_session;
56 static pcep_socket_comm_handle *test_socket_comm_handle = NULL;
57
58 static int test_loop_message_ready_to_read_handler(void *session_data,
59 int socket_fd)
60 {
61 read_handler_info.handler_called = true;
62 read_handler_info.data = session_data;
63 read_handler_info.socket_fd = socket_fd;
64
65 return read_handler_info.bytes_read;
66 }
67
68
69 void test_loop_conn_except_notifier(void *session_data, int socket_fd)
70 {
71 (void)session_data;
72 (void)socket_fd;
73 read_handler_info.except_handler_called = true;
74 }
75
76
77 /*
78 * Test case setup and teardown called before AND after each test.
79 */
80 void pcep_socket_comm_loop_test_setup()
81 {
82 test_socket_comm_handle =
83 pceplib_malloc(PCEPLIB_INFRA, sizeof(pcep_socket_comm_handle));
84 memset(test_socket_comm_handle, 0, sizeof(pcep_socket_comm_handle));
85 test_socket_comm_handle->active = false;
86 test_socket_comm_handle->read_list =
87 ordered_list_initialize(socket_fd_node_compare);
88 test_socket_comm_handle->write_list =
89 ordered_list_initialize(socket_fd_node_compare);
90 test_socket_comm_handle->session_list =
91 ordered_list_initialize(pointer_compare_function);
92 pthread_mutex_init(&test_socket_comm_handle->socket_comm_mutex, NULL);
93 test_socket_comm_handle->num_active_sessions = 0;
94
95 test_comm_session =
96 pceplib_malloc(PCEPLIB_INFRA, sizeof(pcep_socket_comm_session));
97 memset(test_comm_session, 0, sizeof(pcep_socket_comm_session));
98 test_comm_session->message_ready_to_read_handler =
99 test_loop_message_ready_to_read_handler;
100 ordered_list_add_node(test_socket_comm_handle->session_list,
101 test_comm_session);
102
103 read_handler_info.handler_called = false;
104 read_handler_info.except_handler_called = false;
105 read_handler_info.data = NULL;
106 read_handler_info.socket_fd = -1;
107 read_handler_info.bytes_read = 0;
108 }
109
110
111 void pcep_socket_comm_loop_test_teardown()
112 {
113 pthread_mutex_destroy(&test_socket_comm_handle->socket_comm_mutex);
114 ordered_list_destroy(test_socket_comm_handle->read_list);
115 ordered_list_destroy(test_socket_comm_handle->write_list);
116 ordered_list_destroy(test_socket_comm_handle->session_list);
117 pceplib_free(PCEPLIB_INFRA, test_socket_comm_handle);
118 test_socket_comm_handle = NULL;
119
120 if (test_comm_session != NULL) {
121 pceplib_free(PCEPLIB_INFRA, test_comm_session);
122 test_comm_session = NULL;
123 }
124 }
125
126
127 /*
128 * Test cases
129 */
130
131 void test_socket_comm_loop_null_handle()
132 {
133 /* Verify that socket_comm_loop() correctly handles a NULL
134 * timers_context */
135 socket_comm_loop(NULL);
136 }
137
138
139 void test_socket_comm_loop_not_active()
140 {
141 /* Verify that event_loop() correctly handles an inactive flag */
142 pcep_socket_comm_handle handle;
143 handle.active = false;
144 socket_comm_loop(&handle);
145 }
146
147
148 void test_handle_reads_no_read()
149 {
150 CU_ASSERT_PTR_NULL(test_socket_comm_handle->read_list->head);
151
152 handle_reads(test_socket_comm_handle);
153
154 CU_ASSERT_FALSE(read_handler_info.handler_called);
155 CU_ASSERT_FALSE(read_handler_info.except_handler_called);
156 CU_ASSERT_PTR_NULL(test_socket_comm_handle->read_list->head);
157 }
158
159
160 void test_handle_reads_read_message()
161 {
162 /* Setup the comm session so that it can read.
163 * It should read 100 bytes, which simulates a successful read */
164 test_comm_session->socket_fd = 10;
165 read_handler_info.bytes_read = 100;
166 FD_SET(test_comm_session->socket_fd,
167 &test_socket_comm_handle->read_master_set);
168 ordered_list_add_node(test_socket_comm_handle->read_list,
169 test_comm_session);
170
171 handle_reads(test_socket_comm_handle);
172
173 CU_ASSERT_TRUE(read_handler_info.handler_called);
174 CU_ASSERT_FALSE(read_handler_info.except_handler_called);
175 CU_ASSERT_EQUAL(test_comm_session->received_bytes,
176 read_handler_info.bytes_read);
177 }
178
179
180 void test_handle_reads_read_message_close()
181 {
182 /* Setup the comm session so that it can read.
183 * It should read 0 bytes, which simulates that the socket closed */
184 test_comm_session->socket_fd = 11;
185 read_handler_info.bytes_read = 0;
186 FD_SET(test_comm_session->socket_fd,
187 &test_socket_comm_handle->read_master_set);
188 ordered_list_add_node(test_socket_comm_handle->read_list,
189 test_comm_session);
190
191 handle_reads(test_socket_comm_handle);
192
193 CU_ASSERT_TRUE(read_handler_info.handler_called);
194 CU_ASSERT_FALSE(read_handler_info.except_handler_called);
195 CU_ASSERT_EQUAL(test_comm_session->received_bytes,
196 read_handler_info.bytes_read);
197 CU_ASSERT_PTR_NULL(test_socket_comm_handle->read_list->head);
198 }