459e433148f32e8b70045b911d6d1d804db97a30
[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
47 /**
48  * Handle for a GNUnet daemon (technically a set of
49  * daemons; the handle is really for the master ARM
50  * daemon) started by the testing library.
51  */
52 struct GNUNET_TESTING_Daemon;
53
54
55 /**
56  * Prototype of a function that will be called whenever
57  * a daemon was started by the testing library.
58  *
59  * @param cls closure
60  * @param id identifier for the daemon, NULL on error
61  * @param d handle to the daemon, NULL if starting the daemon failed
62  */
63 typedef void (*GNUNET_TESTING_NotifyDaemonRunning)(void *cls,
64                                                    const struct GNUNET_PeerIdentity *id,
65                                                    struct GNUNET_TESTING_Daemon *d);
66
67
68 /**
69  * Starts a GNUnet daemon.
70  *
71  * @param service_home directory to use as the service home directory
72  * @param transports transport services that should be loaded
73  * @param applications application services and daemons that should be started
74  * @param port_offset offset to add to all ports for all services
75  * @param hostname name of the machine where to run GNUnet
76  *        (use NULL for localhost).
77  * @param cb function to call with the result
78  * @param cb_cls closure for cb
79  */
80 void
81 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
82                              struct GNUNET_CONFIGURATION_Handle *cfg,
83                              const char *service_home,
84                              const char *transports,
85                              const char *applications,
86                              uint16_t port_offset,
87                              const char *hostname,
88                              GNUNET_TESTING_NotifyDaemonRunning cb,
89                              void *cb_cls);
90
91
92 /**
93  * Prototype of a function that will be called when a
94  * particular operation was completed the testing library.
95  *
96  * @param cls closure
97  * @param success GNUNET_YES on success
98  */
99 typedef void (*GNUNET_TESTING_NotifyCompletion)(void *cls,
100                                                 int success);
101
102
103 /**
104  * Stops a GNUnet daemon.
105  *
106  * @param d the daemon that should be stopped
107  * @param cb function called once the daemon was stopped
108  * @param cb_cls closure for cb
109  */
110 void GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
111                                  GNUNET_TESTING_NotifyCompletion cb,
112                                  void * cb_cls);
113
114
115
116 /**
117  * Establish a connection between two GNUnet daemons.
118  *
119  * @param d1 handle for the first daemon
120  * @param d2 handle for the second daemon
121  * @param cb function to call at the end
122  * @param cb_cls closure for cb
123  */
124 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
125                                      struct GNUNET_TESTING_Daemon *d2,
126                                      GNUNET_TESTING_NotifyCompletion cb,
127                                      void *cb_cls);
128
129
130
131 /**
132  * Start count gnunetd processes with the same set of
133  * transports and applications.  The port numbers will
134  * be computed by adding delta each time (zero
135  * times for the first peer).
136  *
137  * @param total number of daemons to start
138  * @param service_home_prefix path to use as the prefix for the home of the services
139  * @param transports which transports should all peers use
140  * @param applications which applications should be used?
141  * @param timeout how long is this allowed to take?
142  * @param cb function to call on each daemon that was started
143  * @param cb_cls closure for cb
144  * @param cbe function to call at the end
145  * @param cbe_cls closure for cbe
146  * @param hostname where to run the peers; can be NULL (to run
147  *        everything on localhost).
148  * @param va Additional hosts can be specified using a NULL-terminated list of
149  *        varargs, hosts will then be used round-robin from that
150  *        list; va only contains anything if hostname != NULL.
151  */
152 void
153 GNUNET_TESTING_daemons_start_Va (struct GNUNET_SCHEDULER_Handle *sched,
154                                  struct GNUNET_CONFIGURATION_Handle *cfg,
155                                  unsigned int total,
156                                  const char *service_home_prefix,
157                                  const char *transports,
158                                  const char *applications,
159                                  GNUNET_TESTING_NotifyDaemonRunning cb,
160                                  void *cb_cls,
161                                  GNUNET_TESTING_NotifyCompletion cbe,
162                                  void *cbe_cls,
163                                  const char *hostname,
164                                  va_list va);
165
166 /**
167  * Start count gnunetd processes with the same set of
168  * transports and applications.  The port numbers will
169  * be computed by adding delta each time (zero
170  * times for the first peer).
171  *
172  * @param total number of daemons to start
173  * @param service_home_prefix path to use as the prefix for the home of the services
174  * @param transports which transports should all peers use
175  * @param applications which applications should be used?
176  * @param timeout how long is this allowed to take?
177  * @param cb function to call on each daemon that was started
178  * @param cb_cls closure for cb
179  * @param cbe function to call at the end
180  * @param cbe_cls closure for cbe
181  * @param hostname where to run the peers; can be NULL (to run
182  *        everything on localhost). Additional
183  *        hosts can be specified using a NULL-terminated list of
184  *        varargs, hosts will then be used round-robin from that
185  *        list.
186  */
187 void
188 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
189                               struct GNUNET_CONFIGURATION_Handle *cfg,
190                               unsigned int total,
191                               const char *service_home_prefix,
192                               const char *transports,
193                               const char *applications,
194                               GNUNET_TESTING_NotifyDaemonRunning cb,
195                               void *cb_cls,
196                               GNUNET_TESTING_NotifyCompletion cbe,
197                               void *cbe_cls,
198                               const char *hostname,
199                               ...);
200
201
202 /**
203  * Handle to an entire testbed of GNUnet peers.
204  */
205 struct GNUNET_TESTING_Testbed;
206
207 /**
208  * Prototype of a function that will be called when 
209  * a testbed is being created.
210  *
211  * @param cls closure
212  * @param tb NULL on error
213  */
214 typedef void (*GNUNET_TESTING_NotifyTestbedRunning)(void *cls,
215                                                     struct GNUNET_TESTING_Testbed *tb);
216
217
218 /**
219  * Topologies supported for testbeds.
220  */
221 enum GNUNET_TESTING_Topology
222 {
223   /**
224    * A clique (everyone connected to everyone else).
225    */
226   GNUNET_TESTING_TOPOLOGY_CLIQUE,
227
228   /**
229    * Small-world network (2d torus plus random links).
230    */
231   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
232
233   /**
234    * Ring topology.
235    */
236   GNUNET_TESTING_TOPOLOGY_RING,
237
238   /**
239    * 2-d torus.
240    */
241   GNUNET_TESTING_TOPOLOGY_2D_TORUS,
242
243   /**
244    * Random graph.
245    */
246   GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
247
248   /**
249    * All peers are disconnected.
250    */
251   GNUNET_TESTING_TOPOLOGY_NONE
252 };
253
254
255
256 /**
257  * Start count GNUnet daemons with a particular
258  * topology.
259  *
260  * @param size number of peers the testbed should have
261  * @param topology desired topology (enforced via F2F)
262  * @param service_home_prefix path to use as the prefix for the home of the services
263  * @param transports which transports should all peers use
264  * @param applications which applications should be used?
265  * @param timeout how long is this allowed to take?
266  * @param cb function to call on each daemon that was started
267  * @param cb_cls closure for cb
268  * @param cte function to call at the end
269  * @param cte_cls closure for cbe
270  * @param hostname where to run the peers; can be NULL (to run
271  *        everything on localhost). Additional
272  *        hosts can be specified using a NULL-terminated list of
273  *        varargs, hosts will then be used round-robin from that
274  *        list.
275  */
276 void
277 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
278                               struct GNUNET_CONFIGURATION_Handle *cfg,
279                               unsigned int size,
280                               enum GNUNET_TESTING_Topology topology,
281                               const char *service_home_prefix,
282                               const char *transports,
283                               const char *applications,
284                               GNUNET_TESTING_NotifyDaemonRunning cb,
285                               void *cb_cls,
286                               GNUNET_TESTING_NotifyTestbedRunning cte,
287                               void *cte_cls,
288                               const char *hostname,
289                               ...);
290
291
292
293
294 /**
295  * Stop all of the daemons started with the start function.
296  *
297  * @param tb handle for the testbed
298  * @param cb function to call at the end
299  * @param cb_cls closure for cb
300  */
301 void
302 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
303                              GNUNET_TESTING_NotifyCompletion cb,
304                              void *cb_cls );
305
306
307
308 /**
309  * Simulate churn in the testbed by stopping some peers (and possibly
310  * re-starting others if churn is called multiple times).  This
311  * function can only be used to create leave-join churn (peers "never"
312  * leave for good).  First "voff" random peers that are currently
313  * online will be taken offline; then "von" random peers that are then
314  * offline will be put back online.  No notifications will be
315  * generated for any of these operations except for the callback upon
316  * completion.  Note that the implementation is at liberty to keep
317  * the ARM service itself (but none of the other services or daemons)
318  * running even though the "peer" is being varied offline.
319  *
320  * @param tb handle for the testbed
321  * @param voff number of peers that should go offline
322  * @param von number of peers that should come back online;
323  *            must be zero on first call (since "testbed_start"
324  *            always starts all of the peers)
325  * @param cb function to call at the end
326  * @param cb_cls closure for cb
327  */
328 void
329 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
330                               unsigned int voff,
331                               unsigned int von,
332                               GNUNET_TESTING_NotifyCompletion cb,
333                               void *cb_cls);
334
335
336
337 #if 0                           /* keep Emacsens' auto-indent happy */
338 {
339 #endif
340 #ifdef __cplusplus
341 }
342 #endif
343
344 #endif