dox
[oweals/gnunet.git] / src / include / gnunet_testing_lib.h
1 /*
2       This file is part of GNUnet
3       (C) 2008, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
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
30  */
31
32 #ifndef GNUNET_TESTING_LIB_H
33 #define GNUNET_TESTING_LIB_H
34
35 #include "gnunet_util_lib.h"
36
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #if 0                           /* keep Emacsens' auto-indent happy */
41 }
42 #endif
43 #endif
44
45 /**
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.
49  */
50 struct GNUNET_TESTING_Daemon;
51
52
53 /**
54  * Prototype of a function that will be called whenever
55  * a daemon was started by the testing library.
56  *
57  * @param cls closure
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)
62  */
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,
67                                                    const char *emsg);
68
69
70 /**
71  * Handle to an entire testbed of GNUnet peers.
72  */
73 struct GNUNET_TESTING_Testbed;
74
75 /**
76  * Phases of starting GNUnet on a system.
77  */
78 enum GNUNET_TESTING_StartPhase
79 {
80   /**
81    * Copy the configuration file to the target system.
82    */
83   SP_COPYING,
84
85   /**
86    * Configuration file has been copied, start ARM on target system.
87    */
88   SP_COPIED,
89
90   /**
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).
94    */
95   SP_START_ARMING,
96
97   /**
98    * We're waiting for CORE to start.
99    */
100   SP_START_CORE,
101
102   /**
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.
105    */
106   SP_START_DONE,
107
108   /**
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
111    * files.
112    */
113   SP_SHUTDOWN_START,
114
115   /**
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
119    * services).
120    */
121   SP_CONFIG_UPDATE
122 };
123
124 /**
125  * Prototype of a function that will be called when a
126  * particular operation was completed the testing library.
127  *
128  * @param cls closure
129  * @param emsg NULL on success
130  */
131 typedef void (*GNUNET_TESTING_NotifyCompletion)(void *cls,
132                                                 const char *emsg);
133
134 /**
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.
138  */
139 struct GNUNET_TESTING_Daemon
140 {
141   /**
142    * Our scheduler.
143    */
144   struct GNUNET_SCHEDULER_Handle *sched;
145
146   /**
147    * Our configuration.
148    */
149   struct GNUNET_CONFIGURATION_Handle *cfg;
150
151   /**
152    * Host to run GNUnet on.
153    */
154   char *hostname;
155
156   /**
157    * Result of GNUNET_i2s of this peer,
158    * for printing
159    */
160   char *shortname;
161
162   /**
163    * Username we are using.
164    */
165   char *username;
166
167   /**
168    * Name of the configuration file
169    */
170   char *cfgfile;
171
172   /**
173    * Function to call when the peer is running.
174    */
175   GNUNET_TESTING_NotifyDaemonRunning cb;
176
177   /**
178    * Closure for cb.
179    */
180   void *cb_cls;
181
182   /**
183    * Arguments from "daemon_stop" call.
184    */
185   GNUNET_TESTING_NotifyCompletion dead_cb;
186
187   /**
188    * Closure for 'dead_cb'.
189    */
190   void *dead_cb_cls;
191
192   /**
193    * Arguments from "daemon_stop" call.
194    */
195   GNUNET_TESTING_NotifyCompletion update_cb;
196
197   /**
198    * Closure for 'update_cb'.
199    */
200   void *update_cb_cls;
201
202   /**
203    * Identity of this peer (once started).
204    */
205   struct GNUNET_PeerIdentity id;
206
207   /**
208    * Flag to indicate that we've already been asked
209    * to terminate (but could not because some action
210    * was still pending).
211    */
212   int dead;
213
214   /**
215    * PID of the process that we started last.
216    */
217   pid_t pid;
218
219   /**
220    * How many iterations have we been waiting for
221    * the started process to complete?
222    */
223   unsigned int wait_runs;
224
225   /**
226    * In which phase are we during the start of
227    * this process?
228    */
229   enum GNUNET_TESTING_StartPhase phase;
230
231   /**
232    * ID of the current task.
233    */
234   GNUNET_SCHEDULER_TaskIdentifier task;
235
236   /**
237    * Handle to the server.
238    */
239   struct GNUNET_CORE_Handle *server;
240
241   /**
242    * Handle to the transport service of this peer
243    */
244   struct GNUNET_TRANSPORT_Handle *th;
245
246   /**
247    * HELLO message for this peer
248    */
249   struct GNUNET_HELLO_Message *hello;
250
251   /**
252    * Set to GNUNET_YES once the peer is up.
253    */
254   int running;
255 };
256
257
258 /**
259  * Handle to a group of GNUnet peers.
260  */
261 struct GNUNET_TESTING_PeerGroup;
262
263
264 /**
265  * Prototype of a function that will be called whenever
266  * two daemons are connected by the testing library.
267  *
268  * @param cls closure
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)
276  */
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,
284                                                    const char *emsg);
285
286 /**
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.
291  *
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)
299  */
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,
305                              void *cb_cls);
306
307 struct GNUNET_TESTING_Daemon *
308 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position);
309
310
311 /**
312  * Stops a GNUnet daemon.
313  *
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
317  */
318 void GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
319                                  GNUNET_TESTING_NotifyCompletion cb,
320                                  void * cb_cls);
321
322
323 /**
324  * Changes the configuration of a GNUnet daemon.
325  *
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
330  */
331 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
332                                         struct GNUNET_CONFIGURATION_Handle *cfg,
333                                         GNUNET_TESTING_NotifyCompletion cb,
334                                         void * cb_cls);
335
336
337 /**
338  * Establish a connection between two GNUnet daemons.
339  *
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
343  *        allowed to take?
344  * @param max_connect_attempts how many times should we try to reconnect
345  *        (within timeout)
346  * @param cb function to call at the end
347  * @param cb_cls closure for cb
348  */
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,
354                                      void *cb_cls);
355
356
357
358
359 /**
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.
364  *
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
375  */
376 struct GNUNET_TESTING_PeerGroup *
377 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
378                               const struct GNUNET_CONFIGURATION_Handle *cfg,
379                               unsigned int total,
380                               GNUNET_TESTING_NotifyDaemonRunning cb,
381                               void *cb_cls,
382                               GNUNET_TESTING_NotifyConnection connect_callback,
383                               void *connect_callback_cls,
384                               const char *hostnames);
385
386
387 /**
388  * Shutdown all peers started in the given group.
389  *
390  * @param pg handle to the peer group
391  */
392 void
393 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg);
394
395
396 /**
397  * Topologies supported for testbeds.
398  */
399 enum GNUNET_TESTING_Topology
400 {
401   /**
402    * A clique (everyone connected to everyone else).
403    */
404   GNUNET_TESTING_TOPOLOGY_CLIQUE,
405
406   /**
407    * Small-world network (2d torus plus random links).
408    */
409   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
410
411   /**
412    * Small-world network (ring plus random links).
413    */
414   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
415
416   /**
417    * Ring topology.
418    */
419   GNUNET_TESTING_TOPOLOGY_RING,
420
421   /**
422    * 2-d torus.
423    */
424   GNUNET_TESTING_TOPOLOGY_2D_TORUS,
425
426   /**
427    * Random graph.
428    */
429   GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
430
431   /**
432    * Certain percentage of peers are unable to communicate directly
433    * replicating NAT conditions
434    */
435   GNUNET_TESTING_TOPOLOGY_INTERNAT,
436
437   /**
438    * Scale free topology.
439    */
440   GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
441
442   /**
443    * All peers are disconnected.
444    */
445   GNUNET_TESTING_TOPOLOGY_NONE
446 };
447
448
449 /**
450  * Create a topology out for a group of started peers.
451  *
452  * @param pg the peergroup that has already been started
453  * @param topology the topology to connect the peers into
454  */
455 int
456 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
457                                 enum GNUNET_TESTING_Topology topology);
458
459
460 /**
461  * Start "count" GNUnet daemons with a particular topology.
462  *
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
473  *        list.
474  * @return handle to control the testbed
475  */
476 struct GNUNET_TESTING_Testbed *
477 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
478                               const struct GNUNET_CONFIGURATION_Handle *cfg,
479                               unsigned int count,
480                               enum GNUNET_TESTING_Topology topology,
481                               GNUNET_TESTING_NotifyDaemonRunning cb,
482                               void *cb_cls,
483                               const char *hostname,
484                               ...);
485
486
487 /**
488  * Stop all of the daemons started with the start function.
489  *
490  * @param tb handle for the testbed
491  * @param cb function to call when done
492  * @param cb_cls closure for cb
493  */
494 void
495 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
496                              GNUNET_TESTING_NotifyCompletion cb,
497                              void *cb_cls );
498
499
500 /**
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.
511  *
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
519  */
520 void
521 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
522                               unsigned int voff,
523                               unsigned int von,
524                               GNUNET_TESTING_NotifyCompletion cb,
525                               void *cb_cls);
526
527
528 #if 0                           /* keep Emacsens' auto-indent happy */
529 {
530 #endif
531 #ifdef __cplusplus
532 }
533 #endif
534
535 #endif