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 cb function to call at the end
179 * @param cb_cls closure for cb
181 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
182 struct GNUNET_TESTING_Daemon *d2,
183 struct GNUNET_TIME_Relative timeout,
184 GNUNET_TESTING_NotifyConnection cb,
190 * Handle to a group of GNUnet peers.
192 struct GNUNET_TESTING_PeerGroup;
196 * Start count gnunetd processes with the same set of transports and
197 * applications. The port numbers (any option called "PORT") will be
198 * adjusted to ensure that no two peers running on the same system
199 * have the same port(s) in their respective configurations.
201 * @param sched scheduler to use
202 * @param cfg configuration template to use
203 * @param total number of daemons to start
204 * @param cb function to call on each daemon that was started
205 * @param cb_cls closure for cb
206 * @param connect_callback function to call each time two hosts are connected
207 * @param connect_callback_cls closure for connect_callback
208 * @param hostnames space-separated list of hostnames to use,
209 * NULL to use localhost only
210 * @return NULL on error, otherwise handle to control peer group
212 struct GNUNET_TESTING_PeerGroup *
213 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
214 const struct GNUNET_CONFIGURATION_Handle *cfg,
216 GNUNET_TESTING_NotifyDaemonRunning cb,
218 GNUNET_TESTING_NotifyConnection connect_callback,
219 void *connect_callback_cls,
220 const char *hostnames);
224 * Shutdown all peers started in the given group.
226 * @param pg handle to the peer group
229 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg);
232 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg);
236 * Handle to an entire testbed of GNUnet peers.
238 struct GNUNET_TESTING_Testbed;
241 * Phases of starting GNUnet on a system.
246 * Copy the configuration file to the target system.
251 * Configuration file has been copied, start ARM on target system.
256 * ARM has been started, check that it has properly daemonized and
257 * then try to connect to the CORE service (which should be
258 * auto-started by ARM).
263 * We're waiting for CORE to start.
268 * Core has notified us that we've established a connection to the service.
269 * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
274 * We've been asked to terminate the instance and are now waiting for
275 * the remote command to stop the gnunet-arm process and delete temporary
281 * We've received a configuration update and are currently waiting for
282 * the copy process for the update to complete. Once it is, we will
283 * return to "SP_START_DONE" (and rely on ARM to restart all affected
291 * Handle for a GNUnet daemon (technically a set of
292 * daemons; the handle is really for the master ARM
293 * daemon) started by the testing library.
295 struct GNUNET_TESTING_Daemon
300 struct GNUNET_SCHEDULER_Handle *sched;
305 struct GNUNET_CONFIGURATION_Handle *cfg;
308 * Host to run GNUnet on.
313 * Result of GNUNET_i2s of this peer,
319 * Username we are using.
324 * Name of the configuration file
329 * Function to call when the peer is running.
331 GNUNET_TESTING_NotifyDaemonRunning cb;
339 * Arguments from "daemon_stop" call.
341 GNUNET_TESTING_NotifyCompletion dead_cb;
344 * Closure for 'dead_cb'.
349 * Arguments from "daemon_stop" call.
351 GNUNET_TESTING_NotifyCompletion update_cb;
354 * Closure for 'update_cb'.
359 * Identity of this peer (once started).
361 struct GNUNET_PeerIdentity id;
364 * Flag to indicate that we've already been asked
365 * to terminate (but could not because some action
366 * was still pending).
371 * PID of the process that we started last.
376 * How many iterations have we been waiting for
377 * the started process to complete?
379 unsigned int wait_runs;
382 * In which phase are we during the start of
385 enum StartPhase phase;
388 * ID of the current task.
390 GNUNET_SCHEDULER_TaskIdentifier task;
393 * Handle to the server.
395 struct GNUNET_CORE_Handle *server;
399 * Topologies supported for testbeds.
401 enum GNUNET_TESTING_Topology
404 * A clique (everyone connected to everyone else).
406 GNUNET_TESTING_TOPOLOGY_CLIQUE,
409 * Small-world network (2d torus plus random links).
411 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
414 * Small-world network (ring plus random links).
416 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
421 GNUNET_TESTING_TOPOLOGY_RING,
426 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
431 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
434 * Certain percentage of peers are unable to communicate directly
435 * replicating NAT conditions
437 GNUNET_TESTING_TOPOLOGY_INTERNAT,
440 * All peers are disconnected.
442 GNUNET_TESTING_TOPOLOGY_NONE
447 * Start "count" GNUnet daemons with a particular topology.
449 * @param sched scheduler to use
450 * @param cfg configuration template to use
451 * @param count number of peers the testbed should have
452 * @param topology desired topology (enforced via F2F)
453 * @param cb function to call on each daemon that was started
454 * @param cb_cls closure for cb
455 * @param hostname where to run the peers; can be NULL (to run
456 * everything on localhost). Additional
457 * hosts can be specified using a NULL-terminated list of
458 * varargs, hosts will then be used round-robin from that
460 * @return handle to control the testbed
462 struct GNUNET_TESTING_Testbed *
463 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
464 const struct GNUNET_CONFIGURATION_Handle *cfg,
466 enum GNUNET_TESTING_Topology topology,
467 GNUNET_TESTING_NotifyDaemonRunning cb,
469 const char *hostname,
474 * Stop all of the daemons started with the start function.
476 * @param tb handle for the testbed
477 * @param cb function to call when done
478 * @param cb_cls closure for cb
481 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
482 GNUNET_TESTING_NotifyCompletion cb,
487 * Simulate churn in the testbed by stopping some peers (and possibly
488 * re-starting others if churn is called multiple times). This
489 * function can only be used to create leave-join churn (peers "never"
490 * leave for good). First "voff" random peers that are currently
491 * online will be taken offline; then "von" random peers that are then
492 * offline will be put back online. No notifications will be
493 * generated for any of these operations except for the callback upon
494 * completion. Note that the implementation is at liberty to keep
495 * the ARM service itself (but none of the other services or daemons)
496 * running even though the "peer" is being varied offline.
498 * @param tb handle for the testbed
499 * @param voff number of peers that should go offline
500 * @param von number of peers that should come back online;
501 * must be zero on first call (since "testbed_start"
502 * always starts all of the peers)
503 * @param cb function to call at the end
504 * @param cb_cls closure for cb
507 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
510 GNUNET_TESTING_NotifyCompletion cb,
514 #if 0 /* keep Emacsens' auto-indent happy */