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.
20 #include "execute_tests.h"
21 #include "test_cases_invite.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_invite_01(void **state);
36 static bool test_invite_01(void);
37 static void test_case_invite_02(void **state);
38 static bool test_invite_02(void);
39 static void test_case_invite_03(void **state);
40 static bool test_invite_03(void);
41 static void test_case_invite_04(void **state);
42 static bool test_invite_04(void);
44 /* State structure for invite API Test Case #1 */
45 static black_box_state_t test_case_invite_01_state = {
46 .test_case_name = "test_case_invite_01",
49 /* State structure for invite API Test Case #2 */
50 static black_box_state_t test_case_invite_02_state = {
51 .test_case_name = "test_case_invite_02",
54 /* State structure for invite API Test Case #3 */
55 static black_box_state_t test_case_invite_03_state = {
56 .test_case_name = "test_case_invite_03",
59 /* State structure for invite API Test Case #4 */
60 static black_box_state_t test_case_invite_04_state = {
61 .test_case_name = "test_case_invite_04",
64 /* Execute invite Test Case # 1 - valid case*/
65 static void test_case_invite_01(void **state) {
66 execute_test(test_invite_01, state);
68 /*Test Steps for meshlink_invite Test Case # 1 - Valid case
74 Generates an invitation
76 static bool test_invite_01(void) {
77 meshlink_destroy("inviteconf");
78 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
80 // Create meshlink instance
81 meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "node_sim", 1);
83 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
85 char *invitation = meshlink_invite(mesh_handle, NULL, "new");
86 assert_int_equal(invitation, NULL);
89 meshlink_close(mesh_handle);
90 meshlink_destroy("inviteconf");
94 /* Execute invite Test Case # 2 - Invalid case*/
95 static void test_case_invite_02(void **state) {
96 execute_test(test_invite_02, state);
98 /*Test Steps for meshlink_invite Test Case # 2 - Invalid case
100 1. Calling meshlink_invite API with NULL as mesh handle argument
103 Reports appropriate error by returning NULL
105 static bool test_invite_02(void) {
106 // Trying to generate INVITATION by passing NULL as mesh link handle
107 char *invitation = meshlink_invite(NULL, NULL, "nut");
108 assert_int_equal(invitation, NULL);
113 /* Execute invite Test Case # 3 - Invalid case*/
114 static void test_case_invite_03(void **state) {
115 execute_test(test_invite_03, state);
117 /*Test Steps for meshlink_invite Test Case # 3 - Invalid case
120 2. Call meshlink_invite with NULL node name argument
123 Reports appropriate error by returning NULL
125 static bool test_invite_03(void) {
126 meshlink_destroy("inviteconf");
127 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
129 // Create meshlink instance
130 meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "node_sim", 1);
132 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
134 // Trying to generate INVITATION by passing NULL as mesh link handle
135 char *invitation = meshlink_invite(mesh_handle, NULL, NULL);
136 assert_int_equal(invitation, NULL);
138 meshlink_close(mesh_handle);
139 meshlink_destroy("inviteconf");
143 /* Execute invite Test Case # 4 - Functionality test*/
144 static void test_case_invite_04(void **state) {
145 execute_test(test_invite_04, state);
147 /*Test Steps for meshlink_invite Test Case # 4 - Functionality test
150 1. Create node instance
151 2. Add a new address to the mesh and invite a node
152 3. Add another new address and invite a node
155 Newly added address should be there in the invitation.
157 static bool test_invite_04(void) {
158 meshlink_destroy("inviteconf");
159 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
161 // Create meshlink instance
162 meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "test", 1);
164 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
166 char *hostname1 = "127.1.1.1";
167 bool ret = meshlink_add_address(mesh_handle, hostname1);
168 char *invitation = meshlink_invite(mesh_handle, NULL, "foo");
169 assert_int_not_equal(strstr(invitation, hostname1), NULL);
171 char *hostname2 = "127.1.2.3";
172 ret = meshlink_add_address(mesh_handle, hostname2);
173 invitation = meshlink_invite(mesh_handle, NULL, "bar");
175 // Verify we have both the added addresses
176 assert_int_not_equal(strstr(invitation, hostname1), NULL);
177 assert_int_not_equal(strstr(invitation, hostname2), NULL);
179 meshlink_close(mesh_handle);
180 meshlink_destroy("inviteconf");
185 int test_meshlink_invite(void) {
186 const struct CMUnitTest blackbox_invite_tests[] = {
187 cmocka_unit_test_prestate_setup_teardown(test_case_invite_01, NULL, NULL,
188 (void *)&test_case_invite_01_state),
189 cmocka_unit_test_prestate_setup_teardown(test_case_invite_02, NULL, NULL,
190 (void *)&test_case_invite_02_state),
191 cmocka_unit_test_prestate_setup_teardown(test_case_invite_03, NULL, NULL,
192 (void *)&test_case_invite_03_state),
193 cmocka_unit_test_prestate_setup_teardown(test_case_invite_04, NULL, NULL,
194 (void *)&test_case_invite_04_state)
197 total_tests += sizeof(blackbox_invite_tests) / sizeof(blackbox_invite_tests[0]);
199 return cmocka_run_group_tests(blackbox_invite_tests, NULL, NULL);