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