code
[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 max_connect_attempts how many times should we try to reconnect
179  *        (within timeout)
180  * @param cb function to call at the end
181  * @param cb_cls closure for cb
182  */
183 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
184                                      struct GNUNET_TESTING_Daemon *d2,
185                                      struct GNUNET_TIME_Relative timeout,
186                                      unsigned int max_connect_attempts,
187                                      GNUNET_TESTING_NotifyConnection cb,
188                                      void *cb_cls);
189
190
191
192 /**
193  * Handle to a group of GNUnet peers.
194  */
195 struct GNUNET_TESTING_PeerGroup;
196
197
198 /**
199  * Start count gnunetd processes with the same set of transports and
200  * applications.  The port numbers (any option called "PORT") will be
201  * adjusted to ensure that no two peers running on the same system
202  * have the same port(s) in their respective configurations.
203  *
204  * @param sched scheduler to use
205  * @param cfg configuration template to use
206  * @param total number of daemons to start
207  * @param cb function to call on each daemon that was started
208  * @param cb_cls closure for cb
209  * @param connect_callback function to call each time two hosts are connected
210  * @param connect_callback_cls closure for connect_callback
211  * @param hostnames space-separated list of hostnames to use,
212  *        NULL to use localhost only
213  * @return NULL on error, otherwise handle to control peer group
214  */
215 struct GNUNET_TESTING_PeerGroup *
216 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
217                               const struct GNUNET_CONFIGURATION_Handle *cfg,
218                               unsigned int total,
219                               GNUNET_TESTING_NotifyDaemonRunning cb,
220                               void *cb_cls,
221                               GNUNET_TESTING_NotifyConnection connect_callback,
222                               void *connect_callback_cls,
223                               const char *hostnames);
224
225
226 /**
227  * Shutdown all peers started in the given group.
228  *
229  * @param pg handle to the peer group
230  */
231 void
232 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg);
233
234 int
235 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg);
236
237
238 /**
239  * Handle to an entire testbed of GNUnet peers.
240  */
241 struct GNUNET_TESTING_Testbed;
242
243 /**
244  * Phases of starting GNUnet on a system.
245  */
246 enum StartPhase
247 {
248     /**
249      * Copy the configuration file to the target system.
250      */
251   SP_COPYING,
252
253     /**
254      * Configuration file has been copied, start ARM on target system.
255      */
256   SP_COPIED,
257
258     /**
259      * ARM has been started, check that it has properly daemonized and
260      * then try to connect to the CORE service (which should be
261      * auto-started by ARM).
262      */
263   SP_START_ARMING,
264
265     /**
266      * We're waiting for CORE to start.
267      */
268   SP_START_CORE,
269
270     /**
271      * Core has notified us that we've established a connection to the service.
272      * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
273      */
274   SP_START_DONE,
275
276     /**
277      * We've been asked to terminate the instance and are now waiting for
278      * the remote command to stop the gnunet-arm process and delete temporary
279      * files.
280      */
281   SP_SHUTDOWN_START,
282
283     /**
284      * We've received a configuration update and are currently waiting for
285      * the copy process for the update to complete.  Once it is, we will
286      * return to "SP_START_DONE" (and rely on ARM to restart all affected
287      * services).
288      */
289   SP_CONFIG_UPDATE
290 };
291
292
293 /**
294  * Handle for a GNUnet daemon (technically a set of
295  * daemons; the handle is really for the master ARM
296  * daemon) started by the testing library.
297  */
298 struct GNUNET_TESTING_Daemon
299 {
300   /**
301    * Our scheduler.
302    */
303   struct GNUNET_SCHEDULER_Handle *sched;
304
305   /**
306    * Our configuration.
307    */
308   struct GNUNET_CONFIGURATION_Handle *cfg;
309
310   /**
311    * Host to run GNUnet on.
312    */
313   char *hostname;
314
315   /*
316    * Result of GNUNET_i2s of this peer,
317    * for printing
318    */
319   char *shortname;
320
321   /**
322    * Username we are using.
323    */
324   char *username;
325
326   /**
327    * Name of the configuration file
328    */
329   char *cfgfile;
330
331   /**
332    * Function to call when the peer is running.
333    */
334   GNUNET_TESTING_NotifyDaemonRunning cb;
335
336   /**
337    * Closure for cb.
338    */
339   void *cb_cls;
340
341   /**
342    * Arguments from "daemon_stop" call.
343    */
344   GNUNET_TESTING_NotifyCompletion dead_cb;
345
346   /**
347    * Closure for 'dead_cb'.
348    */
349   void *dead_cb_cls;
350
351   /**
352    * Arguments from "daemon_stop" call.
353    */
354   GNUNET_TESTING_NotifyCompletion update_cb;
355
356   /**
357    * Closure for 'update_cb'.
358    */
359   void *update_cb_cls;
360
361   /**
362    * Identity of this peer (once started).
363    */
364   struct GNUNET_PeerIdentity id;
365
366   /**
367    * Flag to indicate that we've already been asked
368    * to terminate (but could not because some action
369    * was still pending).
370    */
371   int dead;
372
373   /**
374    * PID of the process that we started last.
375    */
376   pid_t pid;
377
378   /**
379    * How many iterations have we been waiting for
380    * the started process to complete?
381    */
382   unsigned int wait_runs;
383
384   /**
385    * In which phase are we during the start of
386    * this process?
387    */
388   enum StartPhase phase;
389
390   /**
391    * ID of the current task.
392    */
393   GNUNET_SCHEDULER_TaskIdentifier task;
394
395   /**
396    * Handle to the server.
397    */
398   struct GNUNET_CORE_Handle *server;
399
400   /**
401    * Handle to the transport service of this peer
402    */
403   struct GNUNET_TRANSPORT_Handle *th;
404
405   /**
406    * HELLO message for this peer
407    */
408   struct GNUNET_HELLO_Message *hello;
409 };
410
411 /**
412  * Topologies supported for testbeds.
413  */
414 enum GNUNET_TESTING_Topology
415 {
416   /**
417    * A clique (everyone connected to everyone else).
418    */
419   GNUNET_TESTING_TOPOLOGY_CLIQUE,
420
421   /**
422    * Small-world network (2d torus plus random links).
423    */
424   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
425
426   /**
427    * Small-world network (ring plus random links).
428    */
429   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
430
431   /**
432    * Ring topology.
433    */
434   GNUNET_TESTING_TOPOLOGY_RING,
435
436   /**
437    * 2-d torus.
438    */
439   GNUNET_TESTING_TOPOLOGY_2D_TORUS,
440
441   /**
442    * Random graph.
443    */
444   GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
445
446   /**
447    * Certain percentage of peers are unable to communicate directly
448    * replicating NAT conditions
449    */
450   GNUNET_TESTING_TOPOLOGY_INTERNAT,
451
452   /**
453    * All peers are disconnected.
454    */
455   GNUNET_TESTING_TOPOLOGY_NONE
456 };
457
458
459 /**
460  * Start "count" GNUnet daemons with a particular topology.
461  *
462  * @param sched scheduler to use
463  * @param cfg configuration template to use
464  * @param count number of peers the testbed should have
465  * @param topology desired topology (enforced via F2F)
466  * @param cb function to call on each daemon that was started
467  * @param cb_cls closure for cb
468  * @param hostname where to run the peers; can be NULL (to run
469  *        everything on localhost). Additional
470  *        hosts can be specified using a NULL-terminated list of
471  *        varargs, hosts will then be used round-robin from that
472  *        list.
473  * @return handle to control the testbed
474  */
475 struct GNUNET_TESTING_Testbed *
476 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
477                               const struct GNUNET_CONFIGURATION_Handle *cfg,
478                               unsigned int count,
479                               enum GNUNET_TESTING_Topology topology,
480                               GNUNET_TESTING_NotifyDaemonRunning cb,
481                               void *cb_cls,
482                               const char *hostname,
483                               ...);
484
485
486 /**
487  * Stop all of the daemons started with the start function.
488  *
489  * @param tb handle for the testbed
490  * @param cb function to call when done
491  * @param cb_cls closure for cb
492  */
493 void
494 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
495                              GNUNET_TESTING_NotifyCompletion cb,
496                              void *cb_cls );
497
498
499 /**
500  * Simulate churn in the testbed by stopping some peers (and possibly
501  * re-starting others if churn is called multiple times).  This
502  * function can only be used to create leave-join churn (peers "never"
503  * leave for good).  First "voff" random peers that are currently
504  * online will be taken offline; then "von" random peers that are then
505  * offline will be put back online.  No notifications will be
506  * generated for any of these operations except for the callback upon
507  * completion.  Note that the implementation is at liberty to keep
508  * the ARM service itself (but none of the other services or daemons)
509  * running even though the "peer" is being varied offline.
510  *
511  * @param tb handle for the testbed
512  * @param voff number of peers that should go offline
513  * @param von number of peers that should come back online;
514  *            must be zero on first call (since "testbed_start"
515  *            always starts all of the peers)
516  * @param cb function to call at the end
517  * @param cb_cls closure for cb
518  */
519 void
520 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
521                               unsigned int voff,
522                               unsigned int von,
523                               GNUNET_TESTING_NotifyCompletion cb,
524                               void *cb_cls);
525
526
527 #if 0                           /* keep Emacsens' auto-indent happy */
528 {
529 #endif
530 #ifdef __cplusplus
531 }
532 #endif
533
534 #endif