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) {
65 const char *msg = data;
68 if(!strcmp(src->name, "bar") && len == 5 && !strcmp(msg, "test")) {
73 static bool bar_reachable;
75 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
76 if(!strcmp(node->name, "bar") && reachable) {
82 /* Test Steps for meshlink_blacklist Test Case # 1
85 1. Open both the node instances
86 2. Join bar node with foo and Send & Receive data
87 3. Blacklist bar and Send & Receive data
90 When default blacklist is disabled, foo node should receive data from bar
91 but when enabled foo node should not receive data
93 bool test_steps_mesh_blacklist_01(void) {
94 meshlink_destroy("blacklist_conf.1");
95 meshlink_destroy("blacklist_conf.2");
97 // Open two new meshlink instance.
98 meshlink_handle_t *mesh1 = meshlink_open("blacklist_conf.1", "foo", "blacklist", DEV_CLASS_BACKBONE);
99 assert(mesh1 != NULL);
100 meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
101 meshlink_handle_t *mesh2 = meshlink_open("blacklist_conf.2", "bar", "blacklist", DEV_CLASS_BACKBONE);
102 assert(mesh2 != NULL);
103 meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
104 meshlink_set_receive_cb(mesh1, receive);
106 // Start both instances
107 bar_reachable = false;
108 meshlink_set_node_status_cb(mesh1, status_cb);
109 assert(meshlink_start(mesh1));
110 assert(meshlink_start(mesh2));
113 char *foo_export = meshlink_export(mesh1);
114 assert(foo_export != NULL);
115 assert(meshlink_import(mesh2, foo_export));
116 char *bar_export = meshlink_export(mesh2);
117 assert(meshlink_import(mesh1, bar_export));
119 assert_int_equal(bar_reachable, true);
121 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
123 meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
127 assert(meshlink_send(mesh2, foo, "test", 5));
131 meshlink_blacklist(mesh1, bar);
134 assert(meshlink_send(mesh2, foo, "test", 5));
136 assert_int_equal(received, false);
139 meshlink_close(mesh2);
140 meshlink_close(mesh1);
141 meshlink_destroy("blacklist_conf.1");
142 meshlink_destroy("blacklist_conf.2");
146 /* Execute meshlink_blacklist Test Case # 2*/
147 void test_case_mesh_blacklist_02(void **state) {
148 execute_test(test_steps_mesh_blacklist_02, state);
152 /* Test Steps for meshlink_blacklist Test Case # 2
155 1. Calling meshlink_blacklist with NULL as mesh handle argument.
158 meshlink_blacklist API handles the invalid parameter when called by giving proper error number.
160 bool test_steps_mesh_blacklist_02(void) {
161 meshlink_destroy("blacklist_conf.3");
163 // Open two new meshlink instance.
164 meshlink_handle_t *mesh = meshlink_open("blacklist_conf.3", "foo", "blacklist", DEV_CLASS_BACKBONE);
165 assert(mesh != NULL);
167 meshlink_node_t *node = meshlink_get_self(mesh);
170 // Passing NULL as mesh handle and node handle being some valid node handle
171 meshlink_blacklist(NULL, node);
172 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
175 meshlink_close(mesh);
176 meshlink_destroy("blacklist_conf.3");
180 /* Execute meshlink_blacklist Test Case # 3*/
181 void test_case_mesh_blacklist_03(void **state) {
182 execute_test(test_steps_mesh_blacklist_03, state);
185 /* Test Steps for meshlink_blacklist Test Case # 3
188 1. Create node instance
189 2. Calling meshlink_blacklist with NULL as node handle argument.
192 meshlink_blacklist API handles the invalid parameter when called by giving proper error number.
194 bool test_steps_mesh_blacklist_03(void) {
195 meshlink_destroy("blacklist_conf.4");
197 // Open two new meshlink instance.
198 meshlink_handle_t *mesh = meshlink_open("blacklist_conf.4", "foo", "blacklist", DEV_CLASS_BACKBONE);
199 assert(mesh != NULL);
201 // Passing NULL as node handle and mesh handle being some valid mesh handle value
202 meshlink_blacklist(mesh, NULL);
203 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
206 meshlink_close(mesh);
207 meshlink_destroy("blacklist_conf.4");
211 int test_meshlink_blacklist(void) {
212 const struct CMUnitTest blackbox_blacklist_tests[] = {
213 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_01, NULL, NULL,
214 (void *)&test_mesh_blacklist_01_state),
215 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_02, NULL, NULL,
216 (void *)&test_mesh_blacklist_02_state),
217 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_03, NULL, NULL,
218 (void *)&test_mesh_blacklist_03_state)
221 total_tests += sizeof(blackbox_blacklist_tests) / sizeof(blackbox_blacklist_tests[0]);
223 return cmocka_run_group_tests(blackbox_blacklist_tests, NULL, NULL);