aboutsummaryrefslogtreecommitdiff
path: root/src/service/transport/test_transport_plugin_cmd_nat_upnp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/transport/test_transport_plugin_cmd_nat_upnp.c')
-rw-r--r--src/service/transport/test_transport_plugin_cmd_nat_upnp.c368
1 files changed, 368 insertions, 0 deletions
diff --git a/src/service/transport/test_transport_plugin_cmd_nat_upnp.c b/src/service/transport/test_transport_plugin_cmd_nat_upnp.c
new file mode 100644
index 000000000..9d7c5d856
--- /dev/null
+++ b/src/service/transport/test_transport_plugin_cmd_nat_upnp.c
@@ -0,0 +1,368 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file testbed/plugin_cmd_simple_send.c
23 * @brief a plugin to provide the API for running test cases.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_testing_barrier.h"
28#include "gnunet_testing_netjail_lib.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_transport_application_service.h"
31#include "transport-testing2.h"
32#include "transport-testing-cmds.h"
33#include "gnunet_testing_barrier.h"
34
35/**
36 * Generic logging shortcut
37 */
38#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
39
40#define BASE_DIR "testdir"
41
42#define TOPOLOGY_CONFIG "test_transport_simple_send_topo.conf"
43
44#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
45
46static struct GNUNET_TESTING_Command block_send;
47
48static struct GNUNET_TESTING_Command block_receive;
49
50static struct GNUNET_TESTING_Command connect_peers;
51
52static struct GNUNET_TESTING_Command local_prepared;
53
54static struct GNUNET_TESTING_Interpreter *is;
55
56/**
57 * Function called to check a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE being
58 * received.
59 *
60 */
61static int
62check_test (void *cls,
63 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
64{
65 return GNUNET_OK;
66}
67
68
69/**
70 * Function called to handle a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE
71 * being received.
72 *
73 */
74static void
75handle_test (void *cls,
76 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
77{
78 struct GNUNET_TESTING_AsyncContext *ac;
79
80 GNUNET_TESTING_get_trait_async_context (&block_receive,
81 &ac);
82 GNUNET_assert (NULL != ac);
83 if (NULL == ac->cont)
84 GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) ac);
85 else
86 GNUNET_TESTING_async_finish ((struct GNUNET_TESTING_AsyncContext *) ac);
87}
88
89
90struct GNUNET_TESTING_BarrierList*
91get_waiting_for_barriers ()
92{
93 struct GNUNET_TESTING_BarrierList*barriers;
94 struct GNUNET_TESTING_BarrierListEntry *ble;
95
96 barriers = GNUNET_new (struct GNUNET_TESTING_BarrierList);
97 ble = GNUNET_new (struct GNUNET_TESTING_BarrierListEntry);
98 ble->barrier_name = "ready-to-connect";
99 ble->expected_reaches = 1;
100 GNUNET_CONTAINER_DLL_insert (barriers->head,
101 barriers->tail,
102 ble);
103
104 ble = GNUNET_new (struct GNUNET_TESTING_BarrierListEntry);
105 ble->barrier_name = "test-case-finished";
106 ble->expected_reaches = 1;
107 GNUNET_CONTAINER_DLL_insert (barriers->head,
108 barriers->tail,
109 ble);
110 return barriers;
111}
112
113
114/**
115 * Function called with the final result of the test.
116 *
117 * @param cls the `struct MainParams`
118 * @param rv #GNUNET_OK if the test passed
119 */
120static void
121handle_result (void *cls,
122 enum GNUNET_GenericReturnValue rv)
123{
124 struct TestState *ts = cls;
125
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Local test exits with status %d\n",
128 rv);
129
130 ts->finished_cb (rv);
131 GNUNET_free (ts->testdir);
132 GNUNET_free (ts->cfgname);
133 GNUNET_TESTING_free_topology (ts->topology);
134 GNUNET_free (ts);
135}
136
137
138/**
139 * Callback from start peer cmd for signaling a peer got connected.
140 *
141 */
142static void *
143notify_connect (struct GNUNET_TESTING_Interpreter *is,
144 const struct GNUNET_PeerIdentity *peer)
145{
146 const struct ConnectPeersState *cps;
147 const struct GNUNET_TESTING_Command *cmd;
148
149 cmd = GNUNET_TESTING_interpreter_lookup_command (is,
150 "connect-peers");
151 GNUNET_TRANSPORT_get_trait_connect_peer_state (cmd,
152 &cps);
153 void *ret = NULL;
154
155 cps->notify_connect (is,
156 peer);
157 return ret;
158}
159
160
161/**
162 * Function to start a local test case.
163 *
164 * @param write_message Callback to send a message to the master loop.
165 * @param router_ip Global address of the network namespace.
166 * @param node_ip The IP address of the node.
167 * @param m The number of the node in a network namespace.
168 * @param n The number of the network namespace.
169 * @param local_m The number of nodes in a network namespace.
170 * @param topology_data A file name for the file containing the topology configuration, or a string containing
171 * the topology configuration.
172 * @param read_file If read_file is GNUNET_YES this string is the filename for the topology configuration,
173 * if read_file is GNUNET_NO the string contains the topology configuration.
174 * @param finish_cb Callback function which writes a message from the helper process running on a netjail
175 * node to the master process * signaling that the test case running on the netjail node finished.
176 * @return Returns the struct GNUNET_TESTING_Interpreter of the command loop running on this netjail node.
177 */
178static struct GNUNET_TESTING_Interpreter *
179start_testcase (GNUNET_TESTING_cmd_helper_write_cb write_message,
180 const char *router_ip,
181 const char *node_ip,
182 const char *m,
183 const char *n,
184 const char *local_m,
185 const char *topology_data,
186 unsigned int *read_file,
187 GNUNET_TESTING_cmd_helper_finish_cb finished_cb)
188{
189
190 unsigned int n_int;
191 unsigned int m_int;
192 unsigned int local_m_int;
193 unsigned int num;
194 struct TestState *ts = GNUNET_new (struct TestState);
195 struct GNUNET_TESTING_NetjailTopology *topology;
196 unsigned int sscanf_ret = 0;
197
198 ts->finished_cb = finished_cb;
199 LOG (GNUNET_ERROR_TYPE_ERROR,
200 "n %s m %s\n",
201 n,
202 m);
203
204 if (GNUNET_YES == *read_file)
205 {
206 LOG (GNUNET_ERROR_TYPE_DEBUG,
207 "read from file\n");
208 topology = GNUNET_TESTING_get_topo_from_file (topology_data);
209 }
210 else
211 topology = GNUNET_TESTING_get_topo_from_string (topology_data);
212
213 ts->topology = topology;
214
215 errno = 0;
216 sscanf_ret = sscanf (m, "%u", &m_int);
217 if (errno != 0)
218 {
219 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
220 }
221 GNUNET_assert (0 < sscanf_ret);
222 errno = 0;
223 sscanf_ret = sscanf (n, "%u", &n_int);
224 if (errno != 0)
225 {
226 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
227 }
228 GNUNET_assert (0 < sscanf_ret);
229 errno = 0;
230 sscanf_ret = sscanf (local_m, "%u", &local_m_int);
231 if (errno != 0)
232 {
233 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
234 }
235 GNUNET_assert (0 < sscanf_ret);
236
237 if (0 == n_int)
238 num = m_int;
239 else
240 num = (n_int - 1) * local_m_int + m_int + topology->nodes_x;
241
242 block_send = GNUNET_TESTING_cmd_block_until_external_trigger (
243 "block");
244 block_receive = GNUNET_TESTING_cmd_block_until_external_trigger (
245 "block-receive");
246 connect_peers = GNUNET_TRANSPORT_cmd_connect_peers ("connect-peers",
247 "start-peer",
248 "system-create",
249 num,
250 topology,
251 0,
252 GNUNET_YES);
253 local_prepared = GNUNET_TESTING_cmd_local_test_prepared (
254 "local-test-prepared",
255 write_message);
256
257
258 GNUNET_asprintf (&ts->cfgname,
259 "test_transport_api2_tcp_node1.conf");
260
261 LOG (GNUNET_ERROR_TYPE_DEBUG,
262 "plugin cfgname: %s\n",
263 ts->cfgname);
264
265 LOG (GNUNET_ERROR_TYPE_DEBUG,
266 "node ip: %s\n",
267 node_ip);
268
269 GNUNET_asprintf (&ts->testdir,
270 "%s%s%s",
271 BASE_DIR,
272 m,
273 n);
274
275 struct GNUNET_MQ_MessageHandler handlers[] = {
276 GNUNET_MQ_hd_var_size (test,
277 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
278 struct GNUNET_TRANSPORT_TESTING_TestMessage,
279 ts),
280 GNUNET_MQ_handler_end ()
281 };
282
283 struct GNUNET_TESTING_Command commands[] = {
284 GNUNET_TESTING_cmd_system_create ("system-create",
285 ts->testdir),
286 GNUNET_TRANSPORT_cmd_start_peer ("start-peer",
287 "system-create",
288 num,
289 node_ip,
290 handlers,
291 ts->cfgname,
292 notify_connect,
293 GNUNET_NO),
294 GNUNET_TESTING_cmd_barrier_reached ("ready-to-connect-reached",
295 "ready-to-connect",
296 GNUNET_NO,
297 num,
298 GNUNET_NO,
299 write_message),
300 connect_peers,
301 GNUNET_TRANSPORT_cmd_send_simple ("send-simple",
302 "start-peer",
303 "system-create",
304 num,
305 topology),
306 block_receive,
307 GNUNET_TESTING_cmd_barrier_reached ("test-case-finished-reached",
308 "test-case-finished",
309 GNUNET_NO,
310 num,
311 GNUNET_NO,
312 write_message),
313 GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer",
314 "start-peer"),
315 GNUNET_TESTING_cmd_system_destroy ("system-destroy",
316 "system-create"),
317 GNUNET_TESTING_cmd_end ()
318 };
319
320 ts->write_message = write_message;
321
322 is = GNUNET_TESTING_run (commands,
323 TIMEOUT,
324 &handle_result,
325 ts);
326 return is;
327}
328
329
330/**
331 * Entry point for the plugin.
332 *
333 * @param cls NULL
334 * @return the exported block API
335 */
336void *
337libgnunet_test_transport_plugin_cmd_nat_upnp_init (void *cls)
338{
339 struct GNUNET_TESTING_PluginFunctions *api;
340
341 GNUNET_log_setup ("simple-send",
342 "DEBUG",
343 NULL);
344
345 api = GNUNET_new (struct GNUNET_TESTING_PluginFunctions);
346 api->start_testcase = &start_testcase;
347 api->get_waiting_for_barriers = get_waiting_for_barriers;
348 return api;
349}
350
351
352/**
353 * Exit point from the plugin.
354 *
355 * @param cls the return value from #libgnunet_test_transport_plugin_nat_upnp_init
356 * @return NULL
357 */
358void *
359libgnunet_test_transport_plugin_cmd_nat_upnp_done (void *cls)
360{
361 struct GNUNET_TESTING_PluginFunctions *api = cls;
362
363 GNUNET_free (api);
364 return NULL;
365}
366
367
368/* end of plugin_cmd_simple_send.c */