]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_open.c
Add missing atomic test cases to the APIs that affects disk writes
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_open.c
1 /*
2     test_cases_open.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 #ifdef NDEBUG
21 #undef NDEBUG
22 #endif
23
24 #include "execute_tests.h"
25 #include "test_cases_open.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include "../../utils.h"
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <setjmp.h>
33 #include <cmocka.h>
34 #include <assert.h>
35 #include <string.h>
36 #include <signal.h>
37 #include <linux/limits.h>
38
39 /* Modify this to change the logging level of Meshlink */
40 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
41
42 #define NUT                         "nut"
43 #define PEER                        "peer"
44 #define TEST_MESHLINK_OPEN          "test_open"
45 #define create_path(confbase, node_name, test_case_no)   assert(snprintf(confbase, sizeof(confbase), TEST_MESHLINK_OPEN "_%ld_%s_%02d", (long) getpid(), node_name, test_case_no) > 0)
46
47 static void test_case_mesh_open_01(void **state);
48 static bool test_steps_mesh_open_01(void);
49 static void test_case_mesh_open_02(void **state);
50 static bool test_steps_mesh_open_02(void);
51 static void test_case_mesh_open_03(void **state);
52 static bool test_steps_mesh_open_03(void);
53 static void test_case_mesh_open_04(void **state);
54 static bool test_steps_mesh_open_04(void);
55 static void test_case_mesh_open_05(void **state);
56 static bool test_steps_mesh_open_05(void);
57 static void test_case_mesh_open_06(void **state);
58 static bool test_steps_mesh_open_06(void);
59 static void test_case_mesh_open_07(void **state);
60 static bool test_steps_mesh_open_07(void);
61
62 /* State structure for meshlink_open Test Case #1 */
63 static black_box_state_t test_mesh_open_01_state = {
64         .test_case_name = "test_case_mesh_open_01",
65 };
66
67 /* State structure for meshlink_open Test Case #2 */
68 static black_box_state_t test_mesh_open_02_state = {
69         .test_case_name = "test_case_mesh_open_02",
70 };
71
72 /* State structure for meshlink_open Test Case #3 */
73 static black_box_state_t test_mesh_open_03_state = {
74         .test_case_name = "test_case_mesh_open_03",
75 };
76
77 /* State structure for meshlink_open Test Case #4 */
78 static black_box_state_t test_mesh_open_04_state = {
79         .test_case_name = "test_case_mesh_open_04",
80 };
81
82 /* State structure for meshlink_open Test Case #5 */
83 static black_box_state_t test_mesh_open_05_state = {
84         .test_case_name = "test_case_mesh_open_05",
85 };
86
87 /* State structure for meshlink_open Test Case #6 */
88 static black_box_state_t test_mesh_open_06_state = {
89         .test_case_name = "test_case_mesh_open_06",
90 };
91
92 /* State structure for meshlink_open Test Case #7 */
93 static black_box_state_t test_mesh_open_07_state = {
94         .test_case_name = "test_case_mesh_open_07",
95 };
96
97 /* Execute meshlink_open Test Case # 1*/
98 static void test_case_mesh_open_01(void **state) {
99         execute_test(test_steps_mesh_open_01, state);
100 }
101
102 /* Test Steps for meshlink_open Test Case # 1
103
104     Test Steps:
105     1. Open the node instance using meshlink_open
106
107     Expected Result:
108     meshlink_open API should successfully return a mesh handle.
109 */
110 static bool test_steps_mesh_open_01(void) {
111         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
112         meshlink_handle_t *mesh = meshlink_open("open_conf", "foo", "test", DEV_CLASS_STATIONARY);
113         assert_int_not_equal(mesh, NULL);
114
115         meshlink_close(mesh);
116         assert(meshlink_destroy("open_conf"));
117         return true;
118 }
119
120 /* Execute meshlink_open Test Case # 2*/
121 static void test_case_mesh_open_02(void **state) {
122         execute_test(test_steps_mesh_open_02, state);
123 }
124
125 /* Test Steps for meshlink_open Test Case # 2
126
127     Test Steps:
128     1. Open the node instance using meshlink_open with NULL as confbase argument
129
130     Expected Result:
131     meshlink_open API should successfully report error by returning NULL pointer
132 */
133 static bool test_steps_mesh_open_02(void) {
134         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
135         meshlink_handle_t *mesh = meshlink_open(NULL, "foo", "test", DEV_CLASS_STATIONARY);
136         assert_int_equal(mesh, NULL);
137
138         return true;
139 }
140
141 /* Execute meshlink_open Test Case # 3 */
142 static void test_case_mesh_open_03(void **state) {
143         execute_test(test_steps_mesh_open_03, state);
144 }
145
146 /* Test Steps for meshlink_open Test Case # 3
147
148     Test Steps:
149     1. Open the node instance using meshlink_open with NULL as node name argument
150
151     Expected Result:
152     meshlink_open API should successfully report error by returning NULL pointer
153 */
154 static bool test_steps_mesh_open_03(void) {
155         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
156         meshlink_handle_t *mesh = meshlink_open("openconf", NULL, "test", DEV_CLASS_STATIONARY);
157         assert_int_equal(mesh, NULL);
158
159         assert(meshlink_destroy("open_conf"));
160         return true;
161 }
162
163 /* Execute meshlink_open Test Case # 4*/
164 static void test_case_mesh_open_04(void **state) {
165         execute_test(test_steps_mesh_open_04, state);
166 }
167
168 /* Test Steps for meshlink_open Test Case # 4
169
170     Test Steps:
171     1. Open the node instance using meshlink_open with NULL as app name argument
172
173     Expected Result:
174     meshlink_open API should successfully report error by returning NULL pointer
175 */
176 static bool test_steps_mesh_open_04(void) {
177         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
178         meshlink_handle_t *mesh = meshlink_open("openconf", "foo", NULL, DEV_CLASS_STATIONARY);
179         assert_int_equal(mesh, NULL);
180
181         assert(meshlink_destroy("open_conf"));
182         return true;
183 }
184
185 /* Execute meshlink_open Test Case # 5*/
186 static void test_case_mesh_open_05(void **state) {
187         execute_test(test_steps_mesh_open_05, state);
188 }
189
190 /* Test Steps for meshlink_open Test Case # 5
191
192     Test Steps:
193     1. Open the node instance using meshlink_open with invalid device class argument
194
195     Expected Result:
196     meshlink_open API should successfully report error by returning NULL pointer
197 */
198 static bool test_steps_mesh_open_05(void) {
199         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
200         meshlink_handle_t *mesh = meshlink_open("openconf", "foo", "test", -1);
201         assert_int_equal(mesh, NULL);
202
203         assert(meshlink_destroy("open_conf"));
204         return true;
205 }
206
207 /* Execute meshlink_open Test Case # 7 - Atomicity testing
208     Validate the meshlink_open behavior opened a new confbase and terminated immediately the open call.
209 */
210 static void test_case_mesh_open_06(void **state) {
211         execute_test(test_steps_mesh_open_06, state);
212 }
213
214 static bool test_steps_mesh_open_06(void) {
215         bool status;
216         pid_t pid;
217         int pid_status;
218         char nut_confbase[PATH_MAX];
219         create_path(nut_confbase, NUT, 6);
220
221         // Fork a new process in which NUT opens it's instance and raises SIGINT to terminate.
222
223         pid = fork();
224         assert_int_not_equal(pid, -1);
225
226         if(!pid) {
227                 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
228                 meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_OPEN, DEV_CLASS_STATIONARY);
229                 assert(mesh);
230                 raise(SIGINT);
231         }
232
233         // Wait for child exit and verify which signal terminated it
234
235         assert_int_not_equal(waitpid(pid, &pid_status, 0), -1);
236         assert_int_equal(WIFSIGNALED(pid_status), true);
237         assert_int_equal(WTERMSIG(pid_status), SIGINT);
238
239         // Reopen the NUT instance in the same test suite
240
241         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
242         meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_OPEN, DEV_CLASS_STATIONARY);
243         assert_non_null(mesh);
244
245         // Validate parameters that were used to open meshlink instance.
246
247         assert_int_equal(strcmp(mesh->name, NUT), 0);
248         meshlink_node_t *self = meshlink_get_self(mesh);
249         assert_int_equal(strcmp(self->name, NUT), 0);
250         assert_int_equal(meshlink_get_node_dev_class(mesh, self), DEV_CLASS_STATIONARY);
251
252         // Cleanup
253
254         meshlink_close(mesh);
255         assert_true(meshlink_destroy(nut_confbase));
256         return true;
257 }
258
259 /* Execute meshlink_open Test Case # 7 - Atomicity testing
260     Validate the meshlink_open behavior opened an existing confbase and terminated immediately the open call.
261 */
262 static void test_case_mesh_open_07(void **state) {
263         execute_test(test_steps_mesh_open_07, state);
264 }
265
266 static bool test_steps_mesh_open_07(void) {
267         bool status;
268         pid_t pid;
269         int pid_status;
270         char nut_confbase[PATH_MAX];
271         char peer_confbase[PATH_MAX];
272         create_path(nut_confbase, NUT, 7);
273         create_path(peer_confbase, PEER, 7);
274
275         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
276         meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_OPEN, DEV_CLASS_BACKBONE);
277         assert_non_null(mesh);
278         meshlink_handle_t *mesh_peer = meshlink_open(peer_confbase, PEER, TEST_MESHLINK_OPEN, DEV_CLASS_STATIONARY);
279         assert_non_null(mesh_peer);
280
281         // Exporting and Importing mutually
282         char *export_data = meshlink_export(mesh);
283         assert_non_null(export_data);
284         assert_true(meshlink_import(mesh_peer, export_data));
285         free(export_data);
286         export_data = meshlink_export(mesh_peer);
287         assert_non_null(export_data);
288         assert_true(meshlink_import(mesh, export_data));
289         free(export_data);
290
291         meshlink_close(mesh);
292         meshlink_close(mesh_peer);
293
294
295         // Fork a new process in which NUT reopens it's instance and raises SIGINT to terminate.
296
297         pid = fork();
298         assert_int_not_equal(pid, -1);
299
300         if(!pid) {
301                 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
302                 meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_OPEN, DEV_CLASS_BACKBONE);
303                 assert(mesh);
304                 raise(SIGINT);
305         }
306
307         // Wait for child exit and verify which signal terminated it
308
309         assert_int_not_equal(waitpid(pid, &pid_status, 0), -1);
310         assert_int_equal(WIFSIGNALED(pid_status), true);
311         assert_int_equal(WTERMSIG(pid_status), SIGINT);
312
313         // Reopen the NUT instance in the same test suite
314
315         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
316         mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_OPEN, DEV_CLASS_STATIONARY);
317         assert_non_null(mesh);
318
319         // Validate parameters that were used to open meshlink instance.
320
321         assert_int_equal(strcmp(mesh->name, NUT), 0);
322         meshlink_node_t *self = meshlink_get_self(mesh);
323         assert_int_equal(strcmp(self->name, NUT), 0);
324         assert_int_equal(meshlink_get_node_dev_class(mesh, self), DEV_CLASS_STATIONARY);
325
326         // Cleanup
327
328         meshlink_close(mesh);
329         assert_true(meshlink_destroy(nut_confbase));
330         assert_true(meshlink_destroy(peer_confbase));
331         return true;
332 }
333
334 int test_meshlink_open(void) {
335         const struct CMUnitTest blackbox_open_tests[] = {
336                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_01, NULL, NULL,
337                                 (void *)&test_mesh_open_01_state),
338                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_02, NULL, NULL,
339                                 (void *)&test_mesh_open_02_state),
340                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_03, NULL, NULL,
341                                 (void *)&test_mesh_open_03_state),
342                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_04, NULL, NULL,
343                                 (void *)&test_mesh_open_04_state),
344                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_05, NULL, NULL,
345                                 (void *)&test_mesh_open_05_state),
346                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_06, NULL, NULL,
347                                 (void *)&test_mesh_open_06_state),
348                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_07, NULL, NULL,
349                                 (void *)&test_mesh_open_07_state)
350
351         };
352         total_tests += sizeof(blackbox_open_tests) / sizeof(blackbox_open_tests[0]);
353
354         return cmocka_run_group_tests(blackbox_open_tests, NULL, NULL);
355 }