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