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