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