]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_get_all_nodes_by_dev_class.c
Add a test case for meshlink_get_all_nodes_by_dev_class().
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_get_all_nodes_by_dev_class.c
1 /*
2     test_cases_get_all_nodes_by_dev_class.c.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2019  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 #include "execute_tests.h"
21 #include "test_cases_get_all_nodes_by_dev_class.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <setjmp.h>
28 #include <cmocka.h>
29 #include <assert.h>
30 #include <string.h>
31
32 /* Modify this to change the logging level of Meshlink */
33 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
34
35 static void test_case_mesh_get_node_by_dev_class_01(void **state);
36 static bool test_steps_mesh_get_node_by_dev_class_01(void);
37 static void test_case_mesh_get_node_by_dev_class_02(void **state);
38 static bool test_steps_mesh_get_node_by_dev_class_02(void);
39 static void test_case_mesh_get_node_dev_class_01(void **state);
40 static bool test_steps_mesh_get_node_dev_class_01(void);
41 static void test_case_mesh_get_node_dev_class_02(void **state);
42 static bool test_steps_mesh_get_node_dev_class_02(void);
43
44 /* State structure for meshlink_get_node Test Case #1 */
45 static black_box_state_t test_mesh_get_node_by_dev_class_01_state = {
46         .test_case_name = "test_case_mesh_get_node_by_dev_class_01",
47 };
48
49 /* State structure for meshlink_get_node Test Case #2 */
50 static black_box_state_t test_mesh_get_node_by_dev_class_02_state = {
51         .test_case_name = "test_case_mesh_get_node_by_dev_class_02",
52 };
53
54 /* State structure for meshlink_get_node Test Case #3 */
55 static black_box_state_t test_mesh_get_node_01_state = {
56         .test_case_name = "test_mesh_get_node_01_state",
57 };
58
59 /* State structure for meshlink_get_node Test Case #4 */
60 static black_box_state_t test_mesh_get_node_02_state = {
61         .test_case_name = "test_mesh_get_node_02_state",
62 };
63
64 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
65         (void)mesh;
66
67         static const char *levelstr[] = {
68                 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
69                 [MESHLINK_INFO] = "\x1b[32mINFO",
70                 [MESHLINK_WARNING] = "\x1b[33mWARNING",
71                 [MESHLINK_ERROR] = "\x1b[31mERROR",
72                 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
73         };
74
75         fprintf(stderr, "%s(%s):\x1b[0m %s\n", mesh->name, levelstr[level], text);
76 }
77
78 /* Execute meshlink_get_node Test Case # 1 */
79 static void test_case_mesh_get_node_by_dev_class_01(void **state) {
80         execute_test(test_steps_mesh_get_node_by_dev_class_01, state);
81 }
82
83 /* Test Steps for meshlink_get_node Test Case # 1
84
85     Test Steps:
86     1. Open nut, peer1, relay1, relay2, relay3 node instances, export and
87         import the configuration of NUT with other nodes.
88     2. Run the node instances.
89     3. Call meshlink_get_all_nodes_by_dev_class API with NULL as nodes array parameter
90         for DEV_CLASS_STATIONARY
91     4. Call meshlink_get_all_nodes_by_dev_class API with previously allocated nodes array
92         parameter for DEV_CLASS_BACKBONE
93     5. Call meshlink_get_all_nodes_by_dev_class API with previously allocated nodes array
94         parameter for DEV_CLASS_PORTABLE
95
96     Expected Result:
97     meshlink_get_all_nodes_by_dev_class API should return appropriate node array pointer and
98     node member parameter when called and return accordingly.
99 */
100 static bool test_steps_mesh_get_node_by_dev_class_01(void) {
101         meshlink_node_t **nodes;
102         size_t nnodes = 0, i;
103
104         /* Create meshlink instance for NUT */
105         meshlink_handle_t *mesh_nut = meshlink_open("getnodeconf.1", "nut", "node_sim", DEV_CLASS_STATIONARY);
106         assert(mesh_nut);
107         meshlink_set_log_cb(mesh_nut, TEST_MESHLINK_LOG_LEVEL, log_message);
108
109         /* Create meshlink instance for peer1 */
110         meshlink_handle_t *mesh_peer1 = meshlink_open("getnodeconf.2", "peer1", "node_sim", DEV_CLASS_STATIONARY);
111         assert(mesh_peer1);
112         meshlink_set_log_cb(mesh_peer1, TEST_MESHLINK_LOG_LEVEL, log_message);
113
114         /* Create meshlink instance for relay1 */
115         meshlink_handle_t *mesh_relay1 = meshlink_open("getnodeconf.3", "relay1", "node_sim", DEV_CLASS_BACKBONE);
116         assert(mesh_relay1);
117         meshlink_set_log_cb(mesh_relay1, TEST_MESHLINK_LOG_LEVEL, log_message);
118
119         /* Create meshlink instance for relay2 */
120         meshlink_handle_t *mesh_relay2 = meshlink_open("getnodeconf.4", "relay2", "node_sim", DEV_CLASS_BACKBONE);
121         assert(mesh_relay2);
122         meshlink_set_log_cb(mesh_relay2, TEST_MESHLINK_LOG_LEVEL, log_message);
123
124         /* Create meshlink instance for relay3 */
125         meshlink_handle_t *mesh_relay3 = meshlink_open("getnodeconf.5", "relay3", "node_sim", DEV_CLASS_BACKBONE);
126         assert(mesh_relay3);
127         meshlink_set_log_cb(mesh_relay3, TEST_MESHLINK_LOG_LEVEL, log_message);
128
129         /* importing and exporting mesh meta data */
130         char *exp_nut = meshlink_export(mesh_nut);
131         assert(exp_nut != NULL);
132         char *export = meshlink_export(mesh_peer1);
133         assert(export != NULL);
134         assert(meshlink_import(mesh_nut, export));
135         assert(meshlink_import(mesh_peer1, exp_nut));
136         free(export);
137
138         export = meshlink_export(mesh_relay1);
139         assert(export != NULL);
140         assert(meshlink_import(mesh_nut, export));
141         assert(meshlink_import(mesh_relay1, exp_nut));
142         free(export);
143
144         export = meshlink_export(mesh_relay2);
145         assert(export != NULL);
146         assert(meshlink_import(mesh_nut, export));
147         assert(meshlink_import(mesh_relay2, exp_nut));
148         free(export);
149
150         export = meshlink_export(mesh_relay3);
151         assert(export != NULL);
152         assert(meshlink_import(mesh_nut, export));
153         assert(meshlink_import(mesh_relay3, exp_nut));
154         free(export);
155         free(exp_nut);
156
157         nodes = meshlink_get_all_nodes_by_dev_class(mesh_nut, DEV_CLASS_STATIONARY, NULL, &nnodes);
158         assert_int_not_equal(nodes, NULL);
159         assert_int_equal(nnodes, 2);
160
161         for(i = 0; i < nnodes; i++) {
162                 if(strcasecmp(nodes[i]->name, "nut") && strcasecmp(nodes[i]->name, "peer1")) {
163                         fail();
164                 }
165         }
166
167         nodes = meshlink_get_all_nodes_by_dev_class(mesh_nut, DEV_CLASS_BACKBONE, nodes, &nnodes);
168         assert_int_not_equal(nodes, NULL);
169         assert_int_equal(nnodes, 3);
170
171         for(i = 0; i < nnodes; i++) {
172                 if(strcasecmp(nodes[i]->name, "relay1") && strcasecmp(nodes[i]->name, "relay2") && strcasecmp(nodes[i]->name, "relay3")) {
173                         fail();
174                 }
175         }
176
177         nodes = meshlink_get_all_nodes_by_dev_class(mesh_nut, DEV_CLASS_PORTABLE, nodes, &nnodes);
178         assert_int_equal(nodes, NULL);
179         assert_int_equal(nnodes, 0);
180         assert_int_equal(meshlink_errno, 0);
181
182         free(nodes);
183         meshlink_close(mesh_nut);
184         meshlink_close(mesh_peer1);
185         meshlink_close(mesh_relay1);
186         meshlink_close(mesh_relay2);
187         meshlink_close(mesh_relay3);
188
189         return true;
190 }
191
192 /* Execute meshlink_get_node Test Case # 2 - Invalid case
193     Passing invalid parameters as input arguments */
194 static void test_case_mesh_get_node_by_dev_class_02(void **state) {
195         execute_test(test_steps_mesh_get_node_by_dev_class_02, state);
196 }
197
198 /* Test Steps for meshlink_get_node Test Case # 2
199
200     Test Steps:
201     1. Create NUT
202     2. Call meshlink_get_all_nodes_by_dev_class API with invalid parameters
203
204     Expected Result:
205     meshlink_get_all_nodes_by_dev_class API should return NULL and set appropriate
206     meshlink_errno.
207 */
208 static bool test_steps_mesh_get_node_by_dev_class_02(void) {
209         meshlink_node_t **nodes;
210         size_t nnodes = 0;
211         int i;
212         meshlink_destroy("getnodeconf.1");
213
214         /* Create meshlink instance for NUT */
215         meshlink_handle_t *mesh_nut = meshlink_open("getnodeconf.1", "nut", "node_sim", DEV_CLASS_STATIONARY);
216         assert(mesh_nut);
217         meshlink_set_log_cb(mesh_nut, TEST_MESHLINK_LOG_LEVEL, log_message);
218
219         nodes = meshlink_get_all_nodes_by_dev_class(mesh_nut, _DEV_CLASS_MAX + 10, NULL, &nnodes);
220         assert_int_equal(nodes, NULL);
221         assert_int_not_equal(meshlink_errno, 0);
222
223         nodes = meshlink_get_all_nodes_by_dev_class(mesh_nut, DEV_CLASS_STATIONARY, NULL, NULL);
224         assert_int_equal(nodes, NULL);
225         assert_int_not_equal(meshlink_errno, 0);
226
227         nodes = meshlink_get_all_nodes_by_dev_class(NULL, DEV_CLASS_STATIONARY, NULL, &nnodes);
228         assert_int_equal(nodes, NULL);
229         assert_int_not_equal(meshlink_errno, 0);
230
231         meshlink_close(mesh_nut);
232         meshlink_destroy("getnodeconf.1");
233         return true;
234 }
235
236 /* Execute meshlink_get_node_dev_class Test Case # 1 */
237 static void test_case_mesh_get_node_dev_class_01(void **state) {
238         execute_test(test_steps_mesh_get_node_dev_class_01, state);
239 }
240
241 /* Test Steps for meshlink_get_node_dev_class Test Case # 1
242
243     Test Steps:
244     1. Create NUT node with DEV_CLASS_STATIONARY device class and obtain node handle
245     2. Call meshlink_get_node_dev_class API
246
247     Expected Result:
248     meshlink_get_node_dev_class API should return DEV_CLASS_STATIONARY device class
249 */
250 static bool test_steps_mesh_get_node_dev_class_01(void) {
251         meshlink_destroy("getnodeconf.1");
252
253         /* Create meshlink instance for NUT */
254         meshlink_handle_t *mesh_nut = meshlink_open("getnodeconf.1", "nut", "node_sim", DEV_CLASS_STATIONARY);
255         assert(mesh_nut);
256         meshlink_set_log_cb(mesh_nut, TEST_MESHLINK_LOG_LEVEL, log_message);
257
258         meshlink_node_t *node;
259         node = meshlink_get_self(mesh_nut);
260         assert(node);
261
262         dev_class_t dev_class = meshlink_get_node_dev_class(mesh_nut, node);
263         assert_int_equal(dev_class, DEV_CLASS_STATIONARY);
264
265         meshlink_close(mesh_nut);
266         meshlink_destroy("getnodeconf.1");
267         return true;
268 }
269
270 /* Execute meshlink_get_node_dev_class Test Case # 2 */
271 static void test_case_mesh_get_node_dev_class_02(void **state) {
272         execute_test(test_steps_mesh_get_node_dev_class_02, state);
273 }
274
275 /* Test Steps for meshlink_get_node_dev_class Test Case # 2
276
277     Test Steps:
278     1. Create NUT and obtain NUT node handle
279     2. Call meshlink_get_node_dev_class API with invalid parameters
280
281     Expected Result:
282     meshlink_get_node_dev_class API should return NULL and set appropriate
283     meshlink_errno.
284 */
285 static bool test_steps_mesh_get_node_dev_class_02(void) {
286         meshlink_destroy("getnodeconf.1");
287
288         /* Create meshlink instance for NUT */
289         meshlink_handle_t *mesh_nut = meshlink_open("getnodeconf.1", "nut", "node_sim", DEV_CLASS_STATIONARY);
290         assert(mesh_nut);
291         meshlink_set_log_cb(mesh_nut, TEST_MESHLINK_LOG_LEVEL, log_message);
292
293         meshlink_node_t *node;
294         node = meshlink_get_self(mesh_nut);
295         assert(node);
296
297         int dev_class = meshlink_get_node_dev_class(NULL, node);
298         assert_int_equal(dev_class, -1);
299         assert_int_not_equal(meshlink_errno, 0);
300
301         dev_class = meshlink_get_node_dev_class(mesh_nut, NULL);
302         assert_int_equal(dev_class, -1);
303         assert_int_not_equal(meshlink_errno, 0);
304
305         meshlink_close(mesh_nut);
306         meshlink_destroy("getnodeconf.1");
307         return true;
308 }
309
310 static int black_box_setup_test_case(void **state) {
311         fprintf(stderr, "Destroying confbases\n");
312         meshlink_destroy("getnodeconf.1");
313         meshlink_destroy("getnodeconf.2");
314         meshlink_destroy("getnodeconf.3");
315         meshlink_destroy("getnodeconf.4");
316         meshlink_destroy("getnodeconf.5");
317         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
318         meshlink_errno = MESHLINK_OK;
319
320         return 0;
321 }
322
323 int test_meshlink_get_all_node_by_device_class(void) {
324         const struct CMUnitTest blackbox_get_node_tests[] = {
325                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_get_node_by_dev_class_01, black_box_setup_test_case, black_box_setup_test_case,
326                                 (void *)&test_mesh_get_node_by_dev_class_01_state),
327                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_get_node_by_dev_class_02, NULL, NULL,
328                                 (void *)&test_mesh_get_node_by_dev_class_02_state),
329                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_get_node_dev_class_01, NULL, NULL,
330                                 (void *)&test_mesh_get_node_01_state),
331                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_get_node_dev_class_02, NULL, NULL,
332                                 (void *)&test_mesh_get_node_02_state),
333         };
334
335         total_tests += sizeof(blackbox_get_node_tests) / sizeof(blackbox_get_node_tests[0]);
336
337         return cmocka_run_group_tests(blackbox_get_node_tests, NULL, NULL);
338 }