de7a01ea1d8a1d5190fef9978f0a0fcfa161576c
[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 /* Forward declaration */
46 struct GNUNET_TESTING_Daemon;
47 /* Forward declaration */
48 struct GNUNET_TESTING_PeerGroup;
49
50 /**
51  * Prototype of a function that will be called whenever
52  * a daemon was started by the testing library.
53  *
54  * @param cls closure
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)
59  */
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,
64                                                    const char *emsg);
65
66
67 /**
68  * Prototype of a function that will be called whenever
69  * two daemons are connected by the testing library.
70  *
71  * @param cls closure
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)
79  */
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,
87                                                    const char *emsg);
88
89 /**
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.
94  *
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)
102  */
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,
108                              void *cb_cls);
109
110 struct GNUNET_TESTING_Daemon *
111 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position);
112
113 /**
114  * Prototype of a function that will be called when a
115  * particular operation was completed the testing library.
116  *
117  * @param cls closure
118  * @param emsg NULL on success
119  */
120 typedef void (*GNUNET_TESTING_NotifyCompletion)(void *cls,
121                                                 const char *emsg);
122
123
124 /**
125  * Stops a GNUnet daemon.
126  *
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
130  */
131 void GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
132                                  GNUNET_TESTING_NotifyCompletion cb,
133                                  void * cb_cls);
134
135
136 /**
137  * Changes the configuration of a GNUnet daemon.
138  *
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
143  */
144 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
145                                         struct GNUNET_CONFIGURATION_Handle *cfg,
146                                         GNUNET_TESTING_NotifyCompletion cb,
147                                         void * cb_cls);
148
149 #if HIDDEN
150 /*
151  * Get the short name of a running peer
152  *
153  * @param d the daemon handle
154  */
155 char *
156 GNUNET_TESTING_daemon_get_shortname(struct GNUNET_TESTING_Daemon *d);
157
158 char *
159 GNUNET_TESTING_daemon_get_hostname (struct GNUNET_TESTING_Daemon *d);
160
161 char *
162 GNUNET_TESTING_daemon_get_username (struct GNUNET_TESTING_Daemon *d);
163
164 struct GNUNET_PeerIdentity *
165 GNUNET_TESTING_daemon_get_peer (struct GNUNET_TESTING_Daemon *d);
166
167 struct GNUNET_CONFIGURATION_Handle *
168 GNUNET_TESTING_daemon_get_config (struct GNUNET_TESTING_Daemon *d);
169 #endif
170
171 /**
172  * Establish a connection between two GNUnet daemons.
173  *
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
177  *        allowed to take?
178  * @param cb function to call at the end
179  * @param cb_cls closure for cb
180  */
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,
185                                      void *cb_cls);
186
187
188
189 /**
190  * Handle to a group of GNUnet peers.
191  */
192 struct GNUNET_TESTING_PeerGroup;
193
194
195 /**
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.
200  *
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
211  */
212 struct GNUNET_TESTING_PeerGroup *
213 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
214                               const struct GNUNET_CONFIGURATION_Handle *cfg,
215                               unsigned int total,
216                               GNUNET_TESTING_NotifyDaemonRunning cb,
217                               void *cb_cls,
218                               GNUNET_TESTING_NotifyConnection connect_callback,
219                               void *connect_callback_cls,
220                               const char *hostnames);
221
222
223 /**
224  * Shutdown all peers started in the given group.
225  *
226  * @param pg handle to the peer group
227  */
228 void
229 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg);
230
231 int
232 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg);
233
234
235 /**
236  * Handle to an entire testbed of GNUnet peers.
237  */
238 struct GNUNET_TESTING_Testbed;
239
240 /**
241  * Phases of starting GNUnet on a system.
242  */
243 enum StartPhase
244 {
245     /**
246      * Copy the configuration file to the target system.
247      */
248   SP_COPYING,
249
250     /**
251      * Configuration file has been copied, start ARM on target system.
252      */
253   SP_COPIED,
254
255     /**
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).
259      */
260   SP_START_ARMING,
261
262     /**
263      * We're waiting for CORE to start.
264      */
265   SP_START_CORE,
266
267     /**
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.
270      */
271   SP_START_DONE,
272
273     /**
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
276      * files.
277      */
278   SP_SHUTDOWN_START,
279
280     /**
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
284      * services).
285      */
286   SP_CONFIG_UPDATE
287 };
288
289
290 /**
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.
294  */
295 struct GNUNET_TESTING_Daemon
296 {
297   /**
298    * Our scheduler.
299    */
300   struct GNUNET_SCHEDULER_Handle *sched;
301
302   /**
303    * Our configuration.
304    */
305   struct GNUNET_CONFIGURATION_Handle *cfg;
306
307   /**
308    * Host to run GNUnet on.
309    */
310   char *hostname;
311
312   /*
313    * Result of GNUNET_i2s of this peer,
314    * for printing
315    */
316   char *shortname;
317
318   /**
319    * Username we are using.
320    */
321   char *username;
322
323   /**
324    * Name of the configuration file
325    */
326   char *cfgfile;
327
328   /**
329    * Function to call when the peer is running.
330    */
331   GNUNET_TESTING_NotifyDaemonRunning cb;
332
333   /**
334    * Closure for cb.
335    */
336   void *cb_cls;
337
338   /**
339    * Arguments from "daemon_stop" call.
340    */
341   GNUNET_TESTING_NotifyCompletion dead_cb;
342
343   /**
344    * Closure for 'dead_cb'.
345    */
346   void *dead_cb_cls;
347
348   /**
349    * Arguments from "daemon_stop" call.
350    */
351   GNUNET_TESTING_NotifyCompletion update_cb;
352
353   /**
354    * Closure for 'update_cb'.
355    */
356   void *update_cb_cls;
357
358   /**
359    * Identity of this peer (once started).
360    */
361   struct GNUNET_PeerIdentity id;
362
363   /**
364    * Flag to indicate that we've already been asked
365    * to terminate (but could not because some action
366    * was still pending).
367    */
368   int dead;
369
370   /**
371    * PID of the process that we started last.
372    */
373   pid_t pid;
374
375   /**
376    * How many iterations have we been waiting for
377    * the started process to complete?
378    */
379   unsigned int wait_runs;
380
381   /**
382    * In which phase are we during the start of
383    * this process?
384    */
385   enum StartPhase phase;
386
387   /**
388    * ID of the current task.
389    */
390   GNUNET_SCHEDULER_TaskIdentifier task;
391
392   /**
393    * Handle to the server.
394    */
395   struct GNUNET_CORE_Handle *server;
396 };
397
398 /**
399  * Topologies supported for testbeds.
400  */
401 enum GNUNET_TESTING_Topology
402 {
403   /**
404    * A clique (everyone connected to everyone else).
405    */
406   GNUNET_TESTING_TOPOLOGY_CLIQUE,
407
408   /**
409    * Small-world network (2d torus plus random links).
410    */
411   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
412
413   /**
414    * Small-world network (ring plus random links).
415    */
416   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
417
418   /**
419    * Ring topology.
420    */
421   GNUNET_TESTING_TOPOLOGY_RING,
422
423   /**
424    * 2-d torus.
425    */
426   GNUNET_TESTING_TOPOLOGY_2D_TORUS,
427
428   /**
429    * Random graph.
430    */
431   GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
432
433   /**
434    * Certain percentage of peers are unable to communicate directly
435    * replicating NAT conditions
436    */
437   GNUNET_TESTING_TOPOLOGY_INTERNAT,
438
439   /**
440    * All peers are disconnected.
441    */
442   GNUNET_TESTING_TOPOLOGY_NONE
443 };
444
445
446 /**
447  * Start "count" GNUnet daemons with a particular topology.
448  *
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
459  *        list.
460  * @return handle to control the testbed
461  */
462 struct GNUNET_TESTING_Testbed *
463 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
464                               const struct GNUNET_CONFIGURATION_Handle *cfg,
465                               unsigned int count,
466                               enum GNUNET_TESTING_Topology topology,
467                               GNUNET_TESTING_NotifyDaemonRunning cb,
468                               void *cb_cls,
469                               const char *hostname,
470                               ...);
471
472
473 /**
474  * Stop all of the daemons started with the start function.
475  *
476  * @param tb handle for the testbed
477  * @param cb function to call when done
478  * @param cb_cls closure for cb
479  */
480 void
481 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
482                              GNUNET_TESTING_NotifyCompletion cb,
483                              void *cb_cls );
484
485
486 /**
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.
497  *
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
505  */
506 void
507 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
508                               unsigned int voff,
509                               unsigned int von,
510                               GNUNET_TESTING_NotifyCompletion cb,
511                               void *cb_cls);
512
513
514 #if 0                           /* keep Emacsens' auto-indent happy */
515 {
516 #endif
517 #ifdef __cplusplus
518 }
519 #endif
520
521 #endif