2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_testing_lib.h
23 * @brief convenience API for writing testcases for GNUnet
24 * Many testcases need to start and stop gnunetd,
25 * and this library is supposed to make that easier
26 * for TESTCASES. Normal programs should always
27 * use functions from gnunet_{util,arm}_lib.h. This API is
28 * ONLY for writing testcases!
29 * @author Christian Grothoff
32 #ifndef GNUNET_TESTING_LIB_H
33 #define GNUNET_TESTING_LIB_H
35 #include "gnunet_util_lib.h"
40 #if 0 /* keep Emacsens' auto-indent happy */
46 * Handle for a GNUnet daemon (technically a set of
47 * daemons; the handle is really for the master ARM
48 * daemon) started by the testing library.
50 struct GNUNET_TESTING_Daemon;
53 * Prototype of a function that will be called whenever
54 * a daemon was started by the testing library.
57 * @param id identifier for the daemon, NULL on error
58 * @param d handle for the daemon
59 * @param emsg error message (NULL on success)
61 typedef void (*GNUNET_TESTING_NotifyHostkeyCreated)(void *cls,
62 const struct GNUNET_PeerIdentity *id,
63 struct GNUNET_TESTING_Daemon *d,
67 * Prototype of a function that will be called whenever
68 * a daemon was started by the testing library.
71 * @param id identifier for the daemon, NULL on error
72 * @param cfg configuration used by this daemon
73 * @param d handle for the daemon
74 * @param emsg error message (NULL on success)
76 typedef void (*GNUNET_TESTING_NotifyDaemonRunning)(void *cls,
77 const struct GNUNET_PeerIdentity *id,
78 const struct GNUNET_CONFIGURATION_Handle *cfg,
79 struct GNUNET_TESTING_Daemon *d,
84 * Handle to an entire testbed of GNUnet peers.
86 struct GNUNET_TESTING_Testbed;
89 * Phases of starting GNUnet on a system.
91 enum GNUNET_TESTING_StartPhase
94 * Copy the configuration file to the target system.
99 * Configuration file has been copied, generate hostkey.
104 * Create the hostkey for the peer.
109 * Hostkey generated, wait for topology to be finished.
114 * Topology has been created, now start ARM.
119 * ARM has been started, check that it has properly daemonized and
120 * then try to connect to the CORE service (which should be
121 * auto-started by ARM).
126 * We're waiting for CORE to start.
131 * Core has notified us that we've established a connection to the service.
132 * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
137 * We've been asked to terminate the instance and are now waiting for
138 * the remote command to stop the gnunet-arm process and delete temporary
144 * We've received a configuration update and are currently waiting for
145 * the copy process for the update to complete. Once it is, we will
146 * return to "SP_START_DONE" (and rely on ARM to restart all affected
153 * Prototype of a function that will be called when a
154 * particular operation was completed the testing library.
157 * @param emsg NULL on success
159 typedef void (*GNUNET_TESTING_NotifyCompletion)(void *cls,
163 * Prototype of a function that will be called with the
164 * number of connections created for a particular topology.
167 * @param num_connections the number of connections created
169 typedef void (*GNUNET_TESTING_NotifyConnections)(void *cls,
170 unsigned int num_connections);
173 * Handle for a GNUnet daemon (technically a set of
174 * daemons; the handle is really for the master ARM
175 * daemon) started by the testing library.
177 struct GNUNET_TESTING_Daemon
182 struct GNUNET_SCHEDULER_Handle *sched;
187 struct GNUNET_CONFIGURATION_Handle *cfg;
190 * At what time to give up starting the peer
192 struct GNUNET_TIME_Absolute max_timeout;
195 * Host to run GNUnet on.
200 * Result of GNUNET_i2s of this peer,
206 * Username we are using.
211 * Name of the configuration file
216 * Callback to inform initiator that the peer's
217 * hostkey has been created.
219 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
222 * Closure for hostkey creation callback.
227 * Function to call when the peer is running.
229 GNUNET_TESTING_NotifyDaemonRunning cb;
237 * Arguments from "daemon_stop" call.
239 GNUNET_TESTING_NotifyCompletion dead_cb;
242 * Closure for 'dead_cb'.
247 * Arguments from "daemon_stop" call.
249 GNUNET_TESTING_NotifyCompletion update_cb;
252 * Closure for 'update_cb'.
257 * Identity of this peer (once started).
259 struct GNUNET_PeerIdentity id;
262 * Flag to indicate that we've already been asked
263 * to terminate (but could not because some action
264 * was still pending).
269 * PID of the process that we started last.
274 * In which phase are we during the start of
277 enum GNUNET_TESTING_StartPhase phase;
280 * ID of the current task.
282 GNUNET_SCHEDULER_TaskIdentifier task;
285 * Handle to the server.
287 struct GNUNET_CORE_Handle *server;
290 * Handle to the transport service of this peer
292 struct GNUNET_TRANSPORT_Handle *th;
295 * HELLO message for this peer
297 struct GNUNET_HELLO_Message *hello;
300 * Handle to a pipe for reading the hostkey.
302 struct GNUNET_DISK_PipeHandle *pipe_stdout;
305 * Set to GNUNET_YES once the peer is up.
310 * Used to tell shutdown not to remove configuration for the peer
311 * (if it's going to be restarted later)
318 * Handle to a group of GNUnet peers.
320 struct GNUNET_TESTING_PeerGroup;
324 * Prototype of a function that will be called whenever
325 * two daemons are connected by the testing library.
328 * @param first peer id for first daemon
329 * @param second peer id for the second daemon
330 * @param first_cfg config for the first daemon
331 * @param second_cfg config for the second daemon
332 * @param first_daemon handle for the first daemon
333 * @param second_daemon handle for the second daemon
334 * @param emsg error message (NULL on success)
336 typedef void (*GNUNET_TESTING_NotifyConnection)(void *cls,
337 const struct GNUNET_PeerIdentity *first,
338 const struct GNUNET_PeerIdentity *second,
339 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
340 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
341 struct GNUNET_TESTING_Daemon *first_daemon,
342 struct GNUNET_TESTING_Daemon *second_daemon,
346 * Starts a GNUnet daemon. GNUnet must be installed on the target
347 * system and available in the PATH. The machine must furthermore be
348 * reachable via "ssh" (unless the hostname is "NULL") without the
349 * need to enter a password.
351 * @param sched scheduler to use
352 * @param cfg configuration to use
353 * @param timeout how long to wait starting up peers
354 * @param hostname name of the machine where to run GNUnet
355 * (use NULL for localhost).
356 * @param hostkey_callback function to call once the hostkey has been
357 * generated for this peer, but it hasn't yet been started
358 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
359 * @param hostkey_cls closure for hostkey callback
360 * @param cb function to call with the result
361 * @param cb_cls closure for cb
362 * @return handle to the daemon (actual start will be completed asynchronously)
364 struct GNUNET_TESTING_Daemon *
365 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
366 const struct GNUNET_CONFIGURATION_Handle *cfg,
367 struct GNUNET_TIME_Relative timeout,
368 const char *hostname,
369 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
371 GNUNET_TESTING_NotifyDaemonRunning cb,
375 * Continues GNUnet daemon startup when user wanted to be notified
376 * once a hostkey was generated (for creating friends files, blacklists,
379 * @param daemon the daemon to finish starting
382 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon);
385 * Restart (stop and start) a GNUnet daemon.
387 * @param d the daemon that should be restarted
388 * @param cb function called once the daemon is (re)started
389 * @param cb_cls closure for cb
392 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
393 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls);
396 * Start a peer that has previously been stopped using the daemon_stop
397 * call (and files weren't deleted and the allow restart flag)
399 * @param daemon the daemon to start (has been previously stopped)
400 * @param timeout how long to wait for restart
401 * @param cb the callback for notification when the peer is running
402 * @param cb_cls closure for the callback
405 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
406 struct GNUNET_TIME_Relative timeout,
407 GNUNET_TESTING_NotifyDaemonRunning cb,
411 * Get a certain testing daemon handle.
413 * @param pg handle to the set of running peers
414 * @param position the number of the peer to return
416 struct GNUNET_TESTING_Daemon *
417 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position);
421 * Stops a GNUnet daemon.
423 * @param d the daemon that should be stopped
424 * @param timeout how long to wait for process for shutdown to complete
425 * @param cb function called once the daemon was stopped
426 * @param cb_cls closure for cb
427 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
428 * to leave them (i.e. for restarting at a later time,
429 * or logfile inspection once finished)
430 * @param allow_restart GNUNET_YES to restart peer later (using this API)
431 * GNUNET_NO to kill off and clean up for good
434 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
435 struct GNUNET_TIME_Relative timeout,
436 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
437 int delete_files, int allow_restart);
441 * Changes the configuration of a GNUnet daemon.
443 * @param d the daemon that should be modified
444 * @param cfg the new configuration for the daemon
445 * @param cb function called once the configuration was changed
446 * @param cb_cls closure for cb
448 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
449 struct GNUNET_CONFIGURATION_Handle *cfg,
450 GNUNET_TESTING_NotifyCompletion cb,
455 * Establish a connection between two GNUnet daemons.
457 * @param d1 handle for the first daemon
458 * @param d2 handle for the second daemon
459 * @param timeout how long is the connection attempt
461 * @param max_connect_attempts how many times should we try to reconnect
463 * @param cb function to call at the end
464 * @param cb_cls closure for cb
466 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
467 struct GNUNET_TESTING_Daemon *d2,
468 struct GNUNET_TIME_Relative timeout,
469 unsigned int max_connect_attempts,
470 GNUNET_TESTING_NotifyConnection cb,
477 * Start count gnunetd processes with the same set of transports and
478 * applications. The port numbers (any option called "PORT") will be
479 * adjusted to ensure that no two peers running on the same system
480 * have the same port(s) in their respective configurations.
482 * @param sched scheduler to use
483 * @param cfg configuration template to use
484 * @param total number of daemons to start
485 * @param timeout total time allowed for peers to start
486 * @param hostkey_callback function to call on each peers hostkey generation
487 * if NULL, peers will be started by this call, if non-null,
488 * GNUNET_TESTING_daemons_continue_startup must be called after
489 * successful hostkey generation
490 * @param hostkey_cls closure for hostkey callback
491 * @param cb function to call on each daemon that was started
492 * @param cb_cls closure for cb
493 * @param connect_callback function to call each time two hosts are connected
494 * @param connect_callback_cls closure for connect_callback
495 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
496 * everything on localhost).
497 * @return NULL on error, otherwise handle to control peer group
499 struct GNUNET_TESTING_PeerGroup *
500 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
501 const struct GNUNET_CONFIGURATION_Handle *cfg,
503 struct GNUNET_TIME_Relative timeout,
504 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
506 GNUNET_TESTING_NotifyDaemonRunning cb,
508 GNUNET_TESTING_NotifyConnection
509 connect_callback, void *connect_callback_cls,
510 const char *hostnames);
513 * Function which continues a peer group starting up
514 * after successfully generating hostkeys for each peer.
516 * @param pg the peer group to continue starting
519 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg);
522 * Restart all peers in the given group.
524 * @param pg the handle to the peer group
525 * @param callback function to call on completion (or failure)
526 * @param callback_cls closure for the callback function
529 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
530 GNUNET_TESTING_NotifyCompletion callback,
535 * Shutdown all peers started in the given group.
537 * @param pg handle to the peer group
538 * @param timeout how long to wait for shutdown
542 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_TIME_Relative timeout);
545 * Simulate churn by stopping some peers (and possibly
546 * re-starting others if churn is called multiple times). This
547 * function can only be used to create leave-join churn (peers "never"
548 * leave for good). First "voff" random peers that are currently
549 * online will be taken offline; then "von" random peers that are then
550 * offline will be put back online. No notifications will be
551 * generated for any of these operations except for the callback upon
552 * completion. Note that the implementation is at liberty to keep
553 * the ARM service itself (but none of the other services or daemons)
554 * running even though the "peer" is being varied offline.
556 * @param pg handle for the peer group
557 * @param voff number of peers that should go offline
558 * @param von number of peers that should come back online;
559 * must be zero on first call (since "testbed_start"
560 * always starts all of the peers)
561 * @param timeout how long to wait for operations to finish before
563 * @param cb function to call at the end
564 * @param cb_cls closure for cb
567 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
570 struct GNUNET_TIME_Relative timeout,
571 GNUNET_TESTING_NotifyCompletion cb,
576 * Topologies supported for testbeds.
578 enum GNUNET_TESTING_Topology
581 * A clique (everyone connected to everyone else).
583 GNUNET_TESTING_TOPOLOGY_CLIQUE,
586 * Small-world network (2d torus plus random links).
588 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
591 * Small-world network (ring plus random links).
593 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
598 GNUNET_TESTING_TOPOLOGY_RING,
603 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
608 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
611 * Certain percentage of peers are unable to communicate directly
612 * replicating NAT conditions
614 GNUNET_TESTING_TOPOLOGY_INTERNAT,
617 * Scale free topology.
619 GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
622 * All peers are disconnected.
624 GNUNET_TESTING_TOPOLOGY_NONE
628 * Options for connecting a topology.
630 enum GNUNET_TESTING_TopologyOption
633 * Try to connect all peers specified in the topology.
635 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
638 * Choose a random subset of connections to create.
640 GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM,
643 * Create at least X connections for each peer.
645 GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM,
648 * Using a depth first search, create one connection
649 * per peer. If any are missed (graph disconnected)
650 * start over at those peers until all have at least one
653 GNUNET_TESTING_TOPOLOGY_OPTION_DFS,
656 * No options specified.
658 GNUNET_TESTING_TOPOLOGY_OPTION_NONE
662 * Takes a peer group and creates a topology based on the
663 * one specified. Creates a topology means generates friend
664 * files for the peers so they can only connect to those allowed
665 * by the topology. This will only have an effect once peers
666 * are started if the FRIENDS_ONLY option is set in the base
667 * config. Also takes an optional restrict topology which
668 * disallows direct TCP connections UNLESS they are specified in
669 * the restricted topology.
671 * @param pg the peer group struct representing the running peers
672 * @param topology which topology to connect the peers in
673 * @param restrict_topology allow only direct TCP connections in this topology
674 * @param restrict_transports space delimited list of transports to blacklist
675 * to create restricted topology
677 * @return the maximum number of connections were all allowed peers
678 * connected to each other
681 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
682 enum GNUNET_TESTING_Topology topology,
683 enum GNUNET_TESTING_Topology restrict_topology,
684 char *restrict_transports);
687 * There are many ways to connect peers that are supported by this function.
688 * To connect peers in the same topology that was created via the
689 * GNUNET_TESTING_create_topology, the topology variable must be set to
690 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
691 * a new instance of that topology will be generated and attempted to be
692 * connected. This could result in some connections being impossible,
693 * because some topologies are non-deterministic.
695 * @param pg the peer group struct representing the running peers
696 * @param topology which topology to connect the peers in
697 * @param options options for connecting the topology
698 * @param option_modifier modifier for options that take a parameter
699 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
702 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
703 enum GNUNET_TESTING_Topology topology,
704 enum GNUNET_TESTING_TopologyOption options,
705 double option_modifier);
708 * Start "count" GNUnet daemons with a particular topology.
710 * @param sched scheduler to use
711 * @param cfg configuration template to use
712 * @param count number of peers the testbed should have
713 * @param topology desired topology (enforced via F2F)
714 * @param cb function to call on each daemon that was started
715 * @param cb_cls closure for cb
716 * @param hostname where to run the peers; can be NULL (to run
717 * everything on localhost). Additional
718 * hosts can be specified using a NULL-terminated list of
719 * varargs, hosts will then be used round-robin from that
721 * @return handle to control the testbed
723 struct GNUNET_TESTING_Testbed *
724 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
725 const struct GNUNET_CONFIGURATION_Handle *cfg,
727 enum GNUNET_TESTING_Topology topology,
728 GNUNET_TESTING_NotifyDaemonRunning cb,
730 const char *hostname,
735 * Stop all of the daemons started with the start function.
737 * @param tb handle for the testbed
738 * @param cb function to call when done
739 * @param cb_cls closure for cb
742 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
743 GNUNET_TESTING_NotifyCompletion cb,
748 * Simulate churn in the testbed by stopping some peers (and possibly
749 * re-starting others if churn is called multiple times). This
750 * function can only be used to create leave-join churn (peers "never"
751 * leave for good). First "voff" random peers that are currently
752 * online will be taken offline; then "von" random peers that are then
753 * offline will be put back online. No notifications will be
754 * generated for any of these operations except for the callback upon
755 * completion. Note that the implementation is at liberty to keep
756 * the ARM service itself (but none of the other services or daemons)
757 * running even though the "peer" is being varied offline.
759 * @param tb handle for the testbed
760 * @param voff number of peers that should go offline
761 * @param von number of peers that should come back online;
762 * must be zero on first call (since "testbed_start"
763 * always starts all of the peers)
764 * @param cb function to call at the end
765 * @param cb_cls closure for cb
768 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
771 GNUNET_TESTING_NotifyCompletion cb,
775 #if 0 /* keep Emacsens' auto-indent happy */