2 test_cases_channel_send.c -- Execution of specific meshlink black box test cases
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
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"
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);
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);
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",
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",
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",
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",
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);
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;
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;
80 static bool bar_reachable;
81 static bool bar_responded;
83 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
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);
94 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
103 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
109 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
114 /* channel receive callback */
115 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
119 if(len == 5 && !memcmp(dat, "Hello", 5)) {
120 pthread_mutex_lock(& bar_responded_lock);
121 bar_responded = true;
122 assert(!pthread_cond_broadcast(&send_cond));
123 pthread_mutex_unlock(& bar_responded_lock);
127 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
130 meshlink_set_channel_poll_cb(mesh, channel, NULL);
131 pthread_mutex_lock(&poll_lock);
133 assert(!pthread_cond_broadcast(&poll_cond));
134 pthread_mutex_unlock(&poll_lock);
138 /* Test Steps for meshlink_channel_send Test Case # 1*/
139 static bool test_steps_mesh_channel_send_01(void) {
140 struct timespec timeout = {0};
141 meshlink_destroy("chan_send_conf.1");
142 meshlink_destroy("chan_send_conf.2");
144 // Open two new meshlink instance.
145 meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
146 assert(mesh1 != NULL);
147 meshlink_handle_t *mesh2 = meshlink_open("chan_send_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
148 assert(mesh2 != NULL);
149 meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, meshlink_callback_logger);
150 meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, meshlink_callback_logger);
152 char *data = meshlink_export(mesh1);
154 assert(meshlink_import(mesh2, data));
156 data = meshlink_export(mesh2);
158 assert(meshlink_import(mesh1, data));
161 // Set the callbacks.
163 meshlink_set_channel_accept_cb(mesh1, reject_cb);
164 meshlink_set_channel_accept_cb(mesh2, accept_cb);
166 meshlink_set_node_status_cb(mesh1, status_cb);
168 // Start both instances
169 bar_reachable = false;
170 assert(meshlink_start(mesh1));
171 assert(meshlink_start(mesh2));
173 timeout.tv_sec = time(NULL) + 10;
174 pthread_mutex_lock(&bar_reach_lock);
176 while(bar_reachable == false) {
177 assert(!pthread_cond_timedwait(&status_cond, &bar_reach_lock, &timeout));
180 pthread_mutex_unlock(&bar_reach_lock);
182 // Open a channel from foo to bar.
184 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
187 bar_responded = false;
188 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, NULL, NULL, 0);
189 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
191 timeout.tv_sec = time(NULL) + 10;
192 pthread_mutex_lock(&poll_lock);
194 while(polled == false) {
195 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
198 pthread_mutex_unlock(&poll_lock);
200 assert(meshlink_channel_send(mesh1, channel, "Hello", 5) >= 0);
202 timeout.tv_sec = time(NULL) + 20;
203 pthread_mutex_lock(&bar_responded_lock);
205 if(bar_responded == false) {
206 assert(!pthread_cond_timedwait(&send_cond, &bar_responded_lock, &timeout));
209 pthread_mutex_unlock(&bar_responded_lock);
213 meshlink_close(mesh2);
214 meshlink_close(mesh1);
215 meshlink_destroy("chan_send_conf.1");
216 meshlink_destroy("chan_send_conf.2");
221 /* Execute meshlink_channel_send Test Case # 2*/
222 static void test_case_mesh_channel_send_02(void **state) {
223 execute_test(test_steps_mesh_channel_send_02, state);
226 /* Test Steps for meshlink_channel_send Test Case # 2*/
227 static bool test_steps_mesh_channel_send_02(void) {
228 struct timespec timeout = {0};
229 meshlink_destroy("chan_send_conf.5");
231 // Open new meshlink instance.
233 meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.5", "foo", "channels", DEV_CLASS_BACKBONE);
234 assert(mesh1 != NULL);
236 meshlink_set_channel_accept_cb(mesh1, accept_cb);
238 // Start node instance
240 assert(meshlink_start(mesh1));
242 meshlink_node_t *node = meshlink_get_self(mesh1);
245 meshlink_channel_t *channel = meshlink_channel_open(mesh1, node, 7, receive_cb, NULL, 0);
246 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
248 timeout.tv_sec = time(NULL) + 10;
249 pthread_mutex_lock(&poll_lock);
251 while(polled == false) {
252 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
255 pthread_mutex_unlock(&poll_lock);
257 ssize_t send_return = meshlink_channel_send(NULL, channel, "Hello", 5);
259 assert_int_equal(send_return, -1);
263 meshlink_close(mesh1);
264 meshlink_destroy("chan_send_conf.5");
269 /* Execute meshlink_channel_send Test Case # 3*/
270 static void test_case_mesh_channel_send_03(void **state) {
271 execute_test(test_steps_mesh_channel_send_03, state);
274 /* Test Steps for meshlink_channel_send Test Case # 3*/
275 static bool test_steps_mesh_channel_send_03(void) {
276 meshlink_destroy("chan_send_conf.7");
277 // Open new meshlink instance.
279 meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.7", "foo", "channels", DEV_CLASS_BACKBONE);
280 assert(mesh1 != NULL);
281 meshlink_set_channel_accept_cb(mesh1, accept_cb);
283 // Start node instance
285 assert(meshlink_start(mesh1));
287 ssize_t send_return = meshlink_channel_send(mesh1, NULL, "Hello", 5);
289 assert_int_equal(send_return, -1);
293 meshlink_close(mesh1);
294 meshlink_destroy("chan_send_conf.7");
299 /* Execute meshlink_channel_send Test Case # 4*/
300 static void test_case_mesh_channel_send_04(void **state) {
301 execute_test(test_steps_mesh_channel_send_04, state);
304 /* Test Steps for meshlink_channel_send Test Case # 4*/
305 static bool test_steps_mesh_channel_send_04(void) {
306 struct timespec timeout = {0};
307 meshlink_destroy("chan_send_conf.9");
308 // Open two new meshlink instance.
310 meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.9", "foo", "channels", DEV_CLASS_BACKBONE);
311 assert(mesh1 != NULL);
313 meshlink_set_channel_accept_cb(mesh1, accept_cb);
315 // Start node instance
317 assert(meshlink_start(mesh1));
319 meshlink_node_t *node = meshlink_get_self(mesh1);
322 meshlink_channel_t *channel = meshlink_channel_open(mesh1, node, 7, receive_cb, NULL, 0);
323 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
325 timeout.tv_sec = time(NULL) + 10;
326 pthread_mutex_lock(&poll_lock);
328 while(polled == false) {
329 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
332 pthread_mutex_unlock(&poll_lock);
334 ssize_t send_return = meshlink_channel_send(mesh1, channel, NULL, 5);
336 assert_int_equal(send_return, -1);
340 meshlink_close(mesh1);
341 meshlink_destroy("chan_send_conf.9");
346 int test_meshlink_channel_send(void) {
347 const struct CMUnitTest blackbox_channel_send_tests[] = {
348 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_01, NULL, NULL,
349 (void *)&test_mesh_channel_send_01_state),
350 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_02, NULL, NULL,
351 (void *)&test_mesh_channel_send_02_state),
352 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_03, NULL, NULL,
353 (void *)&test_mesh_channel_send_03_state),
354 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_04, NULL, NULL,
355 (void *)&test_mesh_channel_send_04_state)
358 total_tests += sizeof(blackbox_channel_send_tests) / sizeof(blackbox_channel_send_tests[0]);
360 return cmocka_run_group_tests(blackbox_channel_send_tests, NULL, NULL);