]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_blacklist.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_blacklist.c
1 /*
2     test_cases_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 /* Modify this to change the logging level of Meshlink */
25 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
26
27 #include "execute_tests.h"
28 #include "test_cases_blacklist.h"
29 #include "../common/containers.h"
30 #include "../common/test_step.h"
31 #include "../common/common_handlers.h"
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <setjmp.h>
35 #include <cmocka.h>
36 #include <assert.h>
37 #include <string.h>
38
39 static void test_case_mesh_blacklist_01(void **state);
40 static bool test_steps_mesh_blacklist_01(void);
41 static void test_case_mesh_blacklist_02(void **state);
42 static bool test_steps_mesh_blacklist_02(void);
43 static void test_case_mesh_blacklist_03(void **state);
44 static bool test_steps_mesh_blacklist_03(void);
45
46 /* State structure for meshlink_blacklist Test Case #1 */
47 static black_box_state_t test_mesh_blacklist_01_state = {
48         .test_case_name = "test_case_mesh_blacklist_01",
49 };
50
51 /* State structure for meshlink_blacklist Test Case #2 */
52 static black_box_state_t test_mesh_blacklist_02_state = {
53         .test_case_name = "test_case_mesh_blacklist_02",
54 };
55
56 /* State structure for meshlink_blacklist Test Case #3 */
57 static black_box_state_t test_mesh_blacklist_03_state = {
58         .test_case_name = "test_case_mesh_blacklist_03",
59 };
60
61 /* Execute meshlink_blacklist Test Case # 1*/
62 void test_case_mesh_blacklist_01(void **state) {
63         execute_test(test_steps_mesh_blacklist_01, state);
64 }
65
66 static bool received;
67
68 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
69         (void)mesh;
70
71         const char *msg = data;
72         assert(len);
73
74         if(!strcmp(src->name, "bar") && len == 5 && !strcmp(msg, "test")) {
75                 received = true;
76         }
77 }
78
79 static bool bar_reachable;
80
81 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
82         (void)mesh;
83
84         if(!strcmp(node->name, "bar") && reachable) {
85                 bar_reachable = true;
86         }
87 }
88
89
90 /* Test Steps for meshlink_blacklist Test Case # 1
91
92     Test Steps:
93     1. Open both the node instances
94     2. Join bar node with foo and Send & Receive data
95     3. Blacklist bar and Send & Receive data
96
97     Expected Result:
98     When default blacklist is disabled, foo node should receive data from bar
99     but when enabled foo node should not receive data
100 */
101 bool test_steps_mesh_blacklist_01(void) {
102         assert(meshlink_destroy("blacklist_conf.1"));
103         assert(meshlink_destroy("blacklist_conf.2"));
104
105         // Open two new meshlink instance.
106         meshlink_handle_t *mesh1 = meshlink_open("blacklist_conf.1", "foo", "blacklist", DEV_CLASS_BACKBONE);
107         assert(mesh1 != NULL);
108         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
109         meshlink_handle_t *mesh2 = meshlink_open("blacklist_conf.2", "bar", "blacklist", DEV_CLASS_BACKBONE);
110         assert(mesh2 != NULL);
111         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
112         meshlink_set_receive_cb(mesh1, receive);
113
114         // Start both instances
115         bar_reachable = false;
116         meshlink_set_node_status_cb(mesh1, status_cb);
117         assert(meshlink_start(mesh1));
118         assert(meshlink_start(mesh2));
119         sleep(1);
120
121         char *foo_export = meshlink_export(mesh1);
122         assert(foo_export != NULL);
123         assert(meshlink_import(mesh2, foo_export));
124         char *bar_export = meshlink_export(mesh2);
125         assert(meshlink_import(mesh1, bar_export));
126         sleep(5);
127         assert_int_equal(bar_reachable, true);
128
129         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
130         assert(bar);
131         meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
132         assert(foo);
133
134         received = false;
135         assert(meshlink_send(mesh2, foo, "test", 5));
136         sleep(1);
137         assert(received);
138
139         assert(meshlink_blacklist(mesh1, bar));
140
141         received = false;
142         assert(meshlink_send(mesh2, foo, "test", 5));
143         sleep(1);
144         assert_int_equal(received, false);
145
146         // Clean up.
147         meshlink_close(mesh2);
148         meshlink_close(mesh1);
149         assert(meshlink_destroy("blacklist_conf.1"));
150         assert(meshlink_destroy("blacklist_conf.2"));
151         return true;
152 }
153
154 /* Execute meshlink_blacklist Test Case # 2*/
155 void test_case_mesh_blacklist_02(void **state) {
156         execute_test(test_steps_mesh_blacklist_02, state);
157 }
158
159
160 /* Test Steps for meshlink_blacklist Test Case # 2
161
162     Test Steps:
163     1. Calling meshlink_blacklist with NULL as mesh handle argument.
164
165     Expected Result:
166     meshlink_blacklist API handles the invalid parameter when called by giving proper error number.
167 */
168 bool test_steps_mesh_blacklist_02(void) {
169         assert(meshlink_destroy("blacklist_conf.3"));
170
171         // Open two new meshlink instance.
172         meshlink_handle_t *mesh = meshlink_open("blacklist_conf.3", "foo", "blacklist", DEV_CLASS_BACKBONE);
173         assert(mesh != NULL);
174
175         meshlink_node_t *node = meshlink_get_self(mesh);
176         assert(node);
177
178         // Passing NULL as mesh handle and node handle being some valid node handle
179         assert(!meshlink_blacklist(NULL, node));
180         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
181
182         // Clean up.
183         meshlink_close(mesh);
184         assert(meshlink_destroy("blacklist_conf.3"));
185         return true;
186 }
187
188 /* Execute meshlink_blacklist Test Case # 3*/
189 void test_case_mesh_blacklist_03(void **state) {
190         execute_test(test_steps_mesh_blacklist_03, state);
191 }
192
193 /* Test Steps for meshlink_blacklist Test Case # 3
194
195     Test Steps:
196     1. Create node instance
197     2. Calling meshlink_blacklist with NULL as node handle argument.
198
199     Expected Result:
200     meshlink_blacklist API handles the invalid parameter when called by giving proper error number.
201 */
202 bool test_steps_mesh_blacklist_03(void) {
203         assert(meshlink_destroy("blacklist_conf.4"));
204
205         // Open two new meshlink instance.
206         meshlink_handle_t *mesh = meshlink_open("blacklist_conf.4", "foo", "blacklist", DEV_CLASS_BACKBONE);
207         assert(mesh != NULL);
208
209         // Passing NULL as node handle and mesh handle being some valid mesh handle value
210         assert(!meshlink_blacklist(mesh, NULL));
211         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
212
213         // Clean up.
214         meshlink_close(mesh);
215         assert(meshlink_destroy("blacklist_conf.4"));
216         return true;
217 }
218
219 int test_meshlink_blacklist(void) {
220         const struct CMUnitTest blackbox_blacklist_tests[] = {
221                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_01, NULL, NULL,
222                                 (void *)&test_mesh_blacklist_01_state),
223                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_02, NULL, NULL,
224                                 (void *)&test_mesh_blacklist_02_state),
225                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_03, NULL, NULL,
226                                 (void *)&test_mesh_blacklist_03_state)
227         };
228
229         total_tests += sizeof(blackbox_blacklist_tests) / sizeof(blackbox_blacklist_tests[0]);
230
231         return cmocka_run_group_tests(blackbox_blacklist_tests, NULL, NULL);
232 }
233