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.
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"
38 #include "../../utils.h"
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);
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",
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",
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",
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;
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);
76 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
83 pthread_mutex_lock(& lock_receive);
85 assert(!pthread_cond_broadcast(&receive_cond));
86 pthread_mutex_unlock(& lock_receive);
90 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reach) {
93 if(!strcmp(node->name, "bar")) {
94 pthread_mutex_lock(&reachable_lock);
96 assert(!pthread_cond_broadcast(&reachable_cond));
97 pthread_mutex_unlock(&reachable_lock);
102 /* Test Steps for meshlink_whitelist Test Case # 1
105 1. Run 2 node instances
106 2. Blacklist one node and again whitelist the blacklisted node
109 meshlink_whitelist API whitelists the blacklisted node
111 static bool test_steps_mesh_whitelist_01(void) {
112 struct timespec timeout = {0};
114 // Open two new meshlink instance.
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);
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);
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);
127 // Export & Import to join the mesh
130 char *data = meshlink_export(mesh1);
132 assert(meshlink_import(mesh2, data));
134 data = meshlink_export(mesh2);
136 assert(meshlink_import(mesh1, data));
139 // Start both instances
141 meshlink_set_node_status_cb(mesh1, status_cb);
142 assert(meshlink_start(mesh1));
143 assert(meshlink_start(mesh2));
145 // Nodes should know each other
146 timeout.tv_sec = time(NULL) + 10;
147 pthread_mutex_lock(&reachable_lock);
149 while(reachable == false) {
150 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
153 pthread_mutex_unlock(&reachable_lock);
156 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
158 meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
162 assert(meshlink_send(mesh1, bar, "test", 5));
163 timeout.tv_sec = time(NULL) + 10;
164 pthread_mutex_lock(& lock_receive);
166 if(rec_stat == false) {
167 assert(pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout) == 0);
170 pthread_mutex_unlock(& lock_receive);
173 assert(meshlink_blacklist(mesh1, foo));
176 assert(meshlink_send(mesh1, bar, "test", 5));
177 timeout.tv_sec = time(NULL) + 10;
178 pthread_mutex_lock(& lock_receive);
180 if(rec_stat == false) {
181 int err = pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout);
182 assert(err == ETIMEDOUT);
185 pthread_mutex_unlock(& lock_receive);
186 assert(meshlink_whitelist(mesh1, foo));
189 bool result = meshlink_send(mesh2, foo, "test", 5);
190 timeout.tv_sec = time(NULL) + 10;
191 pthread_mutex_lock(& lock_receive);
193 if(rec_stat == false) {
194 assert(pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout) == 0);
197 pthread_mutex_unlock(& lock_receive);
201 meshlink_close(mesh2);
202 meshlink_close(mesh1);
203 assert(meshlink_destroy("whitelist_conf.1"));
204 assert(meshlink_destroy("whitelist_conf.2"));
209 /* Test Steps for meshlink_whitelist Test Case # 2
212 1. Calling meshlink_whitelist with NULL as mesh handle argument.
215 meshlink_whitelist API handles the invalid parameter when called by giving proper error number.
217 static void test_case_mesh_whitelist_02(void **state) {
218 execute_test(test_steps_mesh_whitelist_02, state);
221 /* Test Steps for meshlink_whitelist Test Case # 2*/
222 static bool test_steps_mesh_whitelist_02(void) {
223 struct timespec timeout = {0};
225 // Open two new meshlink instance.
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);
231 meshlink_handle_t *mesh2 = meshlink_open("whitelist_conf.4", "bar", "test", DEV_CLASS_BACKBONE);
233 meshlink_set_receive_cb(mesh2, receive);
234 meshlink_set_receive_cb(mesh1, receive);
236 char *data = meshlink_export(mesh1);
238 assert(meshlink_import(mesh2, data));
240 data = meshlink_export(mesh2);
242 assert(meshlink_import(mesh1, data));
245 // Start both instances
248 meshlink_set_node_status_cb(mesh1, status_cb);
249 assert(meshlink_start(mesh1));
250 assert(meshlink_start(mesh2));
252 // Nodes should know each other
253 timeout.tv_sec = time(NULL) + 10;
254 pthread_mutex_lock(&reachable_lock);
256 while(reachable == false) {
257 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
260 pthread_mutex_unlock(&reachable_lock);
262 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
264 meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
267 assert(meshlink_send(mesh1, bar, "test", 5));
269 assert(meshlink_blacklist(mesh1, foo));
271 // Passing NULL as mesh handle but with valid node handle 'foo'
273 assert(!meshlink_whitelist(NULL, foo));
274 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
278 meshlink_close(mesh2);
279 meshlink_close(mesh1);
280 assert(meshlink_destroy("whitelist_conf.3"));
281 assert(meshlink_destroy("whitelist_conf.4"));
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);
291 /* Test Steps for meshlink_whitelist Test Case # 3
294 1. Calling meshlink_whitelist with NULL as node handle argument.
297 meshlink_whitelist API handles the invalid parameter when called by giving proper error number.
299 static bool test_steps_mesh_whitelist_03(void) {
300 // Open meshlink instance.
302 assert(meshlink_destroy("whitelist_conf"));
303 meshlink_handle_t *mesh = meshlink_open("whitelist_conf", "foo", "test", DEV_CLASS_BACKBONE);
307 assert(meshlink_start(mesh));
309 assert(!meshlink_whitelist(mesh, NULL));
310 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
314 meshlink_close(mesh);
315 assert(meshlink_destroy("whitelist_conf"));
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)
329 total_tests += sizeof(blackbox_whitelist_tests) / sizeof(blackbox_whitelist_tests[0]);
331 return cmocka_run_group_tests(blackbox_whitelist_tests, NULL, NULL);