]> git.proxmox.com Git - mirror_frr.git/blob - pceplib/test/pcep_session_logic_loop_test.c
Merge pull request #8229 from idryzhov/bfdd-echo-rx-tx
[mirror_frr.git] / pceplib / test / pcep_session_logic_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 #include <string.h>
27 #include <unistd.h>
28
29 #include <CUnit/CUnit.h>
30
31 #include "pcep_msg_encoding.h"
32 #include "pcep_session_logic.h"
33 #include "pcep_session_logic_internals.h"
34 #include "pcep_timers.h"
35 #include "pcep_utils_ordered_list.h"
36 #include "pcep_utils_memory.h"
37 #include "pcep_session_logic_loop_test.h"
38
39
40 extern pcep_session_logic_handle *session_logic_handle_;
41 extern pcep_event_queue *session_logic_event_queue_;
42
43 /*
44 * Test suite setup and teardown called before AND after the test suite.
45 */
46
47 int pcep_session_logic_loop_test_suite_setup(void)
48 {
49 pceplib_memory_reset();
50 return 0;
51 }
52
53 int pcep_session_logic_loop_test_suite_teardown(void)
54 {
55 printf("\n");
56 pceplib_memory_dump();
57 return 0;
58 }
59
60
61 /*
62 * Test case setup and teardown called before AND after each test.
63 */
64
65 void pcep_session_logic_loop_test_setup()
66 {
67 /* We need to setup the session_logic_handle_ without starting the
68 * thread */
69 session_logic_handle_ = pceplib_malloc(
70 PCEPLIB_INFRA, sizeof(pcep_session_logic_handle));
71 memset(session_logic_handle_, 0, sizeof(pcep_session_logic_handle));
72 session_logic_handle_->active = true;
73 session_logic_handle_->session_logic_condition = false;
74 session_logic_handle_->session_list =
75 ordered_list_initialize(pointer_compare_function);
76 session_logic_handle_->session_event_queue = queue_initialize();
77 pthread_cond_init(&(session_logic_handle_->session_logic_cond_var),
78 NULL);
79 pthread_mutex_init(&(session_logic_handle_->session_logic_mutex), NULL);
80 pthread_mutex_init(&(session_logic_handle_->session_list_mutex), NULL);
81
82 session_logic_event_queue_ =
83 pceplib_malloc(PCEPLIB_INFRA, sizeof(pcep_event_queue));
84 memset(session_logic_event_queue_, 0, sizeof(pcep_event_queue));
85 session_logic_event_queue_->event_queue = queue_initialize();
86 }
87
88
89 void pcep_session_logic_loop_test_teardown()
90 {
91 ordered_list_destroy(session_logic_handle_->session_list);
92 queue_destroy(session_logic_handle_->session_event_queue);
93 pthread_mutex_unlock(&(session_logic_handle_->session_logic_mutex));
94 pthread_mutex_destroy(&(session_logic_handle_->session_logic_mutex));
95 pthread_mutex_destroy(&(session_logic_handle_->session_list_mutex));
96 pceplib_free(PCEPLIB_INFRA, session_logic_handle_);
97 session_logic_handle_ = NULL;
98
99 queue_destroy(session_logic_event_queue_->event_queue);
100 pceplib_free(PCEPLIB_INFRA, session_logic_event_queue_);
101 session_logic_event_queue_ = NULL;
102 }
103
104
105 /*
106 * Test cases
107 */
108
109 void test_session_logic_loop_null_data()
110 {
111 /* Just testing that it does not core dump */
112 session_logic_loop(NULL);
113 }
114
115
116 void test_session_logic_loop_inactive()
117 {
118 session_logic_handle_->active = false;
119
120 session_logic_loop(session_logic_handle_);
121 }
122
123
124 void test_session_logic_msg_ready_handler()
125 {
126 /* Just testing that it does not core dump */
127 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(NULL, 0), -1);
128
129 /* Read from an empty file should return 0, thus
130 * session_logic_msg_ready_handler returns -1 */
131 int fd = fileno(tmpfile());
132 pcep_session session;
133 memset(&session, 0, sizeof(pcep_session));
134 session.session_id = 100;
135 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(&session, fd), 0);
136 CU_ASSERT_EQUAL(session_logic_handle_->session_event_queue->num_entries,
137 1);
138 pcep_event *e = queue_dequeue(session_logic_event_queue_->event_queue);
139 CU_ASSERT_EQUAL(PCE_CLOSED_SOCKET, e->event_type);
140 pceplib_free(PCEPLIB_INFRA, e);
141 pcep_session_event *socket_event = (pcep_session_event *)queue_dequeue(
142 session_logic_handle_->session_event_queue);
143 CU_ASSERT_PTR_NOT_NULL(socket_event);
144 CU_ASSERT_TRUE(socket_event->socket_closed);
145 pceplib_free(PCEPLIB_INFRA, socket_event);
146
147 /* A pcep_session_event should be created */
148 struct pcep_versioning *versioning = create_default_pcep_versioning();
149 struct pcep_message *keep_alive_msg = pcep_msg_create_keepalive();
150 pcep_encode_message(keep_alive_msg, versioning);
151 int retval = write(fd, (char *)keep_alive_msg->encoded_message,
152 keep_alive_msg->encoded_message_length);
153 CU_ASSERT_TRUE(retval > 0);
154 lseek(fd, 0, SEEK_SET);
155 CU_ASSERT_EQUAL(session_logic_msg_ready_handler(&session, fd),
156 keep_alive_msg->encoded_message_length);
157 CU_ASSERT_EQUAL(session_logic_handle_->session_event_queue->num_entries,
158 1);
159 socket_event = (pcep_session_event *)queue_dequeue(
160 session_logic_handle_->session_event_queue);
161 CU_ASSERT_PTR_NOT_NULL(socket_event);
162 CU_ASSERT_FALSE(socket_event->socket_closed);
163 CU_ASSERT_PTR_EQUAL(socket_event->session, &session);
164 CU_ASSERT_EQUAL(socket_event->expired_timer_id, TIMER_ID_NOT_SET);
165 CU_ASSERT_PTR_NOT_NULL(socket_event->received_msg_list);
166 pcep_msg_free_message_list(socket_event->received_msg_list);
167 pcep_msg_free_message(keep_alive_msg);
168 destroy_pcep_versioning(versioning);
169 pceplib_free(PCEPLIB_INFRA, socket_event);
170 close(fd);
171 }
172
173
174 void test_session_logic_conn_except_notifier()
175 {
176 /* Just testing that it does not core dump */
177 session_logic_conn_except_notifier(NULL, 1);
178
179 /* A pcep_session_event should be created */
180 pcep_session session;
181 memset(&session, 0, sizeof(pcep_session));
182 session.session_id = 100;
183 session_logic_conn_except_notifier(&session, 10);
184 CU_ASSERT_EQUAL(session_logic_handle_->session_event_queue->num_entries,
185 1);
186 pcep_session_event *socket_event = (pcep_session_event *)queue_dequeue(
187 session_logic_handle_->session_event_queue);
188 CU_ASSERT_PTR_NOT_NULL_FATAL(socket_event);
189 CU_ASSERT_TRUE(socket_event->socket_closed);
190 CU_ASSERT_PTR_EQUAL(socket_event->session, &session);
191 CU_ASSERT_EQUAL(socket_event->expired_timer_id, TIMER_ID_NOT_SET);
192 CU_ASSERT_PTR_NULL(socket_event->received_msg_list);
193
194 pceplib_free(PCEPLIB_INFRA, socket_event);
195 }
196
197
198 void test_session_logic_timer_expire_handler()
199 {
200 /* Just testing that it does not core dump */
201 session_logic_timer_expire_handler(NULL, 42);
202
203 /* A pcep_session_event should be created */
204 pcep_session session;
205 memset(&session, 0, sizeof(pcep_session));
206 session.session_id = 100;
207 session_logic_timer_expire_handler(&session, 42);
208 CU_ASSERT_EQUAL(session_logic_handle_->session_event_queue->num_entries,
209 1);
210 pcep_session_event *socket_event = (pcep_session_event *)queue_dequeue(
211 session_logic_handle_->session_event_queue);
212 CU_ASSERT_PTR_NOT_NULL_FATAL(socket_event);
213 CU_ASSERT_FALSE(socket_event->socket_closed);
214 CU_ASSERT_PTR_EQUAL(socket_event->session, &session);
215 CU_ASSERT_EQUAL(socket_event->expired_timer_id, 42);
216 CU_ASSERT_PTR_NULL(socket_event->received_msg_list);
217
218 pceplib_free(PCEPLIB_INFRA, socket_event);
219 }