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