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 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
71 /* Test Steps for meshlink_channel_open Test Case # 1
74 1. Open both the node instances
75 2. Join bar node with foo
76 3. Open channel between the nodes
79 meshlink_channel_open should open a channel by returning a channel handler
81 static bool test_steps_mesh_channel_open_01(void) {
82 meshlink_destroy("channels_conf.1");
83 meshlink_destroy("channels_conf.2");
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);
91 // Import and export both side's data
92 char *exp = meshlink_export(mesh1);
94 assert(meshlink_import(mesh2, exp));
96 exp = meshlink_export(mesh2);
98 assert(meshlink_import(mesh1, exp));
101 // Start both instances
102 assert(meshlink_start(mesh1));
103 assert(meshlink_start(mesh2));
106 // Open a channel from foo to bar.
107 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
109 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7000, receive_cb, NULL, 0);
110 assert_int_not_equal(channel, NULL);
113 meshlink_close(mesh2);
114 meshlink_close(mesh1);
115 meshlink_destroy("channels_conf.1");
116 meshlink_destroy("channels_conf.2");
120 /* Execute meshlink_channel_open Test Case # 2
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
128 meshlink_channel_open should open a channel by returning a channel handler
130 static void test_case_mesh_channel_open_02(void **state) {
131 execute_test(test_steps_mesh_channel_open_02, state);
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");
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);
145 char *exp = meshlink_export(mesh1);
147 assert(meshlink_import(mesh2, exp));
149 exp = meshlink_export(mesh2);
151 assert(meshlink_import(mesh1, exp));
154 // Start both instances
155 assert(meshlink_start(mesh1));
156 assert(meshlink_start(mesh2));
159 // Open a channel from foo to bar.
161 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
164 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7000, NULL, NULL, 0);
165 assert_int_not_equal(channel, NULL);
168 meshlink_close(mesh2);
169 meshlink_close(mesh1);
170 meshlink_destroy("channels_conf.3");
171 meshlink_destroy("channels_conf.4");
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);
180 /* Test Steps for meshlink_channel_open Test Case # 3
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.
188 meshlink_channel_open API handles the invalid parameter
189 when called by giving proper error number.
191 static bool test_steps_mesh_channel_open_03(void) {
192 meshlink_destroy("channels_conf.5");
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);
198 meshlink_node_t *node = meshlink_get_self(mesh);
201 meshlink_channel_t *channel = meshlink_channel_open(NULL, node, 7000, NULL, NULL, 0);
202 assert_int_equal(channel, NULL);
205 meshlink_close(mesh);
206 meshlink_destroy("channels_conf.5");
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);
215 /* Test Steps for meshlink_channel_open Test Case # 4
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.
223 meshlink_channel_open API handles the invalid parameter
224 when called by giving proper error number.
226 static bool test_steps_mesh_channel_open_04(void) {
227 meshlink_destroy("channels_conf.7");
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);
233 // Start both instances
234 assert(meshlink_start(mesh));
236 meshlink_channel_t *channel = meshlink_channel_open(mesh, NULL, 7000, NULL, NULL, 0);
237 assert_int_equal(channel, NULL);
240 meshlink_close(mesh);
241 meshlink_destroy("channels_conf.7");
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)
257 total_tests += sizeof(blackbox_channel_open_tests) / sizeof(blackbox_channel_open_tests[0]);
259 return cmocka_run_group_tests(blackbox_channel_open_tests, NULL, NULL);