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 */
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;
54 * Prototype of a function that will be called whenever
55 * a daemon was started by the testing library.
58 * @param id identifier for the daemon, NULL on error
59 * @param cfg configuration used by this daemon
60 * @param d handle for the daemon
61 * @param emsg error message (NULL on success)
63 typedef void (*GNUNET_TESTING_NotifyDaemonRunning)(void *cls,
64 const struct GNUNET_PeerIdentity *id,
65 const struct GNUNET_CONFIGURATION_Handle *cfg,
66 struct GNUNET_TESTING_Daemon *d,
71 * Handle to an entire testbed of GNUnet peers.
73 struct GNUNET_TESTING_Testbed;
76 * Phases of starting GNUnet on a system.
78 enum GNUNET_TESTING_StartPhase;
82 * Handle for a GNUnet daemon (technically a set of
83 * daemons; the handle is really for the master ARM
84 * daemon) started by the testing library.
86 struct GNUNET_TESTING_Daemon
91 struct GNUNET_SCHEDULER_Handle *sched;
96 struct GNUNET_CONFIGURATION_Handle *cfg;
99 * Host to run GNUnet on.
104 * Result of GNUNET_i2s of this peer,
110 * Username we are using.
115 * Name of the configuration file
120 * Function to call when the peer is running.
122 GNUNET_TESTING_NotifyDaemonRunning cb;
130 * Arguments from "daemon_stop" call.
132 GNUNET_TESTING_NotifyCompletion dead_cb;
135 * Closure for 'dead_cb'.
140 * Arguments from "daemon_stop" call.
142 GNUNET_TESTING_NotifyCompletion update_cb;
145 * Closure for 'update_cb'.
150 * Identity of this peer (once started).
152 struct GNUNET_PeerIdentity id;
155 * Flag to indicate that we've already been asked
156 * to terminate (but could not because some action
157 * was still pending).
162 * PID of the process that we started last.
167 * How many iterations have we been waiting for
168 * the started process to complete?
170 unsigned int wait_runs;
173 * In which phase are we during the start of
176 enum GNUNET_TESTING_StartPhase phase;
179 * ID of the current task.
181 GNUNET_SCHEDULER_TaskIdentifier task;
184 * Handle to the server.
186 struct GNUNET_CORE_Handle *server;
189 * Handle to the transport service of this peer
191 struct GNUNET_TRANSPORT_Handle *th;
194 * HELLO message for this peer
196 struct GNUNET_HELLO_Message *hello;
201 * Handle to a group of GNUnet peers.
203 struct GNUNET_TESTING_PeerGroup;
207 * Prototype of a function that will be called whenever
208 * two daemons are connected by the testing library.
211 * @param first peer id for first daemon
212 * @param second peer id for the second daemon
213 * @param first_cfg config for the first daemon
214 * @param second_cfg config for the second daemon
215 * @param first_daemon handle for the first daemon
216 * @param second_daemon handle for the second daemon
217 * @param emsg error message (NULL on success)
219 typedef void (*GNUNET_TESTING_NotifyConnection)(void *cls,
220 const struct GNUNET_PeerIdentity *first,
221 const struct GNUNET_PeerIdentity *second,
222 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
223 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
224 struct GNUNET_TESTING_Daemon *first_daemon,
225 struct GNUNET_TESTING_Daemon *second_daemon,
229 * Starts a GNUnet daemon. GNUnet must be installed on the target
230 * system and available in the PATH. The machine must furthermore be
231 * reachable via "ssh" (unless the hostname is "NULL") without the
232 * need to enter a password.
234 * @param sched scheduler to use
235 * @param cfg configuration to use
236 * @param hostname name of the machine where to run GNUnet
237 * (use NULL for localhost).
238 * @param cb function to call with the result
239 * @param cb_cls closure for cb
240 * @return handle to the daemon (actual start will be completed asynchronously)
242 struct GNUNET_TESTING_Daemon *
243 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
244 const struct GNUNET_CONFIGURATION_Handle *cfg,
245 const char *hostname,
246 GNUNET_TESTING_NotifyDaemonRunning cb,
249 struct GNUNET_TESTING_Daemon *
250 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position);
253 * Prototype of a function that will be called when a
254 * particular operation was completed the testing library.
257 * @param emsg NULL on success
259 typedef void (*GNUNET_TESTING_NotifyCompletion)(void *cls,
264 * Stops a GNUnet daemon.
266 * @param d the daemon that should be stopped
267 * @param cb function called once the daemon was stopped
268 * @param cb_cls closure for cb
270 void GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
271 GNUNET_TESTING_NotifyCompletion cb,
276 * Changes the configuration of a GNUnet daemon.
278 * @param d the daemon that should be modified
279 * @param cfg the new configuration for the daemon
280 * @param cb function called once the configuration was changed
281 * @param cb_cls closure for cb
283 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
284 struct GNUNET_CONFIGURATION_Handle *cfg,
285 GNUNET_TESTING_NotifyCompletion cb,
290 * Establish a connection between two GNUnet daemons.
292 * @param d1 handle for the first daemon
293 * @param d2 handle for the second daemon
294 * @param timeout how long is the connection attempt
296 * @param max_connect_attempts how many times should we try to reconnect
298 * @param cb function to call at the end
299 * @param cb_cls closure for cb
301 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
302 struct GNUNET_TESTING_Daemon *d2,
303 struct GNUNET_TIME_Relative timeout,
304 unsigned int max_connect_attempts,
305 GNUNET_TESTING_NotifyConnection cb,
312 * Start count gnunetd processes with the same set of transports and
313 * applications. The port numbers (any option called "PORT") will be
314 * adjusted to ensure that no two peers running on the same system
315 * have the same port(s) in their respective configurations.
317 * @param sched scheduler to use
318 * @param cfg configuration template to use
319 * @param total number of daemons to start
320 * @param cb function to call on each daemon that was started
321 * @param cb_cls closure for cb
322 * @param connect_callback function to call each time two hosts are connected
323 * @param connect_callback_cls closure for connect_callback
324 * @param hostnames space-separated list of hostnames to use,
325 * NULL to use localhost only
326 * @return NULL on error, otherwise handle to control peer group
328 struct GNUNET_TESTING_PeerGroup *
329 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
330 const struct GNUNET_CONFIGURATION_Handle *cfg,
332 GNUNET_TESTING_NotifyDaemonRunning cb,
334 GNUNET_TESTING_NotifyConnection connect_callback,
335 void *connect_callback_cls,
336 const char *hostnames);
340 * Shutdown all peers started in the given group.
342 * @param pg handle to the peer group
345 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg);
349 * Topologies supported for testbeds.
351 enum GNUNET_TESTING_Topology
354 * A clique (everyone connected to everyone else).
356 GNUNET_TESTING_TOPOLOGY_CLIQUE,
359 * Small-world network (2d torus plus random links).
361 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
364 * Small-world network (ring plus random links).
366 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
371 GNUNET_TESTING_TOPOLOGY_RING,
376 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
381 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
384 * Certain percentage of peers are unable to communicate directly
385 * replicating NAT conditions
387 GNUNET_TESTING_TOPOLOGY_INTERNAT,
392 GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
395 * All peers are disconnected.
397 GNUNET_TESTING_TOPOLOGY_NONE
405 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg
406 /* enum GNUNET_TESTING_Topology topo */);
410 * Start "count" GNUnet daemons with a particular topology.
412 * @param sched scheduler to use
413 * @param cfg configuration template to use
414 * @param count number of peers the testbed should have
415 * @param topology desired topology (enforced via F2F)
416 * @param cb function to call on each daemon that was started
417 * @param cb_cls closure for cb
418 * @param hostname where to run the peers; can be NULL (to run
419 * everything on localhost). Additional
420 * hosts can be specified using a NULL-terminated list of
421 * varargs, hosts will then be used round-robin from that
423 * @return handle to control the testbed
425 struct GNUNET_TESTING_Testbed *
426 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
427 const struct GNUNET_CONFIGURATION_Handle *cfg,
429 enum GNUNET_TESTING_Topology topology,
430 GNUNET_TESTING_NotifyDaemonRunning cb,
432 const char *hostname,
437 * Stop all of the daemons started with the start function.
439 * @param tb handle for the testbed
440 * @param cb function to call when done
441 * @param cb_cls closure for cb
444 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
445 GNUNET_TESTING_NotifyCompletion cb,
450 * Simulate churn in the testbed by stopping some peers (and possibly
451 * re-starting others if churn is called multiple times). This
452 * function can only be used to create leave-join churn (peers "never"
453 * leave for good). First "voff" random peers that are currently
454 * online will be taken offline; then "von" random peers that are then
455 * offline will be put back online. No notifications will be
456 * generated for any of these operations except for the callback upon
457 * completion. Note that the implementation is at liberty to keep
458 * the ARM service itself (but none of the other services or daemons)
459 * running even though the "peer" is being varied offline.
461 * @param tb handle for the testbed
462 * @param voff number of peers that should go offline
463 * @param von number of peers that should come back online;
464 * must be zero on first call (since "testbed_start"
465 * always starts all of the peers)
466 * @param cb function to call at the end
467 * @param cb_cls closure for cb
470 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
473 GNUNET_TESTING_NotifyCompletion cb,
477 #if 0 /* keep Emacsens' auto-indent happy */