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) {
65 if(!strcmp(src->name, "bar") || !strcmp(src->name, "foz")) {
70 static bool bar_reachable = false;
71 static bool foz_reachable = false;
73 void status_cb1(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
76 if(!strcmp(node->name, "bar")) {
77 bar_reachable = reachable;
78 } else if(!strcmp(node->name, "foz")) {
79 foz_reachable = reachable;
83 /* Test Steps for meshlink_default_blacklist Test Case # 1
86 1. Open all the node instances & Disable default blacklist
87 2. Join bar node with foo and Send & Receive data
88 3. Enable default blacklist and join foz node with foo node
89 and follow the steps done for bar node
92 When default blacklist is disabled, foo node should receive data from bar
93 but when enabled foo node should not receive data from foz
95 static bool test_steps_mesh_default_blacklist_01(void) {
96 meshlink_destroy("def_blacklist_conf.1");
97 meshlink_destroy("def_blacklist_conf.2");
98 meshlink_destroy("def_blacklist_conf.3");
100 // Open two new meshlink instance.
101 meshlink_handle_t *mesh1 = meshlink_open("def_blacklist_conf.1", "foo", "blacklist", DEV_CLASS_BACKBONE);
102 assert(mesh1 != NULL);
103 meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
104 meshlink_handle_t *mesh2 = meshlink_open("def_blacklist_conf.2", "bar", "blacklist", DEV_CLASS_BACKBONE);
105 assert(mesh2 != NULL);
106 meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
107 meshlink_handle_t *mesh3 = meshlink_open("def_blacklist_conf.3", "foz", "blacklist", DEV_CLASS_BACKBONE);
109 meshlink_set_log_cb(mesh3, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
110 meshlink_set_receive_cb(mesh1, receive);
112 meshlink_set_default_blacklist(mesh1, false);
114 // Start both instances
115 bar_reachable = false;
116 foz_reachable = false;
117 meshlink_set_node_status_cb(mesh1, status_cb1);
118 assert(meshlink_start(mesh1));
119 assert(meshlink_start(mesh2));
120 assert(meshlink_start(mesh3));
123 char *foo_export = meshlink_export(mesh1);
124 assert(foo_export != NULL);
125 assert(meshlink_import(mesh2, foo_export));
126 char *bar_export = meshlink_export(mesh2);
127 assert(meshlink_import(mesh1, bar_export));
129 assert(bar_reachable);
131 // Nodes should learn about each other
132 meshlink_node_t *foo = NULL;
133 foo = meshlink_get_node(mesh2, "foo");
137 assert(meshlink_send(mesh2, foo, "test", 5));
138 assert_after(received, 2);
140 // Enable default blacklist and join another node
141 meshlink_set_default_blacklist(mesh1, true);
143 char *foz_export = meshlink_export(mesh3);
145 assert(meshlink_import(mesh1, foz_export));
146 assert(meshlink_import(mesh3, foo_export));
148 assert(foz_reachable);
150 foo = meshlink_get_node(mesh3, "foo");
152 assert(meshlink_send(mesh3, foo, "test", 5));
154 assert(meshlink_send(mesh3, foo, "test", 5));
155 assert_after(!received, 2);
161 meshlink_close(mesh1);
162 meshlink_close(mesh2);
163 meshlink_close(mesh3);
164 meshlink_destroy("def_blacklist_conf.1");
165 meshlink_destroy("def_blacklist_conf.2");
166 meshlink_destroy("def_blacklist_conf.3");
171 /* Execute meshlink_default_blacklist Test Case # 2*/
172 static void test_case_mesh_default_blacklist_02(void **state) {
173 execute_test(test_steps_mesh_default_blacklist_02, state);
176 /* Test Steps for meshlink_default_blacklist Test Case # 2
179 1. Calling meshlink_default_blacklist with NULL as mesh handle argument.
182 meshlink_default_blacklist API handles the invalid parameter when called by giving proper error number.
184 static bool test_steps_mesh_default_blacklist_02(void) {
185 // Passing NULL as mesh handle argument to the API
186 meshlink_set_default_blacklist(NULL, true);
187 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
192 int test_meshlink_default_blacklist(void) {
193 const struct CMUnitTest blackbox_default_blacklist_tests[] = {
194 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_default_blacklist_01, NULL, NULL,
195 (void *)&test_mesh_default_blacklist_01_state),
196 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_default_blacklist_02, NULL, NULL,
197 (void *)&test_mesh_default_blacklist_02_state)
200 total_tests += sizeof(blackbox_default_blacklist_tests) / sizeof(blackbox_default_blacklist_tests[0]);
202 return cmocka_run_group_tests(blackbox_default_blacklist_tests, NULL, NULL);