]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_blacklist.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_blacklist.c
1 /*
2     test_cases_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 /* Modify this to change the logging level of Meshlink */
21 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
22
23 #include "execute_tests.h"
24 #include "test_cases_blacklist.h"
25 #include "../common/containers.h"
26 #include "../common/test_step.h"
27 #include "../common/common_handlers.h"
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <setjmp.h>
31 #include <cmocka.h>
32 #include <assert.h>
33 #include <string.h>
34
35 static void test_case_mesh_blacklist_01(void **state);
36 static bool test_steps_mesh_blacklist_01(void);
37 static void test_case_mesh_blacklist_02(void **state);
38 static bool test_steps_mesh_blacklist_02(void);
39 static void test_case_mesh_blacklist_03(void **state);
40 static bool test_steps_mesh_blacklist_03(void);
41
42 /* State structure for meshlink_blacklist Test Case #1 */
43 static black_box_state_t test_mesh_blacklist_01_state = {
44         .test_case_name = "test_case_mesh_blacklist_01",
45 };
46
47 /* State structure for meshlink_blacklist Test Case #2 */
48 static black_box_state_t test_mesh_blacklist_02_state = {
49         .test_case_name = "test_case_mesh_blacklist_02",
50 };
51
52 /* State structure for meshlink_blacklist Test Case #3 */
53 static black_box_state_t test_mesh_blacklist_03_state = {
54         .test_case_name = "test_case_mesh_blacklist_03",
55 };
56
57 /* Execute meshlink_blacklist Test Case # 1*/
58 void test_case_mesh_blacklist_01(void **state) {
59         execute_test(test_steps_mesh_blacklist_01, state);
60 }
61
62 static bool received;
63
64 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
65         (void)mesh;
66
67         const char *msg = data;
68         assert(len);
69
70         if(!strcmp(src->name, "bar") && len == 5 && !strcmp(msg, "test")) {
71                 received = true;
72         }
73 }
74
75 static bool bar_reachable;
76
77 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
78         (void)mesh;
79
80         if(!strcmp(node->name, "bar") && reachable) {
81                 bar_reachable = true;
82         }
83 }
84
85
86 /* Test Steps for meshlink_blacklist Test Case # 1
87
88     Test Steps:
89     1. Open both the node instances
90     2. Join bar node with foo and Send & Receive data
91     3. Blacklist bar and Send & Receive data
92
93     Expected Result:
94     When default blacklist is disabled, foo node should receive data from bar
95     but when enabled foo node should not receive data
96 */
97 bool test_steps_mesh_blacklist_01(void) {
98         meshlink_destroy("blacklist_conf.1");
99         meshlink_destroy("blacklist_conf.2");
100
101         // Open two new meshlink instance.
102         meshlink_handle_t *mesh1 = meshlink_open("blacklist_conf.1", "foo", "blacklist", DEV_CLASS_BACKBONE);
103         assert(mesh1 != NULL);
104         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
105         meshlink_handle_t *mesh2 = meshlink_open("blacklist_conf.2", "bar", "blacklist", DEV_CLASS_BACKBONE);
106         assert(mesh2 != NULL);
107         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
108         meshlink_set_receive_cb(mesh1, receive);
109
110         // Start both instances
111         bar_reachable = false;
112         meshlink_set_node_status_cb(mesh1, status_cb);
113         assert(meshlink_start(mesh1));
114         assert(meshlink_start(mesh2));
115         sleep(1);
116
117         char *foo_export = meshlink_export(mesh1);
118         assert(foo_export != NULL);
119         assert(meshlink_import(mesh2, foo_export));
120         char *bar_export = meshlink_export(mesh2);
121         assert(meshlink_import(mesh1, bar_export));
122         sleep(5);
123         assert_int_equal(bar_reachable, true);
124
125         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
126         assert(bar);
127         meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
128         assert(foo);
129
130         received = false;
131         assert(meshlink_send(mesh2, foo, "test", 5));
132         sleep(1);
133         assert(received);
134
135         meshlink_blacklist(mesh1, bar);
136
137         received = false;
138         assert(meshlink_send(mesh2, foo, "test", 5));
139         sleep(1);
140         assert_int_equal(received, false);
141
142         // Clean up.
143         meshlink_close(mesh2);
144         meshlink_close(mesh1);
145         meshlink_destroy("blacklist_conf.1");
146         meshlink_destroy("blacklist_conf.2");
147         return true;
148 }
149
150 /* Execute meshlink_blacklist Test Case # 2*/
151 void test_case_mesh_blacklist_02(void **state) {
152         execute_test(test_steps_mesh_blacklist_02, state);
153 }
154
155
156 /* Test Steps for meshlink_blacklist Test Case # 2
157
158     Test Steps:
159     1. Calling meshlink_blacklist with NULL as mesh handle argument.
160
161     Expected Result:
162     meshlink_blacklist API handles the invalid parameter when called by giving proper error number.
163 */
164 bool test_steps_mesh_blacklist_02(void) {
165         meshlink_destroy("blacklist_conf.3");
166
167         // Open two new meshlink instance.
168         meshlink_handle_t *mesh = meshlink_open("blacklist_conf.3", "foo", "blacklist", DEV_CLASS_BACKBONE);
169         assert(mesh != NULL);
170
171         meshlink_node_t *node = meshlink_get_self(mesh);
172         assert(node);
173
174         // Passing NULL as mesh handle and node handle being some valid node handle
175         meshlink_blacklist(NULL, node);
176         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
177
178         // Clean up.
179         meshlink_close(mesh);
180         meshlink_destroy("blacklist_conf.3");
181         return true;
182 }
183
184 /* Execute meshlink_blacklist Test Case # 3*/
185 void test_case_mesh_blacklist_03(void **state) {
186         execute_test(test_steps_mesh_blacklist_03, state);
187 }
188
189 /* Test Steps for meshlink_blacklist Test Case # 3
190
191     Test Steps:
192     1. Create node instance
193     2. Calling meshlink_blacklist with NULL as node handle argument.
194
195     Expected Result:
196     meshlink_blacklist API handles the invalid parameter when called by giving proper error number.
197 */
198 bool test_steps_mesh_blacklist_03(void) {
199         meshlink_destroy("blacklist_conf.4");
200
201         // Open two new meshlink instance.
202         meshlink_handle_t *mesh = meshlink_open("blacklist_conf.4", "foo", "blacklist", DEV_CLASS_BACKBONE);
203         assert(mesh != NULL);
204
205         // Passing NULL as node handle and mesh handle being some valid mesh handle value
206         meshlink_blacklist(mesh, NULL);
207         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
208
209         // Clean up.
210         meshlink_close(mesh);
211         meshlink_destroy("blacklist_conf.4");
212         return true;
213 }
214
215 int test_meshlink_blacklist(void) {
216         const struct CMUnitTest blackbox_blacklist_tests[] = {
217                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_01, NULL, NULL,
218                                 (void *)&test_mesh_blacklist_01_state),
219                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_02, NULL, NULL,
220                                 (void *)&test_mesh_blacklist_02_state),
221                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_blacklist_03, NULL, NULL,
222                                 (void *)&test_mesh_blacklist_03_state)
223         };
224
225         total_tests += sizeof(blackbox_blacklist_tests) / sizeof(blackbox_blacklist_tests[0]);
226
227         return cmocka_run_group_tests(blackbox_blacklist_tests, NULL, NULL);
228 }
229