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