]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_whitelist.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_whitelist.c
1 /*
2     test_cases_whitelist.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 "execute_tests.h"
25 #include "test_cases_whitelist.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include <assert.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <setjmp.h>
34 #include <cmocka.h>
35 #include <pthread.h>
36 #include <errno.h>
37
38 #include "../../utils.h"
39
40 static void test_case_mesh_whitelist_01(void **state);
41 static bool test_steps_mesh_whitelist_01(void);
42 static void test_case_mesh_whitelist_02(void **state);
43 static bool test_steps_mesh_whitelist_02(void);
44 static void test_case_mesh_whitelist_03(void **state);
45 static bool test_steps_mesh_whitelist_03(void);
46
47 /* State structure for meshlink_whitelist Test Case #1 */
48 static black_box_state_t test_mesh_whitelist_01_state = {
49         .test_case_name = "test_case_mesh_whitelist_01",
50 };
51
52 /* State structure for meshlink_whitelist Test Case #2 */
53 static black_box_state_t test_mesh_whitelist_02_state = {
54         .test_case_name = "test_case_mesh_whitelist_02",
55 };
56
57 /* State structure for meshlink_whitelist Test Case #3 */
58 static black_box_state_t test_mesh_whitelist_03_state = {
59         .test_case_name = "test_case_mesh_whitelist_03",
60 };
61
62 static bool rec_stat;
63 static bool reachable;
64 static pthread_mutex_t lock_receive = PTHREAD_MUTEX_INITIALIZER;
65 static pthread_cond_t receive_cond = PTHREAD_COND_INITIALIZER;
66 static pthread_mutex_t reachable_lock = PTHREAD_MUTEX_INITIALIZER;
67 static pthread_cond_t reachable_cond = PTHREAD_COND_INITIALIZER;
68
69
70 /* Execute meshlink_whitelist Test Case # 1*/
71 static void test_case_mesh_whitelist_01(void **state) {
72         execute_test(test_steps_mesh_whitelist_01, state);
73 }
74
75
76 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
77         (void)mesh;
78         (void)src;
79         (void)data;
80
81         assert(len);
82
83         pthread_mutex_lock(& lock_receive);
84         rec_stat = true;
85         assert(!pthread_cond_broadcast(&receive_cond));
86         pthread_mutex_unlock(& lock_receive);
87
88 }
89
90 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reach) {
91         (void)mesh;
92
93         if(!strcmp(node->name, "bar")) {
94                 pthread_mutex_lock(&reachable_lock);
95                 reachable = reach;
96                 assert(!pthread_cond_broadcast(&reachable_cond));
97                 pthread_mutex_unlock(&reachable_lock);
98         }
99 }
100
101
102 /* Test Steps for meshlink_whitelist Test Case # 1
103
104     Test Steps:
105     1. Run 2 node instances
106     2. Blacklist one node and again whitelist the blacklisted node
107
108     Expected Result:
109     meshlink_whitelist API whitelists the blacklisted node
110 */
111 static bool test_steps_mesh_whitelist_01(void) {
112         struct timespec timeout = {0};
113
114         // Open two new meshlink instance.
115
116         assert(meshlink_destroy("whitelist_conf.1"));
117         assert(meshlink_destroy("whitelist_conf.2"));
118         meshlink_handle_t *mesh1 = meshlink_open("whitelist_conf.1", "foo", "test", DEV_CLASS_BACKBONE);
119         assert(mesh1);
120         meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, meshlink_callback_logger);
121         meshlink_handle_t *mesh2 = meshlink_open("whitelist_conf.2", "bar", "test", DEV_CLASS_BACKBONE);
122         assert(mesh2);
123         meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, meshlink_callback_logger);
124         meshlink_set_receive_cb(mesh2, receive);
125         meshlink_set_receive_cb(mesh1, receive);
126
127         // Export & Import to join the mesh
128
129         reachable = false;
130         char *data = meshlink_export(mesh1);
131         assert(data);
132         assert(meshlink_import(mesh2, data));
133         free(data);
134         data = meshlink_export(mesh2);
135         assert(data);
136         assert(meshlink_import(mesh1, data));
137         free(data);
138
139         // Start both instances
140
141         meshlink_set_node_status_cb(mesh1, status_cb);
142         assert(meshlink_start(mesh1));
143         assert(meshlink_start(mesh2));
144
145         // Nodes should know each other
146         timeout.tv_sec = time(NULL) + 10;
147         pthread_mutex_lock(&reachable_lock);
148
149         while(reachable == false) {
150                 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
151         }
152
153         pthread_mutex_unlock(&reachable_lock);
154         sleep(1);
155
156         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
157         assert(bar);
158         meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
159         assert(foo);
160
161         rec_stat = false;
162         assert(meshlink_send(mesh1, bar, "test", 5));
163         timeout.tv_sec = time(NULL) + 10;
164         pthread_mutex_lock(& lock_receive);
165
166         if(rec_stat == false) {
167                 assert(pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout) == 0);
168         }
169
170         pthread_mutex_unlock(& lock_receive);
171
172
173         assert(meshlink_blacklist(mesh1, foo));
174
175         rec_stat = false;
176         assert(meshlink_send(mesh1, bar, "test", 5));
177         timeout.tv_sec = time(NULL) + 10;
178         pthread_mutex_lock(& lock_receive);
179
180         if(rec_stat == false) {
181                 int err = pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout);
182                 assert(err == ETIMEDOUT);
183         }
184
185         pthread_mutex_unlock(& lock_receive);
186         assert(meshlink_whitelist(mesh1, foo));
187
188         rec_stat = false;
189         bool result = meshlink_send(mesh2, foo, "test", 5);
190         timeout.tv_sec = time(NULL) + 10;
191         pthread_mutex_lock(& lock_receive);
192
193         if(rec_stat == false) {
194                 assert(pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout) == 0);
195         }
196
197         pthread_mutex_unlock(& lock_receive);
198
199         // Clean up.
200
201         meshlink_close(mesh2);
202         meshlink_close(mesh1);
203         assert(meshlink_destroy("whitelist_conf.1"));
204         assert(meshlink_destroy("whitelist_conf.2"));
205
206         return result;
207 }
208
209 /* Test Steps for meshlink_whitelist Test Case # 2
210
211     Test Steps:
212     1. Calling meshlink_whitelist with NULL as mesh handle argument.
213
214     Expected Result:
215     meshlink_whitelist API handles the invalid parameter when called by giving proper error number.
216 */
217 static void test_case_mesh_whitelist_02(void **state) {
218         execute_test(test_steps_mesh_whitelist_02, state);
219 }
220
221 /* Test Steps for meshlink_whitelist Test Case # 2*/
222 static bool test_steps_mesh_whitelist_02(void) {
223         struct timespec timeout = {0};
224
225         // Open two new meshlink instance.
226
227         assert(meshlink_destroy("whitelist_conf.3"));
228         assert(meshlink_destroy("whitelist_conf.4"));
229         meshlink_handle_t *mesh1 = meshlink_open("whitelist_conf.3", "foo", "test", DEV_CLASS_BACKBONE);
230         assert(mesh1);
231         meshlink_handle_t *mesh2 = meshlink_open("whitelist_conf.4", "bar", "test", DEV_CLASS_BACKBONE);
232         assert(mesh2);
233         meshlink_set_receive_cb(mesh2, receive);
234         meshlink_set_receive_cb(mesh1, receive);
235
236         char *data = meshlink_export(mesh1);
237         assert(data);
238         assert(meshlink_import(mesh2, data));
239         free(data);
240         data = meshlink_export(mesh2);
241         assert(data);
242         assert(meshlink_import(mesh1, data));
243         free(data);
244
245         // Start both instances
246
247         reachable = false;
248         meshlink_set_node_status_cb(mesh1, status_cb);
249         assert(meshlink_start(mesh1));
250         assert(meshlink_start(mesh2));
251
252         // Nodes should know each other
253         timeout.tv_sec = time(NULL) + 10;
254         pthread_mutex_lock(&reachable_lock);
255
256         while(reachable == false) {
257                 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
258         }
259
260         pthread_mutex_unlock(&reachable_lock);
261
262         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
263         assert(bar);
264         meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
265         assert(foo);
266
267         assert(meshlink_send(mesh1, bar, "test", 5));
268
269         assert(meshlink_blacklist(mesh1, foo));
270
271         // Passing NULL as mesh handle but with valid node handle 'foo'
272
273         assert(!meshlink_whitelist(NULL, foo));
274         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
275
276         // Clean up.
277
278         meshlink_close(mesh2);
279         meshlink_close(mesh1);
280         assert(meshlink_destroy("whitelist_conf.3"));
281         assert(meshlink_destroy("whitelist_conf.4"));
282
283         return true;
284 }
285
286 /* Execute meshlink_whitelist Test Case # 3*/
287 static void test_case_mesh_whitelist_03(void **state) {
288         execute_test(test_steps_mesh_whitelist_03, state);
289 }
290
291 /* Test Steps for meshlink_whitelist Test Case # 3
292
293     Test Steps:
294     1. Calling meshlink_whitelist with NULL as node handle argument.
295
296     Expected Result:
297     meshlink_whitelist API handles the invalid parameter when called by giving proper error number.
298 */
299 static bool test_steps_mesh_whitelist_03(void) {
300         // Open meshlink instance.
301
302         assert(meshlink_destroy("whitelist_conf"));
303         meshlink_handle_t *mesh = meshlink_open("whitelist_conf", "foo", "test", DEV_CLASS_BACKBONE);
304         assert(mesh);
305
306         // Start instance
307         assert(meshlink_start(mesh));
308
309         assert(!meshlink_whitelist(mesh, NULL));
310         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
311
312         // Clean up.
313
314         meshlink_close(mesh);
315         assert(meshlink_destroy("whitelist_conf"));
316         return true;
317 }
318
319 int test_meshlink_whitelist(void) {
320         const struct CMUnitTest blackbox_whitelist_tests[] = {
321                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_whitelist_01, NULL, NULL,
322                                 (void *)&test_mesh_whitelist_01_state),
323                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_whitelist_02, NULL, NULL,
324                                 (void *)&test_mesh_whitelist_02_state),
325                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_whitelist_03, NULL, NULL,
326                                 (void *)&test_mesh_whitelist_03_state)
327         };
328
329         total_tests += sizeof(blackbox_whitelist_tests) / sizeof(blackbox_whitelist_tests[0]);
330
331         return cmocka_run_group_tests(blackbox_whitelist_tests, NULL, NULL);
332 }