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.
20 /* Modify this to change the logging level of Meshlink */
21 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
23 #include "execute_tests.h"
24 #include "test_cases_blacklist.h"
25 #include "../common/containers.h"
26 #include "../common/test_step.h"
27 #include "../common/common_handlers.h"
35 static void test_case_mesh_blacklist_01(void **state);
36 static bool test_steps_mesh_blacklist_01(void);
37 static void test_case_mesh_blacklist_02(void **state);
38 static bool test_steps_mesh_blacklist_02(void);
39 static void test_case_mesh_blacklist_03(void **state);
40 static bool test_steps_mesh_blacklist_03(void);
42 /* State structure for meshlink_blacklist Test Case #1 */
43 static black_box_state_t test_mesh_blacklist_01_state = {
44 .test_case_name = "test_case_mesh_blacklist_01",
47 /* State structure for meshlink_blacklist Test Case #2 */
48 static black_box_state_t test_mesh_blacklist_02_state = {
49 .test_case_name = "test_case_mesh_blacklist_02",
52 /* State structure for meshlink_blacklist Test Case #3 */
53 static black_box_state_t test_mesh_blacklist_03_state = {
54 .test_case_name = "test_case_mesh_blacklist_03",
57 /* Execute meshlink_blacklist Test Case # 1*/
58 void test_case_mesh_blacklist_01(void **state) {
59 execute_test(test_steps_mesh_blacklist_01, state);
64 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
67 const char *msg = data;
70 if(!strcmp(src->name, "bar") && len == 5 && !strcmp(msg, "test")) {
75 static bool bar_reachable;
77 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
80 if(!strcmp(node->name, "bar") && reachable) {
86 /* Test Steps for meshlink_blacklist Test Case # 1
89 1. Open both the node instances
90 2. Join bar node with foo and Send & Receive data
91 3. Blacklist bar and Send & Receive data
94 When default blacklist is disabled, foo node should receive data from bar
95 but when enabled foo node should not receive data
97 bool test_steps_mesh_blacklist_01(void) {
98 meshlink_destroy("blacklist_conf.1");
99 meshlink_destroy("blacklist_conf.2");
101 // Open two new meshlink instance.
102 meshlink_handle_t *mesh1 = meshlink_open("blacklist_conf.1", "foo", "blacklist", DEV_CLASS_BACKBONE);
103 assert(mesh1 != NULL);
104 meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
105 meshlink_handle_t *mesh2 = meshlink_open("blacklist_conf.2", "bar", "blacklist", DEV_CLASS_BACKBONE);
106 assert(mesh2 != NULL);
107 meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
108 meshlink_set_receive_cb(mesh1, receive);
110 // Start both instances
111 bar_reachable = false;
112 meshlink_set_node_status_cb(mesh1, status_cb);
113 assert(meshlink_start(mesh1));
114 assert(meshlink_start(mesh2));
117 char *foo_export = meshlink_export(mesh1);
118 assert(foo_export != NULL);
119 assert(meshlink_import(mesh2, foo_export));
120 char *bar_export = meshlink_export(mesh2);
121 assert(meshlink_import(mesh1, bar_export));
123 assert_int_equal(bar_reachable, true);
125 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
127 meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
131 assert(meshlink_send(mesh2, foo, "test", 5));
135 meshlink_blacklist(mesh1, bar);
138 assert(meshlink_send(mesh2, foo, "test", 5));
140 assert_int_equal(received, false);
143 meshlink_close(mesh2);
144 meshlink_close(mesh1);
145 meshlink_destroy("blacklist_conf.1");
146 meshlink_destroy("blacklist_conf.2");
150 /* Execute meshlink_blacklist Test Case # 2*/
151 void test_case_mesh_blacklist_02(void **state) {
152 execute_test(test_steps_mesh_blacklist_02, state);
156 /* Test Steps for meshlink_blacklist Test Case # 2
159 1. Calling meshlink_blacklist with NULL as mesh handle argument.
162 meshlink_blacklist API handles the invalid parameter when called by giving proper error number.
164 bool test_steps_mesh_blacklist_02(void) {
165 meshlink_destroy("blacklist_conf.3");
167 // Open two new meshlink instance.
168 meshlink_handle_t *mesh = meshlink_open("blacklist_conf.3", "foo", "blacklist", DEV_CLASS_BACKBONE);
169 assert(mesh != NULL);
171 meshlink_node_t *node = meshlink_get_self(mesh);
174 // Passing NULL as mesh handle and node handle being some valid node handle
175 meshlink_blacklist(NULL, node);
176 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
179 meshlink_close(mesh);
180 meshlink_destroy("blacklist_conf.3");
184 /* Execute meshlink_blacklist Test Case # 3*/
185 void test_case_mesh_blacklist_03(void **state) {
186 execute_test(test_steps_mesh_blacklist_03, state);
189 /* Test Steps for meshlink_blacklist Test Case # 3
192 1. Create node instance
193 2. Calling meshlink_blacklist with NULL as node handle argument.
196 meshlink_blacklist API handles the invalid parameter when called by giving proper error number.
198 bool test_steps_mesh_blacklist_03(void) {
199 meshlink_destroy("blacklist_conf.4");
201 // Open two new meshlink instance.
202 meshlink_handle_t *mesh = meshlink_open("blacklist_conf.4", "foo", "blacklist", DEV_CLASS_BACKBONE);
203 assert(mesh != NULL);
205 // Passing NULL as node handle and mesh handle being some valid mesh handle value
206 meshlink_blacklist(mesh, NULL);
207 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
210 meshlink_close(mesh);
211 meshlink_destroy("blacklist_conf.4");
215 int test_meshlink_blacklist(void) {
216 const struct CMUnitTest blackbox_blacklist_tests[] = {
217 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_01, NULL, NULL,
218 (void *)&test_mesh_blacklist_01_state),
219 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_02, NULL, NULL,
220 (void *)&test_mesh_blacklist_02_state),
221 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_03, NULL, NULL,
222 (void *)&test_mesh_blacklist_03_state)
225 total_tests += sizeof(blackbox_blacklist_tests) / sizeof(blackbox_blacklist_tests[0]);
227 return cmocka_run_group_tests(blackbox_blacklist_tests, NULL, NULL);