aboutsummaryrefslogtreecommitdiff
path: root/src/service/transport/test_transport_plugin_cmd_simple_send_dv.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/transport/test_transport_plugin_cmd_simple_send_dv.c')
-rw-r--r--src/service/transport/test_transport_plugin_cmd_simple_send_dv.c434
1 files changed, 434 insertions, 0 deletions
diff --git a/src/service/transport/test_transport_plugin_cmd_simple_send_dv.c b/src/service/transport/test_transport_plugin_cmd_simple_send_dv.c
new file mode 100644
index 000000000..d35672cd9
--- /dev/null
+++ b/src/service/transport/test_transport_plugin_cmd_simple_send_dv.c
@@ -0,0 +1,434 @@
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_broadcast.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
46/**
47 * The number of messages received.
48 */
49static unsigned int number_received;
50
51static struct GNUNET_TESTING_Command block_send;
52
53static struct GNUNET_TESTING_Command block_receive;
54
55static struct GNUNET_TESTING_Command connect_peers;
56
57static struct GNUNET_TESTING_Command local_prepared;
58
59static struct GNUNET_TESTING_Command start_peer;
60
61static struct GNUNET_TESTING_Interpreter *is;
62
63/**
64 * Function called to check a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE being
65 * received.
66 *
67 */
68static int
69check_test (void *cls,
70 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
71{
72 GNUNET_assert (NULL != cls);
73 return GNUNET_OK;
74}
75
76
77/**
78 * Function called to handle a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE
79 * being received.
80 *
81 */
82static void
83handle_test (void *cls,
84 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
85{
86 struct GNUNET_PeerIdentity *peer = cls;
87 struct GNUNET_TESTING_AsyncContext *ac_block;
88 const struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map;
89 unsigned int connected;
90 struct GNUNET_TESTING_BlockState *bs;
91 struct GNUNET_TRANSPORT_CoreHandle *ch;
92 const struct GNUNET_TESTING_StartPeerState *sps;
93
94
95 GNUNET_TRANSPORT_get_trait_state (&start_peer,
96 &sps);
97 ch = sps->th;
98 GNUNET_TRANSPORT_get_trait_connected_peers_map (&start_peer,
99 &connected_peers_map);
100
101 if (NULL != connected_peers_map)
102 {
103 connected = GNUNET_CONTAINER_multishortmap_size (
104 connected_peers_map);
105
106 number_received++;
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108 "Received %u test message(s) from %s, %u connected peer(s)\n",
109 number_received,
110 GNUNET_i2s (peer),
111 connected);
112
113 GNUNET_TESTING_get_trait_async_context (&block_receive,
114 &ac_block);
115
116 if (connected == number_received)
117 {
118 if (NULL != ac_block->is)
119 {
120 GNUNET_assert (NULL != ac_block);
121 if (NULL == ac_block->cont)
122 GNUNET_TESTING_async_fail ((struct
123 GNUNET_TESTING_AsyncContext *) ac_block);
124 else
125 GNUNET_TESTING_async_finish ((struct
126 GNUNET_TESTING_AsyncContext *) ac_block);
127 }
128 else
129 {
130 GNUNET_TESTING_get_trait_block_state (
131 &block_receive,
132 &bs);
133 bs->asynchronous_finish = GNUNET_YES;
134 }
135
136 }
137 }
138 GNUNET_TRANSPORT_core_receive_continue (ch, peer);
139}
140
141
142struct GNUNET_TESTING_BarrierList *
143get_waiting_for_barriers ()
144{
145 // No Barrier
146 return GNUNET_new (struct GNUNET_TESTING_BarrierList);
147}
148
149
150/**
151 * Callback to set the flag indicating all peers started. Will be called via the plugin api.
152 *
153 */
154static void
155all_peers_started ()
156{
157 struct GNUNET_TESTING_AsyncContext *ac;
158
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Received message\n");
161 GNUNET_TESTING_get_trait_async_context (&block_send,
162 &ac);
163 GNUNET_assert (NULL != ac);
164 if (NULL == ac->cont)
165 GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) ac);
166 else
167 GNUNET_TESTING_async_finish ((struct GNUNET_TESTING_AsyncContext *) ac);
168}
169
170
171/**
172 * Function called with the final result of the test.
173 *
174 * @param cls the `struct MainParams`
175 * @param rv #GNUNET_OK if the test passed
176 */
177static void
178handle_result (void *cls,
179 enum GNUNET_GenericReturnValue rv)
180{
181 struct TestState *ts = cls;
182
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184 "Local test exits with status %d\n",
185 rv);
186 ts->finished_cb (rv);
187 GNUNET_free (ts->testdir);
188 GNUNET_free (ts->cfgname);
189 GNUNET_TESTING_free_topology (ts->topology);
190 GNUNET_free (ts);
191}
192
193
194/**
195 * Callback from start peer cmd for signaling a peer got connected.
196 *
197 */
198static void *
199notify_connect (struct GNUNET_TESTING_Interpreter *is,
200 const struct GNUNET_PeerIdentity *peer)
201{
202 const struct ConnectPeersState *cps;
203 const struct GNUNET_TESTING_Command *cmd;
204
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "notify_connect peer %s\n",
207 GNUNET_i2s (peer));
208 // FIXME: modifying future is a bit unclean, not easy to follow logic;
209 // might be better to when reaching the future command to look into
210 // the past...
211 cmd = GNUNET_TESTING_interpreter_lookup_command_all (is,
212 "connect-peers");
213 // FIXME: check return value!
214 GNUNET_TRANSPORT_get_trait_connect_peer_state (cmd,
215 &cps);
216 cps->notify_connect (is,
217 peer);
218 return NULL;
219}
220
221
222/**
223 * Callback to set the flag indicating all peers are prepared to finish. Will be called via the plugin api.
224 */
225static void
226all_local_tests_prepared ()
227{
228 const struct GNUNET_TESTING_LocalPreparedState *lfs;
229
230 GNUNET_TESTING_get_trait_local_prepared_state (&local_prepared,
231 &lfs);
232 GNUNET_assert (NULL != &lfs->ac);
233 if (NULL == lfs->ac.cont)
234 GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) &lfs->ac);
235 else
236 GNUNET_TESTING_async_finish ((struct
237 GNUNET_TESTING_AsyncContext *) &lfs->ac);
238}
239
240
241/**
242 * Function to start a local test case.
243 *
244 * @param write_message Callback to send a message to the master loop.
245 * @param router_ip Global address of the network namespace.
246 * @param node_ip The IP address of the node.
247 * @param m The number of the node in a network namespace.
248 * @param n The number of the network namespace.
249 * @param local_m The number of nodes in a network namespace.
250 */
251static struct GNUNET_TESTING_Interpreter *
252start_testcase (GNUNET_TESTING_cmd_helper_write_cb write_message,
253 const char *router_ip,
254 const char *node_ip,
255 const char *m,
256 const char *n,
257 const char *local_m,
258 const char *topology_data,
259 unsigned int *read_file,
260 GNUNET_TESTING_cmd_helper_finish_cb finished_cb)
261{
262 unsigned int n_int;
263 unsigned int m_int;
264 unsigned int local_m_int;
265 unsigned int num;
266 struct TestState *ts = GNUNET_new (struct TestState);
267 struct GNUNET_TESTING_NetjailTopology *topology;
268 struct GNUNET_MQ_MessageHandler handlers[] = {
269 GNUNET_MQ_hd_var_size (test,
270 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
271 struct GNUNET_TRANSPORT_TESTING_TestMessage,
272 ts),
273 GNUNET_MQ_handler_end ()
274 };
275 unsigned int sscanf_ret = 0;
276
277 ts->finished_cb = finished_cb;
278 LOG (GNUNET_ERROR_TYPE_ERROR,
279 "n %s m %s\n",
280 n,
281 m);
282
283 if (GNUNET_YES == *read_file)
284 {
285 LOG (GNUNET_ERROR_TYPE_DEBUG,
286 "read from file\n");
287 topology = GNUNET_TESTING_get_topo_from_file (topology_data);
288 }
289 else
290 topology = GNUNET_TESTING_get_topo_from_string (topology_data);
291
292 ts->topology = topology;
293
294 errno = 0;
295 sscanf_ret = sscanf (m, "%u", &m_int);
296 if (errno != 0)
297 {
298 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
299 }
300 GNUNET_assert (0 < sscanf_ret);
301 errno = 0;
302 sscanf_ret = sscanf (n, "%u", &n_int);
303 if (errno != 0)
304 {
305 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
306 }
307 GNUNET_assert (0 < sscanf_ret);
308 errno = 0;
309 sscanf_ret = sscanf (local_m, "%u", &local_m_int);
310 if (errno != 0)
311 {
312 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
313 }
314 GNUNET_assert (0 < sscanf_ret);
315
316 if (0 == n_int)
317 num = m_int;
318 else
319 num = (n_int - 1) * local_m_int + m_int + topology->nodes_x;
320
321 block_send = GNUNET_TESTING_cmd_block_until_external_trigger ("block");
322 block_receive = GNUNET_TESTING_cmd_block_until_external_trigger (
323 "block-receive");
324 connect_peers = GNUNET_TRANSPORT_cmd_connect_peers (
325 "connect-peers",
326 "start-peer",
327 "system-create",
328 num,
329 topology,
330 topology->additional_connects,
331 GNUNET_YES);
332 local_prepared = GNUNET_TESTING_cmd_local_test_prepared (
333 "local-test-prepared",
334 write_message);
335
336
337 GNUNET_asprintf (&ts->cfgname,
338 "test_transport_api2_tcp_node1.conf");
339
340 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 "plugin cfgname: %s\n",
342 ts->cfgname);
343
344 LOG (GNUNET_ERROR_TYPE_DEBUG,
345 "node ip: %s\n",
346 node_ip);
347
348 GNUNET_asprintf (&ts->testdir,
349 "%s%s%s",
350 BASE_DIR,
351 m,
352 n);
353
354 start_peer = GNUNET_TRANSPORT_cmd_start_peer ("start-peer",
355 "system-create",
356 num,
357 node_ip,
358 handlers,
359 ts->cfgname,
360 notify_connect,
361 GNUNET_NO);
362 struct GNUNET_TESTING_Command commands[] = {
363 GNUNET_TESTING_cmd_system_create ("system-create",
364 ts->testdir),
365 start_peer,
366 GNUNET_TESTING_cmd_send_peer_ready ("send-peer-ready",
367 write_message),
368 block_send,
369 connect_peers,
370 GNUNET_TRANSPORT_cmd_send_simple ("send-simple",
371 "start-peer",
372 "system-create",
373 num,
374 topology),
375 block_receive,
376 local_prepared,
377 GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer",
378 "start-peer"),
379 GNUNET_TESTING_cmd_system_destroy ("system-destroy",
380 "system-create"),
381 GNUNET_TESTING_cmd_end ()
382 };
383
384 ts->write_message = write_message;
385
386 is = GNUNET_TESTING_run (commands,
387 TIMEOUT,
388 &handle_result,
389 ts);
390 return is;
391}
392
393
394/**
395 * Entry point for the plugin.
396 *
397 * @param cls NULL
398 * @return the exported block API
399 */
400void *
401libgnunet_test_transport_plugin_cmd_simple_send_dv_init (void *cls)
402{
403 struct GNUNET_TESTING_PluginFunctions *api;
404
405 GNUNET_log_setup ("simple-send",
406 "DEBUG",
407 NULL);
408
409 api = GNUNET_new (struct GNUNET_TESTING_PluginFunctions);
410 api->start_testcase = &start_testcase;
411 api->all_peers_started = &all_peers_started;
412 api->all_local_tests_prepared = all_local_tests_prepared;
413 api->get_waiting_for_barriers = get_waiting_for_barriers;
414 return api;
415}
416
417
418/**
419 * Exit point from the plugin.
420 *
421 * @param cls the return value from #libgnunet_test_transport_plugin_block_test_init
422 * @return NULL
423 */
424void *
425libgnunet_test_transport_plugin_cmd_simple_send_dv_done (void *cls)
426{
427 struct GNUNET_TESTING_PluginFunctions *api = cls;
428
429 GNUNET_free (api);
430 return NULL;
431}
432
433
434/* end of plugin_cmd_simple_send_broadcast.c */