travelhacking
[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                                         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 hostnames space-separated list of hostnames to use, 
167  *        NULL to use localhost only
168  * @return NULL on error, otherwise handle to control peer group
169  */
170 struct GNUNET_TESTING_PeerGroup *
171 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
172                               const struct GNUNET_CONFIGURATION_Handle *cfg,
173                               unsigned int total,
174                               GNUNET_TESTING_NotifyDaemonRunning cb,
175                               void *cb_cls,
176                               const char *hostnames);
177
178
179 /**
180  * Shutdown all peers started in the given group.
181  * 
182  * @param pg handle to the peer group
183  */
184 void
185 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg);
186
187
188 /**
189  * Handle to an entire testbed of GNUnet peers.
190  */
191 struct GNUNET_TESTING_Testbed;
192
193
194 /**
195  * Topologies supported for testbeds.
196  */
197 enum GNUNET_TESTING_Topology
198 {
199   /**
200    * A clique (everyone connected to everyone else).
201    */
202   GNUNET_TESTING_TOPOLOGY_CLIQUE,
203
204   /**
205    * Small-world network (2d torus plus random links).
206    */
207   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
208
209   /**
210    * Ring topology.
211    */
212   GNUNET_TESTING_TOPOLOGY_RING,
213
214   /**
215    * 2-d torus.
216    */
217   GNUNET_TESTING_TOPOLOGY_2D_TORUS,
218
219   /**
220    * Random graph.
221    */
222   GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
223
224   /**
225    * All peers are disconnected.
226    */
227   GNUNET_TESTING_TOPOLOGY_NONE
228 };
229
230
231 /**
232  * Start "count" GNUnet daemons with a particular topology.
233  *
234  * @param sched scheduler to use 
235  * @param cfg configuration template to use
236  * @param count number of peers the testbed should have
237  * @param topology desired topology (enforced via F2F)
238  * @param cb function to call on each daemon that was started
239  * @param cb_cls closure for cb
240  * @param hostname where to run the peers; can be NULL (to run
241  *        everything on localhost). Additional
242  *        hosts can be specified using a NULL-terminated list of
243  *        varargs, hosts will then be used round-robin from that
244  *        list.
245  * @return handle to control the testbed
246  */
247 struct GNUNET_TESTING_Testbed *
248 GNUNET_TESTING_testbed_start (struct GNUNET_SCHEDULER_Handle *sched,
249                               const struct GNUNET_CONFIGURATION_Handle *cfg,
250                               unsigned int count,
251                               enum GNUNET_TESTING_Topology topology,
252                               GNUNET_TESTING_NotifyDaemonRunning cb,
253                               void *cb_cls,
254                               const char *hostname,
255                               ...);
256
257
258 /**
259  * Stop all of the daemons started with the start function.
260  *
261  * @param tb handle for the testbed
262  * @param cb function to call when done
263  * @param cb_cls closure for cb
264  */
265 void
266 GNUNET_TESTING_testbed_stop (struct GNUNET_TESTING_Testbed *tb,
267                              GNUNET_TESTING_NotifyCompletion cb,
268                              void *cb_cls );
269
270
271 /**
272  * Simulate churn in the testbed by stopping some peers (and possibly
273  * re-starting others if churn is called multiple times).  This
274  * function can only be used to create leave-join churn (peers "never"
275  * leave for good).  First "voff" random peers that are currently
276  * online will be taken offline; then "von" random peers that are then
277  * offline will be put back online.  No notifications will be
278  * generated for any of these operations except for the callback upon
279  * completion.  Note that the implementation is at liberty to keep
280  * the ARM service itself (but none of the other services or daemons)
281  * running even though the "peer" is being varied offline.
282  *
283  * @param tb handle for the testbed
284  * @param voff number of peers that should go offline
285  * @param von number of peers that should come back online;
286  *            must be zero on first call (since "testbed_start"
287  *            always starts all of the peers)
288  * @param cb function to call at the end
289  * @param cb_cls closure for cb
290  */
291 void
292 GNUNET_TESTING_testbed_churn (struct GNUNET_TESTING_Testbed *tb,
293                               unsigned int voff,
294                               unsigned int von,
295                               GNUNET_TESTING_NotifyCompletion cb,
296                               void *cb_cls);
297
298
299 #if 0                           /* keep Emacsens' auto-indent happy */
300 {
301 #endif
302 #ifdef __cplusplus
303 }
304 #endif
305
306 #endif