2 test_cases_channel_ex.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.
19 #include "execute_tests.h"
20 #include "test_cases_channel_ex.h"
21 #include "../common/containers.h"
22 #include "../common/test_step.h"
23 #include "../common/common_handlers.h"
32 /* Modify this to change the logging level of Meshlink */
33 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
34 /* Modify this to change the port number */
37 static void test_case_channel_ex_01(void **state);
38 static bool test_steps_channel_ex_01(void);
39 static void test_case_channel_ex_02(void **state);
40 static bool test_steps_channel_ex_02(void);
41 static void test_case_channel_ex_03(void **state);
42 static bool test_steps_channel_ex_03(void);
43 static void test_case_channel_ex_04(void **state);
44 static bool test_steps_channel_ex_04(void);
45 static void test_case_channel_ex_05(void **state);
46 static bool test_steps_channel_ex_05(void);
47 static void test_case_channel_ex_06(void **state);
48 static bool test_steps_channel_ex_06(void);
50 static void cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
51 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
53 /* channel_acc gives us access to test whether the accept callback has been invoked or not */
54 static bool channel_acc;
55 /* mutex for the common variable */
58 static black_box_state_t test_case_channel_ex_01_state = {
59 .test_case_name = "test_case_channel_ex_01",
61 static black_box_state_t test_case_channel_ex_02_state = {
62 .test_case_name = "test_case_channel_ex_02",
64 static black_box_state_t test_case_channel_ex_03_state = {
65 .test_case_name = "test_case_channel_ex_03",
67 static black_box_state_t test_case_channel_ex_04_state = {
68 .test_case_name = "test_case_channel_ex_04",
70 static black_box_state_t test_case_channel_ex_05_state = {
71 .test_case_name = "test_case_channel_ex_05",
73 static black_box_state_t test_case_channel_ex_06_state = {
74 .test_case_name = "test_case_channel_ex_06",
76 static black_box_state_t test_case_channel_ex_07_state = {
77 .test_case_name = "test_case_channel_ex_07",
80 /* mutex for the common variable */
81 static pthread_mutex_t accept_lock = PTHREAD_MUTEX_INITIALIZER;
82 static pthread_cond_t accept_cond = PTHREAD_COND_INITIALIZER;
84 static bool channel_acc;
86 /* channel receive callback */
87 static void cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
96 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
99 char *data = (char *) dat;
100 assert_int_equal(port, PORT);
102 pthread_mutex_lock(&accept_lock);
104 assert(!pthread_cond_broadcast(&accept_cond));
105 pthread_mutex_unlock(&accept_lock);
110 /* Execute meshlink_channel_open_ex Test Case # 1 - testing meshlink_channel_open_ex API's
111 valid case by passing all valid arguments */
112 static void test_case_channel_ex_01(void **state) {
113 execute_test(test_steps_channel_ex_01, state);
116 /* Test Steps for meshlink_channel_open_ex Test Case # 1 - Valid case
119 1. Run NUT(Node Under Test)
120 2. Open channel to ourself
123 Opens a channel and echoes the send queue data.
125 /* TODO: When send queue & send queue length are passed with some value other
126 than NULL it throws segmentation fault*/
127 static bool test_steps_channel_ex_01(void) {
128 /* Set up logging for Meshlink */
129 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
131 /* Create meshlink instance */
132 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
134 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
135 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
136 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
138 assert(meshlink_start(mesh_handle));
140 /* Getting node handle for itself */
141 meshlink_node_t *node = meshlink_get_self(mesh_handle);
142 assert(node != NULL);
144 char string[100] = "Test the 1st case";
145 pthread_mutex_lock(&lock);
147 pthread_mutex_unlock(&lock);
149 /* Passing all valid arguments for meshlink_channel_open_ex */
150 meshlink_channel_t *channel = NULL;
151 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, string, strlen(string) + 1, MESHLINK_CHANNEL_UDP);
152 assert_int_not_equal(channel, NULL);
154 // Delay for establishing a channel
157 pthread_mutex_lock(&lock);
158 bool ret = channel_acc;
159 pthread_mutex_unlock(&lock);
161 assert_int_equal(ret, true);
163 meshlink_close(mesh_handle);
164 meshlink_destroy("channelexconf");
169 /* Execute meshlink_channel_open_ex Test Case # 2 - testing API's valid case by passing NULL and
170 0 for send queue & it's length respectively and others with valid arguments */
171 static void test_case_channel_ex_02(void **state) {
172 execute_test(test_steps_channel_ex_02, state);
174 /* Test Steps for meshlink_channel_open_ex Test Case # 2 - Valid case (TCP channel)
177 1. Run NUT(Node Under Test)
178 2. Open channel to ourself
181 Opens a TCP channel successfully by setting channel_acc true*/
182 static bool test_steps_channel_ex_02(void) {
183 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
185 /* Create meshlink instance */
186 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
188 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
189 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
190 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
192 assert(meshlink_start(mesh_handle));
194 meshlink_node_t *node = meshlink_get_self(mesh_handle);
195 assert(node != NULL);
197 pthread_mutex_lock(&lock);
199 pthread_mutex_unlock(&lock);
202 PRINT_TEST_CASE_MSG("Opening TCP alike channel ex\n");
203 /* Passing all valid arguments for meshlink_channel_open_ex */
204 meshlink_channel_t *channel;
205 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
206 assert_int_not_equal(channel, NULL);
208 // Delay for establishing a channel
210 pthread_mutex_lock(&lock);
211 bool ret = channel_acc;
212 pthread_mutex_unlock(&lock);
214 assert_int_equal(ret, true);
216 meshlink_close(mesh_handle);
217 meshlink_destroy("channelexconf");
221 /* Execute meshlink_channel_open_ex Test Case # 3 - Open a UDP channel */
222 static void test_case_channel_ex_03(void **state) {
223 execute_test(test_steps_channel_ex_03, state);
225 /* Test Steps for meshlink_channel_open_ex Test Case # 3 - Valid case (UDP channel)
228 1. Run NUT(Node Under Test)
229 2. Open channel to ourself
232 Opens a UDP channel successfully by setting channel_acc true */
233 static bool test_steps_channel_ex_03(void) {
234 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
236 /* Create meshlink instance */
237 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
239 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
240 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
241 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
243 assert(meshlink_start(mesh_handle));
245 /* Getting node handle for itself */
246 meshlink_node_t *node = meshlink_get_self(mesh_handle);
247 assert(node != NULL);
249 pthread_mutex_lock(&lock);
251 pthread_mutex_unlock(&lock);
254 /* Passing all valid arguments for meshlink_channel_open_ex */
255 meshlink_channel_t *channel;
256 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_UDP);
257 assert_int_not_equal(channel, NULL);
259 // Delay for establishing a channel
262 pthread_mutex_lock(&lock);
263 bool ret = channel_acc;
264 pthread_mutex_unlock(&lock);
266 assert_int_equal(ret, true);
268 meshlink_close(mesh_handle);
269 meshlink_destroy("channelexconf");
273 /* Execute meshlink_channel_open_ex Test Case # 4 - Open a TCP channel with no receive callback
275 static void test_case_channel_ex_04(void **state) {
276 execute_test(test_steps_channel_ex_04, state);
278 /* Test Steps for meshlink_channel_open_ex Test Case # 4 - Valid Case (Disabling receive callback)
281 1. Run NUT(Node Under Test)
282 2. Open channel to ourself
288 static bool test_steps_channel_ex_04(void) {
289 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
291 /* Create meshlink instance */
292 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
294 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
295 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
296 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
298 assert(meshlink_start(mesh_handle));
300 /* Getting node handle for itself */
301 meshlink_node_t *node = meshlink_get_self(mesh_handle);
302 assert(node != NULL);
304 pthread_mutex_lock(&lock);
306 pthread_mutex_unlock(&lock);
308 /* Passing all valid arguments for meshlink_channel_open_ex i.e disabling receive callback and send queue */
309 meshlink_channel_t *channel;
310 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, NULL, NULL, 0, MESHLINK_CHANNEL_UDP);
311 assert(channel != NULL);
312 // Delay for establishing a channel
314 pthread_mutex_lock(&lock);
315 bool ret = channel_acc;
316 pthread_mutex_unlock(&lock);
318 assert_int_equal(ret, true);
320 meshlink_close(mesh_handle);
321 meshlink_destroy("channelexconf");
325 /* Execute meshlink_channel_open_ex Test Case # 5 - Opening channel using NULL as mesh handle argument
327 static void test_case_channel_ex_05(void **state) {
328 execute_test(test_steps_channel_ex_05, state);
330 /* Test Steps for meshlink_channel_open_ex Test Case # 5 - Invalid case (NULL as mesh argument)
333 1. Run NUT(Node Under Test)
334 2. Open channel by passing NULL as argument for mesh handle
337 meshlink_channel_open_ex returns NULL as channel handle reporting error accordingly
339 static bool test_steps_channel_ex_05(void) {
340 /* Set up logging for Meshlink */
341 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
343 /* Create meshlink instance */
344 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
347 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
348 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
349 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
351 assert(meshlink_start(mesh_handle));
352 /* Getting node handle for itself */
353 meshlink_node_t *node = meshlink_get_self(mesh_handle);
354 assert(node != NULL);
356 /* Trying to open channel using mesh handle as NULL argument */
357 meshlink_channel_t *channel = meshlink_channel_open_ex(NULL, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
358 assert(channel == NULL);
360 meshlink_close(mesh_handle);
361 meshlink_destroy("channelexconf");
365 /* Execute meshlink_channel_open_ex Test Case # 6 - Opening channel using NULL as node handle argument
367 static void test_case_channel_ex_06(void **state) {
368 execute_test(test_steps_channel_ex_06, state);
371 /* Test Steps for meshlink_channel_open_ex Test Case # 6 - Invalid case (NULL as node argument)
374 1. Run NUT(Node Under Test)
375 2. Open channel by passing NULL as argument for node handle
378 meshlink_channel_open_ex returns NULL as channel handle reporting error accordingly
380 static bool test_steps_channel_ex_06(void) {
381 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
383 /* Create meshlink instance */
384 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
387 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
388 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
389 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
391 assert(meshlink_start(mesh_handle));
393 /* Trying to open channel using node handle as NULL argument */
394 meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_handle, NULL, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
396 assert_int_equal(channel, NULL);
398 meshlink_close(mesh_handle);
399 meshlink_destroy("channelexconf");
403 int test_meshlink_channel_open_ex(void) {
404 const struct CMUnitTest blackbox_channel_ex_tests[] = {
405 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_01, NULL, NULL,
406 (void *)&test_case_channel_ex_01_state),
407 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_02, NULL, NULL,
408 (void *)&test_case_channel_ex_02_state),
409 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_03, NULL, NULL,
410 (void *)&test_case_channel_ex_03_state),
411 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_04, NULL, NULL,
412 (void *)&test_case_channel_ex_04_state),
413 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_05, NULL, NULL,
414 (void *)&test_case_channel_ex_05_state),
415 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_06, NULL, NULL,
416 (void *)&test_case_channel_ex_06_state)
419 total_tests += sizeof(blackbox_channel_ex_tests) / sizeof(blackbox_channel_ex_tests[0]);
421 assert(pthread_mutex_init(&lock, NULL) == 0);
422 int failed = cmocka_run_group_tests(blackbox_channel_ex_tests, NULL, NULL);
423 assert(pthread_mutex_destroy(&lock) == 0);