2 test_cases_invite.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_invite.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_INVITE "test_invite"
45 #define create_path(confbase, node_name, test_case_no) assert(snprintf(confbase, sizeof(confbase), TEST_MESHLINK_INVITE "_%ld_%s_%02d", (long) getpid(), node_name, test_case_no) > 0)
47 static void test_case_invite_01(void **state);
48 static bool test_invite_01(void);
49 static void test_case_invite_02(void **state);
50 static bool test_invite_02(void);
51 static void test_case_invite_03(void **state);
52 static bool test_invite_03(void);
53 static void test_case_invite_04(void **state);
54 static bool test_invite_04(void);
55 static void test_case_invite_05(void **state);
56 static bool test_invite_05(void);
58 /* State structure for invite API Test Case #1 */
59 static black_box_state_t test_case_invite_01_state = {
60 .test_case_name = "test_case_invite_01",
63 /* State structure for invite API Test Case #2 */
64 static black_box_state_t test_case_invite_02_state = {
65 .test_case_name = "test_case_invite_02",
68 /* State structure for invite API Test Case #3 */
69 static black_box_state_t test_case_invite_03_state = {
70 .test_case_name = "test_case_invite_03",
73 /* State structure for invite API Test Case #4 */
74 static black_box_state_t test_case_invite_04_state = {
75 .test_case_name = "test_case_invite_04",
78 /* State structure for invite API Test Case #5 */
79 static black_box_state_t test_case_invite_05_state = {
80 .test_case_name = "test_case_invite_05",
83 /* Execute invite Test Case # 1 - valid case*/
84 static void test_case_invite_01(void **state) {
85 execute_test(test_invite_01, state);
87 /*Test Steps for meshlink_invite Test Case # 1 - Valid case
93 Generates an invitation
95 static bool test_invite_01(void) {
96 char nut_confbase[PATH_MAX];
97 char peer_invitation[1000];
98 create_path(nut_confbase, NUT, 1);
100 // Create meshlink instance
102 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
103 meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY);
104 assert_non_null(mesh);
106 char *invitation = meshlink_invite(mesh, NULL, "new");
107 assert_non_null(invitation);
110 meshlink_close(mesh);
111 assert_true(meshlink_destroy(nut_confbase));
115 /* Execute invite Test Case # 2 - Invalid case*/
116 static void test_case_invite_02(void **state) {
117 execute_test(test_invite_02, state);
119 /*Test Steps for meshlink_invite Test Case # 2 - Invalid case
121 1. Calling meshlink_invite API with NULL as mesh handle argument
124 Reports appropriate error by returning NULL
126 static bool test_invite_02(void) {
127 // Trying to generate INVITATION by passing NULL as mesh link handle
128 char *invitation = meshlink_invite(NULL, NULL, "nut");
129 assert_int_equal(invitation, NULL);
134 /* Execute invite Test Case # 3 - Invalid case*/
135 static void test_case_invite_03(void **state) {
136 execute_test(test_invite_03, state);
138 /*Test Steps for meshlink_invite Test Case # 3 - Invalid case
141 2. Call meshlink_invite with NULL node name argument
144 Reports appropriate error by returning NULL
146 static bool test_invite_03(void) {
147 char nut_confbase[PATH_MAX];
148 char peer_invitation[1000];
149 create_path(nut_confbase, NUT, 3);
151 // Create meshlink instance
153 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
154 meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY);
155 assert_non_null(mesh);
157 char *invitation = meshlink_invite(mesh, NULL, NULL);
158 assert_int_equal(invitation, NULL);
161 meshlink_close(mesh);
162 assert_true(meshlink_destroy(nut_confbase));
166 /* Execute invite Test Case # 4 - Functionality test*/
167 static void test_case_invite_04(void **state) {
168 execute_test(test_invite_04, state);
170 /*Test Steps for meshlink_invite Test Case # 4 - Functionality test
173 1. Create node instance
174 2. Add a new address to the mesh and invite a node
175 3. Add another new address and invite a node
178 Newly added address should be there in the invitation.
180 static bool test_invite_04(void) {
181 char nut_confbase[PATH_MAX];
182 char peer_invitation[1000];
183 create_path(nut_confbase, NUT, 4);
185 // Create meshlink instance
187 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
188 meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY);
189 assert_non_null(mesh);
191 assert_true(meshlink_add_invitation_address(mesh, "11.11.11.11", "2020"));
192 char *invitation = meshlink_invite(mesh, NULL, "foo");
193 assert_non_null(strstr(invitation, "11.11.11.11:2020"));
196 assert_true(meshlink_add_invitation_address(mesh, "fe80::1548:d713:3899:f645", "3030"));
197 invitation = meshlink_invite(mesh, NULL, "bar");
198 assert_non_null(strstr(invitation, "11.11.11.11:2020"));
199 assert_non_null(strstr(invitation, "[fe80::1548:d713:3899:f645]:3030"));
202 meshlink_close(mesh);
203 assert_true(meshlink_destroy(nut_confbase));
207 /* Execute invite Test Case # 5 - Synchronization testing */
208 static void test_case_invite_05(void **state) {
209 execute_test(test_invite_05, state);
212 static bool test_invite_05(void) {
217 char nut_confbase[PATH_MAX];
218 char peer_confbase[PATH_MAX];
219 char peer_invitation[1000];
220 create_path(nut_confbase, NUT, 5);
221 create_path(peer_confbase, PEER, 5);
223 assert_int_not_equal(pipe(pipefd), -1);
225 // Fork a new process in which NUT opens it's instance and raises SIGINT to terminate.
228 assert_int_not_equal(pid, -1);
231 assert(!close(pipefd[0]));
232 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
233 meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY);
236 char *invitation = meshlink_invite(mesh, NULL, PEER);
237 write(pipefd[1], invitation, strlen(invitation) + 1);
242 // Wait for child exit and verify which signal terminated it
244 assert_int_not_equal(waitpid(pid, &pid_status, 0), -1);
245 assert_int_equal(WIFSIGNALED(pid_status), true);
246 assert_int_equal(WTERMSIG(pid_status), SIGINT);
248 assert_int_equal(close(pipefd[1]), 0);
249 assert_int_not_equal(read(pipefd[0], peer_invitation, sizeof(peer_invitation)), -1);
251 // Reopen the NUT instance in the same test suite
253 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
254 meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY);
255 assert_non_null(mesh);
256 meshlink_handle_t *mesh_peer = meshlink_open(peer_confbase, PEER, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY);
257 assert_non_null(mesh);
258 assert_true(meshlink_start(mesh));
259 assert_true(meshlink_join(mesh_peer, peer_invitation));
263 meshlink_close(mesh);
264 meshlink_close(mesh_peer);
265 assert_true(meshlink_destroy(nut_confbase));
266 assert_true(meshlink_destroy(peer_confbase));
270 int test_meshlink_invite(void) {
271 const struct CMUnitTest blackbox_invite_tests[] = {
272 cmocka_unit_test_prestate_setup_teardown(test_case_invite_01, NULL, NULL,
273 (void *)&test_case_invite_01_state),
274 cmocka_unit_test_prestate_setup_teardown(test_case_invite_02, NULL, NULL,
275 (void *)&test_case_invite_02_state),
276 cmocka_unit_test_prestate_setup_teardown(test_case_invite_03, NULL, NULL,
277 (void *)&test_case_invite_03_state),
278 cmocka_unit_test_prestate_setup_teardown(test_case_invite_04, NULL, NULL,
279 (void *)&test_case_invite_04_state),
280 cmocka_unit_test_prestate_setup_teardown(test_case_invite_05, NULL, NULL,
281 (void *)&test_case_invite_05_state)
284 total_tests += sizeof(blackbox_invite_tests) / sizeof(blackbox_invite_tests[0]);
286 return cmocka_run_group_tests(blackbox_invite_tests, NULL, NULL);