]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_channel_open.c
Feature external ip address
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_channel_open.c
1 /*
2     test_cases_channel_open.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_channel_open.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <setjmp.h>
32 #include <cmocka.h>
33 #include <assert.h>
34 #include <string.h>
35
36 static void test_case_mesh_channel_open_01(void **state);
37 static bool test_steps_mesh_channel_open_01(void);
38 static void test_case_mesh_channel_open_02(void **state);
39 static bool test_steps_mesh_channel_open_02(void);
40 static void test_case_mesh_channel_open_03(void **state);
41 static bool test_steps_mesh_channel_open_03(void);
42 static void test_case_mesh_channel_open_04(void **state);
43 static bool test_steps_mesh_channel_open_04(void);
44
45 /* State structure for meshlink_channel_open Test Case #1 */
46 static black_box_state_t test_mesh_channel_open_01_state = {
47         .test_case_name = "test_case_mesh_channel_open_01",
48 };
49
50 /* State structure for meshlink_channel_open Test Case #2 */
51 static black_box_state_t test_mesh_channel_open_02_state = {
52         .test_case_name = "test_case_mesh_channel_open_02",
53 };
54
55 /* State structure for meshlink_channel_open Test Case #3 */
56 static black_box_state_t test_mesh_channel_open_03_state = {
57         .test_case_name = "test_case_mesh_channel_open_03",
58 };
59
60 /* State structure for meshlink_channel_open Test Case #4 */
61 static black_box_state_t test_mesh_channel_open_04_state = {
62         .test_case_name = "test_case_mesh_channel_open_04",
63 };
64
65 /* Execute meshlink_channel_open Test Case # 1*/
66 static void test_case_mesh_channel_open_01(void **state) {
67         execute_test(test_steps_mesh_channel_open_01, state);
68 }
69
70 /* Test Steps for meshlink_channel_open Test Case # 1
71
72     Test Steps:
73     1. Open both the node instances
74     2. Join bar node with foo
75     3. Open channel between the nodes
76
77     Expected Result:
78     meshlink_channel_open should open a channel by returning a channel handler
79 */
80 static bool test_steps_mesh_channel_open_01(void) {
81         assert(meshlink_destroy("channels_conf.1"));
82         assert(meshlink_destroy("channels_conf.2"));
83
84         // Open two new meshlink instance.
85         meshlink_handle_t *mesh1 = meshlink_open("channels_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
86         assert(mesh1 != NULL);
87         meshlink_handle_t *mesh2 = meshlink_open("channels_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
88         assert(mesh2 != NULL);
89
90         // Import and export both side's data
91         char *exp = meshlink_export(mesh1);
92         assert(exp != NULL);
93         assert(meshlink_import(mesh2, exp));
94         free(exp);
95         exp = meshlink_export(mesh2);
96         assert(exp != NULL);
97         assert(meshlink_import(mesh1, exp));
98         free(exp);
99
100         // Start both instances
101         assert(meshlink_start(mesh1));
102         assert(meshlink_start(mesh2));
103         sleep(2);
104
105         // Open a channel from foo to bar.
106         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
107         assert(bar != NULL);
108         meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7000, NULL, NULL, 0);
109         assert_int_not_equal(channel, NULL);
110
111         // Clean up.
112         meshlink_close(mesh2);
113         meshlink_close(mesh1);
114         assert(meshlink_destroy("channels_conf.1"));
115         assert(meshlink_destroy("channels_conf.2"));
116         return true;
117 }
118
119 /* Execute meshlink_channel_open Test Case # 2
120
121     Test Steps:
122     1. Open both the node instances
123     2. Join bar node with foo
124     3. Open channel between the nodes with NULL as receive callback argument
125
126     Expected Result:
127     meshlink_channel_open should open a channel by returning a channel handler
128 */
129 static void test_case_mesh_channel_open_02(void **state) {
130         execute_test(test_steps_mesh_channel_open_02, state);
131 }
132
133 /* Test Steps for meshlink_channel_open Test Case # 2*/
134 static bool test_steps_mesh_channel_open_02(void) {
135         assert(meshlink_destroy("channels_conf.3"));
136         assert(meshlink_destroy("channels_conf.4"));
137
138         // Open two new meshlink instance.
139         meshlink_handle_t *mesh1 = meshlink_open("channels_conf.3", "foo", "channels", DEV_CLASS_BACKBONE);
140         assert(mesh1 != NULL);
141         meshlink_handle_t *mesh2 = meshlink_open("channels_conf.4", "bar", "channels", DEV_CLASS_BACKBONE);
142         assert(mesh2 != NULL);
143
144         char *exp = meshlink_export(mesh1);
145         assert(exp != NULL);
146         assert(meshlink_import(mesh2, exp));
147         free(exp);
148         exp = meshlink_export(mesh2);
149         assert(exp != NULL);
150         assert(meshlink_import(mesh1, exp));
151         free(exp);
152
153         // Start both instances
154         assert(meshlink_start(mesh1));
155         assert(meshlink_start(mesh2));
156         sleep(1);
157
158         // Open a channel from foo to bar.
159
160         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
161         assert(bar != NULL);
162
163         meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7000, NULL, NULL, 0);
164         assert_int_not_equal(channel, NULL);
165
166         // Clean up.
167         meshlink_close(mesh2);
168         meshlink_close(mesh1);
169         assert(meshlink_destroy("channels_conf.3"));
170         assert(meshlink_destroy("channels_conf.4"));
171         return true;
172 }
173
174 /* Execute meshlink_channel_open Test Case # 3 */
175 static void test_case_mesh_channel_open_03(void **state) {
176         execute_test(test_steps_mesh_channel_open_03, state);
177 }
178
179 /* Test Steps for meshlink_channel_open Test Case # 3
180
181     Test Steps:
182     1. Create the node instance & obtain node handle
183     2. Open a channel with NULL as mesh handle argument
184         and rest other arguments being valid.
185
186     Expected Result:
187     meshlink_channel_open API handles the invalid parameter
188     when called by giving proper error number.
189 */
190 static bool test_steps_mesh_channel_open_03(void) {
191         assert(meshlink_destroy("channels_conf.5"));
192
193         // Open two new meshlink instance.
194         meshlink_handle_t *mesh = meshlink_open("channels_conf.5", "foo", "channels", DEV_CLASS_BACKBONE);
195         assert(mesh != NULL);
196
197         meshlink_node_t *node = meshlink_get_self(mesh);
198         assert(node);
199
200         meshlink_channel_t *channel = meshlink_channel_open(NULL, node, 7000, NULL, NULL, 0);
201         assert_int_equal(channel, NULL);
202
203         // Clean up.
204         meshlink_close(mesh);
205         assert(meshlink_destroy("channels_conf.5"));
206         return true;
207 }
208
209 /* Execute meshlink_channel_open Test Case # 4*/
210 static void test_case_mesh_channel_open_04(void **state) {
211         execute_test(test_steps_mesh_channel_open_04, state);
212 }
213
214 /* Test Steps for meshlink_channel_open Test Case # 4
215
216     Test Steps:
217     1. Create the node instance & obtain node handle
218     2. Open a channel with NULL as node handle argument
219         and rest other arguments being valid.
220
221     Expected Result:
222     meshlink_channel_open API handles the invalid parameter
223     when called by giving proper error number.
224 */
225 static bool test_steps_mesh_channel_open_04(void) {
226         assert(meshlink_destroy("channels_conf.7"));
227
228         // Open two new meshlink instance.
229         meshlink_handle_t *mesh = meshlink_open("channels_conf.7", "foo", "channels", DEV_CLASS_BACKBONE);
230         assert(mesh != NULL);
231
232         // Start both instances
233         assert(meshlink_start(mesh));
234
235         meshlink_channel_t *channel = meshlink_channel_open(mesh, NULL, 7000, NULL, NULL, 0);
236         assert_int_equal(channel, NULL);
237
238         // Clean up.
239         meshlink_close(mesh);
240         assert(meshlink_destroy("channels_conf.7"));
241         return true;
242 }
243
244 int test_meshlink_channel_open(void) {
245         const struct CMUnitTest blackbox_channel_open_tests[] = {
246                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_open_01, NULL, NULL,
247                                 (void *)&test_mesh_channel_open_01_state),
248                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_open_02, NULL, NULL,
249                                 (void *)&test_mesh_channel_open_02_state),
250                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_open_03, NULL, NULL,
251                                 (void *)&test_mesh_channel_open_03_state),
252                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_open_04, NULL, NULL,
253                                 (void *)&test_mesh_channel_open_04_state)
254         };
255
256         total_tests += sizeof(blackbox_channel_open_tests) / sizeof(blackbox_channel_open_tests[0]);
257
258         return cmocka_run_group_tests(blackbox_channel_open_tests, NULL, NULL);
259 }