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.
312 * Handle to a group of GNUnet peers.
314 struct GNUNET_TESTING_PeerGroup;
318 * Prototype of a function that will be called whenever
319 * two daemons are connected by the testing library.
322 * @param first peer id for first daemon
323 * @param second peer id for the second daemon
324 * @param first_cfg config for the first daemon
325 * @param second_cfg config for the second daemon
326 * @param first_daemon handle for the first daemon
327 * @param second_daemon handle for the second daemon
328 * @param emsg error message (NULL on success)
330 typedef void (*GNUNET_TESTING_NotifyConnection)(void *cls,
331 const struct GNUNET_PeerIdentity *first,
332 const struct GNUNET_PeerIdentity *second,
333 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
334 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
335 struct GNUNET_TESTING_Daemon *first_daemon,
336 struct GNUNET_TESTING_Daemon *second_daemon,
340 * Starts a GNUnet daemon. GNUnet must be installed on the target
341 * system and available in the PATH. The machine must furthermore be
342 * reachable via "ssh" (unless the hostname is "NULL") without the
343 * need to enter a password.
345 * @param sched scheduler to use
346 * @param cfg configuration to use
347 * @param timeout how long to wait starting up peers
348 * @param hostname name of the machine where to run GNUnet
349 * (use NULL for localhost).
350 * @param hostkey_callback function to call once the hostkey has been
351 * generated for this peer, but it hasn't yet been started
352 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
353 * @param hostkey_cls closure for hostkey callback
354 * @param cb function to call with the result
355 * @param cb_cls closure for cb
356 * @return handle to the daemon (actual start will be completed asynchronously)
358 struct GNUNET_TESTING_Daemon *
359 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
360 const struct GNUNET_CONFIGURATION_Handle *cfg,
361 struct GNUNET_TIME_Relative timeout,
362 const char *hostname,
363 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
365 GNUNET_TESTING_NotifyDaemonRunning cb,
369 * Continues GNUnet daemon startup when user wanted to be notified
370 * once a hostkey was generated (for creating friends files, blacklists,
373 * @param daemon the daemon to finish starting
376 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon);
379 * Restart (stop and start) a GNUnet daemon.
381 * @param d the daemon that should be restarted
382 * @param cb function called once the daemon is (re)started
383 * @param cb_cls closure for cb
386 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
387 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls);
390 * Get a certain testing daemon handle.
392 * @param pg handle to the set of running peers
393 * @param position the number of the peer to return
395 struct GNUNET_TESTING_Daemon *
396 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position);
400 * Stops a GNUnet daemon.
402 * @param d the daemon that should be stopped
403 * @param timeout how long to wait for process for shutdown to complete
404 * @param cb function called once the daemon was stopped
405 * @param cb_cls closure for cb
406 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
407 * to leave them (i.e. for restarting at a later time,
408 * or logfile inspection once finished)
411 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
412 struct GNUNET_TIME_Relative timeout,
413 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
418 * Changes the configuration of a GNUnet daemon.
420 * @param d the daemon that should be modified
421 * @param cfg the new configuration for the daemon
422 * @param cb function called once the configuration was changed
423 * @param cb_cls closure for cb
425 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
426 struct GNUNET_CONFIGURATION_Handle *cfg,
427 GNUNET_TESTING_NotifyCompletion cb,
432 * Establish a connection between two GNUnet daemons.
434 * @param d1 handle for the first daemon
435 * @param d2 handle for the second daemon
436 * @param timeout how long is the connection attempt
438 * @param max_connect_attempts how many times should we try to reconnect
440 * @param cb function to call at the end
441 * @param cb_cls closure for cb
443 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
444 struct GNUNET_TESTING_Daemon *d2,
445 struct GNUNET_TIME_Relative timeout,
446 unsigned int max_connect_attempts,
447 GNUNET_TESTING_NotifyConnection cb,
454 * Start count gnunetd processes with the same set of transports and
455 * applications. The port numbers (any option called "PORT") will be
456 * adjusted to ensure that no two peers running on the same system
457 * have the same port(s) in their respective configurations.
459 * @param sched scheduler to use
460 * @param cfg configuration template to use
461 * @param total number of daemons to start
462 * @param timeout total time allowed for peers to start
463 * @param hostkey_callback function to call on each peers hostkey generation
464 * if NULL, peers will be started by this call, if non-null,
465 * GNUNET_TESTING_daemons_continue_startup must be called after
466 * successful hostkey generation
467 * @param hostkey_cls closure for hostkey callback
468 * @param cb function to call on each daemon that was started
469 * @param cb_cls closure for cb
470 * @param connect_callback function to call each time two hosts are connected
471 * @param connect_callback_cls closure for connect_callback
472 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
473 * everything on localhost).
474 * @return NULL on error, otherwise handle to control peer group
476 struct GNUNET_TESTING_PeerGroup *
477 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
478 const struct GNUNET_CONFIGURATION_Handle *cfg,
480 struct GNUNET_TIME_Relative timeout,
481 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
483 GNUNET_TESTING_NotifyDaemonRunning cb,
485 GNUNET_TESTING_NotifyConnection
486 connect_callback, void *connect_callback_cls,
487 const char *hostnames);
490 * Function which continues a peer group starting up
491 * after successfully generating hostkeys for each peer.
493 * @param pg the peer group to continue starting
496 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg);
499 * Restart all peers in the given group.
501 * @param pg the handle to the peer group
502 * @param callback function to call on completion (or failure)
503 * @param callback_cls closure for the callback function
506 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
507 GNUNET_TESTING_NotifyCompletion callback,
512 * Shutdown all peers started in the given group.
514 * @param pg handle to the peer group
515 * @param timeout how long to wait for shutdown
519 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_TIME_Relative timeout);
523 * Topologies supported for testbeds.
525 enum GNUNET_TESTING_Topology
528 * A clique (everyone connected to everyone else).
530 GNUNET_TESTING_TOPOLOGY_CLIQUE,
533 * Small-world network (2d torus plus random links).
535 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
538 * Small-world network (ring plus random links).
540 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
545 GNUNET_TESTING_TOPOLOGY_RING,
550 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
555 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
558 * Certain percentage of peers are unable to communicate directly
559 * replicating NAT conditions
561 GNUNET_TESTING_TOPOLOGY_INTERNAT,
564 * Scale free topology.
566 GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
569 * All peers are disconnected.
571 GNUNET_TESTING_TOPOLOGY_NONE
575 * Options for connecting a topology.
577 enum GNUNET_TESTING_TopologyOption
580 * Try to connect all peers specified in the topology.
582 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
585 * Choose a random subset of connections to create.
587 GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM,
590 * Create at least X connections for each peer.
592 GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM,
595 * Using a depth first search, create one connection
596 * per peer. If any are missed (graph disconnected)
597 * start over at those peers until all have at least one
600 GNUNET_TESTING_TOPOLOGY_OPTION_DFS,
603 * No options specified.
605 GNUNET_TESTING_TOPOLOGY_OPTION_NONE
609 * Takes a peer group and creates a topology based on the
610 * one specified. Creates a topology means generates friend
611 * files for the peers so they can only connect to those allowed
612 * by the topology. This will only have an effect once peers
613 * are started if the FRIENDS_ONLY option is set in the base
614 * config. Also takes an optional restrict topology which
615 * disallows direct TCP connections UNLESS they are specified in
616 * the restricted topology.
618 * @param pg the peer group struct representing the running peers
619 * @param topology which topology to connect the peers in
620 * @param restrict_topology allow only direct TCP connections in this topology
622 * @return the maximum number of connections were all allowed peers
623 * connected to each other
626 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
627 enum GNUNET_TESTING_Topology topology,
628 enum GNUNET_TESTING_Topology restrict_topology);
631 * @param pg the peer group struct representing the running peers
632 * @param topology which topology to connect the peers in
633 * @param options options for connecting the topology
634 * @param option_modifier modifier for options that take a parameter
637 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
638 enum GNUNET_TESTING_Topology topology,
639 enum GNUNET_TESTING_TopologyOption options,
640 double option_modifier);
643 * Start "count" GNUnet daemons with a particular topology.
645 * @param sched scheduler to use
646 * @param cfg configuration template to use
647 * @param count number of peers the testbed should have
648 * @param topology desired topology (enforced via F2F)
649 * @param cb function to call on each daemon that was started
650 * @param cb_cls closure for cb
651 * @param hostname where to run the peers; can be NULL (to run
652 * everything on localhost). Additional
653 * hosts can be specified using a NULL-terminated list of
654 * varargs, hosts will then be used round-robin from that
656 * @return handle to control the testbed
658 struct GNUNET_TESTING_Testbed *
659 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
660 const struct GNUNET_CONFIGURATION_Handle *cfg,
662 enum GNUNET_TESTING_Topology topology,
663 GNUNET_TESTING_NotifyDaemonRunning cb,
665 const char *hostname,
670 * Stop all of the daemons started with the start function.
672 * @param tb handle for the testbed
673 * @param cb function to call when done
674 * @param cb_cls closure for cb
677 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
678 GNUNET_TESTING_NotifyCompletion cb,
683 * Simulate churn in the testbed by stopping some peers (and possibly
684 * re-starting others if churn is called multiple times). This
685 * function can only be used to create leave-join churn (peers "never"
686 * leave for good). First "voff" random peers that are currently
687 * online will be taken offline; then "von" random peers that are then
688 * offline will be put back online. No notifications will be
689 * generated for any of these operations except for the callback upon
690 * completion. Note that the implementation is at liberty to keep
691 * the ARM service itself (but none of the other services or daemons)
692 * running even though the "peer" is being varied offline.
694 * @param tb handle for the testbed
695 * @param voff number of peers that should go offline
696 * @param von number of peers that should come back online;
697 * must be zero on first call (since "testbed_start"
698 * always starts all of the peers)
699 * @param cb function to call at the end
700 * @param cb_cls closure for cb
703 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
706 GNUNET_TESTING_NotifyCompletion cb,
710 #if 0 /* keep Emacsens' auto-indent happy */