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