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