2 This file is part of GNUnet.
3 Copyright (C) 2016 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file transport-testing-main.c
22 * @brief convenience main function for tests
23 * @author Christian Grothoff
25 #include "transport-testing.h"
29 * Closure for #connect_cb.
31 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList
36 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *next;
41 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *prev;
44 * Overall context we are in.
46 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
49 * Connect request this is about.
51 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cr;
54 * Peer being connected.
56 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
59 * Peer being connected.
61 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
67 * Shutdown function for the test. Stops all peers.
69 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *`
72 do_shutdown (void *cls)
74 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
75 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl;
77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78 "Testcase shutting down\n");
79 if (NULL != ccc->shutdown_task)
80 ccc->shutdown_task (ccc->shutdown_task_cls);
81 if (NULL != ccc->timeout_task)
83 GNUNET_SCHEDULER_cancel (ccc->timeout_task);
84 ccc->timeout_task = NULL;
86 if (NULL != ccc->connect_task)
88 GNUNET_SCHEDULER_cancel (ccc->connect_task);
89 ccc->connect_task = NULL;
91 while (NULL != (crl = ccc->crl_head))
93 GNUNET_CONTAINER_DLL_remove (ccc->crl_head,
96 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (crl->cr);
99 for (unsigned int i=0;i<ccc->num_peers;i++)
101 if (NULL != ccc->p[i])
103 GNUNET_TRANSPORT_TESTING_stop_peer (ccc->p[i]);
111 * Testcase hit timeout, shut it down with error.
113 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *`
116 do_timeout (void *cls)
118 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
120 ccc->timeout_task = NULL;
121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
122 "Testcase timed out\n");
123 ccc->global_ret = GNUNET_SYSERR;
124 GNUNET_SCHEDULER_shutdown ();
129 * Internal data structure. Closure for
130 * #connect_cb, #disconnect_cb, #my_nc and #start_cb.
131 * Allows us to identify which peer this is about.
133 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext
136 * Overall context of the callback.
138 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
141 * Offset of the peer this is about.
148 * Information tracked per connected peer.
150 struct ConnectPairInfo
153 * Peer this is about.
155 const struct GNUNET_PeerIdentity *sender;
158 * Information about the receiving peer.
160 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi;
165 * Function called when we connected two peers. Once we have gotten
166 * to the clique, launch test-specific logic.
168 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *`
171 connect_cb (void *cls)
173 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl = cls;
174 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = crl->ccc;
176 GNUNET_CONTAINER_DLL_remove (ccc->crl_head,
180 char *p1_c = GNUNET_strdup (GNUNET_i2s (&crl->p1->id));
182 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
183 "Peers connected: %u (%s) <-> %u (%s)\n",
187 GNUNET_i2s (&crl->p2->id));
191 if (NULL == ccc->crl_head)
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "All connections UP, launching custom test logic.\n");
195 GNUNET_SCHEDULER_add_now (ccc->connect_continuation,
196 ccc->connect_continuation_cls);
202 * Find peer by peer ID.
204 * @param ccc context to search
205 * @param peer peer to look for
206 * @return NULL if @a peer was not found
208 struct GNUNET_TRANSPORT_TESTING_PeerContext *
209 GNUNET_TRANSPORT_TESTING_find_peer (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
210 const struct GNUNET_PeerIdentity *peer)
212 for (unsigned int i=0;i<ccc->num_peers;i++)
213 if ( (NULL != ccc->p[i]) &&
223 * Wrapper around peers connecting. Calls client's nc function.
225 * @param cls our `struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *`
226 * @param peer peer we got connected to
227 * @param mq message queue for transmissions to @a peer
228 * @return closure for message handlers
232 const struct GNUNET_PeerIdentity *peer,
233 struct GNUNET_MQ_Handle *mq)
235 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cls;
236 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc;
237 struct ConnectPairInfo *cpi;
243 cpi = GNUNET_new (struct ConnectPairInfo);
245 cpi->sender = peer; /* valid until disconnect */
251 * Wrapper around peers disconnecting. Calls client's nd function.
253 * @param cls our `struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *`
254 * @param peer peer we got disconnected from
255 * @param custom_cls return value from @my_nc
259 const struct GNUNET_PeerIdentity *peer,
262 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cls;
263 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc;
264 struct ConnectPairInfo *cpi = custom_cls;
275 * Wrapper around receiving data. Calls client's rec function.
277 * @param cls our `struct ConnectPairInfo *`
278 * @param message message we received
279 * @return #GNUNET_OK (all messages are fine)
282 check_test (void *cls,
283 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
290 * Wrapper around receiving data. Calls client's rec function.
292 * @param cls our `struct ConnectPairInfo *`
293 * @param message message we received
296 handle_test (void *cls,
297 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
299 struct ConnectPairInfo *cpi = cls;
300 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cpi->ipi;
301 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc;
303 if (NULL != ccc->rec)
312 * Wrapper around receiving data. Calls client's rec function.
314 * @param cls our `struct ConnectPairInfo *`
315 * @param message message we received
316 * @return #GNUNET_OK (all messages are fine)
319 check_test2 (void *cls,
320 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
327 * Wrapper around receiving data. Calls client's rec function.
329 * @param cls our `struct ConnectPairInfo *`
330 * @param message message we received
333 handle_test2 (void *cls,
334 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
336 struct ConnectPairInfo *cpi = cls;
337 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cpi->ipi;
338 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc;
340 if (NULL != ccc->rec)
349 * Connect the peers as a clique.
351 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
354 do_connect (void *cls)
356 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
358 ccc->connect_task = NULL;
359 for (unsigned int i=0;i<ccc->num_peers;i++)
360 for (unsigned int j=(ccc->bi_directional ? 0 : i+1);j<ccc->num_peers;j++)
362 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl;
366 crl = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequestList);
367 GNUNET_CONTAINER_DLL_insert (ccc->crl_head,
374 char *sender_c = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));
376 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
377 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
381 GNUNET_i2s (&ccc->p[1]->id));
382 GNUNET_free (sender_c);
384 crl->cr = GNUNET_TRANSPORT_TESTING_connect_peers (ccc->p[i],
393 * Function called once we have successfully launched a peer.
394 * Once all peers have been launched, we connect all of them
397 * @param cls our `struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *`
402 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cls;
403 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc;
404 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = ccc->p[ipi->off];
407 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
408 "Peer %u (`%s') started\n",
410 GNUNET_i2s (&p->id));
411 if (ccc->started != ccc->num_peers)
413 if (NULL != ccc->pre_connect_task)
415 /* Run the custom per-connect job, then give it a second to
416 go into effect before we continue connecting peers. */
417 ccc->pre_connect_task (ccc->pre_connect_task_cls);
418 ccc->connect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
430 * Function run from #GNUNET_TRANSPORT_TESTING_connect_check
431 * once the scheduler is up. Should launch the peers and
432 * then in the continuations try to connect them.
434 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *`
435 * @param args ignored
436 * @param cfgfile ignored
437 * @param cfg configuration
440 connect_check_run (void *cls,
443 const struct GNUNET_CONFIGURATION_Handle *cfg)
445 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
449 ccc->timeout_task = GNUNET_SCHEDULER_add_delayed (ccc->timeout,
452 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
455 for (unsigned int i=0;i<ccc->num_peers;i++)
457 struct GNUNET_MQ_MessageHandler handlers[] = {
458 GNUNET_MQ_hd_var_size (test,
459 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
460 struct GNUNET_TRANSPORT_TESTING_TestMessage,
462 GNUNET_MQ_hd_var_size (test2,
463 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2,
464 struct GNUNET_TRANSPORT_TESTING_TestMessage,
466 GNUNET_MQ_handler_end()
468 ccc->p[i] = GNUNET_TRANSPORT_TESTING_start_peer (ccc->tth,
477 if (NULL == ccc->p[i])
482 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
483 "Fail! Could not start peers!\n");
484 GNUNET_SCHEDULER_shutdown ();
490 * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback.
491 * Starts and connects the two peers, then invokes the
492 * `connect_continuation` from @a cls. Sets up a timeout to
493 * abort the test, and a shutdown handler to clean up properly
496 * @param cls closure of type `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
497 * @param tth_ initialized testing handle
498 * @param test_plugin_ name of the plugin
499 * @param test_name_ name of the test
500 * @param num_peers number of entries in the @a cfg_file array
501 * @param cfg_files array of names of configuration files for the peers
502 * @return #GNUNET_SYSERR on error
505 GNUNET_TRANSPORT_TESTING_connect_check (void *cls,
506 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
507 const char *test_plugin_,
508 const char *test_name_,
509 unsigned int num_peers,
512 static struct GNUNET_GETOPT_CommandLineOption options[] = {
513 GNUNET_GETOPT_OPTION_END
515 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
516 struct GNUNET_TRANSPORT_TESTING_PeerContext *p[num_peers];
517 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext ip[num_peers];
521 (char *) ccc->config_file,
525 ccc->num_peers = num_peers;
526 ccc->cfg_files = cfg_files;
527 ccc->test_plugin = test_plugin_;
528 ccc->test_name = test_name_;
530 ccc->global_ret = GNUNET_OK;
533 for (unsigned int i=0;i<num_peers;i++)
539 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
546 return GNUNET_SYSERR;
547 return ccc->global_ret;
552 * Setup testcase. Calls @a check with the data the test needs.
554 * @param argv0 binary name (argv[0])
555 * @param filename source file name (__FILE__)
556 * @param num_peers number of peers to start
557 * @param check main function to run
558 * @param check_cls closure for @a check
559 * @return #GNUNET_OK on success
562 GNUNET_TRANSPORT_TESTING_main_ (const char *argv0,
563 const char *filename,
564 unsigned int num_peers,
565 GNUNET_TRANSPORT_TESTING_CheckCallback check,
568 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
572 char *cfg_names[num_peers];
576 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0);
577 GNUNET_log_setup (test_name,
580 test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (filename);
581 test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv0,
583 for (unsigned int i=0;i<num_peers;i++)
584 cfg_names[i] = GNUNET_TRANSPORT_TESTING_get_config_name (argv0,
586 tth = GNUNET_TRANSPORT_TESTING_init ();
593 ret = check (check_cls,
599 GNUNET_TRANSPORT_TESTING_done (tth);
601 for (unsigned int i=0;i<num_peers;i++)
602 GNUNET_free (cfg_names[i]);
603 GNUNET_free (test_source);
604 GNUNET_free_non_null (test_plugin);
605 GNUNET_free (test_name);
609 /* end of transport-testing-main.c */