2 test_cases_import.c -- Execution of specific meshlink black box test cases
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
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"
36 /* Modify this to change the logging level of Meshlink */
37 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
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);
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",
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",
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",
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",
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",
75 /* Execute import Test Case # 1 - valid case*/
76 static void test_case_import_01(void **state) {
77 execute_test(test_import_01, state);
79 /* Test Steps for meshlink_import Test Case # 1 - Valid case
82 1. Open NUT(Node Under Test) & bar meshes.
83 2. Export and Import mutually
86 Both the nodes imports successfully
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"));
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);
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);
109 assert_int_equal(imp1 && imp2, true);
111 meshlink_close(mesh1);
112 meshlink_close(mesh2);
113 assert(meshlink_destroy("importconf1"));
114 assert(meshlink_destroy("importconf2"));
118 /* Execute import Test Case # 2 - invalid case*/
119 static void test_case_import_02(void **state) {
120 execute_test(test_import_02, state);
122 /* Test Steps for meshlink_import Test Case # 2 - Invalid case
125 1. Open NUT(Node Under Test) & bar meshes.
126 2. Passing NULL as mesh handle argument for meshlink_import API
129 Reports error successfully by returning false
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"));
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);
144 // Exporting & Importing nodes
145 char *exp1 = meshlink_export(mesh1);
146 assert(exp1 != NULL);
147 char *exp2 = meshlink_export(mesh2);
148 assert(exp2 != NULL);
150 bool imp1 = meshlink_import(NULL, exp2);
151 bool imp2 = meshlink_import(mesh2, exp1);
152 assert_int_equal((!imp1) && imp2, true);
154 meshlink_close(mesh1);
155 meshlink_close(mesh2);
156 assert(meshlink_destroy("importconf1"));
157 assert(meshlink_destroy("importconf2"));
162 /* Execute import Test Case # 3 - invalid case*/
163 static void test_case_import_03(void **state) {
164 execute_test(test_import_03, state);
166 /* Test Steps for meshlink_import Test Case # 3 - Invalid case
169 1. Open NUT(Node Under Test) & bar meshes.
170 2. Passing NULL as exported data argument for meshlink_import API
173 Reports error successfully by returning false
175 static bool test_import_03(void) {
176 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
178 assert(meshlink_destroy("importconf1"));
179 assert(meshlink_destroy("importconf2"));
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);
189 /* Exporting & Importing nodes */
190 char *exp1 = meshlink_export(mesh1);
191 assert(exp1 != NULL);
192 char *exp2 = meshlink_export(mesh2);
193 assert(exp2 != NULL);
195 bool imp1 = meshlink_import(mesh1, NULL);
196 bool imp2 = meshlink_import(mesh2, exp1);
198 assert_int_equal((!imp1) && imp2, true);
200 meshlink_close(mesh1);
201 meshlink_close(mesh2);
202 assert(meshlink_destroy("importconf1"));
203 assert(meshlink_destroy("importconf2"));
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);
211 /* Test Steps for meshlink_import Test Case # 4 - Invalid case
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
219 Reports error successfully by returning false
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"));
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);
234 // Exporting & Importing nodes
235 char *exp1 = meshlink_export(mesh1);
236 assert(exp1 != NULL);
237 char *exp2 = meshlink_export(mesh2);
238 assert(exp2 != NULL);
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);
245 meshlink_close(mesh1);
246 meshlink_close(mesh2);
247 assert(meshlink_destroy("importconf1"));
248 assert(meshlink_destroy("importconf2"));
252 /* Execute import Test Case # 5 - valid case*/
253 static void test_case_import_05(void **state) {
254 execute_test(test_import_05, state);
256 /* Test Steps for meshlink_import Test Case # 5 - Invalid case
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
264 Reports error successfully by returning false
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"));
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);
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);
286 bool imp2 = meshlink_import(mesh2, exp1);
289 /** Trying to import twice **/
290 bool imp3 = meshlink_import(mesh2, exp1);
292 assert_int_equal(imp3, false);
294 meshlink_close(mesh1);
295 meshlink_close(mesh2);
296 assert(meshlink_destroy("importconf1"));
297 assert(meshlink_destroy("importconf2"));
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)
314 total_tests += sizeof(blackbox_import_tests) / sizeof(blackbox_import_tests[0]);
316 return cmocka_run_group_tests(blackbox_import_tests, NULL, NULL);