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"
36 /* Modify this to change the logging level of Meshlink */
37 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
39 static void test_case_invite_01(void **state);
40 static bool test_invite_01(void);
41 static void test_case_invite_02(void **state);
42 static bool test_invite_02(void);
43 static void test_case_invite_03(void **state);
44 static bool test_invite_03(void);
45 static void test_case_invite_04(void **state);
46 static bool test_invite_04(void);
48 /* State structure for invite API Test Case #1 */
49 static black_box_state_t test_case_invite_01_state = {
50 .test_case_name = "test_case_invite_01",
53 /* State structure for invite API Test Case #2 */
54 static black_box_state_t test_case_invite_02_state = {
55 .test_case_name = "test_case_invite_02",
58 /* State structure for invite API Test Case #3 */
59 static black_box_state_t test_case_invite_03_state = {
60 .test_case_name = "test_case_invite_03",
63 /* State structure for invite API Test Case #4 */
64 static black_box_state_t test_case_invite_04_state = {
65 .test_case_name = "test_case_invite_04",
68 /* Execute invite Test Case # 1 - valid case*/
69 static void test_case_invite_01(void **state) {
70 execute_test(test_invite_01, state);
72 /*Test Steps for meshlink_invite Test Case # 1 - Valid case
78 Generates an invitation
80 static bool test_invite_01(void) {
81 assert(meshlink_destroy("inviteconf"));
82 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
84 // Create meshlink instance
85 meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "node_sim", 1);
87 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
89 char *invitation = meshlink_invite(mesh_handle, NULL, "new");
90 assert_int_equal(invitation, NULL);
93 meshlink_close(mesh_handle);
94 assert(meshlink_destroy("inviteconf"));
98 /* Execute invite Test Case # 2 - Invalid case*/
99 static void test_case_invite_02(void **state) {
100 execute_test(test_invite_02, state);
102 /*Test Steps for meshlink_invite Test Case # 2 - Invalid case
104 1. Calling meshlink_invite API with NULL as mesh handle argument
107 Reports appropriate error by returning NULL
109 static bool test_invite_02(void) {
110 // Trying to generate INVITATION by passing NULL as mesh link handle
111 char *invitation = meshlink_invite(NULL, NULL, "nut");
112 assert_int_equal(invitation, NULL);
117 /* Execute invite Test Case # 3 - Invalid case*/
118 static void test_case_invite_03(void **state) {
119 execute_test(test_invite_03, state);
121 /*Test Steps for meshlink_invite Test Case # 3 - Invalid case
124 2. Call meshlink_invite with NULL node name argument
127 Reports appropriate error by returning NULL
129 static bool test_invite_03(void) {
130 assert(meshlink_destroy("inviteconf"));
131 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
133 // Create meshlink instance
134 meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "node_sim", 1);
136 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
138 // Trying to generate INVITATION by passing NULL as mesh link handle
139 char *invitation = meshlink_invite(mesh_handle, NULL, NULL);
140 assert_int_equal(invitation, NULL);
142 meshlink_close(mesh_handle);
143 assert(meshlink_destroy("inviteconf"));
147 /* Execute invite Test Case # 4 - Functionality test*/
148 static void test_case_invite_04(void **state) {
149 execute_test(test_invite_04, state);
151 /*Test Steps for meshlink_invite Test Case # 4 - Functionality test
154 1. Create node instance
155 2. Add a new address to the mesh and invite a node
156 3. Add another new address and invite a node
159 Newly added address should be there in the invitation.
161 static bool test_invite_04(void) {
162 assert(meshlink_destroy("inviteconf"));
163 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
165 // Create meshlink instance
166 meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "test", 1);
168 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
170 char *hostname1 = "127.1.1.1";
171 assert(meshlink_add_address(mesh_handle, hostname1));
172 char *invitation = meshlink_invite(mesh_handle, NULL, "foo");
173 assert_int_not_equal(strstr(invitation, hostname1), NULL);
175 char *hostname2 = "127.1.2.3";
176 assert(meshlink_add_address(mesh_handle, hostname2));
177 invitation = meshlink_invite(mesh_handle, NULL, "bar");
179 // Verify we have both the added addresses
180 assert_int_not_equal(strstr(invitation, hostname1), NULL);
181 assert_int_not_equal(strstr(invitation, hostname2), NULL);
183 meshlink_close(mesh_handle);
184 assert(meshlink_destroy("inviteconf"));
189 int test_meshlink_invite(void) {
190 const struct CMUnitTest blackbox_invite_tests[] = {
191 cmocka_unit_test_prestate_setup_teardown(test_case_invite_01, NULL, NULL,
192 (void *)&test_case_invite_01_state),
193 cmocka_unit_test_prestate_setup_teardown(test_case_invite_02, NULL, NULL,
194 (void *)&test_case_invite_02_state),
195 cmocka_unit_test_prestate_setup_teardown(test_case_invite_03, NULL, NULL,
196 (void *)&test_case_invite_03_state),
197 cmocka_unit_test_prestate_setup_teardown(test_case_invite_04, NULL, NULL,
198 (void *)&test_case_invite_04_state)
201 total_tests += sizeof(blackbox_invite_tests) / sizeof(blackbox_invite_tests[0]);
203 return cmocka_run_group_tests(blackbox_invite_tests, NULL, NULL);