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.
26 #include "execute_tests.h"
27 #include "test_cases_default_blacklist.h"
28 #include "../common/containers.h"
29 #include "../common/test_step.h"
30 #include "../common/common_handlers.h"
31 #include "../../utils.h"
33 /* Modify this to change the logging level of Meshlink */
34 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
36 static void test_case_mesh_default_blacklist_01(void **state);
37 static bool test_steps_mesh_default_blacklist_01(void);
38 static void test_case_mesh_default_blacklist_02(void **state);
39 static bool test_steps_mesh_default_blacklist_02(void);
41 /* State structure for meshlink_default_blacklist Test Case #1 */
42 static black_box_state_t test_mesh_default_blacklist_01_state = {
43 .test_case_name = "test_case_mesh_default_blacklist_01",
46 /* State structure for meshlink_default_blacklist Test Case #2 */
47 static black_box_state_t test_mesh_default_blacklist_02_state = {
48 .test_case_name = "test_case_mesh_default_blacklist_02",
51 /* Execute meshlink_default_blacklist Test Case # 1*/
52 static void test_case_mesh_default_blacklist_01(void **state) {
53 execute_test(test_steps_mesh_default_blacklist_01, state);
57 static bool received = false;
59 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
62 if(!strcmp(src->name, "bar") || !strcmp(src->name, "foz")) {
67 static bool bar_reachable = false;
68 static bool foz_reachable = false;
70 void status_cb1(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
71 if(!strcmp(node->name, "bar")) {
72 bar_reachable = reachable;
73 } else if(!strcmp(node->name, "foz")) {
74 foz_reachable = reachable;
78 /* Test Steps for meshlink_default_blacklist Test Case # 1
81 1. Open all the node instances & Disable default blacklist
82 2. Join bar node with foo and Send & Receive data
83 3. Enable default blacklist and join foz node with foo node
84 and follow the steps done for bar node
87 When default blacklist is disabled, foo node should receive data from bar
88 but when enabled foo node should not receive data from foz
90 static bool test_steps_mesh_default_blacklist_01(void) {
91 meshlink_destroy("def_blacklist_conf.1");
92 meshlink_destroy("def_blacklist_conf.2");
93 meshlink_destroy("def_blacklist_conf.3");
95 // Open two new meshlink instance.
96 meshlink_handle_t *mesh1 = meshlink_open("def_blacklist_conf.1", "foo", "blacklist", DEV_CLASS_BACKBONE);
97 assert(mesh1 != NULL);
98 meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
99 meshlink_handle_t *mesh2 = meshlink_open("def_blacklist_conf.2", "bar", "blacklist", DEV_CLASS_BACKBONE);
100 assert(mesh2 != NULL);
101 meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
102 meshlink_handle_t *mesh3 = meshlink_open("def_blacklist_conf.3", "foz", "blacklist", DEV_CLASS_BACKBONE);
104 meshlink_set_log_cb(mesh3, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
105 meshlink_set_receive_cb(mesh1, receive);
107 meshlink_set_default_blacklist(mesh1, false);
109 // Start both instances
110 bar_reachable = false;
111 foz_reachable = false;
112 meshlink_set_node_status_cb(mesh1, status_cb1);
113 assert(meshlink_start(mesh1));
114 assert(meshlink_start(mesh2));
115 assert(meshlink_start(mesh3));
118 char *foo_export = meshlink_export(mesh1);
119 assert(foo_export != NULL);
120 assert(meshlink_import(mesh2, foo_export));
121 char *bar_export = meshlink_export(mesh2);
122 assert(meshlink_import(mesh1, bar_export));
124 assert(bar_reachable);
126 // Nodes should learn about each other
127 meshlink_node_t *foo = NULL;
128 foo = meshlink_get_node(mesh2, "foo");
132 assert(meshlink_send(mesh2, foo, "test", 5));
133 assert_after(received, 2);
135 // Enable default blacklist and join another node
136 meshlink_set_default_blacklist(mesh1, true);
138 char *foz_export = meshlink_export(mesh3);
140 assert(meshlink_import(mesh1, foz_export));
141 assert(meshlink_import(mesh3, foo_export));
143 assert(foz_reachable);
145 foo = meshlink_get_node(mesh3, "foo");
147 assert(meshlink_send(mesh3, foo, "test", 5));
149 assert(meshlink_send(mesh3, foo, "test", 5));
150 assert_after(!received, 2);
156 meshlink_close(mesh1);
157 meshlink_close(mesh2);
158 meshlink_close(mesh3);
159 meshlink_destroy("def_blacklist_conf.1");
160 meshlink_destroy("def_blacklist_conf.2");
161 meshlink_destroy("def_blacklist_conf.3");
166 /* Execute meshlink_default_blacklist Test Case # 2*/
167 static void test_case_mesh_default_blacklist_02(void **state) {
168 execute_test(test_steps_mesh_default_blacklist_02, state);
171 /* Test Steps for meshlink_default_blacklist Test Case # 2
174 1. Calling meshlink_default_blacklist with NULL as mesh handle argument.
177 meshlink_default_blacklist API handles the invalid parameter when called by giving proper error number.
179 static bool test_steps_mesh_default_blacklist_02(void) {
180 // Passing NULL as mesh handle argument to the API
181 meshlink_set_default_blacklist(NULL, true);
182 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
187 int test_meshlink_default_blacklist(void) {
188 const struct CMUnitTest blackbox_default_blacklist_tests[] = {
189 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_default_blacklist_01, NULL, NULL,
190 (void *)&test_mesh_default_blacklist_01_state),
191 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_default_blacklist_02, NULL, NULL,
192 (void *)&test_mesh_default_blacklist_02_state)
195 total_tests += sizeof(blackbox_default_blacklist_tests) / sizeof(blackbox_default_blacklist_tests[0]);
197 return cmocka_run_group_tests(blackbox_default_blacklist_tests, NULL, NULL);