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);
461 * Stops a GNUnet daemon.
463 * @param d the daemon that should be stopped
464 * @param timeout how long to wait for process for shutdown to complete
465 * @param cb function called once the daemon was stopped
466 * @param cb_cls closure for cb
467 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
468 * to leave them (i.e. for restarting at a later time,
469 * or logfile inspection once finished)
470 * @param allow_restart GNUNET_YES to restart peer later (using this API)
471 * GNUNET_NO to kill off and clean up for good
474 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
475 struct GNUNET_TIME_Relative timeout,
476 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
477 int delete_files, int allow_restart);
481 * Changes the configuration of a GNUnet daemon.
483 * @param d the daemon that should be modified
484 * @param cfg the new configuration for the daemon
485 * @param cb function called once the configuration was changed
486 * @param cb_cls closure for cb
488 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
489 struct GNUNET_CONFIGURATION_Handle *cfg,
490 GNUNET_TESTING_NotifyCompletion cb,
495 * Establish a connection between two GNUnet daemons.
497 * @param d1 handle for the first daemon
498 * @param d2 handle for the second daemon
499 * @param timeout how long is the connection attempt
501 * @param max_connect_attempts how many times should we try to reconnect
503 * @param cb function to call at the end
504 * @param cb_cls closure for cb
506 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
507 struct GNUNET_TESTING_Daemon *d2,
508 struct GNUNET_TIME_Relative timeout,
509 unsigned int max_connect_attempts,
510 GNUNET_TESTING_NotifyConnection cb,
517 * Start count gnunetd processes with the same set of transports and
518 * applications. The port numbers (any option called "PORT") will be
519 * adjusted to ensure that no two peers running on the same system
520 * have the same port(s) in their respective configurations.
522 * @param sched scheduler to use
523 * @param cfg configuration template to use
524 * @param total number of daemons to start
525 * @param timeout total time allowed for peers to start
526 * @param hostkey_callback function to call on each peers hostkey generation
527 * if NULL, peers will be started by this call, if non-null,
528 * GNUNET_TESTING_daemons_continue_startup must be called after
529 * successful hostkey generation
530 * @param hostkey_cls closure for hostkey callback
531 * @param cb function to call on each daemon that was started
532 * @param cb_cls closure for cb
533 * @param connect_callback function to call each time two hosts are connected
534 * @param connect_callback_cls closure for connect_callback
535 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
537 * @return NULL on error, otherwise handle to control peer group
539 struct GNUNET_TESTING_PeerGroup *
540 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
541 const struct GNUNET_CONFIGURATION_Handle *cfg,
543 struct GNUNET_TIME_Relative timeout,
544 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
546 GNUNET_TESTING_NotifyDaemonRunning cb,
548 GNUNET_TESTING_NotifyConnection
549 connect_callback, void *connect_callback_cls,
550 const struct GNUNET_TESTING_Host *hostnames);
553 * Function which continues a peer group starting up
554 * after successfully generating hostkeys for each peer.
556 * @param pg the peer group to continue starting
559 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg);
562 * Restart all peers in the given group.
564 * @param pg the handle to the peer group
565 * @param callback function to call on completion (or failure)
566 * @param callback_cls closure for the callback function
569 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
570 GNUNET_TESTING_NotifyCompletion callback,
575 * Shutdown all peers started in the given group.
577 * @param pg handle to the peer group
578 * @param timeout how long to wait for shutdown
579 * @param cb callback to notify upon success or failure
580 * @param cb_cls closure for cb
583 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
584 struct GNUNET_TIME_Relative timeout,
585 GNUNET_TESTING_NotifyCompletion cb,
590 * Simulate churn by stopping some peers (and possibly
591 * re-starting others if churn is called multiple times). This
592 * function can only be used to create leave-join churn (peers "never"
593 * leave for good). First "voff" random peers that are currently
594 * online will be taken offline; then "von" random peers that are then
595 * offline will be put back online. No notifications will be
596 * generated for any of these operations except for the callback upon
597 * completion. Note that the implementation is at liberty to keep
598 * the ARM service itself (but none of the other services or daemons)
599 * running even though the "peer" is being varied offline.
601 * @param pg handle for the peer group
602 * @param voff number of peers that should go offline
603 * @param von number of peers that should come back online;
604 * must be zero on first call (since "testbed_start"
605 * always starts all of the peers)
606 * @param timeout how long to wait for operations to finish before
608 * @param cb function to call at the end
609 * @param cb_cls closure for cb
612 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
615 struct GNUNET_TIME_Relative timeout,
616 GNUNET_TESTING_NotifyCompletion cb,
620 * Callback function to process statistic values.
623 * @param peer the peer the statistics belong to
624 * @param subsystem name of subsystem that created the statistic
625 * @param name the name of the datum
626 * @param value the current value
627 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
628 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
630 typedef int (*GNUNET_TESTING_STATISTICS_Iterator) (void *cls,
631 const struct GNUNET_PeerIdentity *peer,
632 const char *subsystem,
638 * Iterate over all (running) peers in the peer group, retrieve
639 * all statistics from each.
642 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
643 GNUNET_STATISTICS_Callback cont,
644 GNUNET_TESTING_STATISTICS_Iterator proc, void *cls);
647 * Topologies supported for testbeds.
649 enum GNUNET_TESTING_Topology
652 * A clique (everyone connected to everyone else).
654 GNUNET_TESTING_TOPOLOGY_CLIQUE,
657 * Small-world network (2d torus plus random links).
659 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
662 * Small-world network (ring plus random links).
664 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
669 GNUNET_TESTING_TOPOLOGY_RING,
674 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
679 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
682 * Certain percentage of peers are unable to communicate directly
683 * replicating NAT conditions
685 GNUNET_TESTING_TOPOLOGY_INTERNAT,
688 * Scale free topology.
690 GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
693 * Straight line topology.
695 GNUNET_TESTING_TOPOLOGY_LINE,
698 * All peers are disconnected.
700 GNUNET_TESTING_TOPOLOGY_NONE
704 * Options for connecting a topology.
706 enum GNUNET_TESTING_TopologyOption
709 * Try to connect all peers specified in the topology.
711 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
714 * Choose a random subset of connections to create.
716 GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM,
719 * Create at least X connections for each peer.
721 GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM,
724 * Using a depth first search, create one connection
725 * per peer. If any are missed (graph disconnected)
726 * start over at those peers until all have at least one
729 GNUNET_TESTING_TOPOLOGY_OPTION_DFS,
732 * No options specified.
734 GNUNET_TESTING_TOPOLOGY_OPTION_NONE
739 * Get a topology from a string input.
741 * @param topology where to write the retrieved topology
742 * @param topology_string The string to attempt to
743 * get a configuration value from
744 * @return GNUNET_YES if topology string matched a
745 * known topology, GNUNET_NO if not
748 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string);
751 * Get connect topology option from string input.
753 * @param topology_option 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_option_get(enum GNUNET_TESTING_TopologyOption *topology_option,
761 char * topology_string);
765 * Takes a peer group and creates a topology based on the
766 * one specified. Creates a topology means generates friend
767 * files for the peers so they can only connect to those allowed
768 * by the topology. This will only have an effect once peers
769 * are started if the FRIENDS_ONLY option is set in the base
770 * config. Also takes an optional restrict topology which
771 * disallows direct TCP connections UNLESS they are specified in
772 * the restricted topology.
774 * @param pg the peer group struct representing the running peers
775 * @param topology which topology to connect the peers in
776 * @param restrict_topology allow only direct TCP connections in this topology
777 * @param restrict_transports space delimited list of transports to blacklist
778 * to create restricted topology
780 * @return the maximum number of connections were all allowed peers
781 * connected to each other
784 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
785 enum GNUNET_TESTING_Topology topology,
786 enum GNUNET_TESTING_Topology restrict_topology,
787 char *restrict_transports);
790 * Iterate over all (running) peers in the peer group, retrieve
791 * all connections that each currently has.
793 * @param pg the peer group we are concerned with
794 * @param cb callback for topology information
795 * @param cls closure for callback
798 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls);
801 * There are many ways to connect peers that are supported by this function.
802 * To connect peers in the same topology that was created via the
803 * GNUNET_TESTING_create_topology, the topology variable must be set to
804 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
805 * a new instance of that topology will be generated and attempted to be
806 * connected. This could result in some connections being impossible,
807 * because some topologies are non-deterministic.
809 * @param pg the peer group struct representing the running peers
810 * @param topology which topology to connect the peers in
811 * @param options options for connecting the topology
812 * @param option_modifier modifier for options that take a parameter
813 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
816 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
817 enum GNUNET_TESTING_Topology topology,
818 enum GNUNET_TESTING_TopologyOption options,
819 double option_modifier);
822 * Start or stop an individual peer from the given group.
824 * @param pg handle to the peer group
825 * @param offset which peer to start or stop
826 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
827 * @param timeout how long to wait for shutdown
828 * @param cb function to call at the end
829 * @param cb_cls closure for cb
832 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
835 struct GNUNET_TIME_Relative timeout,
836 GNUNET_TESTING_NotifyCompletion cb,
840 #if 0 /* keep Emacsens' auto-indent happy */