]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_default_blacklist.c
bf53b597128c1943e7313959929b29d303037619
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_default_blacklist.c
1 /*
2     test_cases_default_blacklist.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <setjmp.h>
23 #include <cmocka.h>
24 #include <assert.h>
25 #include <string.h>
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"
32
33 /* Modify this to change the logging level of Meshlink */
34 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
35
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);
40
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",
44 };
45
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",
49 };
50
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);
54         return;
55 }
56
57 static bool received = false;
58
59 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
60         (void)mesh;
61         (void)data;
62
63         assert(len);
64
65         if(!strcmp(src->name, "bar") || !strcmp(src->name, "foz")) {
66                 received = true;
67         }
68 }
69
70 static bool bar_reachable = false;
71 static bool foz_reachable = false;
72
73 void status_cb1(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
74         (void)mesh;
75
76         if(!strcmp(node->name, "bar")) {
77                 bar_reachable = reachable;
78         } else if(!strcmp(node->name, "foz")) {
79                 foz_reachable = reachable;
80         }
81 }
82
83 /* Test Steps for meshlink_default_blacklist Test Case # 1
84
85     Test Steps:
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
90
91     Expected Result:
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
94 */
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");
99
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);
108         assert(mesh3);
109         meshlink_set_log_cb(mesh3, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
110         meshlink_set_receive_cb(mesh1, receive);
111
112         meshlink_set_default_blacklist(mesh1, false);
113
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));
121         sleep(1);
122
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));
128         sleep(5);
129         assert(bar_reachable);
130
131         // Nodes should learn about each other
132         meshlink_node_t *foo = NULL;
133         foo = meshlink_get_node(mesh2, "foo");
134         assert(foo);
135
136         received = false;
137         assert(meshlink_send(mesh2, foo, "test", 5));
138         assert_after(received, 2);
139
140         // Enable default blacklist and join another node
141         meshlink_set_default_blacklist(mesh1, true);
142
143         char *foz_export = meshlink_export(mesh3);
144         assert(foz_export);
145         assert(meshlink_import(mesh1, foz_export));
146         assert(meshlink_import(mesh3, foo_export));
147         sleep(5);
148         assert(foz_reachable);
149
150         foo = meshlink_get_node(mesh3, "foo");
151         assert(foo);
152         assert(meshlink_send(mesh3, foo, "test", 5));
153         received = false;
154         assert(meshlink_send(mesh3, foo, "test", 5));
155         assert_after(!received, 2);
156
157         // Clean up.
158         free(foo_export);
159         free(foz_export);
160         free(bar_export);
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");
167
168         return true;
169 }
170
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);
174 }
175
176 /* Test Steps for meshlink_default_blacklist Test Case # 2
177
178     Test Steps:
179     1. Calling meshlink_default_blacklist with NULL as mesh handle argument.
180
181     Expected Result:
182     meshlink_default_blacklist API handles the invalid parameter when called by giving proper error number.
183 */
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);
188
189         return true;
190 }
191
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)
198         };
199
200         total_tests += sizeof(blackbox_default_blacklist_tests) / sizeof(blackbox_default_blacklist_tests[0]);
201
202         return cmocka_run_group_tests(blackbox_default_blacklist_tests, NULL, NULL);
203 }