aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_plugin_cmd_nat_upnp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/test_transport_plugin_cmd_nat_upnp.c')
-rw-r--r--src/transport/test_transport_plugin_cmd_nat_upnp.c397
1 files changed, 397 insertions, 0 deletions
diff --git a/src/transport/test_transport_plugin_cmd_nat_upnp.c b/src/transport/test_transport_plugin_cmd_nat_upnp.c
new file mode 100644
index 000000000..86280c66b
--- /dev/null
+++ b/src/transport/test_transport_plugin_cmd_nat_upnp.c
@@ -0,0 +1,397 @@
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_ng_lib.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 const 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_Barrier *
91get_waiting_for_barriers ()
92{
93 struct GNUNET_TESTING_Barrier *barriers_head;
94 struct GNUNET_TESTING_Barrier *barriers_tail;
95 struct GNUNET_TESTING_Barrier *ready_to_connect;
96 struct GNUNET_TESTING_Barrier *test_case_finished;
97
98 ready_to_connect = GNUNET_new (struct GNUNET_TESTING_Barrier);
99 ready_to_connect->name = "ready-to-connect";
100 test_case_finished = GNUNET_new (struct GNUNET_TESTING_Barrier);
101 test_case_finished->name = "test-case-finished";
102 GNUNET_CONTAINER_DLL_insert (barriers_head, barriers_tail, ready_to_connect);
103 GNUNET_CONTAINER_DLL_insert (barriers_head, barriers_tail, test_case_finished);
104 return barriers_head;
105}
106
107
108static void
109barrier_advanced (const char *barrier_name)
110{
111 struct GNUNET_TESTING_Barrier *barrier = GNUNET_TESTING_get_barrier (is, barrier_name);
112
113 GNUNET_TESTING_finish_attached_cmds (is, barrier);
114}
115
116
117/**
118 * Callback to set the flag indicating all peers started. Will be called via the plugin api.
119 *
120 */
121static void
122all_peers_started ()
123{
124 const struct GNUNET_TESTING_AsyncContext *ac;
125
126 GNUNET_TESTING_get_trait_async_context (&block_send,
127 &ac);
128 GNUNET_assert (NULL != ac);
129 if (NULL == ac->cont)
130 GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) ac);
131 else
132 GNUNET_TESTING_async_finish ((struct GNUNET_TESTING_AsyncContext *) ac);
133}
134
135
136/**
137 * Function called with the final result of the test.
138 *
139 * @param cls the `struct MainParams`
140 * @param rv #GNUNET_OK if the test passed
141 */
142static void
143handle_result (void *cls,
144 enum GNUNET_GenericReturnValue rv)
145{
146 struct TestState *ts = cls;
147
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Local test exits with status %d\n",
150 rv);
151
152 ts->finished_cb ();
153 GNUNET_free (ts->testdir);
154 GNUNET_free (ts->cfgname);
155 GNUNET_TESTING_free_topology (ts->topology);
156 GNUNET_free (ts);
157}
158
159
160/**
161 * Callback from start peer cmd for signaling a peer got connected.
162 *
163 */
164static void *
165notify_connect (struct GNUNET_TESTING_Interpreter *is,
166 const struct GNUNET_PeerIdentity *peer)
167{
168 const struct ConnectPeersState *cps;
169 const struct GNUNET_TESTING_Command *cmd;
170
171 cmd = GNUNET_TESTING_interpreter_lookup_command (is,
172 "connect-peers");
173 GNUNET_TRANSPORT_get_trait_connect_peer_state (cmd,
174 &cps);
175 void *ret = NULL;
176
177 cps->notify_connect (is,
178 peer);
179 return ret;
180}
181
182
183/**
184 * Callback to set the flag indicating all peers are prepared to finish. Will be called via the plugin api.
185 */
186static void
187all_local_tests_prepared ()
188{
189 const struct LocalPreparedState *lfs;
190
191 GNUNET_TESTING_get_trait_local_prepared_state (&local_prepared,
192 &lfs);
193 GNUNET_assert (NULL != &lfs->ac);
194 if (NULL == lfs->ac.cont)
195 GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) &lfs->ac);
196 else
197 GNUNET_TESTING_async_finish ((struct
198 GNUNET_TESTING_AsyncContext *) &lfs->ac);
199}
200
201
202/**
203 * Function to start a local test case.
204 *
205 * @param write_message Callback to send a message to the master loop.
206 * @param router_ip Global address of the network namespace.
207 * @param node_ip Local address of a node i a network namespace.
208 * @param m The number of the node in a network namespace.
209 * @param n The number of the network namespace.
210 * @param local_m The number of nodes in a network namespace.
211 */
212static void
213start_testcase (TESTING_CMD_HELPER_write_cb write_message, char *router_ip,
214 char *node_ip,
215 char *m,
216 char *n,
217 char *local_m,
218 char *topology_data,
219 unsigned int *read_file,
220 TESTING_CMD_HELPER_finish_cb finished_cb)
221{
222
223 unsigned int n_int;
224 unsigned int m_int;
225 unsigned int local_m_int;
226 unsigned int num;
227 struct TestState *ts = GNUNET_new (struct TestState);
228 struct GNUNET_TESTING_NetjailTopology *topology;
229 unsigned int sscanf_ret = 0;
230
231 ts->finished_cb = finished_cb;
232 LOG (GNUNET_ERROR_TYPE_ERROR,
233 "n %s m %s\n",
234 n,
235 m);
236
237 if (GNUNET_YES == *read_file)
238 {
239 LOG (GNUNET_ERROR_TYPE_DEBUG,
240 "read from file\n");
241 topology = GNUNET_TESTING_get_topo_from_file (topology_data);
242 }
243 else
244 topology = GNUNET_TESTING_get_topo_from_string (topology_data);
245
246 ts->topology = topology;
247
248 errno = 0;
249 sscanf_ret = sscanf (m, "%u", &m_int);
250 if (errno != 0)
251 {
252 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
253 }
254 GNUNET_assert (0 < sscanf_ret);
255 errno = 0;
256 sscanf_ret = sscanf (n, "%u", &n_int);
257 if (errno != 0)
258 {
259 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
260 }
261 GNUNET_assert (0 < sscanf_ret);
262 errno = 0;
263 sscanf_ret = sscanf (local_m, "%u", &local_m_int);
264 if (errno != 0)
265 {
266 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
267 }
268 GNUNET_assert (0 < sscanf_ret);
269
270 if (0 == n_int)
271 num = m_int;
272 else
273 num = (n_int - 1) * local_m_int + m_int + topology->nodes_x;
274
275 block_send = GNUNET_TESTING_cmd_block_until_external_trigger (
276 "block");
277 block_receive = GNUNET_TESTING_cmd_block_until_external_trigger (
278 "block-receive");
279 connect_peers = GNUNET_TRANSPORT_cmd_connect_peers ("connect-peers",
280 "start-peer",
281 "system-create",
282 num,
283 topology,
284 0);
285 local_prepared = GNUNET_TESTING_cmd_local_test_prepared (
286 "local-test-prepared",
287 write_message);
288
289
290 GNUNET_asprintf (&ts->cfgname,
291 "test_transport_api2_tcp_node1.conf");
292
293 LOG (GNUNET_ERROR_TYPE_DEBUG,
294 "plugin cfgname: %s\n",
295 ts->cfgname);
296
297 LOG (GNUNET_ERROR_TYPE_DEBUG,
298 "node ip: %s\n",
299 node_ip);
300
301 GNUNET_asprintf (&ts->testdir,
302 "%s%s%s",
303 BASE_DIR,
304 m,
305 n);
306
307 struct GNUNET_MQ_MessageHandler handlers[] = {
308 GNUNET_MQ_hd_var_size (test,
309 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
310 struct GNUNET_TRANSPORT_TESTING_TestMessage,
311 ts),
312 GNUNET_MQ_handler_end ()
313 };
314
315 struct GNUNET_TESTING_Command commands[] = {
316 GNUNET_TESTING_cmd_system_create ("system-create",
317 ts->testdir),
318 GNUNET_TRANSPORT_cmd_start_peer ("start-peer",
319 "system-create",
320 num,
321 node_ip,
322 handlers,
323 ts->cfgname,
324 notify_connect,
325 GNUNET_NO),
326 GNUNET_TESTING_cmd_barrier_create ("ready-to-connect",
327 0.0,
328 2),
329 connect_peers,
330 GNUNET_TRANSPORT_cmd_send_simple ("send-simple",
331 "start-peer",
332 "system-create",
333 num,
334 topology),
335 block_receive,
336 GNUNET_TESTING_cmd_barrier_create ("test-case-finished",
337 0.0,
338 2),
339 GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer",
340 "start-peer"),
341 GNUNET_TESTING_cmd_system_destroy ("system-destroy",
342 "system-create"),
343 GNUNET_TESTING_cmd_end ()
344 };
345
346 ts->write_message = write_message;
347
348 is = GNUNET_TESTING_run (commands,
349 TIMEOUT,
350 &handle_result,
351 ts);
352
353}
354
355
356/**
357 * Entry point for the plugin.
358 *
359 * @param cls NULL
360 * @return the exported block API
361 */
362void *
363libgnunet_test_transport_plugin_cmd_nat_upnp_init (void *cls)
364{
365 struct GNUNET_TESTING_PluginFunctions *api;
366
367 GNUNET_log_setup ("simple-send",
368 "DEBUG",
369 NULL);
370
371 api = GNUNET_new (struct GNUNET_TESTING_PluginFunctions);
372 api->start_testcase = &start_testcase;
373 api->all_peers_started = &all_peers_started;
374 api->all_local_tests_prepared = all_local_tests_prepared;
375 api->barrier_advanced = barrier_advanced;
376 api->get_waiting_for_barriers = get_waiting_for_barriers;
377 return api;
378}
379
380
381/**
382 * Exit point from the plugin.
383 *
384 * @param cls the return value from #libgnunet_test_transport_plugin_block_test_init
385 * @return NULL
386 */
387void *
388libgnunet_test_transport_plugin_cmd_simple_send_done (void *cls)
389{
390 struct GNUNET_TESTING_PluginFunctions *api = cls;
391
392 GNUNET_free (api);
393 return NULL;
394}
395
396
397/* end of plugin_cmd_simple_send.c */