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) {
107 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
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);
122 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
125 meshlink_set_channel_poll_cb(mesh, channel, NULL);
126 pthread_mutex_lock(&poll_lock);
128 assert(!pthread_cond_broadcast(&poll_cond));
129 pthread_mutex_unlock(&poll_lock);
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");
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);
147 char *data = meshlink_export(mesh1);
149 assert(meshlink_import(mesh2, data));
151 data = meshlink_export(mesh2);
153 assert(meshlink_import(mesh1, data));
156 // Set the callbacks.
158 meshlink_set_channel_accept_cb(mesh1, reject_cb);
159 meshlink_set_channel_accept_cb(mesh2, accept_cb);
161 meshlink_set_node_status_cb(mesh1, status_cb);
163 // Start both instances
164 bar_reachable = false;
165 assert(meshlink_start(mesh1));
166 assert(meshlink_start(mesh2));
168 timeout.tv_sec = time(NULL) + 10;
169 pthread_mutex_lock(&bar_reach_lock);
171 while(bar_reachable == false) {
172 assert(!pthread_cond_timedwait(&status_cond, &bar_reach_lock, &timeout));
175 pthread_mutex_unlock(&bar_reach_lock);
177 // Open a channel from foo to bar.
179 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
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);
186 timeout.tv_sec = time(NULL) + 10;
187 pthread_mutex_lock(&poll_lock);
189 while(polled == false) {
190 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
193 pthread_mutex_unlock(&poll_lock);
195 assert(meshlink_channel_send(mesh1, channel, "Hello", 5) >= 0);
197 timeout.tv_sec = time(NULL) + 20;
198 pthread_mutex_lock(&bar_responded_lock);
200 if(bar_responded == false) {
201 assert(!pthread_cond_timedwait(&send_cond, &bar_responded_lock, &timeout));
204 pthread_mutex_unlock(&bar_responded_lock);
208 meshlink_close(mesh2);
209 meshlink_close(mesh1);
210 meshlink_destroy("chan_send_conf.1");
211 meshlink_destroy("chan_send_conf.2");
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);
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");
226 // Open new meshlink instance.
228 meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.5", "foo", "channels", DEV_CLASS_BACKBONE);
229 assert(mesh1 != NULL);
231 meshlink_set_channel_accept_cb(mesh1, accept_cb);
233 // Start node instance
235 assert(meshlink_start(mesh1));
237 meshlink_node_t *node = meshlink_get_self(mesh1);
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);
243 timeout.tv_sec = time(NULL) + 10;
244 pthread_mutex_lock(&poll_lock);
246 while(polled == false) {
247 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
250 pthread_mutex_unlock(&poll_lock);
252 ssize_t send_return = meshlink_channel_send(NULL, channel, "Hello", 5);
254 assert_int_equal(send_return, -1);
258 meshlink_close(mesh1);
259 meshlink_destroy("chan_send_conf.5");
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);
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.
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);
279 // Start node instance
281 assert(meshlink_start(mesh1));
283 ssize_t send_return = meshlink_channel_send(mesh1, NULL, "Hello", 5);
285 assert_int_equal(send_return, -1);
289 meshlink_close(mesh1);
290 meshlink_destroy("chan_send_conf.7");
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);
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.
306 meshlink_handle_t *mesh1 = meshlink_open("chan_send_conf.9", "foo", "channels", DEV_CLASS_BACKBONE);
307 assert(mesh1 != NULL);
309 meshlink_set_channel_accept_cb(mesh1, accept_cb);
311 // Start node instance
313 assert(meshlink_start(mesh1));
315 meshlink_node_t *node = meshlink_get_self(mesh1);
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);
321 timeout.tv_sec = time(NULL) + 10;
322 pthread_mutex_lock(&poll_lock);
324 while(polled == false) {
325 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
328 pthread_mutex_unlock(&poll_lock);
330 ssize_t send_return = meshlink_channel_send(mesh1, channel, NULL, 5);
332 assert_int_equal(send_return, -1);
336 meshlink_close(mesh1);
337 meshlink_destroy("chan_send_conf.9");
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)
354 total_tests += sizeof(blackbox_channel_send_tests) / sizeof(blackbox_channel_send_tests[0]);
356 return cmocka_run_group_tests(blackbox_channel_send_tests, NULL, NULL);