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