aboutsummaryrefslogtreecommitdiff
path: root/src/core/test_core_plugin_cmd_just_run.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/test_core_plugin_cmd_just_run.c')
-rw-r--r--src/core/test_core_plugin_cmd_just_run.c512
1 files changed, 512 insertions, 0 deletions
diff --git a/src/core/test_core_plugin_cmd_just_run.c b/src/core/test_core_plugin_cmd_just_run.c
new file mode 100644
index 000000000..176d055c8
--- /dev/null
+++ b/src/core/test_core_plugin_cmd_just_run.c
@@ -0,0 +1,512 @@
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 "gnunet_transport_core_service.h"
32#include "gnunet_testing_barrier.h"
33#include "gnunet_core_service.h"
34#include "gnunet_transport_testing_ng_lib.h"
35
36/**
37 * Generic logging shortcut
38 */
39#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
40
41#define BASE_DIR "testdir"
42
43#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
44
45#define MAX_RECEIVED 1000
46
47#define MESSAGE_SIZE 65000
48
49static struct GNUNET_TESTING_Command block_script;
50
51static struct GNUNET_TESTING_Command connect_peers;
52
53static struct GNUNET_TESTING_Command local_prepared;
54
55static struct GNUNET_TESTING_Command start_peer;
56
57static struct GNUNET_TESTING_Interpreter *is;
58
59static struct GNUNET_CONTAINER_MultiPeerMap *senders;
60
61struct TestState
62{
63 /**
64 * Callback to write messages to the master loop.
65 *
66 */
67 GNUNET_TESTING_cmd_helper_write_cb write_message;
68
69 /**
70 * Callback to notify the helper test case has finished.
71 */
72 GNUNET_TESTING_cmd_helper_finish_cb finished_cb;
73
74 /**
75 * The name for a specific test environment directory.
76 *
77 */
78 char *testdir;
79
80 /**
81 * The name for the configuration file of the specific node.
82 *
83 */
84 char *cfgname;
85
86 /**
87 * The complete topology information.
88 */
89 struct GNUNET_TESTING_NetjailTopology *topology;
90};
91
92struct Sender
93{
94 /**
95 * Number of received messages from sender.
96 */
97 unsigned long long num_received;
98
99 /**
100 * Sample mean time the message traveled.
101 */
102 struct GNUNET_TIME_Relative mean_time;
103
104 /**
105 * Time the first message was send.
106 */
107 struct GNUNET_TIME_Absolute time_first;
108};
109
110
111struct GNUNET_TESTING_BarrierList*
112get_waiting_for_barriers ()
113{
114 struct GNUNET_TESTING_BarrierList*barriers;
115 struct GNUNET_TESTING_BarrierListEntry *ble;
116
117 barriers = GNUNET_new (struct GNUNET_TESTING_BarrierList);
118 ble = GNUNET_new (struct GNUNET_TESTING_BarrierListEntry);
119 ble->barrier_name = "ready-to-connect";
120 ble->expected_reaches = 1;
121 GNUNET_CONTAINER_DLL_insert (barriers->head,
122 barriers->tail,
123 ble);
124
125 ble = GNUNET_new (struct GNUNET_TESTING_BarrierListEntry);
126 ble->barrier_name = "test-case-finished";
127 ble->expected_reaches = 1;
128 GNUNET_CONTAINER_DLL_insert (barriers->head,
129 barriers->tail,
130 ble);
131 return barriers;
132}
133
134
135/**
136 * Callback to set the flag indicating all peers started. Will be called via the plugin api.
137 *
138 */
139static void
140all_peers_started ()
141{
142}
143
144
145/**
146 * Function called with the final result of the test.
147 *
148 * @param cls the `struct MainParams`
149 * @param rv #GNUNET_OK if the test passed
150 */
151static void
152handle_result (void *cls,
153 enum GNUNET_GenericReturnValue rv)
154{
155 struct TestState *ts = cls;
156
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Local test exits with status %d\n",
159 rv);
160
161 ts->finished_cb (rv);
162 GNUNET_free (ts->testdir);
163 GNUNET_free (ts->cfgname);
164 GNUNET_TESTING_free_topology (ts->topology);
165 GNUNET_free (ts);
166}
167
168
169/**
170 * Callback from start peer cmd for signaling a peer got connected.
171 *
172 *
173static void *
174notify_connect (struct GNUNET_TESTING_Interpreter *is,
175 const struct GNUNET_PeerIdentity *peer)
176{
177 const struct ConnectPeersState *cps;
178 const struct GNUNET_TESTING_Command *cmd;
179
180 cmd = GNUNET_TESTING_interpreter_lookup_command (is,
181 "connect-peers");
182 GNUNET_TRANSPORT_get_trait_connect_peer_state (cmd,
183 &cps);
184 void *ret = NULL;
185
186 cps->notify_connect (is,
187 peer);
188 return ret;
189 }*/
190
191
192/**
193 * Callback to set the flag indicating all peers are prepared to finish. Will be called via the plugin api.
194 */
195static void
196all_local_tests_prepared ()
197{
198 const struct GNUNET_TESTING_LocalPreparedState *lfs;
199
200 GNUNET_TESTING_get_trait_local_prepared_state (&local_prepared,
201 &lfs);
202 GNUNET_assert (NULL != &lfs->ac);
203 if (NULL == lfs->ac.cont)
204 GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) &lfs->ac);
205 else
206 GNUNET_TESTING_async_finish ((struct
207 GNUNET_TESTING_AsyncContext *) &lfs->ac);
208}
209
210
211static void
212child_completed_callback (void *cls,
213 enum GNUNET_OS_ProcessStatusType type,
214 long unsigned int exit_code)
215{
216
217}
218
219
220/**
221 * Function called to check a message being
222 * received.
223 *
224 */
225static int
226check_encrypted (void *cls, struct GNUNET_MessageHeader *header)
227{
228 return GNUNET_OK;
229}
230
231
232static void
233core_receive_continue (struct GNUNET_PeerIdentity *peer)
234{
235 const struct GNUNET_TESTING_StartPeerState *sps;
236
237 GNUNET_TESTING_get_trait_state (&start_peer,
238 &sps);
239
240 LOG (GNUNET_ERROR_TYPE_DEBUG,
241 "Executing core receive continue\n");
242
243 GNUNET_TRANSPORT_core_receive_continue (sps->th, peer);
244}
245
246
247/*static void
248handle_core (void *cls, struct GNUNET_MessageHeader *header)
249{
250 struct GNUNET_PeerIdentity *peer = cls;
251
252 core_receive_continue (peer);
253 }*/
254
255
256/**
257 * Function called to handle a message being received.
258 *
259 */
260static void
261handle_encrypted (void *cls, struct GNUNET_MessageHeader *header)
262{
263 struct GNUNET_PeerIdentity *peer = cls;
264
265 core_receive_continue (peer);
266}
267
268
269static void
270handle_ephemeral_key (void *cls, struct GNUNET_MessageHeader *header)
271{
272 struct GNUNET_PeerIdentity *peer = cls;
273
274 core_receive_continue (peer);
275}
276
277
278static void
279handle_ping (void *cls, struct GNUNET_MessageHeader *header)
280{
281 struct GNUNET_PeerIdentity *peer = cls;
282
283 core_receive_continue (peer);
284}
285
286
287static void
288handle_pong (void *cls, struct GNUNET_MessageHeader *header)
289{
290 struct GNUNET_PeerIdentity *peer = cls;
291
292 core_receive_continue (peer);
293}
294
295
296/**
297 * Function to start a local test case.
298 *
299 * @param write_message Callback to send a message to the master loop.
300 * @param router_ip Global address of the network namespace.
301 * @param node_ip The IP address of the node.
302 * @param m The number of the node in a network namespace.
303 * @param n The number of the network namespace.
304 * @param local_m The number of nodes in a network namespace.
305 * @param topology_data A file name for the file containing the topology configuration, or a string containing
306 * the topology configuration.
307 * @param read_file If read_file is GNUNET_YES this string is the filename for the topology configuration,
308 * if read_file is GNUNET_NO the string contains the topology configuration.
309 * @param finish_cb Callback function which writes a message from the helper process running on a netjail
310 * node to the master process * signaling that the test case running on the netjail node finished.
311 * @return Returns the struct GNUNET_TESTING_Interpreter of the command loop running on this netjail node.
312 */
313static struct GNUNET_TESTING_Interpreter *
314start_testcase (GNUNET_TESTING_cmd_helper_write_cb write_message,
315 const char *router_ip,
316 const char *node_ip,
317 const char *m,
318 const char *n,
319 const char *local_m,
320 const char *topology_data,
321 unsigned int *read_file,
322 GNUNET_TESTING_cmd_helper_finish_cb finished_cb)
323{
324
325 unsigned int n_int;
326 unsigned int m_int;
327 unsigned int local_m_int;
328 unsigned int num;
329 struct TestState *ts = GNUNET_new (struct TestState);
330 struct GNUNET_TESTING_NetjailTopology *topology;
331 unsigned int sscanf_ret = 0;
332 char **argv = NULL;
333 int argc = 0;
334
335 ts->finished_cb = finished_cb;
336 LOG (GNUNET_ERROR_TYPE_ERROR,
337 "n %s m %s\n",
338 n,
339 m);
340
341 if (GNUNET_YES == *read_file)
342 {
343 LOG (GNUNET_ERROR_TYPE_DEBUG,
344 "read from file\n");
345 topology = GNUNET_TESTING_get_topo_from_file (topology_data);
346 }
347 else
348 topology = GNUNET_TESTING_get_topo_from_string (topology_data);
349
350 ts->topology = topology;
351
352 errno = 0;
353 sscanf_ret = sscanf (m, "%u", &m_int);
354 if (errno != 0)
355 {
356 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
357 }
358 GNUNET_assert (0 < sscanf_ret);
359 errno = 0;
360 sscanf_ret = sscanf (n, "%u", &n_int);
361 if (errno != 0)
362 {
363 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
364 }
365 GNUNET_assert (0 < sscanf_ret);
366 errno = 0;
367 sscanf_ret = sscanf (local_m, "%u", &local_m_int);
368 if (errno != 0)
369 {
370 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf");
371 }
372 GNUNET_assert (0 < sscanf_ret);
373
374 if (0 == n_int)
375 num = m_int;
376 else
377 num = (n_int - 1) * local_m_int + m_int + topology->nodes_x;
378
379 block_script = GNUNET_TESTING_cmd_block_until_external_trigger (
380 "block-script");
381 connect_peers = GNUNET_CORE_cmd_connect_peers ("connect-peers",
382 "start-peer",
383 "system-create",
384 num,
385 topology,
386 0,
387 GNUNET_NO,
388 NULL);
389 local_prepared = GNUNET_TESTING_cmd_local_test_prepared (
390 "local-test-prepared",
391 write_message);
392
393
394 GNUNET_asprintf (&ts->cfgname,
395 "test_core_just_run.conf");
396
397 LOG (GNUNET_ERROR_TYPE_DEBUG,
398 "plugin cfgname: %s\n",
399 ts->cfgname);
400
401 LOG (GNUNET_ERROR_TYPE_DEBUG,
402 "node ip: %s\n",
403 node_ip);
404
405 GNUNET_asprintf (&ts->testdir,
406 "%s%s%s",
407 BASE_DIR,
408 m,
409 n);
410
411 /*struct GNUNET_MQ_MessageHandler handlers[] = {
412 GNUNET_MQ_hd_fixed_size (ephemeral_key,
413 GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY,
414 struct EphemeralKeyMessage,
415 NULL),
416 GNUNET_MQ_hd_fixed_size (ping,
417 GNUNET_MESSAGE_TYPE_CORE_PING,
418 struct PingMessage,
419 NULL),
420 GNUNET_MQ_hd_fixed_size (pong,
421 GNUNET_MESSAGE_TYPE_CORE_PONG,
422 struct PongMessage,
423 NULL),
424 GNUNET_MQ_handler_end ()
425 };*/
426
427 start_peer = GNUNET_TESTING_cmd_start_peer ("start-peer",
428 "system-create",
429 num,
430 node_ip,
431 ts->cfgname,
432 GNUNET_NO);
433
434 struct GNUNET_TESTING_Command commands[] = {
435 GNUNET_TESTING_cmd_system_create ("system-create",
436 ts->testdir),
437 start_peer,
438 GNUNET_TESTING_cmd_barrier_reached ("ready-to-connect-reached",
439 "ready-to-connect",
440 GNUNET_NO,
441 num,
442 GNUNET_NO,
443 write_message),
444 connect_peers,
445 GNUNET_TESTING_cmd_exec_bash_script ("script",
446 "block.sh",
447 argv,
448 argc,
449 &child_completed_callback),
450 block_script,
451 GNUNET_TESTING_cmd_barrier_reached ("test-case-finished-reached",
452 "test-case-finished",
453 GNUNET_NO,
454 num,
455 GNUNET_NO,
456 write_message),
457 GNUNET_TESTING_cmd_stop_peer ("stop-peer",
458 "start-peer"),
459 GNUNET_TESTING_cmd_system_destroy ("system-destroy",
460 "system-create"),
461 GNUNET_TESTING_cmd_end ()
462 };
463
464 ts->write_message = write_message;
465
466 is = GNUNET_TESTING_run (commands,
467 TIMEOUT,
468 &handle_result,
469 ts);
470 return is;
471}
472
473
474/**
475 * Entry point for the plugin.
476 *
477 * @param cls NULL
478 * @return the exported block API
479 */
480void *
481libgnunet_test_core_plugin_cmd_just_run_init (void *cls)
482{
483 struct GNUNET_TESTING_PluginFunctions *api;
484
485 GNUNET_log_setup ("simple-send",
486 "DEBUG",
487 NULL);
488
489 api = GNUNET_new (struct GNUNET_TESTING_PluginFunctions);
490 api->start_testcase = &start_testcase;
491 api->get_waiting_for_barriers = get_waiting_for_barriers;
492 return api;
493}
494
495
496/**
497 * Exit point from the plugin.
498 *
499 * @param cls the return value from #libgnunet_test_transport_plugin_just_run_init
500 * @return NULL
501 */
502void *
503libgnunet_test_core_plugin_cmd_just_run_done (void *cls)
504{
505 struct GNUNET_TESTING_PluginFunctions *api = cls;
506
507 GNUNET_free (api);
508 return NULL;
509}
510
511
512/* end of plugin_cmd_simple_send.c */