]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_default_blacklist.c
Add the blackbox container based test suite.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_default_blacklist.c
1 /*
2     test_cases_default_blacklist.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 <stdlib.h>
21 #include <stdarg.h>
22 #include <setjmp.h>
23 #include <cmocka.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "execute_tests.h"
27 #include "test_cases_default_blacklist.h"
28 #include "../common/containers.h"
29 #include "../common/test_step.h"
30 #include "../common/common_handlers.h"
31 #include "../../utils.h"
32
33 /* Modify this to change the logging level of Meshlink */
34 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
35
36 static void test_case_mesh_default_blacklist_01(void **state);
37 static bool test_steps_mesh_default_blacklist_01(void);
38 static void test_case_mesh_default_blacklist_02(void **state);
39 static bool test_steps_mesh_default_blacklist_02(void);
40
41 /* State structure for meshlink_default_blacklist Test Case #1 */
42 static black_box_state_t test_mesh_default_blacklist_01_state = {
43         .test_case_name = "test_case_mesh_default_blacklist_01",
44 };
45
46 /* State structure for meshlink_default_blacklist Test Case #2 */
47 static black_box_state_t test_mesh_default_blacklist_02_state = {
48         .test_case_name = "test_case_mesh_default_blacklist_02",
49 };
50
51 /* Execute meshlink_default_blacklist Test Case # 1*/
52 static void test_case_mesh_default_blacklist_01(void **state) {
53         execute_test(test_steps_mesh_default_blacklist_01, state);
54         return;
55 }
56
57 static bool received = false;
58
59 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
60         assert(len);
61
62         if(!strcmp(src->name, "bar") || !strcmp(src->name, "foz")) {
63                 received = true;
64         }
65 }
66
67 static bool bar_reachable = false;
68 static bool foz_reachable = false;
69
70 void status_cb1(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
71         if(!strcmp(node->name, "bar")) {
72                 bar_reachable = reachable;
73         } else if(!strcmp(node->name, "foz")) {
74                 foz_reachable = reachable;
75         }
76 }
77
78 /* Test Steps for meshlink_default_blacklist Test Case # 1
79
80     Test Steps:
81     1. Open all the node instances & Disable default blacklist
82     2. Join bar node with foo and Send & Receive data
83     3. Enable default blacklist and join foz node with foo node
84         and follow the steps done for bar node
85
86     Expected Result:
87     When default blacklist is disabled, foo node should receive data from bar
88     but when enabled foo node should not receive data from foz
89 */
90 static bool test_steps_mesh_default_blacklist_01(void) {
91         meshlink_destroy("def_blacklist_conf.1");
92         meshlink_destroy("def_blacklist_conf.2");
93         meshlink_destroy("def_blacklist_conf.3");
94
95         // Open two new meshlink instance.
96         meshlink_handle_t *mesh1 = meshlink_open("def_blacklist_conf.1", "foo", "blacklist", DEV_CLASS_BACKBONE);
97         assert(mesh1 != NULL);
98         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
99         meshlink_handle_t *mesh2 = meshlink_open("def_blacklist_conf.2", "bar", "blacklist", DEV_CLASS_BACKBONE);
100         assert(mesh2 != NULL);
101         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
102         meshlink_handle_t *mesh3 = meshlink_open("def_blacklist_conf.3", "foz", "blacklist", DEV_CLASS_BACKBONE);
103         assert(mesh3);
104         meshlink_set_log_cb(mesh3, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
105         meshlink_set_receive_cb(mesh1, receive);
106
107         meshlink_set_default_blacklist(mesh1, false);
108
109         // Start both instances
110         bar_reachable = false;
111         foz_reachable = false;
112         meshlink_set_node_status_cb(mesh1, status_cb1);
113         assert(meshlink_start(mesh1));
114         assert(meshlink_start(mesh2));
115         assert(meshlink_start(mesh3));
116         sleep(1);
117
118         char *foo_export = meshlink_export(mesh1);
119         assert(foo_export != NULL);
120         assert(meshlink_import(mesh2, foo_export));
121         char *bar_export = meshlink_export(mesh2);
122         assert(meshlink_import(mesh1, bar_export));
123         sleep(5);
124         assert(bar_reachable);
125
126         // Nodes should learn about each other
127         meshlink_node_t *foo = NULL;
128         foo = meshlink_get_node(mesh2, "foo");
129         assert(foo);
130
131         received = false;
132         assert(meshlink_send(mesh2, foo, "test", 5));
133         assert_after(received, 2);
134
135         // Enable default blacklist and join another node
136         meshlink_set_default_blacklist(mesh1, true);
137
138         char *foz_export = meshlink_export(mesh3);
139         assert(foz_export);
140         assert(meshlink_import(mesh1, foz_export));
141         assert(meshlink_import(mesh3, foo_export));
142         sleep(5);
143         assert(foz_reachable);
144
145         foo = meshlink_get_node(mesh3, "foo");
146         assert(foo);
147         assert(meshlink_send(mesh3, foo, "test", 5));
148         received = false;
149         assert(meshlink_send(mesh3, foo, "test", 5));
150         assert_after(!received, 2);
151
152         // Clean up.
153         free(foo_export);
154         free(foz_export);
155         free(bar_export);
156         meshlink_close(mesh1);
157         meshlink_close(mesh2);
158         meshlink_close(mesh3);
159         meshlink_destroy("def_blacklist_conf.1");
160         meshlink_destroy("def_blacklist_conf.2");
161         meshlink_destroy("def_blacklist_conf.3");
162
163         return true;
164 }
165
166 /* Execute meshlink_default_blacklist Test Case # 2*/
167 static void test_case_mesh_default_blacklist_02(void **state) {
168         execute_test(test_steps_mesh_default_blacklist_02, state);
169 }
170
171 /* Test Steps for meshlink_default_blacklist Test Case # 2
172
173     Test Steps:
174     1. Calling meshlink_default_blacklist with NULL as mesh handle argument.
175
176     Expected Result:
177     meshlink_default_blacklist API handles the invalid parameter when called by giving proper error number.
178 */
179 static bool test_steps_mesh_default_blacklist_02(void) {
180         // Passing NULL as mesh handle argument to the API
181         meshlink_set_default_blacklist(NULL, true);
182         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
183
184         return true;
185 }
186
187 int test_meshlink_default_blacklist(void) {
188         const struct CMUnitTest blackbox_default_blacklist_tests[] = {
189                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_default_blacklist_01, NULL, NULL,
190                                 (void *)&test_mesh_default_blacklist_01_state),
191                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_default_blacklist_02, NULL, NULL,
192                                 (void *)&test_mesh_default_blacklist_02_state)
193         };
194
195         total_tests += sizeof(blackbox_default_blacklist_tests) / sizeof(blackbox_default_blacklist_tests[0]);
196
197         return cmocka_run_group_tests(blackbox_default_blacklist_tests, NULL, NULL);
198 }