2 test_cases_channel_open.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_channel_open.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
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);
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",
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",
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",
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",
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);
66 /* Test Steps for meshlink_channel_open Test Case # 1
69 1. Open both the node instances
70 2. Join bar node with foo
71 3. Open channel between the nodes
74 meshlink_channel_open should open a channel by returning a channel handler
76 static bool test_steps_mesh_channel_open_01(void) {
77 meshlink_destroy("channels_conf.1");
78 meshlink_destroy("channels_conf.2");
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);
86 // Import and export both side's data
87 char *exp = meshlink_export(mesh1);
89 assert(meshlink_import(mesh2, exp));
91 exp = meshlink_export(mesh2);
93 assert(meshlink_import(mesh1, exp));
96 // Start both instances
97 assert(meshlink_start(mesh1));
98 assert(meshlink_start(mesh2));
101 // Open a channel from foo to bar.
102 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
104 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7000, NULL, NULL, 0);
105 assert_int_not_equal(channel, NULL);
108 meshlink_close(mesh2);
109 meshlink_close(mesh1);
110 meshlink_destroy("channels_conf.1");
111 meshlink_destroy("channels_conf.2");
115 /* Execute meshlink_channel_open Test Case # 2
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
123 meshlink_channel_open should open a channel by returning a channel handler
125 static void test_case_mesh_channel_open_02(void **state) {
126 execute_test(test_steps_mesh_channel_open_02, state);
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");
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);
140 char *exp = meshlink_export(mesh1);
142 assert(meshlink_import(mesh2, exp));
144 exp = meshlink_export(mesh2);
146 assert(meshlink_import(mesh1, exp));
149 // Start both instances
150 assert(meshlink_start(mesh1));
151 assert(meshlink_start(mesh2));
154 // Open a channel from foo to bar.
156 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
159 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7000, NULL, NULL, 0);
160 assert_int_not_equal(channel, NULL);
163 meshlink_close(mesh2);
164 meshlink_close(mesh1);
165 meshlink_destroy("channels_conf.3");
166 meshlink_destroy("channels_conf.4");
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);
175 /* Test Steps for meshlink_channel_open Test Case # 3
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.
183 meshlink_channel_open API handles the invalid parameter
184 when called by giving proper error number.
186 static bool test_steps_mesh_channel_open_03(void) {
187 meshlink_destroy("channels_conf.5");
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);
193 meshlink_node_t *node = meshlink_get_self(mesh);
196 meshlink_channel_t *channel = meshlink_channel_open(NULL, node, 7000, NULL, NULL, 0);
197 assert_int_equal(channel, NULL);
200 meshlink_close(mesh);
201 meshlink_destroy("channels_conf.5");
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);
210 /* Test Steps for meshlink_channel_open Test Case # 4
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.
218 meshlink_channel_open API handles the invalid parameter
219 when called by giving proper error number.
221 static bool test_steps_mesh_channel_open_04(void) {
222 meshlink_destroy("channels_conf.7");
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);
228 // Start both instances
229 assert(meshlink_start(mesh));
231 meshlink_channel_t *channel = meshlink_channel_open(mesh, NULL, 7000, NULL, NULL, 0);
232 assert_int_equal(channel, NULL);
235 meshlink_close(mesh);
236 meshlink_destroy("channels_conf.7");
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)
252 total_tests += sizeof(blackbox_channel_open_tests) / sizeof(blackbox_channel_open_tests[0]);
254 return cmocka_run_group_tests(blackbox_channel_open_tests, NULL, NULL);