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 delete the configuration file to complete.
280 * We've received a configuration update and are currently waiting for
281 * the copy process for the update to complete. Once it is, we will
282 * return to "SP_START_DONE" (and rely on ARM to restart all affected
290 * Handle for a GNUnet daemon (technically a set of
291 * daemons; the handle is really for the master ARM
292 * daemon) started by the testing library.
294 struct GNUNET_TESTING_Daemon
299 struct GNUNET_SCHEDULER_Handle *sched;
304 struct GNUNET_CONFIGURATION_Handle *cfg;
307 * Host to run GNUnet on.
312 * Result of GNUNET_i2s of this peer,
318 * Username we are using.
323 * Name of the configuration file
328 * Function to call when the peer is running.
330 GNUNET_TESTING_NotifyDaemonRunning cb;
338 * Arguments from "daemon_stop" call.
340 GNUNET_TESTING_NotifyCompletion dead_cb;
343 * Closure for 'dead_cb'.
348 * Arguments from "daemon_stop" call.
350 GNUNET_TESTING_NotifyCompletion update_cb;
353 * Closure for 'update_cb'.
358 * Identity of this peer (once started).
360 struct GNUNET_PeerIdentity id;
363 * Flag to indicate that we've already been asked
364 * to terminate (but could not because some action
365 * was still pending).
370 * PID of the process that we started last.
375 * How many iterations have we been waiting for
376 * the started process to complete?
378 unsigned int wait_runs;
381 * In which phase are we during the start of
384 enum StartPhase phase;
387 * ID of the current task.
389 GNUNET_SCHEDULER_TaskIdentifier task;
392 * Handle to the server.
394 struct GNUNET_CORE_Handle *server;
398 * Topologies supported for testbeds.
400 enum GNUNET_TESTING_Topology
403 * A clique (everyone connected to everyone else).
405 GNUNET_TESTING_TOPOLOGY_CLIQUE,
408 * Small-world network (2d torus plus random links).
410 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
415 GNUNET_TESTING_TOPOLOGY_RING,
420 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
425 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
428 * Certain percentage of peers are unable to communicate directly
429 * replicating NAT conditions
431 GNUNET_TESTING_TOPOLOGY_INTERNAT,
434 * All peers are disconnected.
436 GNUNET_TESTING_TOPOLOGY_NONE
441 * Start "count" GNUnet daemons with a particular topology.
443 * @param sched scheduler to use
444 * @param cfg configuration template to use
445 * @param count number of peers the testbed should have
446 * @param topology desired topology (enforced via F2F)
447 * @param cb function to call on each daemon that was started
448 * @param cb_cls closure for cb
449 * @param hostname where to run the peers; can be NULL (to run
450 * everything on localhost). Additional
451 * hosts can be specified using a NULL-terminated list of
452 * varargs, hosts will then be used round-robin from that
454 * @return handle to control the testbed
456 struct GNUNET_TESTING_Testbed *
457 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
458 const struct GNUNET_CONFIGURATION_Handle *cfg,
460 enum GNUNET_TESTING_Topology topology,
461 GNUNET_TESTING_NotifyDaemonRunning cb,
463 const char *hostname,
468 * Stop all of the daemons started with the start function.
470 * @param tb handle for the testbed
471 * @param cb function to call when done
472 * @param cb_cls closure for cb
475 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
476 GNUNET_TESTING_NotifyCompletion cb,
481 * Simulate churn in the testbed by stopping some peers (and possibly
482 * re-starting others if churn is called multiple times). This
483 * function can only be used to create leave-join churn (peers "never"
484 * leave for good). First "voff" random peers that are currently
485 * online will be taken offline; then "von" random peers that are then
486 * offline will be put back online. No notifications will be
487 * generated for any of these operations except for the callback upon
488 * completion. Note that the implementation is at liberty to keep
489 * the ARM service itself (but none of the other services or daemons)
490 * running even though the "peer" is being varied offline.
492 * @param tb handle for the testbed
493 * @param voff number of peers that should go offline
494 * @param von number of peers that should come back online;
495 * must be zero on first call (since "testbed_start"
496 * always starts all of the peers)
497 * @param cb function to call at the end
498 * @param cb_cls closure for cb
501 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
504 GNUNET_TESTING_NotifyCompletion cb,
508 #if 0 /* keep Emacsens' auto-indent happy */