]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_channel_ex.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_channel_ex.c
1 /*
2     test_cases_channel_ex.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #include "execute_tests.h"
20 #include "test_cases_channel_ex.h"
21 #include "../common/containers.h"
22 #include "../common/test_step.h"
23 #include "../common/common_handlers.h"
24 #include <assert.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <setjmp.h>
29 #include <pthread.h>
30 #include <cmocka.h>
31
32 /* Modify this to change the logging level of Meshlink */
33 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
34 /* Modify this to change the port number */
35 #define PORT 8000
36
37 static void test_case_channel_ex_01(void **state);
38 static bool test_steps_channel_ex_01(void);
39 static void test_case_channel_ex_02(void **state);
40 static bool test_steps_channel_ex_02(void);
41 static void test_case_channel_ex_03(void **state);
42 static bool test_steps_channel_ex_03(void);
43 static void test_case_channel_ex_04(void **state);
44 static bool test_steps_channel_ex_04(void);
45 static void test_case_channel_ex_05(void **state);
46 static bool test_steps_channel_ex_05(void);
47 static void test_case_channel_ex_06(void **state);
48 static bool test_steps_channel_ex_06(void);
49
50 static void cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
51 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
52
53 /* channel_acc gives us access to test whether the accept callback has been invoked or not */
54 static bool channel_acc;
55 /* mutex for the common variable */
56 pthread_mutex_t lock;
57
58 static black_box_state_t test_case_channel_ex_01_state = {
59         .test_case_name = "test_case_channel_ex_01",
60 };
61 static black_box_state_t test_case_channel_ex_02_state = {
62         .test_case_name = "test_case_channel_ex_02",
63 };
64 static black_box_state_t test_case_channel_ex_03_state = {
65         .test_case_name = "test_case_channel_ex_03",
66 };
67 static black_box_state_t test_case_channel_ex_04_state = {
68         .test_case_name = "test_case_channel_ex_04",
69 };
70 static black_box_state_t test_case_channel_ex_05_state = {
71         .test_case_name = "test_case_channel_ex_05",
72 };
73 static black_box_state_t test_case_channel_ex_06_state = {
74         .test_case_name = "test_case_channel_ex_06",
75 };
76 /* mutex for the common variable */
77 static pthread_mutex_t accept_lock = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_cond_t accept_cond = PTHREAD_COND_INITIALIZER;
79
80 static bool channel_acc;
81
82 /* channel receive callback */
83 static void cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
84         (void)mesh;
85         (void)channel;
86         (void)dat;
87         (void)len;
88
89         return;
90 }
91
92 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
93         (void)mesh;
94         (void)channel;
95         (void)dat;
96         (void)len;
97
98         assert_int_equal(port, PORT);
99
100         pthread_mutex_lock(&accept_lock);
101         channel_acc = true;
102         assert(!pthread_cond_broadcast(&accept_cond));
103         pthread_mutex_unlock(&accept_lock);
104
105         return true;
106 }
107
108 /* Execute meshlink_channel_open_ex Test Case # 1 - testing meshlink_channel_open_ex API's
109     valid case by passing all valid arguments */
110 static void test_case_channel_ex_01(void **state) {
111         execute_test(test_steps_channel_ex_01, state);
112 }
113
114 /* Test Steps for meshlink_channel_open_ex Test Case # 1 - Valid case
115
116     Test Steps:
117     1. Run NUT(Node Under Test)
118     2. Open channel to ourself
119
120     Expected Result:
121     Opens a channel and echoes the send queue data.
122 */
123 /* TODO: When send queue & send queue length are passed with some value other
124           than NULL it throws segmentation fault*/
125 static bool test_steps_channel_ex_01(void) {
126         /* Set up logging for Meshlink */
127         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
128
129         /* Create meshlink instance */
130         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
131         assert(mesh_handle);
132         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
133         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
134         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
135
136         assert(meshlink_start(mesh_handle));
137
138         /* Getting node handle for itself */
139         meshlink_node_t *node = meshlink_get_self(mesh_handle);
140         assert(node != NULL);
141
142         char string[100] = "Test the 1st case";
143         pthread_mutex_lock(&lock);
144         channel_acc = false;
145         pthread_mutex_unlock(&lock);
146
147         /* Passing all valid arguments for meshlink_channel_open_ex */
148         meshlink_channel_t *channel = NULL;
149         channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, string, strlen(string) + 1, MESHLINK_CHANNEL_UDP);
150         assert_int_not_equal(channel, NULL);
151
152         // Delay for establishing a channel
153         sleep(1);
154
155         pthread_mutex_lock(&lock);
156         bool ret = channel_acc;
157         pthread_mutex_unlock(&lock);
158
159         assert_int_equal(ret, true);
160
161         meshlink_close(mesh_handle);
162         meshlink_destroy("channelexconf");
163
164         return true;
165 }
166
167 /* Execute meshlink_channel_open_ex Test Case # 2 - testing API's valid case by passing NULL and
168     0 for send queue & it's length respectively and others with valid arguments */
169 static void test_case_channel_ex_02(void **state) {
170         execute_test(test_steps_channel_ex_02, state);
171 }
172 /* Test Steps for meshlink_channel_open_ex Test Case # 2 - Valid case (TCP channel)
173
174     Test Steps:
175     1. Run NUT(Node Under Test)
176     2. Open channel to ourself
177
178     Expected Result:
179     Opens a TCP channel successfully by setting channel_acc true*/
180 static bool test_steps_channel_ex_02(void) {
181         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
182
183         /* Create meshlink instance */
184         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
185         assert(mesh_handle);
186         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
187         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
188         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
189
190         assert(meshlink_start(mesh_handle));
191
192         meshlink_node_t *node = meshlink_get_self(mesh_handle);
193         assert(node != NULL);
194
195         pthread_mutex_lock(&lock);
196         channel_acc = false;
197         pthread_mutex_unlock(&lock);
198         sleep(1);
199
200         PRINT_TEST_CASE_MSG("Opening TCP alike channel ex\n");
201         /* Passing all valid arguments for meshlink_channel_open_ex */
202         meshlink_channel_t *channel;
203         channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
204         assert_int_not_equal(channel, NULL);
205
206         // Delay for establishing a channel
207         sleep(1);
208         pthread_mutex_lock(&lock);
209         bool ret = channel_acc;
210         pthread_mutex_unlock(&lock);
211
212         assert_int_equal(ret, true);
213
214         meshlink_close(mesh_handle);
215         meshlink_destroy("channelexconf");
216         return true;
217 }
218
219 /* Execute meshlink_channel_open_ex Test Case # 3 - Open a UDP channel */
220 static void test_case_channel_ex_03(void **state) {
221         execute_test(test_steps_channel_ex_03, state);
222 }
223 /* Test Steps for meshlink_channel_open_ex Test Case # 3 - Valid case (UDP channel)
224
225     Test Steps:
226     1. Run NUT(Node Under Test)
227     2. Open channel to ourself
228
229     Expected Result:
230     Opens a UDP channel successfully by setting channel_acc true */
231 static bool test_steps_channel_ex_03(void) {
232         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
233
234         /* Create meshlink instance */
235         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
236         assert(mesh_handle);
237         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
238         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
239         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
240
241         assert(meshlink_start(mesh_handle));
242
243         /* Getting node handle for itself */
244         meshlink_node_t *node = meshlink_get_self(mesh_handle);
245         assert(node != NULL);
246
247         pthread_mutex_lock(&lock);
248         channel_acc = false;
249         pthread_mutex_unlock(&lock);
250         sleep(1);
251
252         /* Passing all valid arguments for meshlink_channel_open_ex */
253         meshlink_channel_t *channel;
254         channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_UDP);
255         assert_int_not_equal(channel, NULL);
256
257         // Delay for establishing a channel
258         sleep(1);
259
260         pthread_mutex_lock(&lock);
261         bool ret = channel_acc;
262         pthread_mutex_unlock(&lock);
263
264         assert_int_equal(ret, true);
265
266         meshlink_close(mesh_handle);
267         meshlink_destroy("channelexconf");
268         return true;
269 }
270
271 /* Execute meshlink_channel_open_ex Test Case # 4 - Open a TCP channel with no receive callback
272     and send queue */
273 static void test_case_channel_ex_04(void **state) {
274         execute_test(test_steps_channel_ex_04, state);
275 }
276 /* Test Steps for meshlink_channel_open_ex Test Case # 4 - Valid Case (Disabling receive callback)
277
278     Test Steps:
279     1. Run NUT(Node Under Test)
280     2. Open channel to ourself
281
282     Expected Result:
283     Opens a channel
284 */
285
286 static bool test_steps_channel_ex_04(void) {
287         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
288
289         /* Create meshlink instance */
290         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
291         assert(mesh_handle);
292         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
293         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
294         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
295
296         assert(meshlink_start(mesh_handle));
297
298         /* Getting node handle for itself */
299         meshlink_node_t *node = meshlink_get_self(mesh_handle);
300         assert(node != NULL);
301
302         pthread_mutex_lock(&lock);
303         channel_acc = false;
304         pthread_mutex_unlock(&lock);
305
306         /* Passing all valid arguments for meshlink_channel_open_ex i.e disabling receive callback and send queue */
307         meshlink_channel_t *channel;
308         channel = meshlink_channel_open_ex(mesh_handle, node, PORT, NULL, NULL, 0, MESHLINK_CHANNEL_UDP);
309         assert(channel != NULL);
310         // Delay for establishing a channel
311
312         pthread_mutex_lock(&lock);
313         bool ret = channel_acc;
314         pthread_mutex_unlock(&lock);
315
316         assert_int_equal(ret, true);
317
318         meshlink_close(mesh_handle);
319         meshlink_destroy("channelexconf");
320         return true;
321 }
322
323 /* Execute meshlink_channel_open_ex Test Case # 5 - Opening channel using NULL as mesh handle argument
324     for the API */
325 static void test_case_channel_ex_05(void **state) {
326         execute_test(test_steps_channel_ex_05, state);
327 }
328 /* Test Steps for meshlink_channel_open_ex Test Case # 5 - Invalid case (NULL as mesh argument)
329
330     Test Steps:
331     1. Run NUT(Node Under Test)
332     2. Open channel by passing NULL as argument for mesh handle
333
334     Expected Result:
335     meshlink_channel_open_ex returns NULL as channel handle reporting error accordingly
336 */
337 static bool test_steps_channel_ex_05(void) {
338         /* Set up logging for Meshlink */
339         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
340
341         /* Create meshlink instance */
342         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
343         assert(mesh_handle);
344
345         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
346         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
347         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
348
349         assert(meshlink_start(mesh_handle));
350         /* Getting node handle for itself */
351         meshlink_node_t *node = meshlink_get_self(mesh_handle);
352         assert(node != NULL);
353
354         /* Trying to open channel using mesh handle as NULL argument */
355         meshlink_channel_t *channel = meshlink_channel_open_ex(NULL, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
356         assert(channel == NULL);
357
358         meshlink_close(mesh_handle);
359         meshlink_destroy("channelexconf");
360         return true;
361 }
362
363 /* Execute meshlink_channel_open_ex Test Case # 6 - Opening channel using NULL as node handle argument
364     for the API*/
365 static void test_case_channel_ex_06(void **state) {
366         execute_test(test_steps_channel_ex_06, state);
367 }
368
369 /* Test Steps for meshlink_channel_open_ex Test Case # 6 - Invalid case (NULL as node argument)
370
371     Test Steps:
372     1. Run NUT(Node Under Test)
373     2. Open channel by passing NULL as argument for node handle
374
375     Expected Result:
376     meshlink_channel_open_ex returns NULL as channel handle reporting error accordingly
377 */
378 static bool test_steps_channel_ex_06(void) {
379         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
380
381         /* Create meshlink instance */
382         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
383         assert(mesh_handle);
384
385         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
386         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
387         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
388
389         assert(meshlink_start(mesh_handle));
390
391         /* Trying to open channel using node handle as NULL argument */
392         meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_handle, NULL, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
393
394         assert_int_equal(channel, NULL);
395
396         meshlink_close(mesh_handle);
397         meshlink_destroy("channelexconf");
398         return true;
399 }
400
401 int test_meshlink_channel_open_ex(void) {
402         const struct CMUnitTest blackbox_channel_ex_tests[] = {
403                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_01, NULL, NULL,
404                                 (void *)&test_case_channel_ex_01_state),
405                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_02, NULL, NULL,
406                                 (void *)&test_case_channel_ex_02_state),
407                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_03, NULL, NULL,
408                                 (void *)&test_case_channel_ex_03_state),
409                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_04, NULL, NULL,
410                                 (void *)&test_case_channel_ex_04_state),
411                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_05, NULL, NULL,
412                                 (void *)&test_case_channel_ex_05_state),
413                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_06, NULL, NULL,
414                                 (void *)&test_case_channel_ex_06_state)
415         };
416
417         total_tests += sizeof(blackbox_channel_ex_tests) / sizeof(blackbox_channel_ex_tests[0]);
418
419         assert(pthread_mutex_init(&lock, NULL) == 0);
420         int failed = cmocka_run_group_tests(blackbox_channel_ex_tests, NULL, NULL);
421         assert(pthread_mutex_destroy(&lock) == 0);
422
423         return failed;
424 }