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.
24 #include "execute_tests.h"
25 #include "test_cases_status_cb.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
40 static void test_case_set_status_cb_01(void **state);
41 static bool test_set_status_cb_01(void);
42 static void test_case_set_status_cb_02(void **state);
43 static bool test_set_status_cb_02(void);
45 /* status variable gives access to the status callback to know whether invoked or not */
48 /* State structure for status callback Test Case #1 */
49 static black_box_state_t test_case_set_status_cb_01_state = {
50 .test_case_name = "test_case_set_status_cb_01",
53 /* State structure for status callback Test Case #2 */
54 static black_box_state_t test_case_set_status_cb_02_state = {
55 .test_case_name = "test_case_set_status_cb_02",
59 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *source, bool reach) {
62 fprintf(stderr, "In status callback\n");
65 fprintf(stderr, "[ %s ] node reachable\n", source->name);
67 fprintf(stderr, "[ %s ] node not reachable\n", source->name) ;
73 /* Execute status callback Test Case # 1 - valid case */
74 static void test_case_set_status_cb_01(void **state) {
75 execute_test(test_set_status_cb_01, state);
78 /* Test Steps for meshlink_set_status_cb Test Case # 1
81 1. Run bar and nut node instances
82 2. Set status callback for the NUT and Start NUT
85 status callback should be invoked when NUT connects/disconnects with 'relay' node.
87 static bool test_set_status_cb_01(void) {
88 assert(meshlink_destroy("set_status_cb_conf.1"));
89 assert(meshlink_destroy("set_status_cb_conf.2"));
91 // Opening NUT and bar nodes
92 meshlink_handle_t *mesh1 = meshlink_open("set_status_cb_conf.1", "nut", "test", DEV_CLASS_STATIONARY);
93 assert(mesh1 != NULL);
94 meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
95 meshlink_handle_t *mesh2 = meshlink_open("set_status_cb_conf.2", "bar", "test", DEV_CLASS_STATIONARY);
96 assert(mesh2 != NULL);
97 meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
99 // Set up callback for node status
100 meshlink_set_node_status_cb(mesh1, status_cb);
102 // Exporting and Importing mutually
103 char *exp1 = meshlink_export(mesh1);
104 assert(exp1 != NULL);
105 char *exp2 = meshlink_export(mesh2);
106 assert(exp2 != NULL);
107 assert(meshlink_import(mesh1, exp2));
108 assert(meshlink_import(mesh2, exp1));
110 assert(meshlink_start(mesh1));
111 assert(meshlink_start(mesh2));
114 // Test for status from status callback
115 assert_int_equal(status, true);
117 meshlink_close(mesh2);
120 // Test for status from status callback
121 assert_int_equal(status, false);
125 meshlink_close(mesh1);
126 assert(meshlink_destroy("set_status_cb_conf.1"));
127 assert(meshlink_destroy("set_status_cb_conf.2"));
132 /* Execute status callback Test Case # 2 - Invalid case */
133 static void test_case_set_status_cb_02(void **state) {
134 execute_test(test_set_status_cb_02, state);
137 /* Test Steps for meshlink_set_status_cb Test Case # 2
140 1. Calling meshlink_set_status_cb with NULL as mesh handle argument.
143 set poll callback handles the invalid parameter when called by giving proper error number.
145 static bool test_set_status_cb_02(void) {
147 // Create meshlink instance
149 assert(meshlink_destroy("set_status_cb_conf.3"));
150 meshlink_handle_t *mesh_handle = meshlink_open("set_status_cb_conf.3", "nut", "node_sim", 1);
153 // Pass NULL as meshlink_set_node_status_cb's argument
155 meshlink_set_node_status_cb(NULL, status_cb);
156 meshlink_errno_t meshlink_errno_buff = meshlink_errno;
157 assert_int_equal(meshlink_errno_buff, MESHLINK_EINVAL);
161 meshlink_close(mesh_handle);
162 assert(meshlink_destroy("set_status_cb_conf.3"));
167 int test_meshlink_set_status_cb(void) {
168 const struct CMUnitTest blackbox_status_tests[] = {
169 cmocka_unit_test_prestate_setup_teardown(test_case_set_status_cb_01, NULL, NULL,
170 (void *)&test_case_set_status_cb_01_state),
171 cmocka_unit_test_prestate_setup_teardown(test_case_set_status_cb_02, NULL, NULL,
172 (void *)&test_case_set_status_cb_02_state)
174 total_tests += sizeof(blackbox_status_tests) / sizeof(blackbox_status_tests[0]);
176 int failed = cmocka_run_group_tests(blackbox_status_tests, NULL, NULL);