]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_get_all_nodes.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_get_all_nodes.c
1 /*
2     test_cases_get_all_nodes.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 #include "execute_tests.h"
21 #include "test_cases.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
25 #include "test_cases_get_all_nodes.h"
26 #include <assert.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <setjmp.h>
31 #include <cmocka.h>
32
33
34 /* Modify this to change the logging level of Meshlink */
35 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
36
37 static void test_case_get_all_nodes_01(void **state);
38 static bool test_get_all_nodes_01(void);
39 static void test_case_get_all_nodes_02(void **state);
40 static bool test_get_all_nodes_02(void);
41 static void test_case_get_all_nodes_03(void **state);
42 static bool test_get_all_nodes_03(void);
43
44 /* State structure for get_all_nodes Test Case #1 */
45 static black_box_state_t test_case_get_all_nodes_01_state = {
46         .test_case_name = "test_case_get_all_nodes_01",
47 };
48
49 /* State structure for get_all_nodes Test Case #2 */
50 static black_box_state_t test_case_get_all_nodes_02_state = {
51         .test_case_name = "test_case_get_all_nodes_02",
52 };
53
54 /* State structure for get_all_nodes Test Case #3 */
55 static black_box_state_t test_case_get_all_nodes_03_state = {
56         .test_case_name = "test_case_get_all_nodes_03",
57 };
58
59 /* Execute get_all_nodes Test Case # 1 - Valid case - get all nodes in the mesh */
60 static void test_case_get_all_nodes_01(void **state) {
61         execute_test(test_get_all_nodes_01, state);
62 }
63 /* Test Steps for get_all_nodes Test Case # 1 - Valid case
64
65     Test Steps:
66     1. Open NUT and get list of nodes
67     2. Open bar and join with NUT
68     3. get list of nodes together
69
70     Expected Result:
71     Obtaining list of nodes in the mesh at the given instance
72 */
73 static bool test_get_all_nodes_01(void) {
74         meshlink_destroy("getnodeconf1");
75         meshlink_destroy("getnodeconf2");
76         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
77
78         /* Create meshlink instance for NUT */
79         meshlink_handle_t *mesh1 = meshlink_open("getnodeconf1", "nut", "node_sim", DEV_CLASS_STATIONARY);
80         assert(mesh1);
81         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
82
83         size_t nnodes = 0;
84         meshlink_node_t **nodes = NULL;
85         nodes = meshlink_get_all_nodes(mesh1, nodes, &nnodes);
86         assert_int_not_equal(nodes, NULL);
87         assert_int_equal(nnodes, 1);
88
89         /* Create meshlink instance for bar */
90         meshlink_handle_t *mesh2 = meshlink_open("getnodeconf2", "bar", "node_sim", DEV_CLASS_STATIONARY);
91         assert(mesh2);
92
93         /* importing and exporting mesh meta data */
94         char *exp1 = meshlink_export(mesh1);
95         assert(exp1 != NULL);
96         char *exp2 = meshlink_export(mesh2);
97         assert(exp2 != NULL);
98         assert(meshlink_import(mesh1, exp2));
99         assert(meshlink_import(mesh2, exp1));
100
101         nodes = meshlink_get_all_nodes(mesh1, nodes, &nnodes);
102         assert_int_not_equal(nodes, NULL);
103         assert_int_equal(nnodes, 2);
104
105         meshlink_close(mesh1);
106         meshlink_close(mesh2);
107         meshlink_destroy("getnodeconf1");
108         meshlink_destroy("getnodeconf2");
109
110         return true;
111 }
112
113
114
115 /* Execute get_all_nodes Test Case # 2 - Invalid case - get all nodes in the mesh passing NULL */
116 static void test_case_get_all_nodes_02(void **state) {
117         execute_test(test_get_all_nodes_02, state);
118 }
119
120 /* Test Steps for get_all_nodes Test Case # 2 - Invalid case
121
122     Test Steps:
123     1. Passing NULL as mesh handle argument for meshlink_get_all_nodes
124
125     Expected Result:
126     Error reported correctly by returning NULL
127 */
128 static bool test_get_all_nodes_02(void) {
129         size_t nmemb = 0;
130
131         meshlink_node_t **nodes = meshlink_get_all_nodes(NULL, NULL, &nmemb);
132         assert_null(nodes);
133         assert_int_equal(nmemb, NULL);
134
135         return true;
136 }
137
138 /* Execute get_all_nodes Test Case # 3 - Invalid case - get all nodes in the mesh passing NULL as nmeb arg */
139 static void test_case_get_all_nodes_03(void **state) {
140         execute_test(test_get_all_nodes_03, state);
141 }
142 /* Test Steps for get_all_nodes Test Case # 3 - Invalid case
143
144     Test Steps:
145     1. Passing NULL as pointer to node members argument for meshlink_get_all_nodes
146
147     Expected Result:
148     Error reported correctly by returning NULL
149 */
150 static bool test_get_all_nodes_03(void) {
151         /* Create meshlink instance */
152         meshlink_handle_t *mesh_handle = meshlink_open("getallnodesconf", "nut", "node_sim", 1);
153         assert(mesh_handle);
154         assert(meshlink_start(mesh_handle));
155
156         meshlink_node_t **nodes = NULL;
157         nodes = meshlink_get_all_nodes(mesh_handle, nodes, NULL);
158         assert_int_equal(nodes, NULL);
159
160         meshlink_close(mesh_handle);
161         meshlink_destroy("getallnodesconf");
162
163         return true;
164 }
165
166 int test_meshlink_get_all_nodes(void) {
167         const struct CMUnitTest blackbox_get_all_nodes[] = {
168                 cmocka_unit_test_prestate_setup_teardown(test_case_get_all_nodes_01, NULL, NULL,
169                                 (void *)&test_case_get_all_nodes_01_state),
170                 cmocka_unit_test_prestate_setup_teardown(test_case_get_all_nodes_02, NULL, NULL,
171                                 (void *)&test_case_get_all_nodes_02_state),
172                 cmocka_unit_test_prestate_setup_teardown(test_case_get_all_nodes_03, NULL, NULL,
173                                 (void *)&test_case_get_all_nodes_03_state)
174         };
175         total_tests += sizeof(blackbox_get_all_nodes) / sizeof(blackbox_get_all_nodes[0]);
176
177         return cmocka_run_group_tests(blackbox_get_all_nodes, NULL, NULL);
178 }