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 /* mutex for the common variable */
77 static pthread_mutex_t accept_lock = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_cond_t accept_cond = PTHREAD_COND_INITIALIZER;
80 static bool channel_acc;
82 /* channel receive callback */
83 static void cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
92 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
98 assert_int_equal(port, PORT);
100 pthread_mutex_lock(&accept_lock);
102 assert(!pthread_cond_broadcast(&accept_cond));
103 pthread_mutex_unlock(&accept_lock);
108 /* Execute meshlink_channel_open_ex Test Case # 1 - testing meshlink_channel_open_ex API's
109 valid case by passing all valid arguments */
110 static void test_case_channel_ex_01(void **state) {
111 execute_test(test_steps_channel_ex_01, state);
114 /* Test Steps for meshlink_channel_open_ex Test Case # 1 - Valid case
117 1. Run NUT(Node Under Test)
118 2. Open channel to ourself
121 Opens a channel and echoes the send queue data.
123 /* TODO: When send queue & send queue length are passed with some value other
124 than NULL it throws segmentation fault*/
125 static bool test_steps_channel_ex_01(void) {
126 /* Set up logging for Meshlink */
127 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
129 /* Create meshlink instance */
130 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
132 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
133 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
134 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
136 assert(meshlink_start(mesh_handle));
138 /* Getting node handle for itself */
139 meshlink_node_t *node = meshlink_get_self(mesh_handle);
140 assert(node != NULL);
142 char string[100] = "Test the 1st case";
143 pthread_mutex_lock(&lock);
145 pthread_mutex_unlock(&lock);
147 /* Passing all valid arguments for meshlink_channel_open_ex */
148 meshlink_channel_t *channel = NULL;
149 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, string, strlen(string) + 1, MESHLINK_CHANNEL_UDP);
150 assert_int_not_equal(channel, NULL);
152 // Delay for establishing a channel
155 pthread_mutex_lock(&lock);
156 bool ret = channel_acc;
157 pthread_mutex_unlock(&lock);
159 assert_int_equal(ret, true);
161 meshlink_close(mesh_handle);
162 meshlink_destroy("channelexconf");
167 /* Execute meshlink_channel_open_ex Test Case # 2 - testing API's valid case by passing NULL and
168 0 for send queue & it's length respectively and others with valid arguments */
169 static void test_case_channel_ex_02(void **state) {
170 execute_test(test_steps_channel_ex_02, state);
172 /* Test Steps for meshlink_channel_open_ex Test Case # 2 - Valid case (TCP channel)
175 1. Run NUT(Node Under Test)
176 2. Open channel to ourself
179 Opens a TCP channel successfully by setting channel_acc true*/
180 static bool test_steps_channel_ex_02(void) {
181 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
183 /* Create meshlink instance */
184 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
186 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
187 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
188 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
190 assert(meshlink_start(mesh_handle));
192 meshlink_node_t *node = meshlink_get_self(mesh_handle);
193 assert(node != NULL);
195 pthread_mutex_lock(&lock);
197 pthread_mutex_unlock(&lock);
200 PRINT_TEST_CASE_MSG("Opening TCP alike channel ex\n");
201 /* Passing all valid arguments for meshlink_channel_open_ex */
202 meshlink_channel_t *channel;
203 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
204 assert_int_not_equal(channel, NULL);
206 // Delay for establishing a channel
208 pthread_mutex_lock(&lock);
209 bool ret = channel_acc;
210 pthread_mutex_unlock(&lock);
212 assert_int_equal(ret, true);
214 meshlink_close(mesh_handle);
215 meshlink_destroy("channelexconf");
219 /* Execute meshlink_channel_open_ex Test Case # 3 - Open a UDP channel */
220 static void test_case_channel_ex_03(void **state) {
221 execute_test(test_steps_channel_ex_03, state);
223 /* Test Steps for meshlink_channel_open_ex Test Case # 3 - Valid case (UDP channel)
226 1. Run NUT(Node Under Test)
227 2. Open channel to ourself
230 Opens a UDP channel successfully by setting channel_acc true */
231 static bool test_steps_channel_ex_03(void) {
232 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
234 /* Create meshlink instance */
235 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
237 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
238 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
239 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
241 assert(meshlink_start(mesh_handle));
243 /* Getting node handle for itself */
244 meshlink_node_t *node = meshlink_get_self(mesh_handle);
245 assert(node != NULL);
247 pthread_mutex_lock(&lock);
249 pthread_mutex_unlock(&lock);
252 /* Passing all valid arguments for meshlink_channel_open_ex */
253 meshlink_channel_t *channel;
254 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_UDP);
255 assert_int_not_equal(channel, NULL);
257 // Delay for establishing a channel
260 pthread_mutex_lock(&lock);
261 bool ret = channel_acc;
262 pthread_mutex_unlock(&lock);
264 assert_int_equal(ret, true);
266 meshlink_close(mesh_handle);
267 meshlink_destroy("channelexconf");
271 /* Execute meshlink_channel_open_ex Test Case # 4 - Open a TCP channel with no receive callback
273 static void test_case_channel_ex_04(void **state) {
274 execute_test(test_steps_channel_ex_04, state);
276 /* Test Steps for meshlink_channel_open_ex Test Case # 4 - Valid Case (Disabling receive callback)
279 1. Run NUT(Node Under Test)
280 2. Open channel to ourself
286 static bool test_steps_channel_ex_04(void) {
287 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
289 /* Create meshlink instance */
290 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
292 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
293 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
294 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
296 assert(meshlink_start(mesh_handle));
298 /* Getting node handle for itself */
299 meshlink_node_t *node = meshlink_get_self(mesh_handle);
300 assert(node != NULL);
302 pthread_mutex_lock(&lock);
304 pthread_mutex_unlock(&lock);
306 /* Passing all valid arguments for meshlink_channel_open_ex i.e disabling receive callback and send queue */
307 meshlink_channel_t *channel;
308 channel = meshlink_channel_open_ex(mesh_handle, node, PORT, NULL, NULL, 0, MESHLINK_CHANNEL_UDP);
309 assert(channel != NULL);
310 // Delay for establishing a channel
312 pthread_mutex_lock(&lock);
313 bool ret = channel_acc;
314 pthread_mutex_unlock(&lock);
316 assert_int_equal(ret, true);
318 meshlink_close(mesh_handle);
319 meshlink_destroy("channelexconf");
323 /* Execute meshlink_channel_open_ex Test Case # 5 - Opening channel using NULL as mesh handle argument
325 static void test_case_channel_ex_05(void **state) {
326 execute_test(test_steps_channel_ex_05, state);
328 /* Test Steps for meshlink_channel_open_ex Test Case # 5 - Invalid case (NULL as mesh argument)
331 1. Run NUT(Node Under Test)
332 2. Open channel by passing NULL as argument for mesh handle
335 meshlink_channel_open_ex returns NULL as channel handle reporting error accordingly
337 static bool test_steps_channel_ex_05(void) {
338 /* Set up logging for Meshlink */
339 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
341 /* Create meshlink instance */
342 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
345 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
346 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
347 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
349 assert(meshlink_start(mesh_handle));
350 /* Getting node handle for itself */
351 meshlink_node_t *node = meshlink_get_self(mesh_handle);
352 assert(node != NULL);
354 /* Trying to open channel using mesh handle as NULL argument */
355 meshlink_channel_t *channel = meshlink_channel_open_ex(NULL, node, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
356 assert(channel == NULL);
358 meshlink_close(mesh_handle);
359 meshlink_destroy("channelexconf");
363 /* Execute meshlink_channel_open_ex Test Case # 6 - Opening channel using NULL as node handle argument
365 static void test_case_channel_ex_06(void **state) {
366 execute_test(test_steps_channel_ex_06, state);
369 /* Test Steps for meshlink_channel_open_ex Test Case # 6 - Invalid case (NULL as node argument)
372 1. Run NUT(Node Under Test)
373 2. Open channel by passing NULL as argument for node handle
376 meshlink_channel_open_ex returns NULL as channel handle reporting error accordingly
378 static bool test_steps_channel_ex_06(void) {
379 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
381 /* Create meshlink instance */
382 meshlink_handle_t *mesh_handle = meshlink_open("channelexconf", "nut", "node_sim", 1);
385 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
386 meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
387 meshlink_set_channel_accept_cb(mesh_handle, channel_accept);
389 assert(meshlink_start(mesh_handle));
391 /* Trying to open channel using node handle as NULL argument */
392 meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_handle, NULL, PORT, cb, NULL, 0, MESHLINK_CHANNEL_TCP);
394 assert_int_equal(channel, NULL);
396 meshlink_close(mesh_handle);
397 meshlink_destroy("channelexconf");
401 int test_meshlink_channel_open_ex(void) {
402 const struct CMUnitTest blackbox_channel_ex_tests[] = {
403 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_01, NULL, NULL,
404 (void *)&test_case_channel_ex_01_state),
405 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_02, NULL, NULL,
406 (void *)&test_case_channel_ex_02_state),
407 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_03, NULL, NULL,
408 (void *)&test_case_channel_ex_03_state),
409 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_04, NULL, NULL,
410 (void *)&test_case_channel_ex_04_state),
411 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_05, NULL, NULL,
412 (void *)&test_case_channel_ex_05_state),
413 cmocka_unit_test_prestate_setup_teardown(test_case_channel_ex_06, NULL, NULL,
414 (void *)&test_case_channel_ex_06_state)
417 total_tests += sizeof(blackbox_channel_ex_tests) / sizeof(blackbox_channel_ex_tests[0]);
419 assert(pthread_mutex_init(&lock, NULL) == 0);
420 int failed = cmocka_run_group_tests(blackbox_channel_ex_tests, NULL, NULL);
421 assert(pthread_mutex_destroy(&lock) == 0);