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);
45 static void test_case_import_06(void **state);
46 static bool test_import_06(void);
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",
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",
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",
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",
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",
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",
74 /* Execute import Test Case # 1 - valid case*/
75 static void test_case_import_01(void **state) {
76 execute_test(test_import_01, state);
78 /* Test Steps for meshlink_import Test Case # 1 - Valid case
81 1. Open NUT(Node Under Test) & bar meshes.
82 2. Export and Import mutually
85 Both the nodes imports successfully
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");
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);
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);
108 assert_int_equal(imp1 && imp2, true);
110 meshlink_close(mesh1);
111 meshlink_close(mesh2);
112 meshlink_destroy("importconf1");
113 meshlink_destroy("importconf2");
117 /* Execute import Test Case # 2 - invalid case*/
118 static void test_case_import_02(void **state) {
119 execute_test(test_import_02, state);
121 /* Test Steps for meshlink_import Test Case # 2 - Invalid case
124 1. Open NUT(Node Under Test) & bar meshes.
125 2. Passing NULL as mesh handle argument for meshlink_import API
128 Reports error successfully by returning false
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");
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);
143 // Exporting & Importing nodes
144 char *exp1 = meshlink_export(mesh1);
145 assert(exp1 != NULL);
146 char *exp2 = meshlink_export(mesh2);
147 assert(exp2 != NULL);
149 bool imp1 = meshlink_import(NULL, exp2);
150 bool imp2 = meshlink_import(mesh2, exp1);
151 assert_int_equal((!imp1) && imp2, true);
153 meshlink_close(mesh1);
154 meshlink_close(mesh2);
155 meshlink_destroy("importconf1");
156 meshlink_destroy("importconf2");
161 /* Execute import Test Case # 3 - invalid case*/
162 static void test_case_import_03(void **state) {
163 execute_test(test_import_03, state);
165 /* Test Steps for meshlink_import Test Case # 3 - Invalid case
168 1. Open NUT(Node Under Test) & bar meshes.
169 2. Passing NULL as exported data argument for meshlink_import API
172 Reports error successfully by returning false
174 static bool test_import_03(void) {
175 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
177 meshlink_destroy("importconf1");
178 meshlink_destroy("importconf2");
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);
188 /* Exporting & Importing nodes */
189 char *exp1 = meshlink_export(mesh1);
190 assert(exp1 != NULL);
191 char *exp2 = meshlink_export(mesh2);
192 assert(exp2 != NULL);
194 bool imp1 = meshlink_import(mesh1, NULL);
195 bool imp2 = meshlink_import(mesh2, exp1);
197 assert_int_equal((!imp1) && imp2, true);
199 meshlink_close(mesh1);
200 meshlink_close(mesh2);
201 meshlink_destroy("importconf1");
202 meshlink_destroy("importconf2");
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);
210 /* Test Steps for meshlink_import Test Case # 4 - Invalid case
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
218 Reports error successfully by returning false
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");
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);
233 // Exporting & Importing nodes
234 char *exp1 = meshlink_export(mesh1);
235 assert(exp1 != NULL);
236 char *exp2 = meshlink_export(mesh2);
237 assert(exp2 != NULL);
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);
244 meshlink_close(mesh1);
245 meshlink_close(mesh2);
246 meshlink_destroy("importconf1");
247 meshlink_destroy("importconf2");
251 /* Execute import Test Case # 5 - valid case*/
252 static void test_case_import_05(void **state) {
253 execute_test(test_import_05, state);
255 /* Test Steps for meshlink_import Test Case # 5 - Invalid case
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
263 Reports error successfully by returning false
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");
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);
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);
285 bool imp2 = meshlink_import(mesh2, exp1);
288 /** Trying to import twice **/
289 bool imp3 = meshlink_import(mesh2, exp1);
291 assert_int_equal(imp3, false);
293 meshlink_close(mesh1);
294 meshlink_close(mesh2);
295 meshlink_destroy("importconf1");
296 meshlink_destroy("importconf2");
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)
313 total_tests += sizeof(blackbox_import_tests) / sizeof(blackbox_import_tests[0]);
315 return cmocka_run_group_tests(blackbox_import_tests, NULL, NULL);