2 test_cases_default_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.
30 #include "execute_tests.h"
31 #include "test_cases_default_blacklist.h"
32 #include "../common/containers.h"
33 #include "../common/test_step.h"
34 #include "../common/common_handlers.h"
35 #include "../../utils.h"
37 /* Modify this to change the logging level of Meshlink */
38 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
40 static void test_case_mesh_default_blacklist_01(void **state);
41 static bool test_steps_mesh_default_blacklist_01(void);
42 static void test_case_mesh_default_blacklist_02(void **state);
43 static bool test_steps_mesh_default_blacklist_02(void);
45 /* State structure for meshlink_default_blacklist Test Case #1 */
46 static black_box_state_t test_mesh_default_blacklist_01_state = {
47 .test_case_name = "test_case_mesh_default_blacklist_01",
50 /* State structure for meshlink_default_blacklist Test Case #2 */
51 static black_box_state_t test_mesh_default_blacklist_02_state = {
52 .test_case_name = "test_case_mesh_default_blacklist_02",
55 /* Execute meshlink_default_blacklist Test Case # 1*/
56 static void test_case_mesh_default_blacklist_01(void **state) {
57 execute_test(test_steps_mesh_default_blacklist_01, state);
61 static bool received = false;
63 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
69 if(!strcmp(src->name, "bar") || !strcmp(src->name, "foz")) {
74 static bool bar_reachable = false;
75 static bool foz_reachable = false;
77 void status_cb1(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
80 if(!strcmp(node->name, "bar")) {
81 bar_reachable = reachable;
82 } else if(!strcmp(node->name, "foz")) {
83 foz_reachable = reachable;
87 /* Test Steps for meshlink_default_blacklist Test Case # 1
90 1. Open all the node instances & Disable default blacklist
91 2. Join bar node with foo and Send & Receive data
92 3. Enable default blacklist and join foz node with foo node
93 and follow the steps done for bar node
96 When default blacklist is disabled, foo node should receive data from bar
97 but when enabled foo node should not receive data from foz
99 static bool test_steps_mesh_default_blacklist_01(void) {
100 assert(meshlink_destroy("def_blacklist_conf.1"));
101 assert(meshlink_destroy("def_blacklist_conf.2"));
102 assert(meshlink_destroy("def_blacklist_conf.3"));
104 // Open two new meshlink instance.
105 meshlink_handle_t *mesh1 = meshlink_open("def_blacklist_conf.1", "foo", "blacklist", DEV_CLASS_BACKBONE);
106 assert(mesh1 != NULL);
107 meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
108 meshlink_handle_t *mesh2 = meshlink_open("def_blacklist_conf.2", "bar", "blacklist", DEV_CLASS_BACKBONE);
109 assert(mesh2 != NULL);
110 meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
111 meshlink_handle_t *mesh3 = meshlink_open("def_blacklist_conf.3", "foz", "blacklist", DEV_CLASS_BACKBONE);
113 meshlink_set_log_cb(mesh3, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
114 meshlink_set_receive_cb(mesh1, receive);
116 meshlink_set_default_blacklist(mesh1, false);
118 // Start both instances
119 bar_reachable = false;
120 foz_reachable = false;
121 meshlink_set_node_status_cb(mesh1, status_cb1);
122 assert(meshlink_start(mesh1));
123 assert(meshlink_start(mesh2));
124 assert(meshlink_start(mesh3));
127 char *foo_export = meshlink_export(mesh1);
128 assert(foo_export != NULL);
129 assert(meshlink_import(mesh2, foo_export));
130 char *bar_export = meshlink_export(mesh2);
131 assert(meshlink_import(mesh1, bar_export));
133 assert(bar_reachable);
135 // Nodes should learn about each other
136 meshlink_node_t *foo = NULL;
137 foo = meshlink_get_node(mesh2, "foo");
141 assert(meshlink_send(mesh2, foo, "test", 5));
142 assert_after(received, 2);
144 // Enable default blacklist and join another node
145 meshlink_set_default_blacklist(mesh1, true);
147 char *foz_export = meshlink_export(mesh3);
149 assert(meshlink_import(mesh1, foz_export));
150 assert(meshlink_import(mesh3, foo_export));
152 assert(foz_reachable);
154 foo = meshlink_get_node(mesh3, "foo");
156 assert(meshlink_send(mesh3, foo, "test", 5));
158 assert(meshlink_send(mesh3, foo, "test", 5));
159 assert_after(!received, 2);
165 meshlink_close(mesh1);
166 meshlink_close(mesh2);
167 meshlink_close(mesh3);
168 assert(meshlink_destroy("def_blacklist_conf.1"));
169 assert(meshlink_destroy("def_blacklist_conf.2"));
170 assert(meshlink_destroy("def_blacklist_conf.3"));
175 /* Execute meshlink_default_blacklist Test Case # 2*/
176 static void test_case_mesh_default_blacklist_02(void **state) {
177 execute_test(test_steps_mesh_default_blacklist_02, state);
180 /* Test Steps for meshlink_default_blacklist Test Case # 2
183 1. Calling meshlink_default_blacklist with NULL as mesh handle argument.
186 meshlink_default_blacklist API handles the invalid parameter when called by giving proper error number.
188 static bool test_steps_mesh_default_blacklist_02(void) {
189 // Passing NULL as mesh handle argument to the API
190 meshlink_set_default_blacklist(NULL, true);
191 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
196 int test_meshlink_default_blacklist(void) {
197 const struct CMUnitTest blackbox_default_blacklist_tests[] = {
198 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_default_blacklist_01, NULL, NULL,
199 (void *)&test_mesh_default_blacklist_01_state),
200 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_default_blacklist_02, NULL, NULL,
201 (void *)&test_mesh_default_blacklist_02_state)
204 total_tests += sizeof(blackbox_default_blacklist_tests) / sizeof(blackbox_default_blacklist_tests[0]);
206 return cmocka_run_group_tests(blackbox_default_blacklist_tests, NULL, NULL);