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 * 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 * Output from gnunet-peerinfo is read into this buffer.
307 char hostkeybuf[105];
310 * Current position in 'hostkeybuf' (for reading from gnunet-peerinfo)
312 unsigned int hostkeybufpos;
315 * Set to GNUNET_YES once the peer is up.
320 * Used to tell shutdown not to remove configuration for the peer
321 * (if it's going to be restarted later)
328 * Handle to a group of GNUnet peers.
330 struct GNUNET_TESTING_PeerGroup;
334 * Prototype of a function that will be called whenever
335 * two daemons are connected by the testing library.
338 * @param first peer id for first daemon
339 * @param second peer id for the second daemon
340 * @param distance distance between the connected peers
341 * @param first_cfg config for the first daemon
342 * @param second_cfg config for the second daemon
343 * @param first_daemon handle for the first daemon
344 * @param second_daemon handle for the second daemon
345 * @param emsg error message (NULL on success)
347 typedef void (*GNUNET_TESTING_NotifyConnection)(void *cls,
348 const struct GNUNET_PeerIdentity *first,
349 const struct GNUNET_PeerIdentity *second,
351 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
352 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
353 struct GNUNET_TESTING_Daemon *first_daemon,
354 struct GNUNET_TESTING_Daemon *second_daemon,
358 * Starts a GNUnet daemon. GNUnet must be installed on the target
359 * system and available in the PATH. The machine must furthermore be
360 * reachable via "ssh" (unless the hostname is "NULL") without the
361 * need to enter a password.
363 * @param sched scheduler to use
364 * @param cfg configuration to use
365 * @param timeout how long to wait starting up peers
366 * @param hostname name of the machine where to run GNUnet
367 * (use NULL for localhost).
368 * @param hostkey_callback function to call once the hostkey has been
369 * generated for this peer, but it hasn't yet been started
370 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
371 * @param hostkey_cls closure for hostkey callback
372 * @param cb function to call with the result
373 * @param cb_cls closure for cb
374 * @return handle to the daemon (actual start will be completed asynchronously)
376 struct GNUNET_TESTING_Daemon *
377 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
378 const struct GNUNET_CONFIGURATION_Handle *cfg,
379 struct GNUNET_TIME_Relative timeout,
380 const char *hostname,
381 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
383 GNUNET_TESTING_NotifyDaemonRunning cb,
387 * Continues GNUnet daemon startup when user wanted to be notified
388 * once a hostkey was generated (for creating friends files, blacklists,
391 * @param daemon the daemon to finish starting
394 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon);
397 * Restart (stop and start) a GNUnet daemon.
399 * @param d the daemon that should be restarted
400 * @param cb function called once the daemon is (re)started
401 * @param cb_cls closure for cb
404 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
405 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls);
408 * Start a peer that has previously been stopped using the daemon_stop
409 * call (and files weren't deleted and the allow restart flag)
411 * @param daemon the daemon to start (has been previously stopped)
412 * @param timeout how long to wait for restart
413 * @param cb the callback for notification when the peer is running
414 * @param cb_cls closure for the callback
417 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
418 struct GNUNET_TIME_Relative timeout,
419 GNUNET_TESTING_NotifyDaemonRunning cb,
423 * Get a certain testing daemon handle.
425 * @param pg handle to the set of running peers
426 * @param position the number of the peer to return
428 struct GNUNET_TESTING_Daemon *
429 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
430 unsigned int position);
434 * Stops a GNUnet daemon.
436 * @param d the daemon that should be stopped
437 * @param timeout how long to wait for process for shutdown to complete
438 * @param cb function called once the daemon was stopped
439 * @param cb_cls closure for cb
440 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
441 * to leave them (i.e. for restarting at a later time,
442 * or logfile inspection once finished)
443 * @param allow_restart GNUNET_YES to restart peer later (using this API)
444 * GNUNET_NO to kill off and clean up for good
447 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
448 struct GNUNET_TIME_Relative timeout,
449 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
450 int delete_files, int allow_restart);
454 * Changes the configuration of a GNUnet daemon.
456 * @param d the daemon that should be modified
457 * @param cfg the new configuration for the daemon
458 * @param cb function called once the configuration was changed
459 * @param cb_cls closure for cb
461 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
462 struct GNUNET_CONFIGURATION_Handle *cfg,
463 GNUNET_TESTING_NotifyCompletion cb,
468 * Establish a connection between two GNUnet daemons.
470 * @param d1 handle for the first daemon
471 * @param d2 handle for the second daemon
472 * @param timeout how long is the connection attempt
474 * @param max_connect_attempts how many times should we try to reconnect
476 * @param cb function to call at the end
477 * @param cb_cls closure for cb
479 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
480 struct GNUNET_TESTING_Daemon *d2,
481 struct GNUNET_TIME_Relative timeout,
482 unsigned int max_connect_attempts,
483 GNUNET_TESTING_NotifyConnection cb,
490 * Start count gnunetd processes with the same set of transports and
491 * applications. The port numbers (any option called "PORT") will be
492 * adjusted to ensure that no two peers running on the same system
493 * have the same port(s) in their respective configurations.
495 * @param sched scheduler to use
496 * @param cfg configuration template to use
497 * @param total number of daemons to start
498 * @param timeout total time allowed for peers to start
499 * @param hostkey_callback function to call on each peers hostkey generation
500 * if NULL, peers will be started by this call, if non-null,
501 * GNUNET_TESTING_daemons_continue_startup must be called after
502 * successful hostkey generation
503 * @param hostkey_cls closure for hostkey callback
504 * @param cb function to call on each daemon that was started
505 * @param cb_cls closure for cb
506 * @param connect_callback function to call each time two hosts are connected
507 * @param connect_callback_cls closure for connect_callback
508 * @param hostnames space-separated list of hostnames to use; can be NULL (to run
509 * everything on localhost).
510 * @return NULL on error, otherwise handle to control peer group
512 struct GNUNET_TESTING_PeerGroup *
513 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
514 const struct GNUNET_CONFIGURATION_Handle *cfg,
516 struct GNUNET_TIME_Relative timeout,
517 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
519 GNUNET_TESTING_NotifyDaemonRunning cb,
521 GNUNET_TESTING_NotifyConnection
522 connect_callback, void *connect_callback_cls,
523 const char *hostnames);
526 * Function which continues a peer group starting up
527 * after successfully generating hostkeys for each peer.
529 * @param pg the peer group to continue starting
532 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg);
535 * Restart all peers in the given group.
537 * @param pg the handle to the peer group
538 * @param callback function to call on completion (or failure)
539 * @param callback_cls closure for the callback function
542 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
543 GNUNET_TESTING_NotifyCompletion callback,
548 * Shutdown all peers started in the given group.
550 * @param pg handle to the peer group
551 * @param timeout how long to wait for shutdown
552 * @param cb callback to notify upon success or failure
553 * @param cb_cls closure for cb
556 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
557 struct GNUNET_TIME_Relative timeout,
558 GNUNET_TESTING_NotifyCompletion cb,
563 * Simulate churn by stopping some peers (and possibly
564 * re-starting others if churn is called multiple times). This
565 * function can only be used to create leave-join churn (peers "never"
566 * leave for good). First "voff" random peers that are currently
567 * online will be taken offline; then "von" random peers that are then
568 * offline will be put back online. No notifications will be
569 * generated for any of these operations except for the callback upon
570 * completion. Note that the implementation is at liberty to keep
571 * the ARM service itself (but none of the other services or daemons)
572 * running even though the "peer" is being varied offline.
574 * @param pg handle for the peer group
575 * @param voff number of peers that should go offline
576 * @param von number of peers that should come back online;
577 * must be zero on first call (since "testbed_start"
578 * always starts all of the peers)
579 * @param timeout how long to wait for operations to finish before
581 * @param cb function to call at the end
582 * @param cb_cls closure for cb
585 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
588 struct GNUNET_TIME_Relative timeout,
589 GNUNET_TESTING_NotifyCompletion cb,
594 * Topologies supported for testbeds.
596 enum GNUNET_TESTING_Topology
599 * A clique (everyone connected to everyone else).
601 GNUNET_TESTING_TOPOLOGY_CLIQUE,
604 * Small-world network (2d torus plus random links).
606 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
609 * Small-world network (ring plus random links).
611 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
616 GNUNET_TESTING_TOPOLOGY_RING,
621 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
626 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
629 * Certain percentage of peers are unable to communicate directly
630 * replicating NAT conditions
632 GNUNET_TESTING_TOPOLOGY_INTERNAT,
635 * Scale free topology.
637 GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
640 * Straight line topology.
642 GNUNET_TESTING_TOPOLOGY_LINE,
645 * All peers are disconnected.
647 GNUNET_TESTING_TOPOLOGY_NONE
651 * Options for connecting a topology.
653 enum GNUNET_TESTING_TopologyOption
656 * Try to connect all peers specified in the topology.
658 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
661 * Choose a random subset of connections to create.
663 GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM,
666 * Create at least X connections for each peer.
668 GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM,
671 * Using a depth first search, create one connection
672 * per peer. If any are missed (graph disconnected)
673 * start over at those peers until all have at least one
676 GNUNET_TESTING_TOPOLOGY_OPTION_DFS,
679 * No options specified.
681 GNUNET_TESTING_TOPOLOGY_OPTION_NONE
686 * Get a topology from a string input.
688 * @param topology where to write the retrieved topology
689 * @param topology_string The string to attempt to
690 * get a configuration value from
691 * @return GNUNET_YES if topology string matched a
692 * known topology, GNUNET_NO if not
695 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string);
698 * Get connect topology option from string input.
700 * @param topology_option where to write the retrieved topology
701 * @param topology_string The string to attempt to
702 * get a configuration value from
703 * @return GNUNET_YES if topology string matched a
704 * known topology, GNUNET_NO if not
707 GNUNET_TESTING_topology_option_get(enum GNUNET_TESTING_TopologyOption *topology_option,
708 char * topology_string);
712 * Takes a peer group and creates a topology based on the
713 * one specified. Creates a topology means generates friend
714 * files for the peers so they can only connect to those allowed
715 * by the topology. This will only have an effect once peers
716 * are started if the FRIENDS_ONLY option is set in the base
717 * config. Also takes an optional restrict topology which
718 * disallows direct TCP connections UNLESS they are specified in
719 * the restricted topology.
721 * @param pg the peer group struct representing the running peers
722 * @param topology which topology to connect the peers in
723 * @param restrict_topology allow only direct TCP connections in this topology
724 * @param restrict_transports space delimited list of transports to blacklist
725 * to create restricted topology
727 * @return the maximum number of connections were all allowed peers
728 * connected to each other
731 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
732 enum GNUNET_TESTING_Topology topology,
733 enum GNUNET_TESTING_Topology restrict_topology,
734 char *restrict_transports);
737 * There are many ways to connect peers that are supported by this function.
738 * To connect peers in the same topology that was created via the
739 * GNUNET_TESTING_create_topology, the topology variable must be set to
740 * GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
741 * a new instance of that topology will be generated and attempted to be
742 * connected. This could result in some connections being impossible,
743 * because some topologies are non-deterministic.
745 * @param pg the peer group struct representing the running peers
746 * @param topology which topology to connect the peers in
747 * @param options options for connecting the topology
748 * @param option_modifier modifier for options that take a parameter
749 * @return the number of connections that will be attempted, GNUNET_SYSERR on error
752 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
753 enum GNUNET_TESTING_Topology topology,
754 enum GNUNET_TESTING_TopologyOption options,
755 double option_modifier);
758 * Start or stop an individual peer from the given group.
760 * @param pg handle to the peer group
761 * @param offset which peer to start or stop
762 * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
763 * @param timeout how long to wait for shutdown
764 * @param cb function to call at the end
765 * @param cb_cls closure for cb
768 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
771 struct GNUNET_TIME_Relative timeout,
772 GNUNET_TESTING_NotifyCompletion cb,
776 #if 0 /* keep Emacsens' auto-indent happy */