]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_invite.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_invite.c
1 /*
2     test_cases_invite.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_invite.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include <assert.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <setjmp.h>
34 #include <cmocka.h>
35
36 /* Modify this to change the logging level of Meshlink */
37 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
38
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);
47
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",
51 };
52
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",
56 };
57
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",
61 };
62
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",
66 };
67
68 /* Execute invite Test Case # 1 - valid case*/
69 static void test_case_invite_01(void **state) {
70         execute_test(test_invite_01, state);
71 }
72 /*Test Steps for meshlink_invite Test Case # 1 - Valid case
73     Test Steps:
74     1. Run NUT
75     2. Invite 'new' node
76
77     Expected Result:
78     Generates an invitation
79 */
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);
83
84         // Create meshlink instance
85         meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "node_sim", 1);
86         assert(mesh_handle);
87         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
88
89         char *invitation = meshlink_invite(mesh_handle, NULL, "new");
90         assert_int_equal(invitation, NULL);
91
92         free(invitation);
93         meshlink_close(mesh_handle);
94         assert(meshlink_destroy("inviteconf"));
95         return true;
96 }
97
98 /* Execute invite Test Case # 2 - Invalid case*/
99 static void test_case_invite_02(void **state) {
100         execute_test(test_invite_02, state);
101 }
102 /*Test Steps for meshlink_invite Test Case # 2 - Invalid case
103     Test Steps:
104     1. Calling meshlink_invite API with NULL as mesh handle argument
105
106     Expected Result:
107     Reports appropriate error by returning NULL
108 */
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);
113
114         return true;
115 }
116
117 /* Execute invite Test Case # 3 - Invalid case*/
118 static void test_case_invite_03(void **state) {
119         execute_test(test_invite_03, state);
120 }
121 /*Test Steps for meshlink_invite Test Case # 3 - Invalid case
122     Test Steps:
123     1. Run NUT
124     2. Call meshlink_invite with NULL node name argument
125
126     Expected Result:
127     Reports appropriate error by returning NULL
128 */
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);
132
133         // Create meshlink instance
134         meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "node_sim", 1);
135         assert(mesh_handle);
136         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
137
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);
141
142         meshlink_close(mesh_handle);
143         assert(meshlink_destroy("inviteconf"));
144         return true;
145 }
146
147 /* Execute invite Test Case # 4 - Functionality test*/
148 static void test_case_invite_04(void **state) {
149         execute_test(test_invite_04, state);
150 }
151 /*Test Steps for meshlink_invite Test Case # 4 - Functionality test
152
153     Test Steps:
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
157
158     Expected Result:
159     Newly added address should be there in the invitation.
160 */
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);
164
165         // Create meshlink instance
166         meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "test", 1);
167         assert(mesh_handle);
168         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
169
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);
174
175         char *hostname2 = "127.1.2.3";
176         assert(meshlink_add_address(mesh_handle, hostname2));
177         invitation = meshlink_invite(mesh_handle, NULL, "bar");
178
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);
182
183         meshlink_close(mesh_handle);
184         assert(meshlink_destroy("inviteconf"));
185
186         return true;
187 }
188
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)
199         };
200
201         total_tests += sizeof(blackbox_invite_tests) / sizeof(blackbox_invite_tests[0]);
202
203         return cmocka_run_group_tests(blackbox_invite_tests, NULL, NULL);
204 }