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) {
79 pthread_mutex_lock(& lock_receive);
81 assert(!pthread_cond_broadcast(&receive_cond));
82 pthread_mutex_unlock(& lock_receive);
86 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reach) {
89 if(!strcmp(node->name, "bar")) {
90 pthread_mutex_lock(&reachable_lock);
92 assert(!pthread_cond_broadcast(&reachable_cond));
93 pthread_mutex_unlock(&reachable_lock);
98 /* Test Steps for meshlink_whitelist Test Case # 1
101 1. Run 2 node instances
102 2. Blacklist one node and again whitelist the blacklisted node
105 meshlink_whitelist API whitelists the blacklisted node
107 static bool test_steps_mesh_whitelist_01(void) {
108 struct timespec timeout = {0};
110 // Open two new meshlink instance.
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);
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);
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);
123 // Export & Import to join the mesh
126 char *data = meshlink_export(mesh1);
128 assert(meshlink_import(mesh2, data));
130 data = meshlink_export(mesh2);
132 assert(meshlink_import(mesh1, data));
135 // Start both instances
137 meshlink_set_node_status_cb(mesh1, status_cb);
138 assert(meshlink_start(mesh1));
139 assert(meshlink_start(mesh2));
141 // Nodes should know each other
142 timeout.tv_sec = time(NULL) + 10;
143 pthread_mutex_lock(&reachable_lock);
145 while(reachable == false) {
146 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
149 pthread_mutex_unlock(&reachable_lock);
152 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
154 meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
158 assert(meshlink_send(mesh1, bar, "test", 5));
159 timeout.tv_sec = time(NULL) + 10;
160 pthread_mutex_lock(& lock_receive);
162 if(rec_stat == false) {
163 assert(pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout) == 0);
166 pthread_mutex_unlock(& lock_receive);
169 meshlink_blacklist(mesh1, foo);
172 assert(meshlink_send(mesh1, bar, "test", 5));
173 timeout.tv_sec = time(NULL) + 10;
174 pthread_mutex_lock(& lock_receive);
176 if(rec_stat == false) {
177 int err = pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout);
178 assert(err == ETIMEDOUT);
181 pthread_mutex_unlock(& lock_receive);
182 meshlink_whitelist(mesh1, foo);
185 bool result = meshlink_send(mesh2, foo, "test", 5);
186 timeout.tv_sec = time(NULL) + 10;
187 pthread_mutex_lock(& lock_receive);
189 if(rec_stat == false) {
190 assert(pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout) == 0);
193 pthread_mutex_unlock(& lock_receive);
197 meshlink_close(mesh2);
198 meshlink_close(mesh1);
199 meshlink_destroy("whitelist_conf.1");
200 meshlink_destroy("whitelist_conf.2");
205 /* Test Steps for meshlink_whitelist Test Case # 2
208 1. Calling meshlink_whitelist with NULL as mesh handle argument.
211 meshlink_whitelist API handles the invalid parameter when called by giving proper error number.
213 static void test_case_mesh_whitelist_02(void **state) {
214 execute_test(test_steps_mesh_whitelist_02, state);
217 /* Test Steps for meshlink_whitelist Test Case # 2*/
218 static bool test_steps_mesh_whitelist_02(void) {
219 struct timespec timeout = {0};
221 // Open two new meshlink instance.
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);
227 meshlink_handle_t *mesh2 = meshlink_open("whitelist_conf.4", "bar", "test", DEV_CLASS_BACKBONE);
229 meshlink_set_receive_cb(mesh2, receive);
230 meshlink_set_receive_cb(mesh1, receive);
232 char *data = meshlink_export(mesh1);
234 assert(meshlink_import(mesh2, data));
236 data = meshlink_export(mesh2);
238 assert(meshlink_import(mesh1, data));
241 // Start both instances
244 meshlink_set_node_status_cb(mesh1, status_cb);
245 assert(meshlink_start(mesh1));
246 assert(meshlink_start(mesh2));
248 // Nodes should know each other
249 timeout.tv_sec = time(NULL) + 10;
250 pthread_mutex_lock(&reachable_lock);
252 while(reachable == false) {
253 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
256 pthread_mutex_unlock(&reachable_lock);
258 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
260 meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
263 assert(meshlink_send(mesh1, bar, "test", 5));
265 meshlink_blacklist(mesh1, foo);
267 // Passing NULL as mesh handle but with valid node handle 'foo'
269 meshlink_whitelist(NULL, foo);
270 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
274 meshlink_close(mesh2);
275 meshlink_close(mesh1);
276 meshlink_destroy("whitelist_conf.3");
277 meshlink_destroy("whitelist_conf.4");
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);
287 /* Test Steps for meshlink_whitelist Test Case # 3
290 1. Calling meshlink_whitelist with NULL as node handle argument.
293 meshlink_whitelist API handles the invalid parameter when called by giving proper error number.
295 static bool test_steps_mesh_whitelist_03(void) {
296 // Open meshlink instance.
298 meshlink_destroy("whitelist_conf");
299 meshlink_handle_t *mesh = meshlink_open("whitelist_conf", "foo", "test", DEV_CLASS_BACKBONE);
303 assert(meshlink_start(mesh));
305 meshlink_whitelist(mesh, NULL);
306 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
310 meshlink_close(mesh);
311 meshlink_destroy("whitelist_conf");
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)
325 total_tests += sizeof(blackbox_whitelist_tests) / sizeof(blackbox_whitelist_tests[0]);
327 return cmocka_run_group_tests(blackbox_whitelist_tests, NULL, NULL);