f7d79b001eccbafb0101ce7f58c0e76c2e630263
[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 delete the configuration file to complete.
276      */
277   SP_CLEANUP,
278
279     /**
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
283      * services).
284      */
285   SP_CONFIG_UPDATE
286 };
287
288
289 /**
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.
293  */
294 struct GNUNET_TESTING_Daemon
295 {
296   /**
297    * Our scheduler.
298    */
299   struct GNUNET_SCHEDULER_Handle *sched;
300
301   /**
302    * Our configuration.
303    */
304   struct GNUNET_CONFIGURATION_Handle *cfg;
305
306   /**
307    * Host to run GNUnet on.
308    */
309   char *hostname;
310
311   /*
312    * Result of GNUNET_i2s of this peer,
313    * for printing
314    */
315   char *shortname;
316
317   /**
318    * Username we are using.
319    */
320   char *username;
321
322   /**
323    * Name of the configuration file
324    */
325   char *cfgfile;
326
327   /**
328    * Function to call when the peer is running.
329    */
330   GNUNET_TESTING_NotifyDaemonRunning cb;
331
332   /**
333    * Closure for cb.
334    */
335   void *cb_cls;
336
337   /**
338    * Arguments from "daemon_stop" call.
339    */
340   GNUNET_TESTING_NotifyCompletion dead_cb;
341
342   /**
343    * Closure for 'dead_cb'.
344    */
345   void *dead_cb_cls;
346
347   /**
348    * Arguments from "daemon_stop" call.
349    */
350   GNUNET_TESTING_NotifyCompletion update_cb;
351
352   /**
353    * Closure for 'update_cb'.
354    */
355   void *update_cb_cls;
356
357   /**
358    * Identity of this peer (once started).
359    */
360   struct GNUNET_PeerIdentity id;
361
362   /**
363    * Flag to indicate that we've already been asked
364    * to terminate (but could not because some action
365    * was still pending).
366    */
367   int dead;
368
369   /**
370    * PID of the process that we started last.
371    */
372   pid_t pid;
373
374   /**
375    * How many iterations have we been waiting for
376    * the started process to complete?
377    */
378   unsigned int wait_runs;
379
380   /**
381    * In which phase are we during the start of
382    * this process?
383    */
384   enum StartPhase phase;
385
386   /**
387    * ID of the current task.
388    */
389   GNUNET_SCHEDULER_TaskIdentifier task;
390
391   /**
392    * Handle to the server.
393    */
394   struct GNUNET_CORE_Handle *server;
395 };
396
397 /**
398  * Topologies supported for testbeds.
399  */
400 enum GNUNET_TESTING_Topology
401 {
402   /**
403    * A clique (everyone connected to everyone else).
404    */
405   GNUNET_TESTING_TOPOLOGY_CLIQUE,
406
407   /**
408    * Small-world network (2d torus plus random links).
409    */
410   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
411
412   /**
413    * Small-world network (ring plus random links).
414    */
415   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
416
417   /**
418    * Ring topology.
419    */
420   GNUNET_TESTING_TOPOLOGY_RING,
421
422   /**
423    * 2-d torus.
424    */
425   GNUNET_TESTING_TOPOLOGY_2D_TORUS,
426
427   /**
428    * Random graph.
429    */
430   GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
431
432   /**
433    * Certain percentage of peers are unable to communicate directly
434    * replicating NAT conditions
435    */
436   GNUNET_TESTING_TOPOLOGY_INTERNAT,
437
438   /**
439    * All peers are disconnected.
440    */
441   GNUNET_TESTING_TOPOLOGY_NONE
442 };
443
444
445 /**
446  * Start "count" GNUnet daemons with a particular topology.
447  *
448  * @param sched scheduler to use 
449  * @param cfg configuration template to use
450  * @param count number of peers the testbed should have
451  * @param topology desired topology (enforced via F2F)
452  * @param cb function to call on each daemon that was started
453  * @param cb_cls closure for cb
454  * @param hostname where to run the peers; can be NULL (to run
455  *        everything on localhost). Additional
456  *        hosts can be specified using a NULL-terminated list of
457  *        varargs, hosts will then be used round-robin from that
458  *        list.
459  * @return handle to control the testbed
460  */
461 struct GNUNET_TESTING_Testbed *
462 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
463                               const struct GNUNET_CONFIGURATION_Handle *cfg,
464                               unsigned int count,
465                               enum GNUNET_TESTING_Topology topology,
466                               GNUNET_TESTING_NotifyDaemonRunning cb,
467                               void *cb_cls,
468                               const char *hostname,
469                               ...);
470
471
472 /**
473  * Stop all of the daemons started with the start function.
474  *
475  * @param tb handle for the testbed
476  * @param cb function to call when done
477  * @param cb_cls closure for cb
478  */
479 void
480 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
481                              GNUNET_TESTING_NotifyCompletion cb,
482                              void *cb_cls );
483
484
485 /**
486  * Simulate churn in the testbed by stopping some peers (and possibly
487  * re-starting others if churn is called multiple times).  This
488  * function can only be used to create leave-join churn (peers "never"
489  * leave for good).  First "voff" random peers that are currently
490  * online will be taken offline; then "von" random peers that are then
491  * offline will be put back online.  No notifications will be
492  * generated for any of these operations except for the callback upon
493  * completion.  Note that the implementation is at liberty to keep
494  * the ARM service itself (but none of the other services or daemons)
495  * running even though the "peer" is being varied offline.
496  *
497  * @param tb handle for the testbed
498  * @param voff number of peers that should go offline
499  * @param von number of peers that should come back online;
500  *            must be zero on first call (since "testbed_start"
501  *            always starts all of the peers)
502  * @param cb function to call at the end
503  * @param cb_cls closure for cb
504  */
505 void
506 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
507                               unsigned int voff,
508                               unsigned int von,
509                               GNUNET_TESTING_NotifyCompletion cb,
510                               void *cb_cls);
511
512
513 #if 0                           /* keep Emacsens' auto-indent happy */
514 {
515 #endif
516 #ifdef __cplusplus
517 }
518 #endif
519
520 #endif