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