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