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