]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_import.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_import.c
1 /*
2     test_cases_import.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_import.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
36 /* Modify this to change the logging level of Meshlink */
37 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
38
39 static void test_case_import_01(void **state);
40 static bool test_import_01(void);
41 static void test_case_import_02(void **state);
42 static bool test_import_02(void);
43 static void test_case_import_03(void **state);
44 static bool test_import_03(void);
45 static void test_case_import_04(void **state);
46 static bool test_import_04(void);
47 static void test_case_import_05(void **state);
48 static bool test_import_05(void);
49
50 /* State structure for import API Test Case #1 */
51 static black_box_state_t test_case_import_01_state = {
52         .test_case_name = "test_case_import_01",
53 };
54
55 /* State structure for import API Test Case #2 */
56 static black_box_state_t test_case_import_02_state = {
57         .test_case_name = "test_case_import_02",
58 };
59
60 /* State structure for import API Test Case #3 */
61 static black_box_state_t test_case_import_03_state = {
62         .test_case_name = "test_case_import_03",
63 };
64
65 /* State structure for import API Test Case #4 */
66 static black_box_state_t test_case_import_04_state = {
67         .test_case_name = "test_case_import_04",
68 };
69
70 /* State structure for import API Test Case #5 */
71 static black_box_state_t test_case_import_05_state = {
72         .test_case_name = "test_case_import_05",
73 };
74
75 /* Execute import Test Case # 1 - valid case*/
76 static void test_case_import_01(void **state) {
77         execute_test(test_import_01, state);
78 }
79 /* Test Steps for meshlink_import Test Case # 1 - Valid case
80
81     Test Steps:
82     1. Open NUT(Node Under Test) & bar meshes.
83     2. Export and Import mutually
84
85     Expected Result:
86     Both the nodes imports successfully
87 */
88 static bool test_import_01(void) {
89         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
90         assert(meshlink_destroy("importconf1"));
91         assert(meshlink_destroy("importconf2"));
92
93         // Opening NUT and bar nodes
94         meshlink_handle_t *mesh1 = meshlink_open("importconf1", "nut", "test", DEV_CLASS_STATIONARY);
95         assert(mesh1 != NULL);
96         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
97         meshlink_handle_t *mesh2 = meshlink_open("importconf2", "bar", "test", DEV_CLASS_STATIONARY);
98         assert(mesh2 != NULL);
99         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
100
101         // Exporting and Importing mutually
102         char *exp1 = meshlink_export(mesh1);
103         assert(exp1 != NULL);
104         char *exp2 = meshlink_export(mesh2);
105         assert(exp2 != NULL);
106         bool imp1 = meshlink_import(mesh1, exp2);
107         bool imp2 = meshlink_import(mesh2, exp1);
108
109         assert_int_equal(imp1 && imp2, true);
110
111         meshlink_close(mesh1);
112         meshlink_close(mesh2);
113         assert(meshlink_destroy("importconf1"));
114         assert(meshlink_destroy("importconf2"));
115         return imp1 && imp2;
116 }
117
118 /* Execute import Test Case # 2 - invalid case*/
119 static void test_case_import_02(void **state) {
120         execute_test(test_import_02, state);
121 }
122 /* Test Steps for meshlink_import Test Case # 2 - Invalid case
123
124     Test Steps:
125     1. Open NUT(Node Under Test) & bar meshes.
126     2. Passing NULL as mesh handle argument for meshlink_import API
127
128     Expected Result:
129     Reports error successfully by returning false
130 */
131 static bool test_import_02(void) {
132         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
133         assert(meshlink_destroy("importconf1"));
134         assert(meshlink_destroy("importconf2"));
135
136         // Opening NUT and bar nodes
137         meshlink_handle_t *mesh1 = meshlink_open("importconf1", "nut", "test", DEV_CLASS_STATIONARY);
138         assert(mesh1 != NULL);
139         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
140         meshlink_handle_t *mesh2 = meshlink_open("importconf2", "bar", "test", DEV_CLASS_STATIONARY);
141         assert(mesh2 != NULL);
142         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
143
144         // Exporting & Importing nodes
145         char *exp1 = meshlink_export(mesh1);
146         assert(exp1 != NULL);
147         char *exp2 = meshlink_export(mesh2);
148         assert(exp2 != NULL);
149
150         bool imp1 = meshlink_import(NULL, exp2);
151         bool imp2 = meshlink_import(mesh2, exp1);
152         assert_int_equal((!imp1) && imp2, true);
153
154         meshlink_close(mesh1);
155         meshlink_close(mesh2);
156         assert(meshlink_destroy("importconf1"));
157         assert(meshlink_destroy("importconf2"));
158         return true;
159 }
160
161
162 /* Execute import Test Case # 3 - invalid case*/
163 static void test_case_import_03(void **state) {
164         execute_test(test_import_03, state);
165 }
166 /* Test Steps for meshlink_import Test Case # 3 - Invalid case
167
168     Test Steps:
169     1. Open NUT(Node Under Test) & bar meshes.
170     2. Passing NULL as exported data argument for meshlink_import API
171
172     Expected Result:
173     Reports error successfully by returning false
174 */
175 static bool test_import_03(void) {
176         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
177
178         assert(meshlink_destroy("importconf1"));
179         assert(meshlink_destroy("importconf2"));
180
181         /* Opening NUT and bar nodes */
182         meshlink_handle_t *mesh1 = meshlink_open("importconf1", "nut", "chat", DEV_CLASS_STATIONARY);
183         assert(mesh1 != NULL);
184         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
185         meshlink_handle_t *mesh2 = meshlink_open("importconf2", "bar", "chat", DEV_CLASS_STATIONARY);
186         assert(mesh2 != NULL);
187         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
188
189         /* Exporting & Importing nodes */
190         char *exp1 = meshlink_export(mesh1);
191         assert(exp1 != NULL);
192         char *exp2 = meshlink_export(mesh2);
193         assert(exp2 != NULL);
194
195         bool imp1 = meshlink_import(mesh1, NULL);
196         bool imp2 = meshlink_import(mesh2, exp1);
197
198         assert_int_equal((!imp1) && imp2, true);
199
200         meshlink_close(mesh1);
201         meshlink_close(mesh2);
202         assert(meshlink_destroy("importconf1"));
203         assert(meshlink_destroy("importconf2"));
204         return true;
205 }
206
207 /* Execute import Test Case # 4 - invalid case garbage string*/
208 static void test_case_import_04(void **state) {
209         execute_test(test_import_04, state);
210 }
211 /* Test Steps for meshlink_import Test Case # 4 - Invalid case
212
213     Test Steps:
214     1. Open NUT(Node Under Test) & bar meshes.
215     2. Passing some garbage string(NULL terminated)
216         as an argument for meshlink_import API
217
218     Expected Result:
219     Reports error successfully by returning false
220 */
221 static bool test_import_04(void) {
222         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
223         assert(meshlink_destroy("importconf1"));
224         assert(meshlink_destroy("importconf2"));
225
226         // Opening NUT and bar nodes
227         meshlink_handle_t *mesh1 = meshlink_open("importconf1", "nut", "chat", DEV_CLASS_STATIONARY);
228         assert(mesh1 != NULL);
229         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
230         meshlink_handle_t *mesh2 = meshlink_open("importconf2", "bar", "chat", DEV_CLASS_STATIONARY);
231         assert(mesh2 != NULL);
232         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
233
234         // Exporting & Importing nodes
235         char *exp1 = meshlink_export(mesh1);
236         assert(exp1 != NULL);
237         char *exp2 = meshlink_export(mesh2);
238         assert(exp2 != NULL);
239
240         // Importing NUT with garbage string as exported data argument
241         bool imp1 = meshlink_import(mesh1, "1/2/3");
242         bool imp2 = meshlink_import(mesh2, exp1);
243         assert_int_equal((!imp1) && imp2, true);
244
245         meshlink_close(mesh1);
246         meshlink_close(mesh2);
247         assert(meshlink_destroy("importconf1"));
248         assert(meshlink_destroy("importconf2"));
249         return true;
250 }
251
252 /* Execute import Test Case # 5 - valid case*/
253 static void test_case_import_05(void **state) {
254         execute_test(test_import_05, state);
255 }
256 /* Test Steps for meshlink_import Test Case # 5 - Invalid case
257
258     Test Steps:
259     1. Open NUT(Node Under Test) & bar meshes.
260     2. Export and Import mutually
261     2. Try to import NUT again/twice at 'bar' node
262
263     Expected Result:
264     Reports error successfully by returning false
265 */
266 static bool test_import_05(void) {
267         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
268         assert(meshlink_destroy("importconf1"));
269         assert(meshlink_destroy("importconf2"));
270
271         /* Opening NUT and bar nodes */
272         meshlink_handle_t *mesh1 = meshlink_open("importconf1", "nut", "chat", DEV_CLASS_STATIONARY);
273         assert(mesh1 != NULL);
274         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
275         meshlink_handle_t *mesh2 = meshlink_open("importconf2", "bar", "chat", DEV_CLASS_STATIONARY);
276         assert(mesh2 != NULL);
277         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
278
279         /* Exporting  & Importing nodes */
280         char *exp1 = meshlink_export(mesh1);
281         assert(exp1 != NULL);
282         char *exp2 = meshlink_export(mesh2);
283         assert(exp2 != NULL);
284         bool imp1 = meshlink_import(mesh1, exp2);
285         assert(imp1);
286         bool imp2 = meshlink_import(mesh2, exp1);
287         assert(imp2);
288
289         /** Trying to import twice **/
290         bool imp3 = meshlink_import(mesh2, exp1);
291
292         assert_int_equal(imp3, false);
293
294         meshlink_close(mesh1);
295         meshlink_close(mesh2);
296         assert(meshlink_destroy("importconf1"));
297         assert(meshlink_destroy("importconf2"));
298         return true;
299 }
300
301 int test_meshlink_import(void) {
302         const struct CMUnitTest blackbox_import_tests[] = {
303                 cmocka_unit_test_prestate_setup_teardown(test_case_import_01, NULL, NULL,
304                                 (void *)&test_case_import_01_state),
305                 cmocka_unit_test_prestate_setup_teardown(test_case_import_02, NULL, NULL,
306                                 (void *)&test_case_import_02_state),
307                 cmocka_unit_test_prestate_setup_teardown(test_case_import_03, NULL, NULL,
308                                 (void *)&test_case_import_03_state),
309                 cmocka_unit_test_prestate_setup_teardown(test_case_import_04, NULL, NULL,
310                                 (void *)&test_case_import_04_state),
311                 cmocka_unit_test_prestate_setup_teardown(test_case_import_05, NULL, NULL,
312                                 (void *)&test_case_import_05_state)
313         };
314         total_tests += sizeof(blackbox_import_tests) / sizeof(blackbox_import_tests[0]);
315
316         return cmocka_run_group_tests(blackbox_import_tests, NULL, NULL);
317 }