]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_channel_blacklist.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_channel_blacklist.c
1 /*
2     test_optimal_pmtu.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2019  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 #ifdef NDEBUG
21 #undef NDEBUG
22 #endif
23
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <setjmp.h>
27 #include <cmocka.h>
28 #include <assert.h>
29 #include <pthread.h>
30 #include "../common/containers.h"
31 #include "../common/test_step.h"
32 #include "../common/common_handlers.h"
33 #include "../common/network_namespace_framework.h"
34 #include "../../utils.h"
35 #include "test_cases_channel_blacklist.h"
36 #include "../test_case_channel_blacklist_01/node_sim_nut_01.h"
37
38 typedef bool (*test_step_func_t)(void);
39 extern int total_tests;
40
41 static bool test_steps_channel_blacklist_01(void);
42 static void test_case_channel_blacklist_01(void **state);
43
44 static int setup_test(void **state);
45 static int teardown_test(void **state);
46 static void *gen_inv(void *arg);
47
48 netns_state_t *test_channel_disconnection_state;
49
50 static mesh_arg_t relay_arg = {.node_name = "relay", .confbase = "relay", .app_name = "relay", .dev_class = 0 };
51 static mesh_arg_t peer_arg = {.node_name = "peer", .confbase = "peer", .app_name = "peer", .dev_class = 1 };
52 static mesh_arg_t nut_arg = {.node_name = "nut", .confbase = "nut", .app_name = "nut", .dev_class = 1 };
53 static mesh_invite_arg_t relay_nut_invite_arg = {.mesh_arg = &relay_arg, .invitee_name = "nut" };
54 static mesh_invite_arg_t relay_peer_invite_arg = {.mesh_arg = &relay_arg, .invitee_name = "peer" };
55 static netns_thread_t netns_relay_nut_invite = {.namespace_name = "relay", .netns_thread = gen_inv, .arg = &relay_nut_invite_arg};
56 static netns_thread_t netns_relay_peer_invite = {.namespace_name = "relay", .netns_thread = gen_inv, .arg = &relay_peer_invite_arg};
57 static netns_thread_t netns_relay_handle = {.namespace_name = "relay", .netns_thread = test_channel_blacklist_disonnection_relay_01, .arg = &relay_arg};
58 static netns_thread_t netns_peer_handle = {.namespace_name = "peer", .netns_thread = test_channel_blacklist_disonnection_peer_01, .arg = &peer_arg};
59 static netns_thread_t netns_nut_handle = {.namespace_name = "nut", .netns_thread = test_channel_blacklist_disonnection_nut_01, .arg = &nut_arg};
60
61 struct sync_flag test_channel_discon_nut_close = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
62
63 static int setup_test(void **state) {
64         (void)state;
65
66         netns_create_topology(test_channel_disconnection_state);
67         fprintf(stderr, "\nCreated topology\n");
68
69         assert(meshlink_destroy("nut"));
70         assert(meshlink_destroy("peer"));
71         assert(meshlink_destroy("relay"));
72         channel_discon_case_ping = false;
73         channel_discon_network_failure_01 = false;
74         channel_discon_network_failure_02 = false;
75         test_channel_restart_01 = false;
76         set_sync_flag(&test_channel_discon_nut_close, false);
77
78         return EXIT_SUCCESS;
79 }
80
81 static int teardown_test(void **state) {
82         (void)state;
83
84         assert(meshlink_destroy("nut"));
85         assert(meshlink_destroy("peer"));
86         assert(meshlink_destroy("relay"));
87         netns_destroy_topology(test_channel_disconnection_state);
88
89         return EXIT_SUCCESS;
90 }
91
92 static void execute_test(test_step_func_t step_func, void **state) {
93         (void)state;
94
95
96         fprintf(stderr, "\n\x1b[32mRunning Test\x1b[0m\n");
97         bool test_result = step_func();
98
99         if(!test_result) {
100                 fail();
101         }
102 }
103
104 static void *gen_inv(void *arg) {
105         mesh_invite_arg_t *mesh_invite_arg = (mesh_invite_arg_t *)arg;
106         meshlink_handle_t *mesh;
107         mesh = meshlink_open(mesh_invite_arg->mesh_arg->node_name, mesh_invite_arg->mesh_arg->confbase, mesh_invite_arg->mesh_arg->app_name, mesh_invite_arg->mesh_arg->dev_class);
108         assert(mesh);
109
110         char *invitation = meshlink_invite(mesh, NULL, mesh_invite_arg->invitee_name);
111         assert(invitation);
112         mesh_invite_arg->invite_str = invitation;
113         meshlink_close(mesh);
114
115         return NULL;
116 }
117
118 static void launch_3_nodes(void) {
119         run_node_in_namespace_thread(&netns_relay_nut_invite);
120         sleep(1);
121         assert(relay_nut_invite_arg.invite_str);
122         nut_arg.join_invitation = relay_nut_invite_arg.invite_str;
123
124         run_node_in_namespace_thread(&netns_relay_peer_invite);
125         sleep(1);
126         assert(relay_peer_invite_arg.invite_str);
127         peer_arg.join_invitation = relay_peer_invite_arg.invite_str;
128
129         relay_arg.join_invitation = NULL;
130
131         run_node_in_namespace_thread(&netns_relay_handle);
132         sleep(1);
133
134         run_node_in_namespace_thread(&netns_peer_handle);
135         sleep(1);
136
137         run_node_in_namespace_thread(&netns_nut_handle);
138 }
139
140 static void test_case_channel_blacklist_01(void **state) {
141         execute_test(test_steps_channel_blacklist_01, state);
142         return;
143 }
144
145 static bool test_steps_channel_blacklist_01(void) {
146
147         launch_3_nodes();
148
149         wait_sync_flag(&test_channel_discon_nut_close, 240);
150
151         test_channel_blacklist_disonnection_peer_01_running = false;
152         test_channel_blacklist_disonnection_relay_01_running = false;
153         assert_int_equal(total_reachable_callbacks_01, 1);
154         assert_int_equal(total_unreachable_callbacks_01, 1);
155         assert_int_equal(total_channel_closure_callbacks_01, 2);
156
157         return true;
158 }
159
160 int test_meshlink_channel_blacklist(void) {
161
162         interface_t relay_ifs[] = { { .if_peer = "wan_bridge" } };
163         namespace_t relay = {
164                 .name = "relay",
165                 .type = HOST,
166                 .interfaces = relay_ifs,
167                 .interfaces_no = 1,
168         };
169
170         interface_t peer_ifs[] = { { .if_peer = "wan_bridge" } };
171         namespace_t peer = {
172                 .name = "peer",
173                 .type = HOST,
174                 .interfaces = peer_ifs,
175                 .interfaces_no = 1,
176         };
177
178         interface_t nut_ifs[] = { { .if_peer = "wan_bridge" } };
179         namespace_t nut = {
180                 .name = "nut",
181                 .type = HOST,
182                 .interfaces = nut_ifs,
183                 .interfaces_no = 1,
184         };
185
186         interface_t wan_ifs[] = { { .if_peer = "peer" }, { .if_peer = "nut" }, { .if_peer = "relay" } };
187         namespace_t wan_bridge = {
188                 .name = "wan_bridge",
189                 .type = BRIDGE,
190                 .interfaces = wan_ifs,
191                 .interfaces_no = 3,
192         };
193
194         namespace_t test_channel_nodes[] = {  relay, wan_bridge, nut, peer };
195
196         netns_state_t test_channels_nodes = {
197                 .test_case_name =  "test_case_channel",
198                 .namespaces =  test_channel_nodes,
199                 .num_namespaces = 4,
200         };
201         test_channel_disconnection_state = &test_channels_nodes;
202
203         const struct CMUnitTest blackbox_group0_tests[] = {
204                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_blacklist_01, setup_test, teardown_test,
205                                 (void *)NULL),
206         };
207         total_tests += sizeof(blackbox_group0_tests) / sizeof(blackbox_group0_tests[0]);
208
209         return cmocka_run_group_tests(blackbox_group0_tests, NULL, NULL);
210 }