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 3, 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"
36 #include "gnunet_statistics_service.h"
41 #if 0 /* keep Emacsens' auto-indent happy */
47 * Handle for a GNUnet daemon (technically a set of
48 * daemons; the handle is really for the master ARM
49 * daemon) started by the testing library.
51 struct GNUNET_TESTING_Daemon;
54 * Linked list of hostnames to use for starting daemons.
56 struct GNUNET_TESTING_Host
58 struct GNUNET_TESTING_Host *next;
63 * Prototype of a function that will be called whenever
64 * a daemon was started by the testing library.
67 * @param id identifier for the daemon, NULL on error
68 * @param d handle for the daemon
69 * @param emsg error message (NULL on success)
71 typedef void (*GNUNET_TESTING_NotifyHostkeyCreated)(void *cls,
72 const struct GNUNET_PeerIdentity *id,
73 struct GNUNET_TESTING_Daemon *d,
77 * Prototype of a function that will be called whenever
78 * a daemon was started by the testing library.
81 * @param id identifier for the daemon, NULL on error
82 * @param cfg configuration used by this daemon
83 * @param d handle for the daemon
84 * @param emsg error message (NULL on success)
86 typedef void (*GNUNET_TESTING_NotifyDaemonRunning)(void *cls,
87 const struct GNUNET_PeerIdentity *id,
88 const struct GNUNET_CONFIGURATION_Handle *cfg,
89 struct GNUNET_TESTING_Daemon *d,
94 * Handle to an entire testbed of GNUnet peers.
96 struct GNUNET_TESTING_Testbed;
99 * Phases of starting GNUnet on a system.
101 enum GNUNET_TESTING_StartPhase
104 * Copy the configuration file to the target system.
109 * Configuration file has been copied, generate hostkey.
114 * Create the hostkey for the peer.
119 * Hostkey generated, wait for topology to be finished.
124 * Topology has been created, now start ARM.
129 * ARM has been started, check that it has properly daemonized and
130 * then try to connect to the CORE service (which should be
131 * auto-started by ARM).
136 * We're waiting for CORE to start.
141 * Core has notified us that we've established a connection to the service.
142 * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
147 * We've been asked to terminate the instance and are now waiting for
148 * the remote command to stop the gnunet-arm process and delete temporary
154 * We've received a configuration update and are currently waiting for
155 * the copy process for the update to complete. Once it is, we will
156 * return to "SP_START_DONE" (and rely on ARM to restart all affected
163 * Prototype of a function that will be called when a
164 * particular operation was completed the testing library.
167 * @param emsg NULL on success
169 typedef void (*GNUNET_TESTING_NotifyCompletion)(void *cls,
173 * Prototype of a function that will be called with the
174 * number of connections created for a particular topology.
177 * @param num_connections the number of connections created
179 typedef void (*GNUNET_TESTING_NotifyConnections)(void *cls,
180 unsigned int num_connections);
183 * Handle for a GNUnet daemon (technically a set of
184 * daemons; the handle is really for the master ARM
185 * daemon) started by the testing library.
187 struct GNUNET_TESTING_Daemon
192 struct GNUNET_SCHEDULER_Handle *sched;
197 struct GNUNET_CONFIGURATION_Handle *cfg;
200 * At what time to give up starting the peer
202 struct GNUNET_TIME_Absolute max_timeout;
205 * Host to run GNUnet on.
210 * Result of GNUNET_i2s of this peer,
216 * Username we are using.
221 * Name of the configuration file
226 * Callback to inform initiator that the peer's
227 * hostkey has been created.
229 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
232 * Closure for hostkey creation callback.
237 * Function to call when the peer is running.
239 GNUNET_TESTING_NotifyDaemonRunning cb;
247 * Arguments from "daemon_stop" call.
249 GNUNET_TESTING_NotifyCompletion dead_cb;
252 * Closure for 'dead_cb'.
257 * Arguments from "daemon_stop" call.
259 GNUNET_TESTING_NotifyCompletion update_cb;
262 * Closure for 'update_cb'.
267 * Identity of this peer (once started).
269 struct GNUNET_PeerIdentity id;
272 * Flag to indicate that we've already been asked
273 * to terminate (but could not because some action
274 * was still pending).
279 * PID of the process that we started last.
284 * In which phase are we during the start of
287 enum GNUNET_TESTING_StartPhase phase;
290 * ID of the current task.
292 GNUNET_SCHEDULER_TaskIdentifier task;
295 * Handle to the server.
297 struct GNUNET_CORE_Handle *server;
300 * Handle to the transport service of this peer
302 struct GNUNET_TRANSPORT_Handle *th;
305 * HELLO message for this peer
307 struct GNUNET_HELLO_Message *hello;
310 * Handle to a pipe for reading the hostkey.
312 struct GNUNET_DISK_PipeHandle *pipe_stdout;
315 * Output from gnunet-peerinfo is read into this buffer.
317 char hostkeybuf[105];
320 * Current position in 'hostkeybuf' (for reading from gnunet-peerinfo)
322 unsigned int hostkeybufpos;
325 * Set to GNUNET_YES once the peer is up.
330 * Used to tell shutdown not to remove configuration for the peer
331 * (if it's going to be restarted later)
338 * Handle to a group of GNUnet peers.
340 struct GNUNET_TESTING_PeerGroup;
344 * Prototype of a function that will be called whenever
345 * two daemons are connected by the testing library.
348 * @param first peer id for first daemon
349 * @param second peer id for the second daemon
350 * @param distance distance between the connected peers
351 * @param first_cfg config for the first daemon
352 * @param second_cfg config for the second daemon
353 * @param first_daemon handle for the first daemon
354 * @param second_daemon handle for the second daemon
355 * @param emsg error message (NULL on success)
357 typedef void (*GNUNET_TESTING_NotifyConnection)(void *cls,
358 const struct GNUNET_PeerIdentity *first,
359 const struct GNUNET_PeerIdentity *second,
361 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
362 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
363 struct GNUNET_TESTING_Daemon *first_daemon,
364 struct GNUNET_TESTING_Daemon *second_daemon,
368 * Prototype of a callback function indicating that two peers
369 * are currently connected.
372 * @param first peer id for first daemon
373 * @param second peer id for the second daemon
374 * @param distance distance between the connected peers
375 * @param emsg error message (NULL on success)
377 typedef void (*GNUNET_TESTING_NotifyTopology)(void *cls,
378 const struct GNUNET_PeerIdentity *first,
379 const struct GNUNET_PeerIdentity *second,
380 struct GNUNET_TIME_Relative latency,
385 * Starts a GNUnet daemon. GNUnet must be installed on the target
386 * system and available in the PATH. The machine must furthermore be
387 * reachable via "ssh" (unless the hostname is "NULL") without the
388 * need to enter a password.
390 * @param sched scheduler to use
391 * @param cfg configuration to use
392 * @param timeout how long to wait starting up peers
393 * @param hostname name of the machine where to run GNUnet
394 * (use NULL for localhost).
395 * @param hostkey_callback function to call once the hostkey has been
396 * generated for this peer, but it hasn't yet been started
397 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
398 * @param hostkey_cls closure for hostkey callback
399 * @param cb function to call with the result
400 * @param cb_cls closure for cb
401 * @return handle to the daemon (actual start will be completed asynchronously)
403 struct GNUNET_TESTING_Daemon *
404 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
405 const struct GNUNET_CONFIGURATION_Handle *cfg,
406 struct GNUNET_TIME_Relative timeout,
407 const char *hostname,
408 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
410 GNUNET_TESTING_NotifyDaemonRunning cb,
414 * Continues GNUnet daemon startup when user wanted to be notified
415 * once a hostkey was generated (for creating friends files, blacklists,
418 * @param daemon the daemon to finish starting
421 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon);
424 * Restart (stop and start) a GNUnet daemon.
426 * @param d the daemon that should be restarted
427 * @param cb function called once the daemon is (re)started
428 * @param cb_cls closure for cb
431 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
432 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls);
435 * Start a peer that has previously been stopped using the daemon_stop
436 * call (and files weren't deleted and the allow restart flag)
438 * @param daemon the daemon to start (has been previously stopped)
439 * @param timeout how long to wait for restart
440 * @param cb the callback for notification when the peer is running
441 * @param cb_cls closure for the callback
444 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
445 struct GNUNET_TIME_Relative timeout,
446 GNUNET_TESTING_NotifyDaemonRunning cb,
450 * Get a certain testing daemon handle.
452 * @param pg handle to the set of running peers
453 * @param position the number of the peer to return
455 struct GNUNET_TESTING_Daemon *
456 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
457 unsigned int position);
460 * Get a daemon by peer identity, so callers can
461 * retrieve the daemon without knowing it's offset.
463 * @param pg the peer group to retrieve the daemon from
464 * @param peer_id the peer identity of the daemon to retrieve
466 * @return the daemon on success, or NULL if no such peer identity is found
468 struct GNUNET_TESTING_Daemon *
469 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
470 struct GNUNET_PeerIdentity *peer_id);
473 * Stops a GNUnet daemon.
475 * @param d the daemon that should be stopped
476 * @param timeout how long to wait for process for shutdown to complete
477 * @param cb function called once the daemon was stopped
478 * @param cb_cls closure for cb
479 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
480 * to leave them (i.e. for restarting at a later time,
481 * or logfile inspection once finished)
482 * @param allow_restart GNUNET_YES to restart peer later (using this API)
483 * GNUNET_NO to kill off and clean up for good
486 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
487 struct GNUNET_TIME_Relative timeout,
488 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
489 int delete_files, int allow_restart);
493 * Changes the configuration of a GNUnet daemon.
495 * @param d the daemon that should be modified
496 * @param cfg the new configuration for the daemon
497 * @param cb function called once the configuration was changed
498 * @param cb_cls closure for cb
500 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
501 struct GNUNET_CONFIGURATION_Handle *cfg,
502 GNUNET_TESTING_NotifyCompletion cb,
507 * Establish a connection between two GNUnet daemons.
509 * @param d1 handle for the first daemon
510 * @param d2 handle for the second daemon
511 * @param timeout how long is the connection attempt
513 * @param max_connect_attempts how many times should we try to reconnect
515 * @param cb function to call at the end
516 * @param cb_cls closure for cb
518 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
519 struct GNUNET_TESTING_Daemon *d2,
520 struct GNUNET_TIME_Relative timeout,
521 unsigned int max_connect_attempts,
522 GNUNET_TESTING_NotifyConnection cb,
529 * Start count gnunetd processes with the same set of transports and
530 * applications. The port numbers (any option called "PORT") will be
531 * adjusted to ensure that no two peers running on the same system
532 * have the same port(s) in their respective configurations.
534 * @param sched scheduler to use
535 * @param cfg configuration template to use
536 * @param total number of daemons to start
537 * @param timeout total time allowed for peers to start
538 * @param hostkey_callback function to call on each peers hostkey generation
539 * if NULL, peers will be started by this call, if non-null,
540 * GNUNET_TESTING_daemons_continue_startup must be called after
541 * successful hostkey generation
542 * @param hostkey_cls closure for hostkey callback
543 * @param cb function to call on each daemon that was started
544 * @param cb_cls closure for cb
545 * @param connect_callback function to call each time two hosts are connected
546 * @param connect_callback_cls closure for connect_callback
547 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
549 * @return NULL on error, otherwise handle to control peer group
551 struct GNUNET_TESTING_PeerGroup *
552 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
553 const struct GNUNET_CONFIGURATION_Handle *cfg,
555 struct GNUNET_TIME_Relative timeout,
556 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
558 GNUNET_TESTING_NotifyDaemonRunning cb,
560 GNUNET_TESTING_NotifyConnection
561 connect_callback, void *connect_callback_cls,
562 const struct GNUNET_TESTING_Host *hostnames);
565 * Function which continues a peer group starting up
566 * after successfully generating hostkeys for each peer.
568 * @param pg the peer group to continue starting
571 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg);
574 * Restart all peers in the given group.
576 * @param pg the handle to the peer group
577 * @param callback function to call on completion (or failure)
578 * @param callback_cls closure for the callback function
581 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
582 GNUNET_TESTING_NotifyCompletion callback,
587 * Shutdown all peers started in the given group.
589 * @param pg handle to the peer group
590 * @param timeout how long to wait for shutdown
591 * @param cb callback to notify upon success or failure
592 * @param cb_cls closure for cb
595 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
596 struct GNUNET_TIME_Relative timeout,
597 GNUNET_TESTING_NotifyCompletion cb,
602 * Simulate churn by stopping some peers (and possibly
603 * re-starting others if churn is called multiple times). This
604 * function can only be used to create leave-join churn (peers "never"
605 * leave for good). First "voff" random peers that are currently
606 * online will be taken offline; then "von" random peers that are then
607 * offline will be put back online. No notifications will be
608 * generated for any of these operations except for the callback upon
609 * completion. Note that the implementation is at liberty to keep
610 * the ARM service itself (but none of the other services or daemons)
611 * running even though the "peer" is being varied offline.
613 * @param pg handle for the peer group
614 * @param voff number of peers that should go offline
615 * @param von number of peers that should come back online;
616 * must be zero on first call (since "testbed_start"
617 * always starts all of the peers)
618 * @param timeout how long to wait for operations to finish before
620 * @param cb function to call at the end
621 * @param cb_cls closure for cb
624 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
627 struct GNUNET_TIME_Relative timeout,
628 GNUNET_TESTING_NotifyCompletion cb,
632 * Callback function to process statistic values.
635 * @param peer the peer the statistics belong to
636 * @param subsystem name of subsystem that created the statistic
637 * @param name the name of the datum
638 * @param value the current value
639 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
640 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
642 typedef int (*GNUNET_TESTING_STATISTICS_Iterator) (void *cls,
643 const struct GNUNET_PeerIdentity *peer,
644 const char *subsystem,
650 * Iterate over all (running) peers in the peer group, retrieve
651 * all statistics from each.
654 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
655 GNUNET_STATISTICS_Callback cont,
656 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls);
659 * Topologies supported for testbeds.
661 enum GNUNET_TESTING_Topology
664 * A clique (everyone connected to everyone else).
666 GNUNET_TESTING_TOPOLOGY_CLIQUE,
669 * Small-world network (2d torus plus random links).
671 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
674 * Small-world network (ring plus random links).
676 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
681 GNUNET_TESTING_TOPOLOGY_RING,
686 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
691 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
694 * Certain percentage of peers are unable to communicate directly
695 * replicating NAT conditions
697 GNUNET_TESTING_TOPOLOGY_INTERNAT,
700 * Scale free topology.
702 GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
705 * Straight line topology.
707 GNUNET_TESTING_TOPOLOGY_LINE,
710 * All peers are disconnected.
712 GNUNET_TESTING_TOPOLOGY_NONE
716 * Options for connecting a topology.
718 enum GNUNET_TESTING_TopologyOption
721 * Try to connect all peers specified in the topology.
723 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
726 * Choose a random subset of connections to create.
728 GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM,
731 * Create at least X connections for each peer.
733 GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM,
736 * Using a depth first search, create one connection
737 * per peer. If any are missed (graph disconnected)
738 * start over at those peers until all have at least one
741 GNUNET_TESTING_TOPOLOGY_OPTION_DFS,
744 * No options specified.
746 GNUNET_TESTING_TOPOLOGY_OPTION_NONE
751 * Get a topology from a string input.
753 * @param topology where to write the retrieved topology
754 * @param topology_string The string to attempt to
755 * get a configuration value from
756 * @return GNUNET_YES if topology string matched a
757 * known topology, GNUNET_NO if not
760 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string);
763 * Get connect topology option from string input.
765 * @param topology_option where to write the retrieved topology
766 * @param topology_string The string to attempt to
767 * get a configuration value from
768 * @return GNUNET_YES if topology string matched a
769 * known topology, GNUNET_NO if not
772 GNUNET_TESTING_topology_option_get(enum GNUNET_TESTING_TopologyOption *topology_option,
773 char * topology_string);
777 * Takes a peer group and creates a topology based on the
778 * one specified. Creates a topology means generates friend
779 * files for the peers so they can only connect to those allowed
780 * by the topology. This will only have an effect once peers
781 * are started if the FRIENDS_ONLY option is set in the base
782 * config. Also takes an optional restrict topology which
783 * disallows direct TCP connections UNLESS they are specified in
784 * the restricted topology.
786 * @param pg the peer group struct representing the running peers
787 * @param topology which topology to connect the peers in
788 * @param restrict_topology allow only direct TCP connections in this topology
789 * @param restrict_transports space delimited list of transports to blacklist
790 * to create restricted topology
792 * @return the maximum number of connections were all allowed peers
793 * connected to each other
796 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
797 enum GNUNET_TESTING_Topology topology,
798 enum GNUNET_TESTING_Topology restrict_topology,
799 char *restrict_transports);
802 * Iterate over all (running) peers in the peer group, retrieve
803 * all connections that each currently has.
805 * @param pg the peer group we are concerned with
806 * @param cb callback for topology information
807 * @param cls closure for callback
810 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls);
813 * There are many ways to connect peers that are supported by this function.
814 * To connect peers in the same topology that was created via the
815 * GNUNET_TESTING_create_topology, the topology variable must be set to
816 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
817 * a new instance of that topology will be generated and attempted to be
818 * connected. This could result in some connections being impossible,
819 * because some topologies are non-deterministic.
821 * @param pg the peer group struct representing the running peers
822 * @param topology which topology to connect the peers in
823 * @param options options for connecting the topology
824 * @param option_modifier modifier for options that take a parameter
825 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
828 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
829 enum GNUNET_TESTING_Topology topology,
830 enum GNUNET_TESTING_TopologyOption options,
831 double option_modifier);
834 * Start or stop an individual peer from the given group.
836 * @param pg handle to the peer group
837 * @param offset which peer to start or stop
838 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
839 * @param timeout how long to wait for shutdown
840 * @param cb function to call at the end
841 * @param cb_cls closure for cb
844 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
847 struct GNUNET_TIME_Relative timeout,
848 GNUNET_TESTING_NotifyCompletion cb,
852 #if 0 /* keep Emacsens' auto-indent happy */