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.
24 #include "execute_tests.h"
25 #include "test_cases_channel_ex.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
37 /* Modify this to change the logging level of Meshlink */
38 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
39 /* Modify this to change the port number */
42 static void test_case_channel_ex_01(void **state);
43 static bool test_steps_channel_ex_01(void);
44 static void test_case_channel_ex_02(void **state);
45 static bool test_steps_channel_ex_02(void);
46 static void test_case_channel_ex_03(void **state);
47 static bool test_steps_channel_ex_03(void);
48 static void test_case_channel_ex_04(void **state);
49 static bool test_steps_channel_ex_04(void);
50 static void test_case_channel_ex_05(void **state);
51 static bool test_steps_channel_ex_05(void);
52 static void test_case_channel_ex_06(void **state);
53 static bool test_steps_channel_ex_06(void);
55 static void cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
56 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
58 /* channel_acc gives us access to test whether the accept callback has been invoked or not */
59 static bool channel_acc;
60 /* mutex for the common variable */
63 static black_box_state_t test_case_channel_ex_01_state = {
64 .test_case_name = "test_case_channel_ex_01",
66 static black_box_state_t test_case_channel_ex_02_state = {
67 .test_case_name = "test_case_channel_ex_02",
69 static black_box_state_t test_case_channel_ex_03_state = {
70 .test_case_name = "test_case_channel_ex_03",
72 static black_box_state_t test_case_channel_ex_04_state = {
73 .test_case_name = "test_case_channel_ex_04",
75 static black_box_state_t test_case_channel_ex_05_state = {
76 .test_case_name = "test_case_channel_ex_05",
78 static black_box_state_t test_case_channel_ex_06_state = {
79 .test_case_name = "test_case_channel_ex_06",
81 /* mutex for the common variable */
82 static pthread_mutex_t accept_lock = PTHREAD_MUTEX_INITIALIZER;
83 static pthread_cond_t accept_cond = PTHREAD_COND_INITIALIZER;
85 static bool channel_acc;
87 /* channel receive callback */
88 static void cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
97 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
103 assert_int_equal(port, PORT);
105 pthread_mutex_lock(&accept_lock);
107 assert(!pthread_cond_broadcast(&accept_cond));
108 pthread_mutex_unlock(&accept_lock);
113 /* Execute meshlink_channel_open_ex Test Case # 1 - testing meshlink_channel_open_ex API's
114 valid case by passing all valid arguments */
115 static void test_case_channel_ex_01(void **state) {
116 execute_test(test_steps_channel_ex_01, state);
119 /* Test Steps for meshlink_channel_open_ex Test Case # 1 - Valid case
122 1. Run NUT(Node Under Test)
123 2. Open channel to ourself
126 Opens a channel and echoes the send queue data.
128 /* TODO: When send queue & send queue length are passed with some value other
129 than NULL it throws segmentation fault*/
130 static bool test_steps_channel_ex_01(void) {
131 /* Set up logging for Meshlink */
132 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
134 /* Create meshlink instance */
135 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
137 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
138 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
139 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
141 assert(meshlink_start(mesh_handle));
143 /* Getting node handle for itself */
144 meshlink_node_t *node = meshlink_get_self(mesh_handle);
145 assert(node != NULL);
147 char string[100] = "Test the 1st case";
148 pthread_mutex_lock(&lock);
150 pthread_mutex_unlock(&lock);
152 /* Passing all valid arguments for meshlink_channel_open_ex */
153 meshlink_channel_t *channel = NULL;
154 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, string, strlen(string) + 1, MESHLINK_CHANNEL_UDP);
155 assert_int_not_equal(channel, NULL);
157 // Delay for establishing a channel
160 pthread_mutex_lock(&lock);
161 bool ret = channel_acc;
162 pthread_mutex_unlock(&lock);
164 assert_int_equal(ret, true);
166 meshlink_close(mesh_handle);
167 assert(meshlink_destroy("channelexconf"));
172 /* Execute meshlink_channel_open_ex Test Case # 2 - testing API's valid case by passing NULL and
173 0 for send queue & it's length respectively and others with valid arguments */
174 static void test_case_channel_ex_02(void **state) {
175 execute_test(test_steps_channel_ex_02, state);
177 /* Test Steps for meshlink_channel_open_ex Test Case # 2 - Valid case (TCP channel)
180 1. Run NUT(Node Under Test)
181 2. Open channel to ourself
184 Opens a TCP channel successfully by setting channel_acc true*/
185 static bool test_steps_channel_ex_02(void) {
186 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
188 /* Create meshlink instance */
189 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
191 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
192 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
193 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
195 assert(meshlink_start(mesh_handle));
197 meshlink_node_t *node = meshlink_get_self(mesh_handle);
198 assert(node != NULL);
200 pthread_mutex_lock(&lock);
202 pthread_mutex_unlock(&lock);
205 PRINT_TEST_CASE_MSG("Opening TCP alike channel ex\n");
206 /* Passing all valid arguments for meshlink_channel_open_ex */
207 meshlink_channel_t *channel;
208 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
209 assert_int_not_equal(channel, NULL);
211 // Delay for establishing a channel
213 pthread_mutex_lock(&lock);
214 bool ret = channel_acc;
215 pthread_mutex_unlock(&lock);
217 assert_int_equal(ret, true);
219 meshlink_close(mesh_handle);
220 assert(meshlink_destroy("channelexconf"));
224 /* Execute meshlink_channel_open_ex Test Case # 3 - Open a UDP channel */
225 static void test_case_channel_ex_03(void **state) {
226 execute_test(test_steps_channel_ex_03, state);
228 /* Test Steps for meshlink_channel_open_ex Test Case # 3 - Valid case (UDP channel)
231 1. Run NUT(Node Under Test)
232 2. Open channel to ourself
235 Opens a UDP channel successfully by setting channel_acc true */
236 static bool test_steps_channel_ex_03(void) {
237 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
239 /* Create meshlink instance */
240 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
242 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
243 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
244 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
246 assert(meshlink_start(mesh_handle));
248 /* Getting node handle for itself */
249 meshlink_node_t *node = meshlink_get_self(mesh_handle);
250 assert(node != NULL);
252 pthread_mutex_lock(&lock);
254 pthread_mutex_unlock(&lock);
257 /* Passing all valid arguments for meshlink_channel_open_ex */
258 meshlink_channel_t *channel;
259 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_UDP);
260 assert_int_not_equal(channel, NULL);
262 // Delay for establishing a channel
265 pthread_mutex_lock(&lock);
266 bool ret = channel_acc;
267 pthread_mutex_unlock(&lock);
269 assert_int_equal(ret, true);
271 meshlink_close(mesh_handle);
272 assert(meshlink_destroy("channelexconf"));
276 /* Execute meshlink_channel_open_ex Test Case # 4 - Open a TCP channel with no receive callback
278 static void test_case_channel_ex_04(void **state) {
279 execute_test(test_steps_channel_ex_04, state);
281 /* Test Steps for meshlink_channel_open_ex Test Case # 4 - Valid Case (Disabling receive callback)
284 1. Run NUT(Node Under Test)
285 2. Open channel to ourself
291 static bool test_steps_channel_ex_04(void) {
292 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
294 /* Create meshlink instance */
295 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
297 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
298 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
299 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
301 assert(meshlink_start(mesh_handle));
303 /* Getting node handle for itself */
304 meshlink_node_t *node = meshlink_get_self(mesh_handle);
305 assert(node != NULL);
307 pthread_mutex_lock(&lock);
309 pthread_mutex_unlock(&lock);
311 /* Passing all valid arguments for meshlink_channel_open_ex i.e disabling receive callback and send queue */
312 meshlink_channel_t *channel;
313 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, NULL, NULL, 0, MESHLINK_CHANNEL_UDP);
314 assert(channel != NULL);
315 // Delay for establishing a channel
317 pthread_mutex_lock(&lock);
318 bool ret = channel_acc;
319 pthread_mutex_unlock(&lock);
321 assert_int_equal(ret, true);
323 meshlink_close(mesh_handle);
324 assert(meshlink_destroy("channelexconf"));
328 /* Execute meshlink_channel_open_ex Test Case # 5 - Opening channel using NULL as mesh handle argument
330 static void test_case_channel_ex_05(void **state) {
331 execute_test(test_steps_channel_ex_05, state);
333 /* Test Steps for meshlink_channel_open_ex Test Case # 5 - Invalid case (NULL as mesh argument)
336 1. Run NUT(Node Under Test)
337 2. Open channel by passing NULL as argument for mesh handle
340 meshlink_channel_open_ex returns NULL as channel handle reporting error accordingly
342 static bool test_steps_channel_ex_05(void) {
343 /* Set up logging for Meshlink */
344 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
346 /* Create meshlink instance */
347 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
350 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
351 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
352 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
354 assert(meshlink_start(mesh_handle));
355 /* Getting node handle for itself */
356 meshlink_node_t *node = meshlink_get_self(mesh_handle);
357 assert(node != NULL);
359 /* Trying to open channel using mesh handle as NULL argument */
360 meshlink_channel_t *channel = meshlink_channel_open_ex(NULL, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
361 assert(channel == NULL);
363 meshlink_close(mesh_handle);
364 assert(meshlink_destroy("channelexconf"));
368 /* Execute meshlink_channel_open_ex Test Case # 6 - Opening channel using NULL as node handle argument
370 static void test_case_channel_ex_06(void **state) {
371 execute_test(test_steps_channel_ex_06, state);
374 /* Test Steps for meshlink_channel_open_ex Test Case # 6 - Invalid case (NULL as node argument)
377 1. Run NUT(Node Under Test)
378 2. Open channel by passing NULL as argument for node handle
381 meshlink_channel_open_ex returns NULL as channel handle reporting error accordingly
383 static bool test_steps_channel_ex_06(void) {
384 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
386 /* Create meshlink instance */
387 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
390 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
391 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
392 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
394 assert(meshlink_start(mesh_handle));
396 /* Trying to open channel using node handle as NULL argument */
397 meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_handle, NULL, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
399 assert_int_equal(channel, NULL);
401 meshlink_close(mesh_handle);
402 assert(meshlink_destroy("channelexconf"));
406 int test_meshlink_channel_open_ex(void) {
407 const struct CMUnitTest blackbox_channel_ex_tests[] = {
408 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_01, NULL, NULL,
409 (void *)&test_case_channel_ex_01_state),
410 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_02, NULL, NULL,
411 (void *)&test_case_channel_ex_02_state),
412 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_03, NULL, NULL,
413 (void *)&test_case_channel_ex_03_state),
414 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_04, NULL, NULL,
415 (void *)&test_case_channel_ex_04_state),
416 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_05, NULL, NULL,
417 (void *)&test_case_channel_ex_05_state),
418 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_06, NULL, NULL,
419 (void *)&test_case_channel_ex_06_state)
422 total_tests += sizeof(blackbox_channel_ex_tests) / sizeof(blackbox_channel_ex_tests[0]);
424 assert(pthread_mutex_init(&lock, NULL) == 0);
425 int failed = cmocka_run_group_tests(blackbox_channel_ex_tests, NULL, NULL);
426 assert(pthread_mutex_destroy(&lock) == 0);