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