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;
66 * Shutdown function for the test. Stops all peers.
68 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *`
71 do_shutdown (void *cls)
73 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
74 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl;
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
77 "Testcase shutting down\n");
78 if (NULL != ccc->shutdown_task)
79 ccc->shutdown_task (ccc->shutdown_task_cls);
80 if (NULL != ccc->timeout_task)
82 GNUNET_SCHEDULER_cancel (ccc->timeout_task);
83 ccc->timeout_task = NULL;
85 if (NULL != ccc->connect_task)
87 GNUNET_SCHEDULER_cancel (ccc->connect_task);
88 ccc->connect_task = NULL;
90 while (NULL != (crl = ccc->crl_head))
92 GNUNET_CONTAINER_DLL_remove (ccc->crl_head,
95 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (crl->cr);
98 for (unsigned int i = 0; i < ccc->num_peers; i++)
100 if (NULL != ccc->p[i])
102 GNUNET_TRANSPORT_TESTING_stop_peer (ccc->p[i]);
110 * Testcase hit timeout, shut it down with error.
112 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *`
115 do_timeout (void *cls)
117 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
119 ccc->timeout_task = NULL;
120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
121 "Testcase timed out\n");
122 ccc->global_ret = GNUNET_SYSERR;
123 GNUNET_SCHEDULER_shutdown ();
128 * Internal data structure. Closure for
129 * #connect_cb, #disconnect_cb, #my_nc and #start_cb.
130 * Allows us to identify which peer this is about.
132 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext
135 * Overall context of the callback.
137 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
140 * Offset of the peer this is about.
147 * Information tracked per connected peer.
149 struct ConnectPairInfo
152 * Peer this is about.
154 const struct GNUNET_PeerIdentity *sender;
157 * Information about the receiving peer.
159 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi;
164 * Function called when we connected two peers. Once we have gotten
165 * to the clique, launch test-specific logic.
167 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *`
170 connect_cb (void *cls)
172 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl = cls;
173 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = crl->ccc;
175 GNUNET_CONTAINER_DLL_remove (ccc->crl_head,
179 char *p1_c = GNUNET_strdup (GNUNET_i2s (&crl->p1->id));
181 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
182 "Peers connected: %u (%s) <-> %u (%s)\n",
186 GNUNET_i2s (&crl->p2->id));
190 if (NULL == ccc->crl_head)
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193 "All connections UP, launching custom test logic.\n");
194 GNUNET_SCHEDULER_add_now (ccc->connect_continuation,
195 ccc->connect_continuation_cls);
201 * Find peer by peer ID.
203 * @param ccc context to search
204 * @param peer peer to look for
205 * @return NULL if @a peer was not found
207 struct GNUNET_TRANSPORT_TESTING_PeerContext *
208 GNUNET_TRANSPORT_TESTING_find_peer (struct
209 GNUNET_TRANSPORT_TESTING_ConnectCheckContext
211 const struct GNUNET_PeerIdentity *peer)
213 for (unsigned int i = 0; i < ccc->num_peers; i++)
214 if ((NULL != ccc->p[i]) &&
224 * Wrapper around peers connecting. Calls client's nc function.
226 * @param cls our `struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *`
227 * @param peer peer we got connected to
228 * @param mq message queue for transmissions to @a peer
229 * @return closure for message handlers
233 const struct GNUNET_PeerIdentity *peer,
234 struct GNUNET_MQ_Handle *mq)
236 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cls;
237 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc;
238 struct ConnectPairInfo *cpi;
244 cpi = GNUNET_new (struct ConnectPairInfo);
246 cpi->sender = peer; /* valid until disconnect */
252 * Wrapper around peers disconnecting. Calls client's nd function.
254 * @param cls our `struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *`
255 * @param peer peer we got disconnected from
256 * @param custom_cls return value from @my_nc
260 const struct GNUNET_PeerIdentity *peer,
263 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cls;
264 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc;
265 struct ConnectPairInfo *cpi = custom_cls;
276 * Wrapper around receiving data. Calls client's rec function.
278 * @param cls our `struct ConnectPairInfo *`
279 * @param message message we received
280 * @return #GNUNET_OK (all messages are fine)
283 check_test (void *cls,
284 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
291 * Wrapper around receiving data. Calls client's rec function.
293 * @param cls our `struct ConnectPairInfo *`
294 * @param message message we received
297 handle_test (void *cls,
298 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
300 struct ConnectPairInfo *cpi = cls;
301 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cpi->ipi;
302 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc;
304 if (NULL != ccc->rec)
313 * Wrapper around receiving data. Calls client's rec function.
315 * @param cls our `struct ConnectPairInfo *`
316 * @param message message we received
317 * @return #GNUNET_OK (all messages are fine)
320 check_test2 (void *cls,
321 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
328 * Wrapper around receiving data. Calls client's rec function.
330 * @param cls our `struct ConnectPairInfo *`
331 * @param message message we received
334 handle_test2 (void *cls,
335 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
337 struct ConnectPairInfo *cpi = cls;
338 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cpi->ipi;
339 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc;
341 if (NULL != ccc->rec)
350 * Connect the peers as a clique.
352 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
355 do_connect (void *cls)
357 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
359 ccc->connect_task = NULL;
360 for (unsigned int i = 0; i < ccc->num_peers; i++)
361 for (unsigned int j = (ccc->bi_directional ? 0 : i + 1); j < ccc->num_peers;
364 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl;
368 crl = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequestList);
369 GNUNET_CONTAINER_DLL_insert (ccc->crl_head,
376 char *sender_c = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));
378 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
379 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
383 GNUNET_i2s (&ccc->p[1]->id));
384 GNUNET_free (sender_c);
386 crl->cr = GNUNET_TRANSPORT_TESTING_connect_peers (ccc->p[i],
395 * Function called once we have successfully launched a peer.
396 * Once all peers have been launched, we connect all of them
399 * @param cls our `struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *`
404 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cls;
405 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc;
406 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = ccc->p[ipi->off];
409 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
410 "Peer %u (`%s') started\n",
412 GNUNET_i2s (&p->id));
413 if (ccc->started != ccc->num_peers)
415 if (NULL != ccc->pre_connect_task)
417 /* Run the custom per-connect job, then give it a second to
418 go into effect before we continue connecting peers. */
419 ccc->pre_connect_task (ccc->pre_connect_task_cls);
420 ccc->connect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
432 * Function run from #GNUNET_TRANSPORT_TESTING_connect_check
433 * once the scheduler is up. Should launch the peers and
434 * then in the continuations try to connect them.
436 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *`
437 * @param args ignored
438 * @param cfgfile ignored
439 * @param cfg configuration
442 connect_check_run (void *cls,
445 const struct GNUNET_CONFIGURATION_Handle *cfg)
447 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
451 ccc->timeout_task = GNUNET_SCHEDULER_add_delayed (ccc->timeout,
454 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
457 for (unsigned int i = 0; i < ccc->num_peers; i++)
459 struct GNUNET_MQ_MessageHandler handlers[] = {
460 GNUNET_MQ_hd_var_size (test,
461 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
462 struct GNUNET_TRANSPORT_TESTING_TestMessage,
464 GNUNET_MQ_hd_var_size (test2,
465 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2,
466 struct GNUNET_TRANSPORT_TESTING_TestMessage,
468 GNUNET_MQ_handler_end ()
470 ccc->p[i] = GNUNET_TRANSPORT_TESTING_start_peer (ccc->tth,
479 if (NULL == ccc->p[i])
484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
485 "Fail! Could not start peers!\n");
486 GNUNET_SCHEDULER_shutdown ();
492 * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback.
493 * Starts and connects the two peers, then invokes the
494 * `connect_continuation` from @a cls. Sets up a timeout to
495 * abort the test, and a shutdown handler to clean up properly
498 * @param cls closure of type `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
499 * @param tth_ initialized testing handle
500 * @param test_plugin_ name of the plugin
501 * @param test_name_ name of the test
502 * @param num_peers number of entries in the @a cfg_file array
503 * @param cfg_files array of names of configuration files for the peers
504 * @return #GNUNET_SYSERR on error
507 GNUNET_TRANSPORT_TESTING_connect_check (void *cls,
508 struct GNUNET_TRANSPORT_TESTING_Handle *
510 const char *test_plugin_,
511 const char *test_name_,
512 unsigned int num_peers,
515 static struct GNUNET_GETOPT_CommandLineOption options[] = {
516 GNUNET_GETOPT_OPTION_END
518 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
519 struct GNUNET_TRANSPORT_TESTING_PeerContext *p[num_peers];
520 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext ip[num_peers];
524 (char *) ccc->config_file,
528 ccc->num_peers = num_peers;
529 ccc->cfg_files = cfg_files;
530 ccc->test_plugin = test_plugin_;
531 ccc->test_name = test_name_;
533 ccc->global_ret = GNUNET_OK;
536 for (unsigned int i = 0; i < num_peers; i++)
542 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1,
549 return GNUNET_SYSERR;
550 return ccc->global_ret;
555 * Setup testcase. Calls @a check with the data the test needs.
557 * @param argv0 binary name (argv[0])
558 * @param filename source file name (__FILE__)
559 * @param num_peers number of peers to start
560 * @param check main function to run
561 * @param check_cls closure for @a check
562 * @return #GNUNET_OK on success
565 GNUNET_TRANSPORT_TESTING_main_ (const char *argv0,
566 const char *filename,
567 unsigned int num_peers,
568 GNUNET_TRANSPORT_TESTING_CheckCallback check,
571 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
575 char *cfg_names[num_peers];
579 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0);
580 GNUNET_log_setup (test_name,
583 test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (filename);
584 test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv0,
586 for (unsigned int i = 0; i < num_peers; i++)
587 cfg_names[i] = GNUNET_TRANSPORT_TESTING_get_config_name (argv0,
589 tth = GNUNET_TRANSPORT_TESTING_init ();
596 ret = check (check_cls,
602 GNUNET_TRANSPORT_TESTING_done (tth);
604 for (unsigned int i = 0; i < num_peers; i++)
605 GNUNET_free (cfg_names[i]);
606 GNUNET_free (test_source);
607 GNUNET_free_non_null (test_plugin);
608 GNUNET_free (test_name);
613 /* end of transport-testing-main.c */