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