]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_channel_send.c
Add the blackbox container based test suite.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_channel_send.c
1 /*
2     test_cases_channel_send.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 #include "execute_tests.h"
21 #include "test_cases_channel_send.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <setjmp.h>
28 #include <cmocka.h>
29 #include <assert.h>
30 #include <string.h>
31 #include <pthread.h>
32
33 static void test_case_mesh_channel_send_01(void **state);
34 static bool test_steps_mesh_channel_send_01(void);
35 static void test_case_mesh_channel_send_02(void **state);
36 static bool test_steps_mesh_channel_send_02(void);
37 static void test_case_mesh_channel_send_03(void **state);
38 static bool test_steps_mesh_channel_send_03(void);
39 static void test_case_mesh_channel_send_04(void **state);
40 static bool test_steps_mesh_channel_send_04(void);
41
42 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
43 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable);
44 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len);
45
46 /* State structure for meshlink_channel_send Test Case #1 */
47 static black_box_state_t test_mesh_channel_send_01_state = {
48         .test_case_name = "test_case_mesh_channel_send_01",
49 };
50
51 /* State structure for meshlink_channel_send Test Case #2 */
52 static black_box_state_t test_mesh_channel_send_02_state = {
53         .test_case_name = "test_case_mesh_channel_send_02",
54 };
55
56 /* State structure for meshlink_channel_send Test Case #3 */
57 static black_box_state_t test_mesh_channel_send_03_state = {
58         .test_case_name = "test_case_mesh_channel_send_03",
59 };
60
61 /* State structure for meshlink_channel_send Test Case #4 */
62 static black_box_state_t test_mesh_channel_send_04_state = {
63         .test_case_name = "test_case_mesh_channel_send_04",
64 };
65
66 /* Execute meshlink_channel_send Test Case # 1*/
67 static void test_case_mesh_channel_send_01(void **state) {
68         execute_test(test_steps_mesh_channel_send_01, state);
69 }
70
71 static pthread_mutex_t poll_lock = PTHREAD_MUTEX_INITIALIZER;
72 static pthread_mutex_t bar_reach_lock = PTHREAD_MUTEX_INITIALIZER;
73 static pthread_mutex_t bar_responded_lock = PTHREAD_MUTEX_INITIALIZER;
74
75 static pthread_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
76 static pthread_cond_t status_cond = PTHREAD_COND_INITIALIZER;
77 static pthread_cond_t send_cond = PTHREAD_COND_INITIALIZER;
78
79 static bool polled;
80 static bool bar_reachable;
81 static bool bar_responded;
82
83 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
84         (void)mesh;
85
86         if(!strcmp(node->name, "bar")) {
87                 pthread_mutex_lock(& bar_reach_lock);
88                 bar_reachable = reachable;
89                 assert(!pthread_cond_broadcast(&status_cond));
90                 pthread_mutex_unlock(& bar_reach_lock);
91         }
92 }
93
94 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
95         (void)mesh;
96         (void)channel;
97         (void)port;
98         (void)data;
99         (void)len;
100         return false;
101 }
102
103 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
104         assert(port == 7);
105         assert(!len);
106
107         meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
108
109         return true;
110 }
111
112 /* channel receive callback */
113 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
114         if(len == 5 && !memcmp(dat, "Hello", 5)) {
115                 pthread_mutex_lock(& bar_responded_lock);
116                 bar_responded = true;
117                 assert(!pthread_cond_broadcast(&send_cond));
118                 pthread_mutex_unlock(& bar_responded_lock);
119         }
120 }
121
122 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
123         (void)len;
124
125         meshlink_set_channel_poll_cb(mesh, channel, NULL);
126         pthread_mutex_lock(&poll_lock);
127         polled = true;
128         assert(!pthread_cond_broadcast(&poll_cond));
129         pthread_mutex_unlock(&poll_lock);
130 }
131
132
133 /* Test Steps for meshlink_channel_send Test Case # 1*/
134 static bool test_steps_mesh_channel_send_01(void) {
135         struct timespec timeout = {0};
136         meshlink_destroy("chan_send_conf.1");
137         meshlink_destroy("chan_send_conf.2");
138
139         // Open two new meshlink instance.
140         meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
141         assert(mesh1 != NULL);
142         meshlink_handle_t *mesh2 = meshlink_open("chan_send_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
143         assert(mesh2 != NULL);
144         meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, meshlink_callback_logger);
145         meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, meshlink_callback_logger);
146
147         char *data = meshlink_export(mesh1);
148         assert(data);
149         assert(meshlink_import(mesh2, data));
150         free(data);
151         data = meshlink_export(mesh2);
152         assert(data);
153         assert(meshlink_import(mesh1, data));
154         free(data);
155
156         // Set the callbacks.
157
158         meshlink_set_channel_accept_cb(mesh1, reject_cb);
159         meshlink_set_channel_accept_cb(mesh2, accept_cb);
160
161         meshlink_set_node_status_cb(mesh1, status_cb);
162
163         // Start both instances
164         bar_reachable = false;
165         assert(meshlink_start(mesh1));
166         assert(meshlink_start(mesh2));
167
168         timeout.tv_sec = time(NULL) + 10;
169         pthread_mutex_lock(&bar_reach_lock);
170
171         while(bar_reachable == false) {
172                 assert(!pthread_cond_timedwait(&status_cond, &bar_reach_lock, &timeout));
173         }
174
175         pthread_mutex_unlock(&bar_reach_lock);
176
177         // Open a channel from foo to bar.
178
179         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
180         assert(bar);
181
182         bar_responded = false;
183         meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, NULL, NULL, 0);
184         meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
185
186         timeout.tv_sec = time(NULL) + 10;
187         pthread_mutex_lock(&poll_lock);
188
189         while(polled == false) {
190                 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
191         }
192
193         pthread_mutex_unlock(&poll_lock);
194
195         assert(meshlink_channel_send(mesh1, channel, "Hello", 5) >= 0);
196
197         timeout.tv_sec = time(NULL) + 20;
198         pthread_mutex_lock(&bar_responded_lock);
199
200         if(bar_responded == false) {
201                 assert(!pthread_cond_timedwait(&send_cond, &bar_responded_lock, &timeout));
202         }
203
204         pthread_mutex_unlock(&bar_responded_lock);
205
206         // Clean up.
207
208         meshlink_close(mesh2);
209         meshlink_close(mesh1);
210         meshlink_destroy("chan_send_conf.1");
211         meshlink_destroy("chan_send_conf.2");
212
213         return true;
214 }
215
216 /* Execute meshlink_channel_send Test Case # 2*/
217 static void test_case_mesh_channel_send_02(void **state) {
218         execute_test(test_steps_mesh_channel_send_02, state);
219 }
220
221 /* Test Steps for meshlink_channel_send Test Case # 2*/
222 static bool test_steps_mesh_channel_send_02(void) {
223         struct timespec timeout = {0};
224         meshlink_destroy("chan_send_conf.5");
225
226         // Open new meshlink instance.
227
228         meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.5", "foo", "channels", DEV_CLASS_BACKBONE);
229         assert(mesh1 != NULL);
230
231         meshlink_set_channel_accept_cb(mesh1, accept_cb);
232
233         // Start node instance
234
235         assert(meshlink_start(mesh1));
236
237         meshlink_node_t *node = meshlink_get_self(mesh1);
238         assert(node);
239
240         meshlink_channel_t *channel = meshlink_channel_open(mesh1, node, 7, receive_cb, NULL, 0);
241         meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
242
243         timeout.tv_sec = time(NULL) + 10;
244         pthread_mutex_lock(&poll_lock);
245
246         while(polled == false) {
247                 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
248         }
249
250         pthread_mutex_unlock(&poll_lock);
251
252         ssize_t send_return = meshlink_channel_send(NULL, channel, "Hello", 5);
253
254         assert_int_equal(send_return, -1);
255
256         // Clean up.
257
258         meshlink_close(mesh1);
259         meshlink_destroy("chan_send_conf.5");
260
261         return true;
262 }
263
264 /* Execute meshlink_channel_send Test Case # 3*/
265 static void test_case_mesh_channel_send_03(void **state) {
266         execute_test(test_steps_mesh_channel_send_03, state);
267 }
268
269 /* Test Steps for meshlink_channel_send Test Case # 3*/
270 static bool test_steps_mesh_channel_send_03(void) {
271         struct timespec timeout = {0};
272         meshlink_destroy("chan_send_conf.7");
273         // Open new meshlink instance.
274
275         meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.7", "foo", "channels", DEV_CLASS_BACKBONE);
276         assert(mesh1 != NULL);
277         meshlink_set_channel_accept_cb(mesh1, accept_cb);
278
279         // Start node instance
280
281         assert(meshlink_start(mesh1));
282
283         ssize_t send_return = meshlink_channel_send(mesh1, NULL, "Hello", 5);
284
285         assert_int_equal(send_return, -1);
286
287         // Clean up.
288
289         meshlink_close(mesh1);
290         meshlink_destroy("chan_send_conf.7");
291
292         return true;
293 }
294
295 /* Execute meshlink_channel_send Test Case # 4*/
296 static void test_case_mesh_channel_send_04(void **state) {
297         execute_test(test_steps_mesh_channel_send_04, state);
298 }
299
300 /* Test Steps for meshlink_channel_send Test Case # 4*/
301 static bool test_steps_mesh_channel_send_04(void) {
302         struct timespec timeout = {0};
303         meshlink_destroy("chan_send_conf.9");
304         // Open two new meshlink instance.
305
306         meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.9", "foo", "channels", DEV_CLASS_BACKBONE);
307         assert(mesh1 != NULL);
308
309         meshlink_set_channel_accept_cb(mesh1, accept_cb);
310
311         // Start node instance
312
313         assert(meshlink_start(mesh1));
314
315         meshlink_node_t *node = meshlink_get_self(mesh1);
316         assert(node);
317
318         meshlink_channel_t *channel = meshlink_channel_open(mesh1, node, 7, receive_cb, NULL, 0);
319         meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
320
321         timeout.tv_sec = time(NULL) + 10;
322         pthread_mutex_lock(&poll_lock);
323
324         while(polled == false) {
325                 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
326         }
327
328         pthread_mutex_unlock(&poll_lock);
329
330         ssize_t send_return = meshlink_channel_send(mesh1, channel, NULL, 5);
331
332         assert_int_equal(send_return, -1);
333
334         // Clean up.
335
336         meshlink_close(mesh1);
337         meshlink_destroy("chan_send_conf.9");
338
339         return true;
340 }
341
342 int test_meshlink_channel_send(void) {
343         const struct CMUnitTest blackbox_channel_send_tests[] = {
344                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_01, NULL, NULL,
345                                 (void *)&test_mesh_channel_send_01_state),
346                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_02, NULL, NULL,
347                                 (void *)&test_mesh_channel_send_02_state),
348                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_03, NULL, NULL,
349                                 (void *)&test_mesh_channel_send_03_state),
350                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_04, NULL, NULL,
351                                 (void *)&test_mesh_channel_send_04_state)
352         };
353
354         total_tests += sizeof(blackbox_channel_send_tests) / sizeof(blackbox_channel_send_tests[0]);
355
356         return cmocka_run_group_tests(blackbox_channel_send_tests, NULL, NULL);
357 }