2 test_cases_open.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_open.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include "../../utils.h"
37 #include <linux/limits.h>
39 /* Modify this to change the logging level of Meshlink */
40 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
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)
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);
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",
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",
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",
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",
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",
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",
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",
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);
102 /* Test Steps for meshlink_open Test Case # 1
105 1. Open the node instance using meshlink_open
108 meshlink_open API should successfully return a mesh handle.
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);
115 meshlink_close(mesh);
116 assert(meshlink_destroy("open_conf"));
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);
125 /* Test Steps for meshlink_open Test Case # 2
128 1. Open the node instance using meshlink_open with NULL as confbase argument
131 meshlink_open API should successfully report error by returning NULL pointer
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);
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);
146 /* Test Steps for meshlink_open Test Case # 3
149 1. Open the node instance using meshlink_open with NULL as node name argument
152 meshlink_open API should successfully report error by returning NULL pointer
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);
159 assert(meshlink_destroy("open_conf"));
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);
168 /* Test Steps for meshlink_open Test Case # 4
171 1. Open the node instance using meshlink_open with NULL as app name argument
174 meshlink_open API should successfully report error by returning NULL pointer
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);
181 assert(meshlink_destroy("open_conf"));
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);
190 /* Test Steps for meshlink_open Test Case # 5
193 1. Open the node instance using meshlink_open with invalid device class argument
196 meshlink_open API should successfully report error by returning NULL pointer
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);
203 assert(meshlink_destroy("open_conf"));
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.
210 static void test_case_mesh_open_06(void **state) {
211 execute_test(test_steps_mesh_open_06, state);
214 static bool test_steps_mesh_open_06(void) {
218 char nut_confbase[PATH_MAX];
219 create_path(nut_confbase, NUT, 6);
221 // Fork a new process in which NUT opens it's instance and raises SIGINT to terminate.
224 assert_int_not_equal(pid, -1);
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);
233 // Wait for child exit and verify which signal terminated it
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);
239 // Reopen the NUT instance in the same test suite
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);
245 // Validate parameters that were used to open meshlink instance.
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);
254 meshlink_close(mesh);
255 assert_true(meshlink_destroy(nut_confbase));
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.
262 static void test_case_mesh_open_07(void **state) {
263 execute_test(test_steps_mesh_open_07, state);
266 static bool test_steps_mesh_open_07(void) {
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);
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);
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));
286 export_data = meshlink_export(mesh_peer);
287 assert_non_null(export_data);
288 assert_true(meshlink_import(mesh, export_data));
291 meshlink_close(mesh);
292 meshlink_close(mesh_peer);
295 // Fork a new process in which NUT reopens it's instance and raises SIGINT to terminate.
298 assert_int_not_equal(pid, -1);
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);
307 // Wait for child exit and verify which signal terminated it
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);
313 // Reopen the NUT instance in the same test suite
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);
319 // Validate parameters that were used to open meshlink instance.
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);
328 meshlink_close(mesh);
329 assert_true(meshlink_destroy(nut_confbase));
330 assert_true(meshlink_destroy(peer_confbase));
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)
352 total_tests += sizeof(blackbox_open_tests) / sizeof(blackbox_open_tests[0]);
354 return cmocka_run_group_tests(blackbox_open_tests, NULL, NULL);