2 test_cases_blacklist.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 /* Modify this to change the logging level of Meshlink */
25 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
27 #include "execute_tests.h"
28 #include "test_cases_blacklist.h"
29 #include "../common/containers.h"
30 #include "../common/test_step.h"
31 #include "../common/common_handlers.h"
39 static void test_case_mesh_blacklist_01(void **state);
40 static bool test_steps_mesh_blacklist_01(void);
41 static void test_case_mesh_blacklist_02(void **state);
42 static bool test_steps_mesh_blacklist_02(void);
43 static void test_case_mesh_blacklist_03(void **state);
44 static bool test_steps_mesh_blacklist_03(void);
46 /* State structure for meshlink_blacklist Test Case #1 */
47 static black_box_state_t test_mesh_blacklist_01_state = {
48 .test_case_name = "test_case_mesh_blacklist_01",
51 /* State structure for meshlink_blacklist Test Case #2 */
52 static black_box_state_t test_mesh_blacklist_02_state = {
53 .test_case_name = "test_case_mesh_blacklist_02",
56 /* State structure for meshlink_blacklist Test Case #3 */
57 static black_box_state_t test_mesh_blacklist_03_state = {
58 .test_case_name = "test_case_mesh_blacklist_03",
61 /* Execute meshlink_blacklist Test Case # 1*/
62 void test_case_mesh_blacklist_01(void **state) {
63 execute_test(test_steps_mesh_blacklist_01, state);
68 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
71 const char *msg = data;
74 if(!strcmp(src->name, "bar") && len == 5 && !strcmp(msg, "test")) {
79 static bool bar_reachable;
81 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
84 if(!strcmp(node->name, "bar") && reachable) {
90 /* Test Steps for meshlink_blacklist Test Case # 1
93 1. Open both the node instances
94 2. Join bar node with foo and Send & Receive data
95 3. Blacklist bar and Send & Receive data
98 When default blacklist is disabled, foo node should receive data from bar
99 but when enabled foo node should not receive data
101 bool test_steps_mesh_blacklist_01(void) {
102 assert(meshlink_destroy("blacklist_conf.1"));
103 assert(meshlink_destroy("blacklist_conf.2"));
105 // Open two new meshlink instance.
106 meshlink_handle_t *mesh1 = meshlink_open("blacklist_conf.1", "foo", "blacklist", DEV_CLASS_BACKBONE);
107 assert(mesh1 != NULL);
108 meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
109 meshlink_handle_t *mesh2 = meshlink_open("blacklist_conf.2", "bar", "blacklist", DEV_CLASS_BACKBONE);
110 assert(mesh2 != NULL);
111 meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
112 meshlink_set_receive_cb(mesh1, receive);
114 // Start both instances
115 bar_reachable = false;
116 meshlink_set_node_status_cb(mesh1, status_cb);
117 assert(meshlink_start(mesh1));
118 assert(meshlink_start(mesh2));
121 char *foo_export = meshlink_export(mesh1);
122 assert(foo_export != NULL);
123 assert(meshlink_import(mesh2, foo_export));
124 char *bar_export = meshlink_export(mesh2);
125 assert(meshlink_import(mesh1, bar_export));
127 assert_int_equal(bar_reachable, true);
129 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
131 meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
135 assert(meshlink_send(mesh2, foo, "test", 5));
139 assert(meshlink_blacklist(mesh1, bar));
142 assert(meshlink_send(mesh2, foo, "test", 5));
144 assert_int_equal(received, false);
147 meshlink_close(mesh2);
148 meshlink_close(mesh1);
149 assert(meshlink_destroy("blacklist_conf.1"));
150 assert(meshlink_destroy("blacklist_conf.2"));
154 /* Execute meshlink_blacklist Test Case # 2*/
155 void test_case_mesh_blacklist_02(void **state) {
156 execute_test(test_steps_mesh_blacklist_02, state);
160 /* Test Steps for meshlink_blacklist Test Case # 2
163 1. Calling meshlink_blacklist with NULL as mesh handle argument.
166 meshlink_blacklist API handles the invalid parameter when called by giving proper error number.
168 bool test_steps_mesh_blacklist_02(void) {
169 assert(meshlink_destroy("blacklist_conf.3"));
171 // Open two new meshlink instance.
172 meshlink_handle_t *mesh = meshlink_open("blacklist_conf.3", "foo", "blacklist", DEV_CLASS_BACKBONE);
173 assert(mesh != NULL);
175 meshlink_node_t *node = meshlink_get_self(mesh);
178 // Passing NULL as mesh handle and node handle being some valid node handle
179 assert(!meshlink_blacklist(NULL, node));
180 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
183 meshlink_close(mesh);
184 assert(meshlink_destroy("blacklist_conf.3"));
188 /* Execute meshlink_blacklist Test Case # 3*/
189 void test_case_mesh_blacklist_03(void **state) {
190 execute_test(test_steps_mesh_blacklist_03, state);
193 /* Test Steps for meshlink_blacklist Test Case # 3
196 1. Create node instance
197 2. Calling meshlink_blacklist with NULL as node handle argument.
200 meshlink_blacklist API handles the invalid parameter when called by giving proper error number.
202 bool test_steps_mesh_blacklist_03(void) {
203 assert(meshlink_destroy("blacklist_conf.4"));
205 // Open two new meshlink instance.
206 meshlink_handle_t *mesh = meshlink_open("blacklist_conf.4", "foo", "blacklist", DEV_CLASS_BACKBONE);
207 assert(mesh != NULL);
209 // Passing NULL as node handle and mesh handle being some valid mesh handle value
210 assert(!meshlink_blacklist(mesh, NULL));
211 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
214 meshlink_close(mesh);
215 assert(meshlink_destroy("blacklist_conf.4"));
219 int test_meshlink_blacklist(void) {
220 const struct CMUnitTest blackbox_blacklist_tests[] = {
221 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_01, NULL, NULL,
222 (void *)&test_mesh_blacklist_01_state),
223 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_02, NULL, NULL,
224 (void *)&test_mesh_blacklist_02_state),
225 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_03, NULL, NULL,
226 (void *)&test_mesh_blacklist_03_state)
229 total_tests += sizeof(blackbox_blacklist_tests) / sizeof(blackbox_blacklist_tests[0]);
231 return cmocka_run_group_tests(blackbox_blacklist_tests, NULL, NULL);