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