2 test_cases_whitelist.c -- Execution of specific meshlink black box test cases
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
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"
34 #include "../../utils.h"
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);
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",
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",
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",
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;
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);
72 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
73 const char *msg = data;
76 pthread_mutex_lock(& lock_receive);
78 assert(!pthread_cond_broadcast(&receive_cond));
79 pthread_mutex_unlock(& lock_receive);
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);
87 assert(!pthread_cond_broadcast(&reachable_cond));
88 pthread_mutex_unlock(&reachable_lock);
93 /* Test Steps for meshlink_whitelist Test Case # 1
96 1. Run 2 node instances
97 2. Blacklist one node and again whitelist the blacklisted node
100 meshlink_whitelist API whitelists the blacklisted node
102 static bool test_steps_mesh_whitelist_01(void) {
103 struct timespec timeout = {0};
105 // Open two new meshlink instance.
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);
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);
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);
118 // Export & Import to join the mesh
121 char *data = meshlink_export(mesh1);
123 assert(meshlink_import(mesh2, data));
125 data = meshlink_export(mesh2);
127 assert(meshlink_import(mesh1, data));
130 // Start both instances
132 meshlink_set_node_status_cb(mesh1, status_cb);
133 assert(meshlink_start(mesh1));
134 assert(meshlink_start(mesh2));
136 // Nodes should know each other
137 timeout.tv_sec = time(NULL) + 10;
138 pthread_mutex_lock(&reachable_lock);
140 while(reachable == false) {
141 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
144 pthread_mutex_unlock(&reachable_lock);
147 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
149 meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
153 assert(meshlink_send(mesh1, bar, "test", 5));
154 timeout.tv_sec = time(NULL) + 10;
155 pthread_mutex_lock(& lock_receive);
157 if(rec_stat == false) {
158 assert(pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout) == 0);
161 pthread_mutex_unlock(& lock_receive);
164 meshlink_blacklist(mesh1, foo);
167 assert(meshlink_send(mesh1, bar, "test", 5));
168 timeout.tv_sec = time(NULL) + 10;
169 pthread_mutex_lock(& lock_receive);
171 if(rec_stat == false) {
172 int err = pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout);
173 assert(err == ETIMEDOUT);
176 pthread_mutex_unlock(& lock_receive);
177 meshlink_whitelist(mesh1, foo);
180 bool result = meshlink_send(mesh2, foo, "test", 5);
181 timeout.tv_sec = time(NULL) + 10;
182 pthread_mutex_lock(& lock_receive);
184 if(rec_stat == false) {
185 assert(pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout) == 0);
188 pthread_mutex_unlock(& lock_receive);
192 meshlink_close(mesh2);
193 meshlink_close(mesh1);
194 meshlink_destroy("whitelist_conf.1");
195 meshlink_destroy("whitelist_conf.2");
200 /* Test Steps for meshlink_whitelist Test Case # 2
203 1. Calling meshlink_whitelist with NULL as mesh handle argument.
206 meshlink_whitelist API handles the invalid parameter when called by giving proper error number.
208 static void test_case_mesh_whitelist_02(void **state) {
209 execute_test(test_steps_mesh_whitelist_02, state);
212 /* Test Steps for meshlink_whitelist Test Case # 2*/
213 static bool test_steps_mesh_whitelist_02(void) {
214 struct timespec timeout = {0};
216 // Open two new meshlink instance.
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);
222 meshlink_handle_t *mesh2 = meshlink_open("whitelist_conf.4", "bar", "test", DEV_CLASS_BACKBONE);
224 meshlink_set_receive_cb(mesh2, receive);
225 meshlink_set_receive_cb(mesh1, receive);
227 char *data = meshlink_export(mesh1);
229 assert(meshlink_import(mesh2, data));
231 data = meshlink_export(mesh2);
233 assert(meshlink_import(mesh1, data));
236 // Start both instances
239 meshlink_set_node_status_cb(mesh1, status_cb);
240 assert(meshlink_start(mesh1));
241 assert(meshlink_start(mesh2));
243 // Nodes should know each other
244 timeout.tv_sec = time(NULL) + 10;
245 pthread_mutex_lock(&reachable_lock);
247 while(reachable == false) {
248 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
251 pthread_mutex_unlock(&reachable_lock);
253 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
255 meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
258 assert(meshlink_send(mesh1, bar, "test", 5));
260 meshlink_blacklist(mesh1, foo);
262 // Passing NULL as mesh handle but with valid node handle 'foo'
264 meshlink_whitelist(NULL, foo);
265 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
269 meshlink_close(mesh2);
270 meshlink_close(mesh1);
271 meshlink_destroy("whitelist_conf.3");
272 meshlink_destroy("whitelist_conf.4");
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);
282 /* Test Steps for meshlink_whitelist Test Case # 3
285 1. Calling meshlink_whitelist with NULL as node handle argument.
288 meshlink_whitelist API handles the invalid parameter when called by giving proper error number.
290 static bool test_steps_mesh_whitelist_03(void) {
291 // Open meshlink instance.
293 meshlink_destroy("whitelist_conf");
294 meshlink_handle_t *mesh = meshlink_open("whitelist_conf", "foo", "test", DEV_CLASS_BACKBONE);
298 assert(meshlink_start(mesh));
300 meshlink_whitelist(mesh, NULL);
301 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
305 meshlink_close(mesh);
306 meshlink_destroy("whitelist_conf");
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)
320 total_tests += sizeof(blackbox_whitelist_tests) / sizeof(blackbox_whitelist_tests[0]);
322 return cmocka_run_group_tests(blackbox_whitelist_tests, NULL, NULL);