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