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.
24 #include "execute_tests.h"
25 #include "test_cases_channel_send.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
37 static void test_case_mesh_channel_send_01(void **state);
38 static bool test_steps_mesh_channel_send_01(void);
39 static void test_case_mesh_channel_send_02(void **state);
40 static bool test_steps_mesh_channel_send_02(void);
41 static void test_case_mesh_channel_send_03(void **state);
42 static bool test_steps_mesh_channel_send_03(void);
43 static void test_case_mesh_channel_send_04(void **state);
44 static bool test_steps_mesh_channel_send_04(void);
46 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
47 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable);
48 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len);
50 /* State structure for meshlink_channel_send Test Case #1 */
51 static black_box_state_t test_mesh_channel_send_01_state = {
52 .test_case_name = "test_case_mesh_channel_send_01",
55 /* State structure for meshlink_channel_send Test Case #2 */
56 static black_box_state_t test_mesh_channel_send_02_state = {
57 .test_case_name = "test_case_mesh_channel_send_02",
60 /* State structure for meshlink_channel_send Test Case #3 */
61 static black_box_state_t test_mesh_channel_send_03_state = {
62 .test_case_name = "test_case_mesh_channel_send_03",
65 /* State structure for meshlink_channel_send Test Case #4 */
66 static black_box_state_t test_mesh_channel_send_04_state = {
67 .test_case_name = "test_case_mesh_channel_send_04",
70 /* Execute meshlink_channel_send Test Case # 1*/
71 static void test_case_mesh_channel_send_01(void **state) {
72 execute_test(test_steps_mesh_channel_send_01, state);
75 static pthread_mutex_t poll_lock = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_mutex_t bar_reach_lock = PTHREAD_MUTEX_INITIALIZER;
77 static pthread_mutex_t bar_responded_lock = PTHREAD_MUTEX_INITIALIZER;
79 static pthread_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
80 static pthread_cond_t status_cond = PTHREAD_COND_INITIALIZER;
81 static pthread_cond_t send_cond = PTHREAD_COND_INITIALIZER;
84 static bool bar_reachable;
85 static bool bar_responded;
87 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
90 if(!strcmp(node->name, "bar")) {
91 pthread_mutex_lock(& bar_reach_lock);
92 bar_reachable = reachable;
93 assert(!pthread_cond_broadcast(&status_cond));
94 pthread_mutex_unlock(& bar_reach_lock);
98 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
107 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
113 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
118 /* channel receive callback */
119 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
123 if(len == 5 && !memcmp(dat, "Hello", 5)) {
124 pthread_mutex_lock(& bar_responded_lock);
125 bar_responded = true;
126 assert(!pthread_cond_broadcast(&send_cond));
127 pthread_mutex_unlock(& bar_responded_lock);
131 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
134 meshlink_set_channel_poll_cb(mesh, channel, NULL);
135 pthread_mutex_lock(&poll_lock);
137 assert(!pthread_cond_broadcast(&poll_cond));
138 pthread_mutex_unlock(&poll_lock);
142 /* Test Steps for meshlink_channel_send Test Case # 1*/
143 static bool test_steps_mesh_channel_send_01(void) {
144 struct timespec timeout = {0};
145 meshlink_destroy("chan_send_conf.1");
146 meshlink_destroy("chan_send_conf.2");
148 // Open two new meshlink instance.
149 meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
150 assert(mesh1 != NULL);
151 meshlink_handle_t *mesh2 = meshlink_open("chan_send_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
152 assert(mesh2 != NULL);
153 meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, meshlink_callback_logger);
154 meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, meshlink_callback_logger);
156 char *data = meshlink_export(mesh1);
158 assert(meshlink_import(mesh2, data));
160 data = meshlink_export(mesh2);
162 assert(meshlink_import(mesh1, data));
165 // Set the callbacks.
167 meshlink_set_channel_accept_cb(mesh1, reject_cb);
168 meshlink_set_channel_accept_cb(mesh2, accept_cb);
170 meshlink_set_node_status_cb(mesh1, status_cb);
172 // Start both instances
173 bar_reachable = false;
174 assert(meshlink_start(mesh1));
175 assert(meshlink_start(mesh2));
177 timeout.tv_sec = time(NULL) + 10;
178 pthread_mutex_lock(&bar_reach_lock);
180 while(bar_reachable == false) {
181 assert(!pthread_cond_timedwait(&status_cond, &bar_reach_lock, &timeout));
184 pthread_mutex_unlock(&bar_reach_lock);
186 // Open a channel from foo to bar.
188 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
191 bar_responded = false;
192 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, NULL, NULL, 0);
193 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
195 timeout.tv_sec = time(NULL) + 10;
196 pthread_mutex_lock(&poll_lock);
198 while(polled == false) {
199 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
202 pthread_mutex_unlock(&poll_lock);
204 assert(meshlink_channel_send(mesh1, channel, "Hello", 5) >= 0);
206 timeout.tv_sec = time(NULL) + 20;
207 pthread_mutex_lock(&bar_responded_lock);
209 if(bar_responded == false) {
210 assert(!pthread_cond_timedwait(&send_cond, &bar_responded_lock, &timeout));
213 pthread_mutex_unlock(&bar_responded_lock);
217 meshlink_close(mesh2);
218 meshlink_close(mesh1);
219 meshlink_destroy("chan_send_conf.1");
220 meshlink_destroy("chan_send_conf.2");
225 /* Execute meshlink_channel_send Test Case # 2*/
226 static void test_case_mesh_channel_send_02(void **state) {
227 execute_test(test_steps_mesh_channel_send_02, state);
230 /* Test Steps for meshlink_channel_send Test Case # 2*/
231 static bool test_steps_mesh_channel_send_02(void) {
232 struct timespec timeout = {0};
233 meshlink_destroy("chan_send_conf.5");
235 // Open new meshlink instance.
237 meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.5", "foo", "channels", DEV_CLASS_BACKBONE);
238 assert(mesh1 != NULL);
240 meshlink_set_channel_accept_cb(mesh1, accept_cb);
242 // Start node instance
244 assert(meshlink_start(mesh1));
246 meshlink_node_t *node = meshlink_get_self(mesh1);
249 meshlink_channel_t *channel = meshlink_channel_open(mesh1, node, 7, receive_cb, NULL, 0);
250 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
252 timeout.tv_sec = time(NULL) + 10;
253 pthread_mutex_lock(&poll_lock);
255 while(polled == false) {
256 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
259 pthread_mutex_unlock(&poll_lock);
261 ssize_t send_return = meshlink_channel_send(NULL, channel, "Hello", 5);
263 assert_int_equal(send_return, -1);
267 meshlink_close(mesh1);
268 meshlink_destroy("chan_send_conf.5");
273 /* Execute meshlink_channel_send Test Case # 3*/
274 static void test_case_mesh_channel_send_03(void **state) {
275 execute_test(test_steps_mesh_channel_send_03, state);
278 /* Test Steps for meshlink_channel_send Test Case # 3*/
279 static bool test_steps_mesh_channel_send_03(void) {
280 meshlink_destroy("chan_send_conf.7");
281 // Open new meshlink instance.
283 meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.7", "foo", "channels", DEV_CLASS_BACKBONE);
284 assert(mesh1 != NULL);
285 meshlink_set_channel_accept_cb(mesh1, accept_cb);
287 // Start node instance
289 assert(meshlink_start(mesh1));
291 ssize_t send_return = meshlink_channel_send(mesh1, NULL, "Hello", 5);
293 assert_int_equal(send_return, -1);
297 meshlink_close(mesh1);
298 meshlink_destroy("chan_send_conf.7");
303 /* Execute meshlink_channel_send Test Case # 4*/
304 static void test_case_mesh_channel_send_04(void **state) {
305 execute_test(test_steps_mesh_channel_send_04, state);
308 /* Test Steps for meshlink_channel_send Test Case # 4*/
309 static bool test_steps_mesh_channel_send_04(void) {
310 struct timespec timeout = {0};
311 meshlink_destroy("chan_send_conf.9");
312 // Open two new meshlink instance.
314 meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.9", "foo", "channels", DEV_CLASS_BACKBONE);
315 assert(mesh1 != NULL);
317 meshlink_set_channel_accept_cb(mesh1, accept_cb);
319 // Start node instance
321 assert(meshlink_start(mesh1));
323 meshlink_node_t *node = meshlink_get_self(mesh1);
326 meshlink_channel_t *channel = meshlink_channel_open(mesh1, node, 7, receive_cb, NULL, 0);
327 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
329 timeout.tv_sec = time(NULL) + 10;
330 pthread_mutex_lock(&poll_lock);
332 while(polled == false) {
333 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
336 pthread_mutex_unlock(&poll_lock);
338 ssize_t send_return = meshlink_channel_send(mesh1, channel, NULL, 5);
340 assert_int_equal(send_return, -1);
344 meshlink_close(mesh1);
345 meshlink_destroy("chan_send_conf.9");
350 int test_meshlink_channel_send(void) {
351 const struct CMUnitTest blackbox_channel_send_tests[] = {
352 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_01, NULL, NULL,
353 (void *)&test_mesh_channel_send_01_state),
354 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_02, NULL, NULL,
355 (void *)&test_mesh_channel_send_02_state),
356 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_03, NULL, NULL,
357 (void *)&test_mesh_channel_send_03_state),
358 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_send_04, NULL, NULL,
359 (void *)&test_mesh_channel_send_04_state)
362 total_tests += sizeof(blackbox_channel_send_tests) / sizeof(blackbox_channel_send_tests[0]);
364 return cmocka_run_group_tests(blackbox_channel_send_tests, NULL, NULL);