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