]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_channel_ex.c
Add the blackbox container based test suite.
[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 static black_box_state_t test_case_channel_ex_07_state = {
77         .test_case_name = "test_case_channel_ex_07",
78 };
79
80 /* mutex for the common variable */
81 static pthread_mutex_t accept_lock = PTHREAD_MUTEX_INITIALIZER;
82 static pthread_cond_t accept_cond = PTHREAD_COND_INITIALIZER;
83
84 static bool channel_acc;
85
86 /* channel receive callback */
87 static void cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
88         (void)mesh;
89         (void)channel;
90         (void)dat;
91         (void)len;
92
93         return;
94 }
95
96 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
97         (void)dat;
98         (void)len;
99         char *data = (char *) dat;
100         assert_int_equal(port, PORT);
101
102         pthread_mutex_lock(&accept_lock);
103         channel_acc = true;
104         assert(!pthread_cond_broadcast(&accept_cond));
105         pthread_mutex_unlock(&accept_lock);
106
107         return true;
108 }
109
110 /* Execute meshlink_channel_open_ex Test Case # 1 - testing meshlink_channel_open_ex API's
111     valid case by passing all valid arguments */
112 static void test_case_channel_ex_01(void **state) {
113         execute_test(test_steps_channel_ex_01, state);
114 }
115
116 /* Test Steps for meshlink_channel_open_ex Test Case # 1 - Valid case
117
118     Test Steps:
119     1. Run NUT(Node Under Test)
120     2. Open channel to ourself
121
122     Expected Result:
123     Opens a channel and echoes the send queue data.
124 */
125 /* TODO: When send queue & send queue length are passed with some value other
126           than NULL it throws segmentation fault*/
127 static bool test_steps_channel_ex_01(void) {
128         /* Set up logging for Meshlink */
129         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
130
131         /* Create meshlink instance */
132         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
133         assert(mesh_handle);
134         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
135         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
136         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
137
138         assert(meshlink_start(mesh_handle));
139
140         /* Getting node handle for itself */
141         meshlink_node_t *node = meshlink_get_self(mesh_handle);
142         assert(node != NULL);
143
144         char string[100] = "Test the 1st case";
145         pthread_mutex_lock(&lock);
146         channel_acc = false;
147         pthread_mutex_unlock(&lock);
148
149         /* Passing all valid arguments for meshlink_channel_open_ex */
150         meshlink_channel_t *channel = NULL;
151         channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, string, strlen(string) + 1, MESHLINK_CHANNEL_UDP);
152         assert_int_not_equal(channel, NULL);
153
154         // Delay for establishing a channel
155         sleep(1);
156
157         pthread_mutex_lock(&lock);
158         bool ret = channel_acc;
159         pthread_mutex_unlock(&lock);
160
161         assert_int_equal(ret, true);
162
163         meshlink_close(mesh_handle);
164         meshlink_destroy("channelexconf");
165
166         return true;
167 }
168
169 /* Execute meshlink_channel_open_ex Test Case # 2 - testing API's valid case by passing NULL and
170     0 for send queue & it's length respectively and others with valid arguments */
171 static void test_case_channel_ex_02(void **state) {
172         execute_test(test_steps_channel_ex_02, state);
173 }
174 /* Test Steps for meshlink_channel_open_ex Test Case # 2 - Valid case (TCP channel)
175
176     Test Steps:
177     1. Run NUT(Node Under Test)
178     2. Open channel to ourself
179
180     Expected Result:
181     Opens a TCP channel successfully by setting channel_acc true*/
182 static bool test_steps_channel_ex_02(void) {
183         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
184
185         /* Create meshlink instance */
186         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
187         assert(mesh_handle);
188         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
189         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
190         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
191
192         assert(meshlink_start(mesh_handle));
193
194         meshlink_node_t *node = meshlink_get_self(mesh_handle);
195         assert(node != NULL);
196
197         pthread_mutex_lock(&lock);
198         channel_acc = false;
199         pthread_mutex_unlock(&lock);
200         sleep(1);
201
202         PRINT_TEST_CASE_MSG("Opening TCP alike channel ex\n");
203         /* Passing all valid arguments for meshlink_channel_open_ex */
204         meshlink_channel_t *channel;
205         channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
206         assert_int_not_equal(channel, NULL);
207
208         // Delay for establishing a channel
209         sleep(1);
210         pthread_mutex_lock(&lock);
211         bool ret = channel_acc;
212         pthread_mutex_unlock(&lock);
213
214         assert_int_equal(ret, true);
215
216         meshlink_close(mesh_handle);
217         meshlink_destroy("channelexconf");
218         return true;
219 }
220
221 /* Execute meshlink_channel_open_ex Test Case # 3 - Open a UDP channel */
222 static void test_case_channel_ex_03(void **state) {
223         execute_test(test_steps_channel_ex_03, state);
224 }
225 /* Test Steps for meshlink_channel_open_ex Test Case # 3 - Valid case (UDP channel)
226
227     Test Steps:
228     1. Run NUT(Node Under Test)
229     2. Open channel to ourself
230
231     Expected Result:
232     Opens a UDP channel successfully by setting channel_acc true */
233 static bool test_steps_channel_ex_03(void) {
234         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
235
236         /* Create meshlink instance */
237         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
238         assert(mesh_handle);
239         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
240         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
241         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
242
243         assert(meshlink_start(mesh_handle));
244
245         /* Getting node handle for itself */
246         meshlink_node_t *node = meshlink_get_self(mesh_handle);
247         assert(node != NULL);
248
249         pthread_mutex_lock(&lock);
250         channel_acc = false;
251         pthread_mutex_unlock(&lock);
252         sleep(1);
253
254         /* Passing all valid arguments for meshlink_channel_open_ex */
255         meshlink_channel_t *channel;
256         channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_UDP);
257         assert_int_not_equal(channel, NULL);
258
259         // Delay for establishing a channel
260         sleep(1);
261
262         pthread_mutex_lock(&lock);
263         bool ret = channel_acc;
264         pthread_mutex_unlock(&lock);
265
266         assert_int_equal(ret, true);
267
268         meshlink_close(mesh_handle);
269         meshlink_destroy("channelexconf");
270         return true;
271 }
272
273 /* Execute meshlink_channel_open_ex Test Case # 4 - Open a TCP channel with no receive callback
274     and send queue */
275 static void test_case_channel_ex_04(void **state) {
276         execute_test(test_steps_channel_ex_04, state);
277 }
278 /* Test Steps for meshlink_channel_open_ex Test Case # 4 - Valid Case (Disabling receive callback)
279
280     Test Steps:
281     1. Run NUT(Node Under Test)
282     2. Open channel to ourself
283
284     Expected Result:
285     Opens a channel
286 */
287
288 static bool test_steps_channel_ex_04(void) {
289         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
290
291         /* Create meshlink instance */
292         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
293         assert(mesh_handle);
294         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
295         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
296         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
297
298         assert(meshlink_start(mesh_handle));
299
300         /* Getting node handle for itself */
301         meshlink_node_t *node = meshlink_get_self(mesh_handle);
302         assert(node != NULL);
303
304         pthread_mutex_lock(&lock);
305         channel_acc = false;
306         pthread_mutex_unlock(&lock);
307
308         /* Passing all valid arguments for meshlink_channel_open_ex i.e disabling receive callback and send queue */
309         meshlink_channel_t *channel;
310         channel = meshlink_channel_open_ex(mesh_handle, node, PORT, NULL, NULL, 0, MESHLINK_CHANNEL_UDP);
311         assert(channel != NULL);
312         // Delay for establishing a channel
313
314         pthread_mutex_lock(&lock);
315         bool ret = channel_acc;
316         pthread_mutex_unlock(&lock);
317
318         assert_int_equal(ret, true);
319
320         meshlink_close(mesh_handle);
321         meshlink_destroy("channelexconf");
322         return true;
323 }
324
325 /* Execute meshlink_channel_open_ex Test Case # 5 - Opening channel using NULL as mesh handle argument
326     for the API */
327 static void test_case_channel_ex_05(void **state) {
328         execute_test(test_steps_channel_ex_05, state);
329 }
330 /* Test Steps for meshlink_channel_open_ex Test Case # 5 - Invalid case (NULL as mesh argument)
331
332     Test Steps:
333     1. Run NUT(Node Under Test)
334     2. Open channel by passing NULL as argument for mesh handle
335
336     Expected Result:
337     meshlink_channel_open_ex returns NULL as channel handle reporting error accordingly
338 */
339 static bool test_steps_channel_ex_05(void) {
340         /* Set up logging for Meshlink */
341         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
342
343         /* Create meshlink instance */
344         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
345         assert(mesh_handle);
346
347         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
348         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
349         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
350
351         assert(meshlink_start(mesh_handle));
352         /* Getting node handle for itself */
353         meshlink_node_t *node = meshlink_get_self(mesh_handle);
354         assert(node != NULL);
355
356         /* Trying to open channel using mesh handle as NULL argument */
357         meshlink_channel_t *channel = meshlink_channel_open_ex(NULL, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
358         assert(channel == NULL);
359
360         meshlink_close(mesh_handle);
361         meshlink_destroy("channelexconf");
362         return true;
363 }
364
365 /* Execute meshlink_channel_open_ex Test Case # 6 - Opening channel using NULL as node handle argument
366     for the API*/
367 static void test_case_channel_ex_06(void **state) {
368         execute_test(test_steps_channel_ex_06, state);
369 }
370
371 /* Test Steps for meshlink_channel_open_ex Test Case # 6 - Invalid case (NULL as node argument)
372
373     Test Steps:
374     1. Run NUT(Node Under Test)
375     2. Open channel by passing NULL as argument for node handle
376
377     Expected Result:
378     meshlink_channel_open_ex returns NULL as channel handle reporting error accordingly
379 */
380 static bool test_steps_channel_ex_06(void) {
381         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
382
383         /* Create meshlink instance */
384         meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
385         assert(mesh_handle);
386
387         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
388         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
389         meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
390
391         assert(meshlink_start(mesh_handle));
392
393         /* Trying to open channel using node handle as NULL argument */
394         meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_handle, NULL, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
395
396         assert_int_equal(channel, NULL);
397
398         meshlink_close(mesh_handle);
399         meshlink_destroy("channelexconf");
400         return true;
401 }
402
403 int test_meshlink_channel_open_ex(void) {
404         const struct CMUnitTest blackbox_channel_ex_tests[] = {
405                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_01, NULL, NULL,
406                                 (void *)&test_case_channel_ex_01_state),
407                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_02, NULL, NULL,
408                                 (void *)&test_case_channel_ex_02_state),
409                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_03, NULL, NULL,
410                                 (void *)&test_case_channel_ex_03_state),
411                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_04, NULL, NULL,
412                                 (void *)&test_case_channel_ex_04_state),
413                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_05, NULL, NULL,
414                                 (void *)&test_case_channel_ex_05_state),
415                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_06, NULL, NULL,
416                                 (void *)&test_case_channel_ex_06_state)
417         };
418
419         total_tests += sizeof(blackbox_channel_ex_tests) / sizeof(blackbox_channel_ex_tests[0]);
420
421         assert(pthread_mutex_init(&lock, NULL) == 0);
422         int failed = cmocka_run_group_tests(blackbox_channel_ex_tests, NULL, NULL);
423         assert(pthread_mutex_destroy(&lock) == 0);
424
425         return failed;
426 }