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