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