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.
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"
32 /* Modify this to change the logging level of Meshlink */
33 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
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);
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",
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",
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",
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",
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",
71 /* Execute import Test Case # 1 - valid case*/
72 static void test_case_import_01(void **state) {
73 execute_test(test_import_01, state);
75 /* Test Steps for meshlink_import Test Case # 1 - Valid case
78 1. Open NUT(Node Under Test) & bar meshes.
79 2. Export and Import mutually
82 Both the nodes imports successfully
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");
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);
97 // Exporting and Importing mutually
98 char *exp1 = meshlink_export(mesh1);
100 char *exp2 = meshlink_export(mesh2);
101 assert(exp2 != NULL);
102 bool imp1 = meshlink_import(mesh1, exp2);
103 bool imp2 = meshlink_import(mesh2, exp1);
105 assert_int_equal(imp1 && imp2, true);
107 meshlink_close(mesh1);
108 meshlink_close(mesh2);
109 meshlink_destroy("importconf1");
110 meshlink_destroy("importconf2");
114 /* Execute import Test Case # 2 - invalid case*/
115 static void test_case_import_02(void **state) {
116 execute_test(test_import_02, state);
118 /* Test Steps for meshlink_import Test Case # 2 - Invalid case
121 1. Open NUT(Node Under Test) & bar meshes.
122 2. Passing NULL as mesh handle argument for meshlink_import API
125 Reports error successfully by returning false
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");
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);
140 // Exporting & Importing nodes
141 char *exp1 = meshlink_export(mesh1);
142 assert(exp1 != NULL);
143 char *exp2 = meshlink_export(mesh2);
144 assert(exp2 != NULL);
146 bool imp1 = meshlink_import(NULL, exp2);
147 bool imp2 = meshlink_import(mesh2, exp1);
148 assert_int_equal((!imp1) && imp2, true);
150 meshlink_close(mesh1);
151 meshlink_close(mesh2);
152 meshlink_destroy("importconf1");
153 meshlink_destroy("importconf2");
158 /* Execute import Test Case # 3 - invalid case*/
159 static void test_case_import_03(void **state) {
160 execute_test(test_import_03, state);
162 /* Test Steps for meshlink_import Test Case # 3 - Invalid case
165 1. Open NUT(Node Under Test) & bar meshes.
166 2. Passing NULL as exported data argument for meshlink_import API
169 Reports error successfully by returning false
171 static bool test_import_03(void) {
172 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
174 meshlink_destroy("importconf1");
175 meshlink_destroy("importconf2");
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);
185 /* Exporting & Importing nodes */
186 char *exp1 = meshlink_export(mesh1);
187 assert(exp1 != NULL);
188 char *exp2 = meshlink_export(mesh2);
189 assert(exp2 != NULL);
191 bool imp1 = meshlink_import(mesh1, NULL);
192 bool imp2 = meshlink_import(mesh2, exp1);
194 assert_int_equal((!imp1) && imp2, true);
196 meshlink_close(mesh1);
197 meshlink_close(mesh2);
198 meshlink_destroy("importconf1");
199 meshlink_destroy("importconf2");
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);
207 /* Test Steps for meshlink_import Test Case # 4 - Invalid case
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
215 Reports error successfully by returning false
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");
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);
230 // Exporting & Importing nodes
231 char *exp1 = meshlink_export(mesh1);
232 assert(exp1 != NULL);
233 char *exp2 = meshlink_export(mesh2);
234 assert(exp2 != NULL);
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);
241 meshlink_close(mesh1);
242 meshlink_close(mesh2);
243 meshlink_destroy("importconf1");
244 meshlink_destroy("importconf2");
248 /* Execute import Test Case # 5 - valid case*/
249 static void test_case_import_05(void **state) {
250 execute_test(test_import_05, state);
252 /* Test Steps for meshlink_import Test Case # 5 - Invalid case
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
260 Reports error successfully by returning false
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");
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);
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);
282 bool imp2 = meshlink_import(mesh2, exp1);
285 /** Trying to import twice **/
286 bool imp3 = meshlink_import(mesh2, exp1);
288 assert_int_equal(imp3, false);
290 meshlink_close(mesh1);
291 meshlink_close(mesh2);
292 meshlink_destroy("importconf1");
293 meshlink_destroy("importconf2");
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)
310 total_tests += sizeof(blackbox_import_tests) / sizeof(blackbox_import_tests[0]);
312 return cmocka_run_group_tests(blackbox_import_tests, NULL, NULL);