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"
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 * Linked list of hostnames to use for starting daemons.
55 struct GNUNET_TESTING_Host
57 struct GNUNET_TESTING_Host *next;
62 * Prototype of a function that will be called whenever
63 * a daemon was started by the testing library.
66 * @param id identifier for the daemon, NULL on error
67 * @param d handle for the daemon
68 * @param emsg error message (NULL on success)
70 typedef void (*GNUNET_TESTING_NotifyHostkeyCreated)(void *cls,
71 const struct GNUNET_PeerIdentity *id,
72 struct GNUNET_TESTING_Daemon *d,
76 * Prototype of a function that will be called whenever
77 * a daemon was started by the testing library.
80 * @param id identifier for the daemon, NULL on error
81 * @param cfg configuration used by this daemon
82 * @param d handle for the daemon
83 * @param emsg error message (NULL on success)
85 typedef void (*GNUNET_TESTING_NotifyDaemonRunning)(void *cls,
86 const struct GNUNET_PeerIdentity *id,
87 const struct GNUNET_CONFIGURATION_Handle *cfg,
88 struct GNUNET_TESTING_Daemon *d,
93 * Handle to an entire testbed of GNUnet peers.
95 struct GNUNET_TESTING_Testbed;
98 * Phases of starting GNUnet on a system.
100 enum GNUNET_TESTING_StartPhase
103 * Copy the configuration file to the target system.
108 * Configuration file has been copied, generate hostkey.
113 * Create the hostkey for the peer.
118 * Hostkey generated, wait for topology to be finished.
123 * Topology has been created, now start ARM.
128 * ARM has been started, check that it has properly daemonized and
129 * then try to connect to the CORE service (which should be
130 * auto-started by ARM).
135 * We're waiting for CORE to start.
140 * Core has notified us that we've established a connection to the service.
141 * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
146 * We've been asked to terminate the instance and are now waiting for
147 * the remote command to stop the gnunet-arm process and delete temporary
153 * We've received a configuration update and are currently waiting for
154 * the copy process for the update to complete. Once it is, we will
155 * return to "SP_START_DONE" (and rely on ARM to restart all affected
162 * Prototype of a function that will be called when a
163 * particular operation was completed the testing library.
166 * @param emsg NULL on success
168 typedef void (*GNUNET_TESTING_NotifyCompletion)(void *cls,
172 * Prototype of a function that will be called with the
173 * number of connections created for a particular topology.
176 * @param num_connections the number of connections created
178 typedef void (*GNUNET_TESTING_NotifyConnections)(void *cls,
179 unsigned int num_connections);
182 * Handle for a GNUnet daemon (technically a set of
183 * daemons; the handle is really for the master ARM
184 * daemon) started by the testing library.
186 struct GNUNET_TESTING_Daemon
191 struct GNUNET_SCHEDULER_Handle *sched;
196 struct GNUNET_CONFIGURATION_Handle *cfg;
199 * At what time to give up starting the peer
201 struct GNUNET_TIME_Absolute max_timeout;
204 * Host to run GNUnet on.
209 * Result of GNUNET_i2s of this peer,
215 * Username we are using.
220 * Name of the configuration file
225 * Callback to inform initiator that the peer's
226 * hostkey has been created.
228 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
231 * Closure for hostkey creation callback.
236 * Function to call when the peer is running.
238 GNUNET_TESTING_NotifyDaemonRunning cb;
246 * Arguments from "daemon_stop" call.
248 GNUNET_TESTING_NotifyCompletion dead_cb;
251 * Closure for 'dead_cb'.
256 * Arguments from "daemon_stop" call.
258 GNUNET_TESTING_NotifyCompletion update_cb;
261 * Closure for 'update_cb'.
266 * Identity of this peer (once started).
268 struct GNUNET_PeerIdentity id;
271 * Flag to indicate that we've already been asked
272 * to terminate (but could not because some action
273 * was still pending).
278 * PID of the process that we started last.
283 * In which phase are we during the start of
286 enum GNUNET_TESTING_StartPhase phase;
289 * ID of the current task.
291 GNUNET_SCHEDULER_TaskIdentifier task;
294 * Handle to the server.
296 struct GNUNET_CORE_Handle *server;
299 * Handle to the transport service of this peer
301 struct GNUNET_TRANSPORT_Handle *th;
304 * HELLO message for this peer
306 struct GNUNET_HELLO_Message *hello;
309 * Handle to a pipe for reading the hostkey.
311 struct GNUNET_DISK_PipeHandle *pipe_stdout;
314 * Output from gnunet-peerinfo is read into this buffer.
316 char hostkeybuf[105];
319 * Current position in 'hostkeybuf' (for reading from gnunet-peerinfo)
321 unsigned int hostkeybufpos;
324 * Set to GNUNET_YES once the peer is up.
329 * Used to tell shutdown not to remove configuration for the peer
330 * (if it's going to be restarted later)
337 * Handle to a group of GNUnet peers.
339 struct GNUNET_TESTING_PeerGroup;
343 * Prototype of a function that will be called whenever
344 * two daemons are connected by the testing library.
347 * @param first peer id for first daemon
348 * @param second peer id for the second daemon
349 * @param distance distance between the connected peers
350 * @param first_cfg config for the first daemon
351 * @param second_cfg config for the second daemon
352 * @param first_daemon handle for the first daemon
353 * @param second_daemon handle for the second daemon
354 * @param emsg error message (NULL on success)
356 typedef void (*GNUNET_TESTING_NotifyConnection)(void *cls,
357 const struct GNUNET_PeerIdentity *first,
358 const struct GNUNET_PeerIdentity *second,
360 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
361 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
362 struct GNUNET_TESTING_Daemon *first_daemon,
363 struct GNUNET_TESTING_Daemon *second_daemon,
367 * Starts a GNUnet daemon. GNUnet must be installed on the target
368 * system and available in the PATH. The machine must furthermore be
369 * reachable via "ssh" (unless the hostname is "NULL") without the
370 * need to enter a password.
372 * @param sched scheduler to use
373 * @param cfg configuration to use
374 * @param timeout how long to wait starting up peers
375 * @param hostname name of the machine where to run GNUnet
376 * (use NULL for localhost).
377 * @param hostkey_callback function to call once the hostkey has been
378 * generated for this peer, but it hasn't yet been started
379 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
380 * @param hostkey_cls closure for hostkey callback
381 * @param cb function to call with the result
382 * @param cb_cls closure for cb
383 * @return handle to the daemon (actual start will be completed asynchronously)
385 struct GNUNET_TESTING_Daemon *
386 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
387 const struct GNUNET_CONFIGURATION_Handle *cfg,
388 struct GNUNET_TIME_Relative timeout,
389 const char *hostname,
390 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
392 GNUNET_TESTING_NotifyDaemonRunning cb,
396 * Continues GNUnet daemon startup when user wanted to be notified
397 * once a hostkey was generated (for creating friends files, blacklists,
400 * @param daemon the daemon to finish starting
403 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon);
406 * Restart (stop and start) a GNUnet daemon.
408 * @param d the daemon that should be restarted
409 * @param cb function called once the daemon is (re)started
410 * @param cb_cls closure for cb
413 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
414 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls);
417 * Start a peer that has previously been stopped using the daemon_stop
418 * call (and files weren't deleted and the allow restart flag)
420 * @param daemon the daemon to start (has been previously stopped)
421 * @param timeout how long to wait for restart
422 * @param cb the callback for notification when the peer is running
423 * @param cb_cls closure for the callback
426 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
427 struct GNUNET_TIME_Relative timeout,
428 GNUNET_TESTING_NotifyDaemonRunning cb,
432 * Get a certain testing daemon handle.
434 * @param pg handle to the set of running peers
435 * @param position the number of the peer to return
437 struct GNUNET_TESTING_Daemon *
438 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
439 unsigned int position);
443 * Stops a GNUnet daemon.
445 * @param d the daemon that should be stopped
446 * @param timeout how long to wait for process for shutdown to complete
447 * @param cb function called once the daemon was stopped
448 * @param cb_cls closure for cb
449 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
450 * to leave them (i.e. for restarting at a later time,
451 * or logfile inspection once finished)
452 * @param allow_restart GNUNET_YES to restart peer later (using this API)
453 * GNUNET_NO to kill off and clean up for good
456 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
457 struct GNUNET_TIME_Relative timeout,
458 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
459 int delete_files, int allow_restart);
463 * Changes the configuration of a GNUnet daemon.
465 * @param d the daemon that should be modified
466 * @param cfg the new configuration for the daemon
467 * @param cb function called once the configuration was changed
468 * @param cb_cls closure for cb
470 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
471 struct GNUNET_CONFIGURATION_Handle *cfg,
472 GNUNET_TESTING_NotifyCompletion cb,
477 * Establish a connection between two GNUnet daemons.
479 * @param d1 handle for the first daemon
480 * @param d2 handle for the second daemon
481 * @param timeout how long is the connection attempt
483 * @param max_connect_attempts how many times should we try to reconnect
485 * @param cb function to call at the end
486 * @param cb_cls closure for cb
488 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
489 struct GNUNET_TESTING_Daemon *d2,
490 struct GNUNET_TIME_Relative timeout,
491 unsigned int max_connect_attempts,
492 GNUNET_TESTING_NotifyConnection cb,
499 * Start count gnunetd processes with the same set of transports and
500 * applications. The port numbers (any option called "PORT") will be
501 * adjusted to ensure that no two peers running on the same system
502 * have the same port(s) in their respective configurations.
504 * @param sched scheduler to use
505 * @param cfg configuration template to use
506 * @param total number of daemons to start
507 * @param timeout total time allowed for peers to start
508 * @param hostkey_callback function to call on each peers hostkey generation
509 * if NULL, peers will be started by this call, if non-null,
510 * GNUNET_TESTING_daemons_continue_startup must be called after
511 * successful hostkey generation
512 * @param hostkey_cls closure for hostkey callback
513 * @param cb function to call on each daemon that was started
514 * @param cb_cls closure for cb
515 * @param connect_callback function to call each time two hosts are connected
516 * @param connect_callback_cls closure for connect_callback
517 * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
519 * @return NULL on error, otherwise handle to control peer group
521 struct GNUNET_TESTING_PeerGroup *
522 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
523 const struct GNUNET_CONFIGURATION_Handle *cfg,
525 struct GNUNET_TIME_Relative timeout,
526 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
528 GNUNET_TESTING_NotifyDaemonRunning cb,
530 GNUNET_TESTING_NotifyConnection
531 connect_callback, void *connect_callback_cls,
532 const struct GNUNET_TESTING_Host *hostnames);
535 * Function which continues a peer group starting up
536 * after successfully generating hostkeys for each peer.
538 * @param pg the peer group to continue starting
541 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg);
544 * Restart all peers in the given group.
546 * @param pg the handle to the peer group
547 * @param callback function to call on completion (or failure)
548 * @param callback_cls closure for the callback function
551 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
552 GNUNET_TESTING_NotifyCompletion callback,
557 * Shutdown all peers started in the given group.
559 * @param pg handle to the peer group
560 * @param timeout how long to wait for shutdown
561 * @param cb callback to notify upon success or failure
562 * @param cb_cls closure for cb
565 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
566 struct GNUNET_TIME_Relative timeout,
567 GNUNET_TESTING_NotifyCompletion cb,
572 * Simulate churn by stopping some peers (and possibly
573 * re-starting others if churn is called multiple times). This
574 * function can only be used to create leave-join churn (peers "never"
575 * leave for good). First "voff" random peers that are currently
576 * online will be taken offline; then "von" random peers that are then
577 * offline will be put back online. No notifications will be
578 * generated for any of these operations except for the callback upon
579 * completion. Note that the implementation is at liberty to keep
580 * the ARM service itself (but none of the other services or daemons)
581 * running even though the "peer" is being varied offline.
583 * @param pg handle for the peer group
584 * @param voff number of peers that should go offline
585 * @param von number of peers that should come back online;
586 * must be zero on first call (since "testbed_start"
587 * always starts all of the peers)
588 * @param timeout how long to wait for operations to finish before
590 * @param cb function to call at the end
591 * @param cb_cls closure for cb
594 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
597 struct GNUNET_TIME_Relative timeout,
598 GNUNET_TESTING_NotifyCompletion cb,
603 * Topologies supported for testbeds.
605 enum GNUNET_TESTING_Topology
608 * A clique (everyone connected to everyone else).
610 GNUNET_TESTING_TOPOLOGY_CLIQUE,
613 * Small-world network (2d torus plus random links).
615 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
618 * Small-world network (ring plus random links).
620 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
625 GNUNET_TESTING_TOPOLOGY_RING,
630 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
635 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
638 * Certain percentage of peers are unable to communicate directly
639 * replicating NAT conditions
641 GNUNET_TESTING_TOPOLOGY_INTERNAT,
644 * Scale free topology.
646 GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
649 * Straight line topology.
651 GNUNET_TESTING_TOPOLOGY_LINE,
654 * All peers are disconnected.
656 GNUNET_TESTING_TOPOLOGY_NONE
660 * Options for connecting a topology.
662 enum GNUNET_TESTING_TopologyOption
665 * Try to connect all peers specified in the topology.
667 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
670 * Choose a random subset of connections to create.
672 GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM,
675 * Create at least X connections for each peer.
677 GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM,
680 * Using a depth first search, create one connection
681 * per peer. If any are missed (graph disconnected)
682 * start over at those peers until all have at least one
685 GNUNET_TESTING_TOPOLOGY_OPTION_DFS,
688 * No options specified.
690 GNUNET_TESTING_TOPOLOGY_OPTION_NONE
695 * Get a topology from a string input.
697 * @param topology where to write the retrieved topology
698 * @param topology_string The string to attempt to
699 * get a configuration value from
700 * @return GNUNET_YES if topology string matched a
701 * known topology, GNUNET_NO if not
704 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string);
707 * Get connect topology option from string input.
709 * @param topology_option where to write the retrieved topology
710 * @param topology_string The string to attempt to
711 * get a configuration value from
712 * @return GNUNET_YES if topology string matched a
713 * known topology, GNUNET_NO if not
716 GNUNET_TESTING_topology_option_get(enum GNUNET_TESTING_TopologyOption *topology_option,
717 char * topology_string);
721 * Takes a peer group and creates a topology based on the
722 * one specified. Creates a topology means generates friend
723 * files for the peers so they can only connect to those allowed
724 * by the topology. This will only have an effect once peers
725 * are started if the FRIENDS_ONLY option is set in the base
726 * config. Also takes an optional restrict topology which
727 * disallows direct TCP connections UNLESS they are specified in
728 * the restricted topology.
730 * @param pg the peer group struct representing the running peers
731 * @param topology which topology to connect the peers in
732 * @param restrict_topology allow only direct TCP connections in this topology
733 * @param restrict_transports space delimited list of transports to blacklist
734 * to create restricted topology
736 * @return the maximum number of connections were all allowed peers
737 * connected to each other
740 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
741 enum GNUNET_TESTING_Topology topology,
742 enum GNUNET_TESTING_Topology restrict_topology,
743 char *restrict_transports);
746 * There are many ways to connect peers that are supported by this function.
747 * To connect peers in the same topology that was created via the
748 * GNUNET_TESTING_create_topology, the topology variable must be set to
749 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
750 * a new instance of that topology will be generated and attempted to be
751 * connected. This could result in some connections being impossible,
752 * because some topologies are non-deterministic.
754 * @param pg the peer group struct representing the running peers
755 * @param topology which topology to connect the peers in
756 * @param options options for connecting the topology
757 * @param option_modifier modifier for options that take a parameter
758 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
761 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
762 enum GNUNET_TESTING_Topology topology,
763 enum GNUNET_TESTING_TopologyOption options,
764 double option_modifier);
767 * Start or stop an individual peer from the given group.
769 * @param pg handle to the peer group
770 * @param offset which peer to start or stop
771 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
772 * @param timeout how long to wait for shutdown
773 * @param cb function to call at the end
774 * @param cb_cls closure for cb
777 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
780 struct GNUNET_TIME_Relative timeout,
781 GNUNET_TESTING_NotifyCompletion cb,
785 #if 0 /* keep Emacsens' auto-indent happy */