2 test_cases_status_cb.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_status_cb.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
33 /* Modify this to change the logging level of Meshlink */
34 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
36 static void test_case_set_status_cb_01(void **state);
37 static bool test_set_status_cb_01(void);
38 static void test_case_set_status_cb_02(void **state);
39 static bool test_set_status_cb_02(void);
41 /* status variable gives access to the status callback to know whether invoked or not */
44 /* State structure for status callback Test Case #1 */
45 static black_box_state_t test_case_set_status_cb_01_state = {
46 .test_case_name = "test_case_set_status_cb_01",
49 /* State structure for status callback Test Case #2 */
50 static black_box_state_t test_case_set_status_cb_02_state = {
51 .test_case_name = "test_case_set_status_cb_02",
55 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *source, bool reach) {
56 fprintf(stderr, "In status callback\n");
59 fprintf(stderr, "[ %s ] node reachable\n", source->name);
61 fprintf(stderr, "[ %s ] node not reachable\n", source->name) ;
67 /* Execute status callback Test Case # 1 - valid case */
68 static void test_case_set_status_cb_01(void **state) {
69 execute_test(test_set_status_cb_01, state);
72 /* Test Steps for meshlink_set_status_cb Test Case # 1
75 1. Run bar and nut node instances
76 2. Set status callback for the NUT and Start NUT
79 status callback should be invoked when NUT connects/disconnects with 'relay' node.
81 static bool test_set_status_cb_01(void) {
82 meshlink_destroy("set_status_cb_conf.1");
83 meshlink_destroy("set_status_cb_conf.2");
85 // Opening NUT and bar nodes
86 meshlink_handle_t *mesh1 = meshlink_open("set_status_cb_conf.1", "nut", "test", DEV_CLASS_STATIONARY);
87 assert(mesh1 != NULL);
88 meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
89 meshlink_handle_t *mesh2 = meshlink_open("set_status_cb_conf.2", "bar", "test", DEV_CLASS_STATIONARY);
90 assert(mesh2 != NULL);
91 meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
93 // Set up callback for node status
94 meshlink_set_node_status_cb(mesh1, status_cb);
96 // Exporting and Importing mutually
97 char *exp1 = meshlink_export(mesh1);
99 char *exp2 = meshlink_export(mesh2);
100 assert(exp2 != NULL);
101 bool imp1 = meshlink_import(mesh1, exp2);
102 bool imp2 = meshlink_import(mesh2, exp1);
104 assert(meshlink_start(mesh1));
105 assert(meshlink_start(mesh2));
108 // Test for status from status callback
109 assert_int_equal(status, true);
111 meshlink_close(mesh2);
114 // Test for status from status callback
115 assert_int_equal(status, false);
119 meshlink_close(mesh1);
120 meshlink_destroy("set_status_cb_conf.1");
121 meshlink_destroy("set_status_cb_conf.2");
126 /* Execute status callback Test Case # 2 - Invalid case */
127 static void test_case_set_status_cb_02(void **state) {
128 execute_test(test_set_status_cb_02, state);
131 /* Test Steps for meshlink_set_status_cb Test Case # 2
134 1. Calling meshlink_set_status_cb with NULL as mesh handle argument.
137 set poll callback handles the invalid parameter when called by giving proper error number.
139 static bool test_set_status_cb_02(void) {
141 // Create meshlink instance
143 meshlink_destroy("set_status_cb_conf.3");
144 meshlink_handle_t *mesh_handle = meshlink_open("set_status_cb_conf.3", "nut", "node_sim", 1);
147 // Pass NULL as meshlink_set_node_status_cb's argument
149 meshlink_set_node_status_cb(NULL, status_cb);
150 meshlink_errno_t meshlink_errno_buff = meshlink_errno;
151 assert_int_equal(meshlink_errno_buff, MESHLINK_EINVAL);
155 meshlink_close(mesh_handle);
156 meshlink_destroy("set_status_cb_conf.3");
161 int test_meshlink_set_status_cb(void) {
162 const struct CMUnitTest blackbox_status_tests[] = {
163 cmocka_unit_test_prestate_setup_teardown(test_case_set_status_cb_01, NULL, NULL,
164 (void *)&test_case_set_status_cb_01_state),
165 cmocka_unit_test_prestate_setup_teardown(test_case_set_status_cb_02, NULL, NULL,
166 (void *)&test_case_set_status_cb_02_state)
168 total_tests += sizeof(blackbox_status_tests) / sizeof(blackbox_status_tests[0]);
170 int failed = cmocka_run_group_tests(blackbox_status_tests, NULL, NULL);