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
81 * Copy the configuration file to the target system.
86 * Configuration file has been copied, start ARM on target system.
91 * ARM has been started, check that it has properly daemonized and
92 * then try to connect to the CORE service (which should be
93 * auto-started by ARM).
98 * We're waiting for CORE to start.
103 * Core has notified us that we've established a connection to the service.
104 * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
109 * We've been asked to terminate the instance and are now waiting for
110 * the remote command to stop the gnunet-arm process and delete temporary
116 * We've received a configuration update and are currently waiting for
117 * the copy process for the update to complete. Once it is, we will
118 * return to "SP_START_DONE" (and rely on ARM to restart all affected
125 * Prototype of a function that will be called when a
126 * particular operation was completed the testing library.
129 * @param emsg NULL on success
131 typedef void (*GNUNET_TESTING_NotifyCompletion)(void *cls,
135 * Handle for a GNUnet daemon (technically a set of
136 * daemons; the handle is really for the master ARM
137 * daemon) started by the testing library.
139 struct GNUNET_TESTING_Daemon
144 struct GNUNET_SCHEDULER_Handle *sched;
149 struct GNUNET_CONFIGURATION_Handle *cfg;
152 * Host to run GNUnet on.
157 * Result of GNUNET_i2s of this peer,
163 * Username we are using.
168 * Name of the configuration file
173 * Function to call when the peer is running.
175 GNUNET_TESTING_NotifyDaemonRunning cb;
183 * Arguments from "daemon_stop" call.
185 GNUNET_TESTING_NotifyCompletion dead_cb;
188 * Closure for 'dead_cb'.
193 * Arguments from "daemon_stop" call.
195 GNUNET_TESTING_NotifyCompletion update_cb;
198 * Closure for 'update_cb'.
203 * Identity of this peer (once started).
205 struct GNUNET_PeerIdentity id;
208 * Flag to indicate that we've already been asked
209 * to terminate (but could not because some action
210 * was still pending).
215 * PID of the process that we started last.
220 * How many iterations have we been waiting for
221 * the started process to complete?
223 unsigned int wait_runs;
226 * In which phase are we during the start of
229 enum GNUNET_TESTING_StartPhase phase;
232 * ID of the current task.
234 GNUNET_SCHEDULER_TaskIdentifier task;
237 * Handle to the server.
239 struct GNUNET_CORE_Handle *server;
242 * Handle to the transport service of this peer
244 struct GNUNET_TRANSPORT_Handle *th;
247 * HELLO message for this peer
249 struct GNUNET_HELLO_Message *hello;
252 * Set to GNUNET_YES once the peer is up.
259 * Handle to a group of GNUnet peers.
261 struct GNUNET_TESTING_PeerGroup;
265 * Prototype of a function that will be called whenever
266 * two daemons are connected by the testing library.
269 * @param first peer id for first daemon
270 * @param second peer id for the second daemon
271 * @param first_cfg config for the first daemon
272 * @param second_cfg config for the second daemon
273 * @param first_daemon handle for the first daemon
274 * @param second_daemon handle for the second daemon
275 * @param emsg error message (NULL on success)
277 typedef void (*GNUNET_TESTING_NotifyConnection)(void *cls,
278 const struct GNUNET_PeerIdentity *first,
279 const struct GNUNET_PeerIdentity *second,
280 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
281 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
282 struct GNUNET_TESTING_Daemon *first_daemon,
283 struct GNUNET_TESTING_Daemon *second_daemon,
287 * Starts a GNUnet daemon. GNUnet must be installed on the target
288 * system and available in the PATH. The machine must furthermore be
289 * reachable via "ssh" (unless the hostname is "NULL") without the
290 * need to enter a password.
292 * @param sched scheduler to use
293 * @param cfg configuration to use
294 * @param hostname name of the machine where to run GNUnet
295 * (use NULL for localhost).
296 * @param cb function to call with the result
297 * @param cb_cls closure for cb
298 * @return handle to the daemon (actual start will be completed asynchronously)
300 struct GNUNET_TESTING_Daemon *
301 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
302 const struct GNUNET_CONFIGURATION_Handle *cfg,
303 const char *hostname,
304 GNUNET_TESTING_NotifyDaemonRunning cb,
307 struct GNUNET_TESTING_Daemon *
308 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position);
312 * Stops a GNUnet daemon.
314 * @param d the daemon that should be stopped
315 * @param cb function called once the daemon was stopped
316 * @param cb_cls closure for cb
318 void GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
319 GNUNET_TESTING_NotifyCompletion cb,
324 * Changes the configuration of a GNUnet daemon.
326 * @param d the daemon that should be modified
327 * @param cfg the new configuration for the daemon
328 * @param cb function called once the configuration was changed
329 * @param cb_cls closure for cb
331 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
332 struct GNUNET_CONFIGURATION_Handle *cfg,
333 GNUNET_TESTING_NotifyCompletion cb,
338 * Establish a connection between two GNUnet daemons.
340 * @param d1 handle for the first daemon
341 * @param d2 handle for the second daemon
342 * @param timeout how long is the connection attempt
344 * @param max_connect_attempts how many times should we try to reconnect
346 * @param cb function to call at the end
347 * @param cb_cls closure for cb
349 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
350 struct GNUNET_TESTING_Daemon *d2,
351 struct GNUNET_TIME_Relative timeout,
352 unsigned int max_connect_attempts,
353 GNUNET_TESTING_NotifyConnection cb,
360 * Start count gnunetd processes with the same set of transports and
361 * applications. The port numbers (any option called "PORT") will be
362 * adjusted to ensure that no two peers running on the same system
363 * have the same port(s) in their respective configurations.
365 * @param sched scheduler to use
366 * @param cfg configuration template to use
367 * @param total number of daemons to start
368 * @param cb function to call on each daemon that was started
369 * @param cb_cls closure for cb
370 * @param connect_callback function to call each time two hosts are connected
371 * @param connect_callback_cls closure for connect_callback
372 * @param hostnames space-separated list of hostnames to use,
373 * NULL to use localhost only
374 * @return NULL on error, otherwise handle to control peer group
376 struct GNUNET_TESTING_PeerGroup *
377 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
378 const struct GNUNET_CONFIGURATION_Handle *cfg,
380 GNUNET_TESTING_NotifyDaemonRunning cb,
382 GNUNET_TESTING_NotifyConnection connect_callback,
383 void *connect_callback_cls,
384 const char *hostnames);
388 * Shutdown all peers started in the given group.
390 * @param pg handle to the peer group
393 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg);
397 * Topologies supported for testbeds.
399 enum GNUNET_TESTING_Topology
402 * A clique (everyone connected to everyone else).
404 GNUNET_TESTING_TOPOLOGY_CLIQUE,
407 * Small-world network (2d torus plus random links).
409 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
412 * Small-world network (ring plus random links).
414 GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
419 GNUNET_TESTING_TOPOLOGY_RING,
424 GNUNET_TESTING_TOPOLOGY_2D_TORUS,
429 GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
432 * Certain percentage of peers are unable to communicate directly
433 * replicating NAT conditions
435 GNUNET_TESTING_TOPOLOGY_INTERNAT,
438 * Scale free topology.
440 GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
443 * All peers are disconnected.
445 GNUNET_TESTING_TOPOLOGY_NONE
450 * Create a topology out for a group of started peers.
452 * @param pg the peergroup that has already been started
453 * @param topology the topology to connect the peers into
456 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
457 enum GNUNET_TESTING_Topology topology);
461 * Start "count" GNUnet daemons with a particular topology.
463 * @param sched scheduler to use
464 * @param cfg configuration template to use
465 * @param count number of peers the testbed should have
466 * @param topology desired topology (enforced via F2F)
467 * @param cb function to call on each daemon that was started
468 * @param cb_cls closure for cb
469 * @param hostname where to run the peers; can be NULL (to run
470 * everything on localhost). Additional
471 * hosts can be specified using a NULL-terminated list of
472 * varargs, hosts will then be used round-robin from that
474 * @return handle to control the testbed
476 struct GNUNET_TESTING_Testbed *
477 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
478 const struct GNUNET_CONFIGURATION_Handle *cfg,
480 enum GNUNET_TESTING_Topology topology,
481 GNUNET_TESTING_NotifyDaemonRunning cb,
483 const char *hostname,
488 * Stop all of the daemons started with the start function.
490 * @param tb handle for the testbed
491 * @param cb function to call when done
492 * @param cb_cls closure for cb
495 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
496 GNUNET_TESTING_NotifyCompletion cb,
501 * Simulate churn in the testbed by stopping some peers (and possibly
502 * re-starting others if churn is called multiple times). This
503 * function can only be used to create leave-join churn (peers "never"
504 * leave for good). First "voff" random peers that are currently
505 * online will be taken offline; then "von" random peers that are then
506 * offline will be put back online. No notifications will be
507 * generated for any of these operations except for the callback upon
508 * completion. Note that the implementation is at liberty to keep
509 * the ARM service itself (but none of the other services or daemons)
510 * running even though the "peer" is being varied offline.
512 * @param tb handle for the testbed
513 * @param voff number of peers that should go offline
514 * @param von number of peers that should come back online;
515 * must be zero on first call (since "testbed_start"
516 * always starts all of the peers)
517 * @param cb function to call at the end
518 * @param cb_cls closure for cb
521 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
524 GNUNET_TESTING_NotifyCompletion cb,
528 #if 0 /* keep Emacsens' auto-indent happy */