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