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