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