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 */
45 /* Forward declaration */
46 struct GNUNET_TESTING_Daemon;
47 /* Forward declaration */
48 struct GNUNET_TESTING_PeerGroup;
51 * Prototype of a function that will be called whenever
52 * a daemon was started by the testing library.
55 * @param id identifier for the daemon, NULL on error
56 * @param cfg configuration used by this daemon
57 * @param d handle for the daemon
58 * @param emsg error message (NULL on success)
60 typedef void (*GNUNET_TESTING_NotifyDaemonRunning)(void *cls,
61 const struct GNUNET_PeerIdentity *id,
62 const struct GNUNET_CONFIGURATION_Handle *cfg,
63 struct GNUNET_TESTING_Daemon *d,
68 * Prototype of a function that will be called whenever
69 * two daemons are connected by the testing library.
72 * @param first peer id for first daemon
73 * @param second peer id for the second daemon
74 * @param first_cfg config for the first daemon
75 * @param second_cfg config for the second daemon
76 * @param first_daemon handle for the first daemon
77 * @param second_daemon handle for the second daemon
78 * @param emsg error message (NULL on success)
80 typedef void (*GNUNET_TESTING_NotifyConnection)(void *cls,
81 const struct GNUNET_PeerIdentity *first,
82 const struct GNUNET_PeerIdentity *second,
83 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
84 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
85 struct GNUNET_TESTING_Daemon *first_daemon,
86 struct GNUNET_TESTING_Daemon *second_daemon,
90 * Starts a GNUnet daemon. GNUnet must be installed on the target
91 * system and available in the PATH. The machine must furthermore be
92 * reachable via "ssh" (unless the hostname is "NULL") without the
93 * need to enter a password.
95 * @param sched scheduler to use
96 * @param cfg configuration to use
97 * @param hostname name of the machine where to run GNUnet
98 * (use NULL for localhost).
99 * @param cb function to call with the result
100 * @param cb_cls closure for cb
101 * @return handle to the daemon (actual start will be completed asynchronously)
103 struct GNUNET_TESTING_Daemon *
104 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
105 const struct GNUNET_CONFIGURATION_Handle *cfg,
106 const char *hostname,
107 GNUNET_TESTING_NotifyDaemonRunning cb,
110 struct GNUNET_TESTING_Daemon *
111 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position);
114 * Prototype of a function that will be called when a
115 * particular operation was completed the testing library.
118 * @param emsg NULL on success
120 typedef void (*GNUNET_TESTING_NotifyCompletion)(void *cls,
125 * Stops a GNUnet daemon.
127 * @param d the daemon that should be stopped
128 * @param cb function called once the daemon was stopped
129 * @param cb_cls closure for cb
131 void GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
132 GNUNET_TESTING_NotifyCompletion cb,
137 * Changes the configuration of a GNUnet daemon.
139 * @param d the daemon that should be modified
140 * @param cfg the new configuration for the daemon
141 * @param cb function called once the configuration was changed
142 * @param cb_cls closure for cb
144 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
145 struct GNUNET_CONFIGURATION_Handle *cfg,
146 GNUNET_TESTING_NotifyCompletion cb,
151 * Get the short name of a running peer
153 * @param d the daemon handle
156 GNUNET_TESTING_daemon_get_shortname(struct GNUNET_TESTING_Daemon *d);
159 GNUNET_TESTING_daemon_get_hostname (struct GNUNET_TESTING_Daemon *d);
162 GNUNET_TESTING_daemon_get_username (struct GNUNET_TESTING_Daemon *d);
164 struct GNUNET_PeerIdentity *
165 GNUNET_TESTING_daemon_get_peer (struct GNUNET_TESTING_Daemon *d);
167 struct GNUNET_CONFIGURATION_Handle *
168 GNUNET_TESTING_daemon_get_config (struct GNUNET_TESTING_Daemon *d);
172 * Establish a connection between two GNUnet daemons.
174 * @param d1 handle for the first daemon
175 * @param d2 handle for the second daemon
176 * @param timeout how long is the connection attempt
178 * @param max_connect_attempts how many times should we try to reconnect
180 * @param cb function to call at the end
181 * @param cb_cls closure for cb
183 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
184 struct GNUNET_TESTING_Daemon *d2,
185 struct GNUNET_TIME_Relative timeout,
186 unsigned int max_connect_attempts,
187 GNUNET_TESTING_NotifyConnection cb,
193 * Handle to a group of GNUnet peers.
195 struct GNUNET_TESTING_PeerGroup;
199 * Start count gnunetd processes with the same set of transports and
200 * applications. The port numbers (any option called "PORT") will be
201 * adjusted to ensure that no two peers running on the same system
202 * have the same port(s) in their respective configurations.
204 * @param sched scheduler to use
205 * @param cfg configuration template to use
206 * @param total number of daemons to start
207 * @param cb function to call on each daemon that was started
208 * @param cb_cls closure for cb
209 * @param connect_callback function to call each time two hosts are connected
210 * @param connect_callback_cls closure for connect_callback
211 * @param hostnames space-separated list of hostnames to use,
212 * NULL to use localhost only
213 * @return NULL on error, otherwise handle to control peer group
215 struct GNUNET_TESTING_PeerGroup *
216 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
217 const struct GNUNET_CONFIGURATION_Handle *cfg,
219 GNUNET_TESTING_NotifyDaemonRunning cb,
221 GNUNET_TESTING_NotifyConnection connect_callback,
222 void *connect_callback_cls,
223 const char *hostnames);
227 * Shutdown all peers started in the given group.
229 * @param pg handle to the peer group
232 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg);
235 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg);
239 * Handle to an entire testbed of GNUnet peers.
241 struct GNUNET_TESTING_Testbed;
244 * Phases of starting GNUnet on a system.
249 * Copy the configuration file to the target system.
254 * Configuration file has been copied, start ARM on target system.
259 * ARM has been started, check that it has properly daemonized and
260 * then try to connect to the CORE service (which should be
261 * auto-started by ARM).
266 * We're waiting for CORE to start.
271 * Core has notified us that we've established a connection to the service.
272 * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
277 * We've been asked to terminate the instance and are now waiting for
278 * the remote command to stop the gnunet-arm process and delete temporary
284 * We've received a configuration update and are currently waiting for
285 * the copy process for the update to complete. Once it is, we will
286 * return to "SP_START_DONE" (and rely on ARM to restart all affected
294 * Handle for a GNUnet daemon (technically a set of
295 * daemons; the handle is really for the master ARM
296 * daemon) started by the testing library.
298 struct GNUNET_TESTING_Daemon
303 struct GNUNET_SCHEDULER_Handle *sched;
308 struct GNUNET_CONFIGURATION_Handle *cfg;
311 * Host to run GNUnet on.
316 * Result of GNUNET_i2s of this peer,
322 * Username we are using.
327 * Name of the configuration file
332 * Function to call when the peer is running.
334 GNUNET_TESTING_NotifyDaemonRunning cb;
342 * Arguments from "daemon_stop" call.
344 GNUNET_TESTING_NotifyCompletion dead_cb;
347 * Closure for 'dead_cb'.
352 * Arguments from "daemon_stop" call.
354 GNUNET_TESTING_NotifyCompletion update_cb;
357 * Closure for 'update_cb'.
362 * Identity of this peer (once started).
364 struct GNUNET_PeerIdentity id;
367 * Flag to indicate that we've already been asked
368 * to terminate (but could not because some action
369 * was still pending).
374 * PID of the process that we started last.
379 * How many iterations have we been waiting for
380 * the started process to complete?
382 unsigned int wait_runs;
385 * In which phase are we during the start of
388 enum StartPhase phase;
391 * ID of the current task.
393 GNUNET_SCHEDULER_TaskIdentifier task;
396 * Handle to the server.
398 struct GNUNET_CORE_Handle *server;
401 * Handle to the transport service of this peer
403 struct GNUNET_TRANSPORT_Handle *th;
406 * HELLO message for this peer
408 struct GNUNET_HELLO_Message *hello;
412 * Topologies supported for testbeds.
414 enum GNUNET_TESTING_Topology
417 * A clique (everyone connected to everyone else).
419 GNUNET_TESTING_TOPOLOGY_CLIQUE,
422 * Small-world network (2d torus plus random links).
424 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
427 * Small-world network (ring plus random links).
429 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
434 GNUNET_TESTING_TOPOLOGY_RING,
439 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
444 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
447 * Certain percentage of peers are unable to communicate directly
448 * replicating NAT conditions
450 GNUNET_TESTING_TOPOLOGY_INTERNAT,
453 * All peers are disconnected.
455 GNUNET_TESTING_TOPOLOGY_NONE
460 * Start "count" GNUnet daemons with a particular topology.
462 * @param sched scheduler to use
463 * @param cfg configuration template to use
464 * @param count number of peers the testbed should have
465 * @param topology desired topology (enforced via F2F)
466 * @param cb function to call on each daemon that was started
467 * @param cb_cls closure for cb
468 * @param hostname where to run the peers; can be NULL (to run
469 * everything on localhost). Additional
470 * hosts can be specified using a NULL-terminated list of
471 * varargs, hosts will then be used round-robin from that
473 * @return handle to control the testbed
475 struct GNUNET_TESTING_Testbed *
476 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
477 const struct GNUNET_CONFIGURATION_Handle *cfg,
479 enum GNUNET_TESTING_Topology topology,
480 GNUNET_TESTING_NotifyDaemonRunning cb,
482 const char *hostname,
487 * Stop all of the daemons started with the start function.
489 * @param tb handle for the testbed
490 * @param cb function to call when done
491 * @param cb_cls closure for cb
494 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
495 GNUNET_TESTING_NotifyCompletion cb,
500 * Simulate churn in the testbed by stopping some peers (and possibly
501 * re-starting others if churn is called multiple times). This
502 * function can only be used to create leave-join churn (peers "never"
503 * leave for good). First "voff" random peers that are currently
504 * online will be taken offline; then "von" random peers that are then
505 * offline will be put back online. No notifications will be
506 * generated for any of these operations except for the callback upon
507 * completion. Note that the implementation is at liberty to keep
508 * the ARM service itself (but none of the other services or daemons)
509 * running even though the "peer" is being varied offline.
511 * @param tb handle for the testbed
512 * @param voff number of peers that should go offline
513 * @param von number of peers that should come back online;
514 * must be zero on first call (since "testbed_start"
515 * always starts all of the peers)
516 * @param cb function to call at the end
517 * @param cb_cls closure for cb
520 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
523 GNUNET_TESTING_NotifyCompletion cb,
527 #if 0 /* keep Emacsens' auto-indent happy */