]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_default_blacklist.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[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 #ifdef NDEBUG
21 #undef NDEBUG
22 #endif
23
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <setjmp.h>
27 #include <cmocka.h>
28 #include <assert.h>
29 #include <string.h>
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"
36
37 /* Modify this to change the logging level of Meshlink */
38 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
39
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);
44
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",
48 };
49
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",
53 };
54
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);
58         return;
59 }
60
61 static bool received = false;
62
63 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
64         (void)mesh;
65         (void)data;
66
67         assert(len);
68
69         if(!strcmp(src->name, "bar") || !strcmp(src->name, "foz")) {
70                 received = true;
71         }
72 }
73
74 static bool bar_reachable = false;
75 static bool foz_reachable = false;
76
77 void status_cb1(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
78         (void)mesh;
79
80         if(!strcmp(node->name, "bar")) {
81                 bar_reachable = reachable;
82         } else if(!strcmp(node->name, "foz")) {
83                 foz_reachable = reachable;
84         }
85 }
86
87 /* Test Steps for meshlink_default_blacklist Test Case # 1
88
89     Test Steps:
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
94
95     Expected Result:
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
98 */
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"));
103
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);
112         assert(mesh3);
113         meshlink_set_log_cb(mesh3, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
114         meshlink_set_receive_cb(mesh1, receive);
115
116         meshlink_set_default_blacklist(mesh1, false);
117
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));
125         sleep(1);
126
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));
132         sleep(5);
133         assert(bar_reachable);
134
135         // Nodes should learn about each other
136         meshlink_node_t *foo = NULL;
137         foo = meshlink_get_node(mesh2, "foo");
138         assert(foo);
139
140         received = false;
141         assert(meshlink_send(mesh2, foo, "test", 5));
142         assert_after(received, 2);
143
144         // Enable default blacklist and join another node
145         meshlink_set_default_blacklist(mesh1, true);
146
147         char *foz_export = meshlink_export(mesh3);
148         assert(foz_export);
149         assert(meshlink_import(mesh1, foz_export));
150         assert(meshlink_import(mesh3, foo_export));
151         sleep(5);
152         assert(foz_reachable);
153
154         foo = meshlink_get_node(mesh3, "foo");
155         assert(foo);
156         assert(meshlink_send(mesh3, foo, "test", 5));
157         received = false;
158         assert(meshlink_send(mesh3, foo, "test", 5));
159         assert_after(!received, 2);
160
161         // Clean up.
162         free(foo_export);
163         free(foz_export);
164         free(bar_export);
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"));
171
172         return true;
173 }
174
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);
178 }
179
180 /* Test Steps for meshlink_default_blacklist Test Case # 2
181
182     Test Steps:
183     1. Calling meshlink_default_blacklist with NULL as mesh handle argument.
184
185     Expected Result:
186     meshlink_default_blacklist API handles the invalid parameter when called by giving proper error number.
187 */
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);
192
193         return true;
194 }
195
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)
202         };
203
204         total_tests += sizeof(blackbox_default_blacklist_tests) / sizeof(blackbox_default_blacklist_tests[0]);
205
206         return cmocka_run_group_tests(blackbox_default_blacklist_tests, NULL, NULL);
207 }