]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_status_cb.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_status_cb.c
1 /*
2     test_cases_status_cb.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #ifdef NDEBUG
21 #undef NDEBUG
22 #endif
23
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"
29 #include <assert.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <setjmp.h>
34 #include <cmocka.h>
35 #include <pthread.h>
36
37 /* Modify this to change the logging level of Meshlink */
38 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
39
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);
44
45 /* status variable gives access to the status callback to know whether invoked or not */
46 static bool status;
47
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",
51 };
52
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",
56 };
57
58
59 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *source, bool reach) {
60         (void)mesh;
61
62         fprintf(stderr, "In status callback\n");
63
64         if(reach) {
65                 fprintf(stderr, "[ %s ] node reachable\n", source->name);
66         } else {
67                 fprintf(stderr, "[ %s ] node not reachable\n", source->name) ;
68         }
69
70         status = reach;
71 }
72
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);
76 }
77
78 /* Test Steps for meshlink_set_status_cb Test Case # 1
79
80     Test Steps:
81     1. Run bar and nut node instances
82     2. Set status callback for the NUT and Start NUT
83
84     Expected Result:
85     status callback should be invoked when NUT connects/disconnects with 'relay' node.
86 */
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"));
90
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);
98
99         // Set up callback for node status
100         meshlink_set_node_status_cb(mesh1, status_cb);
101
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));
109
110         assert(meshlink_start(mesh1));
111         assert(meshlink_start(mesh2));
112         sleep(1);
113
114         // Test for status from status callback
115         assert_int_equal(status, true);
116
117         meshlink_close(mesh2);
118         sleep(1);
119
120         // Test for status from status callback
121         assert_int_equal(status, false);
122
123         free(exp1);
124         free(exp2);
125         meshlink_close(mesh1);
126         assert(meshlink_destroy("set_status_cb_conf.1"));
127         assert(meshlink_destroy("set_status_cb_conf.2"));
128
129         return true;
130 }
131
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);
135 }
136
137 /* Test Steps for meshlink_set_status_cb Test Case # 2
138
139     Test Steps:
140     1. Calling meshlink_set_status_cb with NULL as mesh handle argument.
141
142     Expected Result:
143     set poll callback handles the invalid parameter when called by giving proper error number.
144 */
145 static bool test_set_status_cb_02(void) {
146
147         // Create meshlink instance
148
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);
151         assert(mesh_handle);
152
153         // Pass NULL as meshlink_set_node_status_cb's argument
154
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);
158
159         // Clean up
160
161         meshlink_close(mesh_handle);
162         assert(meshlink_destroy("set_status_cb_conf.3"));
163         return true;
164 }
165
166
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)
173         };
174         total_tests += sizeof(blackbox_status_tests) / sizeof(blackbox_status_tests[0]);
175
176         int failed = cmocka_run_group_tests(blackbox_status_tests, NULL, NULL);
177
178         return failed;
179 }