fix bad free
[oweals/gnunet.git] / src / include / gnunet_testing_lib.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008, 2009, 2012 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14      
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @author Christian Grothoff
21  *
22  * @file
23  * Convenience API for writing testcases for GNUnet
24  *
25  * @defgroup testing  Testing library
26  * Library for writing testcases for GNUnet.
27  *
28  * It can start/stop one or more peers on a system; testing is responsible for
29  * managing private keys, ports and paths; it is a low-level library that does
30  * not support higher-level functions such as P2P connection, topology
31  * management or distributed testbed maintenance (those are provided by the
32  * [Testbed service](@ref testbed))
33  *
34  * @see [Documentation](https://gnunet.org/writing_testcases)
35  *
36  * @{
37  */
38
39 #ifndef GNUNET_TESTING_LIB_H
40 #define GNUNET_TESTING_LIB_H
41
42 #include "gnunet_util_lib.h"
43 #include "gnunet_statistics_service.h"
44 #include "gnunet_arm_service.h"
45
46 #ifdef __cplusplus
47 extern "C"
48 {
49 #if 0                           /* keep Emacsens' auto-indent happy */
50 }
51 #endif
52 #endif
53
54 /**
55  * Size of each hostkey in the hostkey file (in BYTES).
56  */
57 #define GNUNET_TESTING_HOSTKEYFILESIZE sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)
58
59 /**
60  * The environmental variable, if set, that dictates where testing should place
61  * generated peer configurations
62  */
63 #define GNUNET_TESTING_PREFIX "GNUNET_TESTING_PREFIX"
64
65
66 /**
67  * Handle for a system on which GNUnet peers are executed;
68  * a system is used for reserving unique paths and ports.
69  */
70 struct GNUNET_TESTING_System;
71
72
73 /**
74  * Handle for a GNUnet peer controlled by testing.
75  */
76 struct GNUNET_TESTING_Peer;
77
78
79 /**
80  * Specification of a service that is to be shared among peers
81  */
82 struct GNUNET_TESTING_SharedService
83 {
84   /**
85    * The name of the service.
86    */
87   const char *service;
88
89   /**
90    * The configuration template for the service.  Cannot be NULL
91    */
92   const struct GNUNET_CONFIGURATION_Handle *cfg;
93
94   /**
95    * The number of peers which share an instance of the service.  0 for sharing
96    * among all peers
97    */
98   unsigned int share;
99 };
100
101
102 /**
103  * Create a system handle.  There must only be one system handle per operating
104  * system.  Uses a default range for allowed ports.  Ports are still tested for
105  * availability.
106  *
107  * @param testdir only the directory name without any path. This is used for all
108  *          service homes; the directory will be created in a temporary location
109  *          depending on the underlying OS.  This variable will be
110  *          overridden with the value of the environmental variable
111  *          GNUNET_TESTING_PREFIX, if it exists.
112  * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
113  *          service configurations generated to allow control connections from
114  *          this ip. This can either be a single ip address or a network address
115  *          in CIDR notation.
116  * @param hostname the hostname of the system we are using for testing; NULL for
117  *          localhost
118  * @param shared_services NULL terminated array describing services that are to
119  *          be shared among peers
120  * @return handle to this system, NULL on error
121  */
122 struct GNUNET_TESTING_System *
123 GNUNET_TESTING_system_create (const char *testdir,
124                               const char *trusted_ip,
125                               const char *hostname,
126                               const struct GNUNET_TESTING_SharedService *
127                               shared_services);
128
129
130 /**
131  * Create a system handle.  There must only be one system
132  * handle per operating system.  Use this function directly
133  * if multiple system objects are created for the same host
134  * (only really useful when testing --- or to make the port
135  * range configureable).
136  *
137  * @param testdir only the directory name without any path. This is used for
138  *          all service homes; the directory will be created in a temporary
139  *          location depending on the underlying OS.  This variable will be
140  *          overridden with the value of the environmental variable
141  *          GNUNET_TESTING_PREFIX, if it exists.
142  * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
143  *          service configurations generated to allow control connections from
144  *          this ip. This can either be a single ip address or a network address
145  *          in CIDR notation.
146  * @param hostname the hostname of the system we are using for testing; NULL for
147  *          localhost
148  * @param shared_services NULL terminated array describing services that are to
149  *          be shared among peers
150  * @param lowport lowest port number this system is allowed to allocate (inclusive)
151  * @param highport highest port number this system is allowed to allocate (exclusive)
152  * @return handle to this system, NULL on error
153  */
154 struct GNUNET_TESTING_System *
155 GNUNET_TESTING_system_create_with_portrange (const char *testdir,
156                                              const char *trusted_ip,
157                                              const char *hostname,
158                                              const struct GNUNET_TESTING_SharedService *shared_services,
159                                              uint16_t lowport,
160                                              uint16_t highport);
161
162
163 /**
164  * Free system resources.
165  *
166  * @param system system to be freed
167  * @param remove_paths should the 'testdir' and all subdirectories
168  *        be removed (clean up on shutdown)?
169  */
170 void
171 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
172                                int remove_paths);
173
174
175 /**
176  * Testing includes a number of pre-created hostkeys for
177  * faster peer startup.  This function can be used to
178  * access the n-th key of those pre-created hostkeys; note
179  * that these keys are ONLY useful for testing and not
180  * secure as the private keys are part of the public
181  * GNUnet source code.
182  *
183  * This is primarily a helper function used internally
184  * by #GNUNET_TESTING_peer_configure().
185  *
186  * @param system the testing system handle
187  * @param key_number desired pre-created hostkey to obtain
188  * @param id set to the peer's identity (hash of the public
189  *        key; if NULL, #GNUNET_SYSERR is returned immediately
190  * @return NULL on error (not enough keys)
191  */
192 struct GNUNET_CRYPTO_EddsaPrivateKey *
193 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
194                             uint32_t key_number,
195                             struct GNUNET_PeerIdentity *id);
196
197
198 /**
199  * Reserve a port for a peer.
200  *
201  * @param system system to use for reservation tracking
202  * @return 0 if no free port was available
203  */
204 uint16_t
205 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system);
206
207
208 /**
209  * Release reservation of a TCP or UDP port for a peer
210  * (used during GNUNET_TESTING_peer_destroy).
211  *
212  * @param system system to use for reservation tracking
213  * @param port reserved port to release
214  */
215 void
216 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
217                              uint16_t port);
218
219
220 /**
221  * Create a new configuration using the given configuration as a template;
222  * ports and paths will be modified to select available ports on the local
223  * system. The default configuration will be available in PATHS section under
224  * the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS
225  * section to the temporary directory specific to this configuration. If we run
226  * out of "*port" numbers, return #GNUNET_SYSERR.
227  *
228  * This is primarily a helper function used internally
229  * by #GNUNET_TESTING_peer_configure().
230  *
231  * @param system system to use to coordinate resource usage
232  * @param cfg template configuration to update
233  * @return #GNUNET_OK on success,
234  *         #GNUNET_SYSERR on error - the configuration will
235  *           be incomplete and should not be used there upon
236  */
237 int
238 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
239                                      struct GNUNET_CONFIGURATION_Handle *cfg);
240 // FIXME: add dual to 'release' ports again...
241
242
243 /**
244  * Configure a GNUnet peer.  GNUnet must be installed on the local
245  * system and available in the PATH.
246  *
247  * @param system system to use to coordinate resource usage
248  * @param cfg configuration to use; will be UPDATED (to reflect needed
249  *            changes in port numbers and paths)
250  * @param key_number number of the hostkey to use for the peer
251  * @param id identifier for the daemon, will be set, can be NULL
252  * @param emsg set to freshly allocated error message (set to NULL on success),
253  *          can be NULL
254  * @return handle to the peer, NULL on error
255  */
256 struct GNUNET_TESTING_Peer *
257 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
258                                struct GNUNET_CONFIGURATION_Handle *cfg,
259                                uint32_t key_number,
260                                struct GNUNET_PeerIdentity *id,
261                                char **emsg);
262
263
264 /**
265  * Obtain the peer identity from a peer handle.
266  *
267  * @param peer peer handle for which we want the peer's identity
268  * @param id identifier for the daemon, will be set
269  */
270 void
271 GNUNET_TESTING_peer_get_identity (struct GNUNET_TESTING_Peer *peer,
272                                   struct GNUNET_PeerIdentity *id);
273
274
275 /**
276  * Start the peer.
277  *
278  * @param peer peer to start
279  * @return #GNUNET_OK on success,
280  *         #GNUNET_SYSERR on error (i.e. peer already running)
281  */
282 int
283 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
284
285
286 /**
287  * Stop the peer. This call is blocking as it kills the peer's main ARM process
288  * by sending a SIGTERM and waits on it.  For asynchronous shutdown of peer, see
289  * GNUNET_TESTING_peer_stop_async().
290  *
291  * @param peer peer to stop
292  * @return #GNUNET_OK on success,
293  *         #GNUNET_SYSERR on error (i.e. peer not running)
294  */
295 int
296 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
297
298
299 /**
300  * Destroy the peer.  Releases resources locked during peer configuration.
301  * If the peer is still running, it will be stopped AND a warning will be
302  * printed (users of the API should stop the peer explicitly first).
303  *
304  * @param peer peer to destroy
305  */
306 void
307 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
308
309
310 /**
311  * Sends SIGTERM to the peer's main process
312  *
313  * @param peer the handle to the peer
314  * @return #GNUNET_OK if successful; #GNUNET_SYSERR if the main process is NULL
315  *           or upon any error while sending SIGTERM
316  */
317 int
318 GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer);
319
320
321 /**
322  * Waits for a peer to terminate. The peer's main process will also be destroyed.
323  *
324  * @param peer the handle to the peer
325  * @return #GNUNET_OK if successful; #GNUNET_SYSERR if the main process is NULL
326  *           or upon any error while waiting
327  */
328 int
329 GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer);
330
331
332 /**
333  * Callback to inform whether the peer is running or stopped.
334  *
335  * @param cls the closure given to GNUNET_TESTING_peer_stop_async()
336  * @param peer the respective peer whose status is being reported
337  * @param success #GNUNET_YES if the peer is stopped; #GNUNET_SYSERR upon any
338  *          error
339  */
340 typedef void
341 (*GNUNET_TESTING_PeerStopCallback) (void *cls,
342                                     struct GNUNET_TESTING_Peer *peer,
343                                     int success);
344
345
346 /**
347  * Stop a peer asynchronously using ARM API.  Peer's shutdown is signaled
348  * through the GNUNET_TESTING_PeerStopCallback().
349  *
350  * @param peer the peer to stop
351  * @param cb the callback to signal peer shutdown
352  * @param cb_cls closure for the @a cb
353  * @return #GNUNET_OK upon successfully giving the request to the ARM API (this
354  *           does not mean that the peer is successfully stopped); #GNUNET_SYSERR
355  *           upon any error.
356  */
357 int
358 GNUNET_TESTING_peer_stop_async (struct GNUNET_TESTING_Peer *peer,
359                                 GNUNET_TESTING_PeerStopCallback cb,
360                                 void *cb_cls);
361
362
363 /**
364  * Cancel a previous asynchronous peer stop request.
365  * GNUNET_TESTING_peer_stop_async() should have been called before on the given
366  * peer.  It is an error to call this function if the peer stop callback was
367  * already called
368  *
369  * @param peer the peer on which GNUNET_TESTING_peer_stop_async() was called
370  *          before.
371  */
372 void
373 GNUNET_TESTING_peer_stop_async_cancel (struct GNUNET_TESTING_Peer *peer);
374
375
376 /**
377  * Signature of the 'main' function for a (single-peer) testcase that
378  * is run using #GNUNET_TESTING_peer_run().
379  *
380  * @param cls closure
381  * @param cfg configuration of the peer that was started
382  * @param peer identity of the peer that was created
383  */
384 typedef void
385 (*GNUNET_TESTING_TestMain) (void *cls,
386                             const struct GNUNET_CONFIGURATION_Handle *cfg,
387                             struct GNUNET_TESTING_Peer *peer);
388
389
390 /**
391  * Start a single peer and run a test using the testing library.
392  * Starts a peer using the given configuration and then invokes the
393  * given callback.  This function ALSO initializes the scheduler loop
394  * and should thus be called directly from "main".  The testcase
395  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
396  *
397  * @param testdir only the directory name without any path. This is used for
398  *          all service homes; the directory will be created in a temporary
399  *          location depending on the underlying OS
400  * @param cfgfilename name of the configuration file to use;
401  *         use NULL to only run with defaults
402  * @param tm main function of the testcase
403  * @param tm_cls closure for 'tm'
404  * @return 0 on success, 1 on error
405  */
406 int
407 GNUNET_TESTING_peer_run (const char *testdir,
408                          const char *cfgfilename,
409                          GNUNET_TESTING_TestMain tm,
410                          void *tm_cls);
411
412
413 /**
414  * Start a single service (no ARM, except of course if the given
415  * service name is 'arm') and run a test using the testing library.
416  * Starts a service using the given configuration and then invokes the
417  * given callback.  This function ALSO initializes the scheduler loop
418  * and should thus be called directly from "main".  The testcase
419  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
420  *
421  * This function is useful if the testcase is for a single service
422  * and if that service doesn't itself depend on other services.
423  *
424  * @param testdir only the directory name without any path. This is used for
425  *          all service homes; the directory will be created in a temporary
426  *          location depending on the underlying OS
427  * @param service_name name of the service to run
428  * @param cfgfilename name of the configuration file to use;
429  *         use NULL to only run with defaults
430  * @param tm main function of the testcase
431  * @param tm_cls closure for @a tm
432  * @return 0 on success, 1 on error
433  */
434 int
435 GNUNET_TESTING_service_run (const char *testdir,
436                             const char *service_name,
437                             const char *cfgfilename,
438                             GNUNET_TESTING_TestMain tm,
439                             void *tm_cls);
440
441
442 /**
443  * Sometimes we use the binary name to determine which specific
444  * test to run.  In those cases, the string after the last "_"
445  * in 'argv[0]' specifies a string that determines the configuration
446  * file or plugin to use.
447  *
448  * This function returns the respective substring, taking care
449  * of issues such as binaries ending in '.exe' on W32.
450  *
451  * @param argv0 the name of the binary
452  * @return string between the last '_' and the '.exe' (or the end of the string),
453  *         NULL if argv0 has no '_'
454  */
455 char *
456 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
457
458
459 #if 0                           /* keep Emacsens' auto-indent happy */
460 {
461 #endif
462 #ifdef __cplusplus
463 }
464 #endif
465
466 #endif
467
468 /** @} */  /* end of group */