1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * This file is part of the PCEPlib, a PCEP protocol library.
5 * Copyright (C) 2020 Volta Networks https://voltanet.io/
7 * Author : Brady Johnson <brady@voltanet.io>
19 #include <CUnit/CUnit.h>
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"
27 void test_loop_conn_except_notifier(void *session_data
, int socket_fd
);
30 * Functions to be tested, implemented in pcep_socket_comm_loop.c
33 typedef struct ready_to_read_handler_info_
{
35 bool except_handler_called
;
40 } ready_to_read_handler_info
;
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
;
46 static int test_loop_message_ready_to_read_handler(void *session_data
,
49 read_handler_info
.handler_called
= true;
50 read_handler_info
.data
= session_data
;
51 read_handler_info
.socket_fd
= socket_fd
;
53 return read_handler_info
.bytes_read
;
57 void test_loop_conn_except_notifier(void *session_data
, int socket_fd
)
61 read_handler_info
.except_handler_called
= true;
66 * Test case setup and teardown called before AND after each test.
68 void pcep_socket_comm_loop_test_setup()
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;
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
,
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;
99 void pcep_socket_comm_loop_test_teardown()
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
;
108 if (test_comm_session
!= NULL
) {
109 pceplib_free(PCEPLIB_INFRA
, test_comm_session
);
110 test_comm_session
= NULL
;
119 void test_socket_comm_loop_null_handle()
121 /* Verify that socket_comm_loop() correctly handles a NULL
123 socket_comm_loop(NULL
);
127 void test_socket_comm_loop_not_active()
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
);
136 void test_handle_reads_no_read()
138 CU_ASSERT_PTR_NULL(test_socket_comm_handle
->read_list
->head
);
140 handle_reads(test_socket_comm_handle
);
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
);
148 void test_handle_reads_read_message()
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
,
159 handle_reads(test_socket_comm_handle
);
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
);
168 void test_handle_reads_read_message_close()
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
,
179 handle_reads(test_socket_comm_handle
);
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
);