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.
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"
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);
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",
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",
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",
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",
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);
70 /* Test Steps for meshlink_channel_open Test Case # 1
73 1. Open both the node instances
74 2. Join bar node with foo
75 3. Open channel between the nodes
78 meshlink_channel_open should open a channel by returning a channel handler
80 static bool test_steps_mesh_channel_open_01(void) {
81 assert(meshlink_destroy("channels_conf.1"));
82 assert(meshlink_destroy("channels_conf.2"));
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);
90 // Import and export both side's data
91 char *exp = meshlink_export(mesh1);
93 assert(meshlink_import(mesh2, exp));
95 exp = meshlink_export(mesh2);
97 assert(meshlink_import(mesh1, exp));
100 // Start both instances
101 assert(meshlink_start(mesh1));
102 assert(meshlink_start(mesh2));
105 // Open a channel from foo to bar.
106 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
108 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7000, NULL, NULL, 0);
109 assert_int_not_equal(channel, NULL);
112 meshlink_close(mesh2);
113 meshlink_close(mesh1);
114 assert(meshlink_destroy("channels_conf.1"));
115 assert(meshlink_destroy("channels_conf.2"));
119 /* Execute meshlink_channel_open Test Case # 2
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
127 meshlink_channel_open should open a channel by returning a channel handler
129 static void test_case_mesh_channel_open_02(void **state) {
130 execute_test(test_steps_mesh_channel_open_02, state);
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"));
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);
144 char *exp = meshlink_export(mesh1);
146 assert(meshlink_import(mesh2, exp));
148 exp = meshlink_export(mesh2);
150 assert(meshlink_import(mesh1, exp));
153 // Start both instances
154 assert(meshlink_start(mesh1));
155 assert(meshlink_start(mesh2));
158 // Open a channel from foo to bar.
160 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
163 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7000, NULL, NULL, 0);
164 assert_int_not_equal(channel, NULL);
167 meshlink_close(mesh2);
168 meshlink_close(mesh1);
169 assert(meshlink_destroy("channels_conf.3"));
170 assert(meshlink_destroy("channels_conf.4"));
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);
179 /* Test Steps for meshlink_channel_open Test Case # 3
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.
187 meshlink_channel_open API handles the invalid parameter
188 when called by giving proper error number.
190 static bool test_steps_mesh_channel_open_03(void) {
191 assert(meshlink_destroy("channels_conf.5"));
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);
197 meshlink_node_t *node = meshlink_get_self(mesh);
200 meshlink_channel_t *channel = meshlink_channel_open(NULL, node, 7000, NULL, NULL, 0);
201 assert_int_equal(channel, NULL);
204 meshlink_close(mesh);
205 assert(meshlink_destroy("channels_conf.5"));
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);
214 /* Test Steps for meshlink_channel_open Test Case # 4
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.
222 meshlink_channel_open API handles the invalid parameter
223 when called by giving proper error number.
225 static bool test_steps_mesh_channel_open_04(void) {
226 assert(meshlink_destroy("channels_conf.7"));
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);
232 // Start both instances
233 assert(meshlink_start(mesh));
235 meshlink_channel_t *channel = meshlink_channel_open(mesh, NULL, 7000, NULL, NULL, 0);
236 assert_int_equal(channel, NULL);
239 meshlink_close(mesh);
240 assert(meshlink_destroy("channels_conf.7"));
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)
256 total_tests += sizeof(blackbox_channel_open_tests) / sizeof(blackbox_channel_open_tests[0]);
258 return cmocka_run_group_tests(blackbox_channel_open_tests, NULL, NULL);