2 This file is part of GNUnet
3 Copyright (C) 2008, 2009, 2012 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @author Christian Grothoff
25 * Convenience API for writing testcases for GNUnet
27 * @defgroup testing Testing library
28 * Library for writing testcases for GNUnet.
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))
36 * @see [Documentation](https://gnunet.org/writing_testcases)
41 #ifndef GNUNET_TESTING_LIB_H
42 #define GNUNET_TESTING_LIB_H
44 #include "gnunet_util_lib.h"
45 #include "gnunet_statistics_service.h"
46 #include "gnunet_arm_service.h"
51 #if 0 /* keep Emacsens' auto-indent happy */
57 * Size of each hostkey in the hostkey file (in BYTES).
59 #define GNUNET_TESTING_HOSTKEYFILESIZE sizeof(struct \
60 GNUNET_CRYPTO_EddsaPrivateKey)
63 * The environmental variable, if set, that dictates where testing should place
64 * generated peer configurations
66 #define GNUNET_TESTING_PREFIX "GNUNET_TESTING_PREFIX"
70 * Handle for a system on which GNUnet peers are executed;
71 * a system is used for reserving unique paths and ports.
73 struct GNUNET_TESTING_System;
77 * Handle for a GNUnet peer controlled by testing.
79 struct GNUNET_TESTING_Peer;
83 * Specification of a service that is to be shared among peers
85 struct GNUNET_TESTING_SharedService
88 * The name of the service.
93 * The configuration template for the service. Cannot be NULL
95 const struct GNUNET_CONFIGURATION_Handle *cfg;
98 * The number of peers which share an instance of the service. 0 for sharing
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
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
119 * @param hostname the hostname of the system we are using for testing; NULL for
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
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 *
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).
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
149 * @param hostname the hostname of the system we are using for testing; NULL for
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
157 struct GNUNET_TESTING_System *
158 GNUNET_TESTING_system_create_with_portrange (const char *testdir,
159 const char *trusted_ip,
160 const char *hostname,
162 GNUNET_TESTING_SharedService *
169 * Free system resources.
171 * @param system system to be freed
172 * @param remove_paths should the 'testdir' and all subdirectories
173 * be removed (clean up on shutdown)?
176 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
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.
188 * This is primarily a helper function used internally
189 * by #GNUNET_TESTING_peer_configure().
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)
197 struct GNUNET_CRYPTO_EddsaPrivateKey *
198 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
200 struct GNUNET_PeerIdentity *id);
204 * Reserve a port for a peer.
206 * @param system system to use for reservation tracking
207 * @return 0 if no free port was available
210 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system);
214 * Release reservation of a TCP or UDP port for a peer
215 * (used during GNUNET_TESTING_peer_destroy).
217 * @param system system to use for reservation tracking
218 * @param port reserved port to release
221 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
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.
233 * This is primarily a helper function used internally
234 * by #GNUNET_TESTING_peer_configure().
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
243 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
244 struct GNUNET_CONFIGURATION_Handle *cfg);
246 // FIXME: add dual to 'release' ports again...
250 * Configure a GNUnet peer. GNUnet must be installed on the local
251 * system and available in the PATH.
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),
260 * @return handle to the peer, NULL on error
262 struct GNUNET_TESTING_Peer *
263 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
264 struct GNUNET_CONFIGURATION_Handle *cfg,
266 struct GNUNET_PeerIdentity *id,
271 * Obtain the peer identity from a peer handle.
273 * @param peer peer handle for which we want the peer's identity
274 * @param id identifier for the daemon, will be set
277 GNUNET_TESTING_peer_get_identity (struct GNUNET_TESTING_Peer *peer,
278 struct GNUNET_PeerIdentity *id);
284 * @param peer peer to start
285 * @return #GNUNET_OK on success,
286 * #GNUNET_SYSERR on error (i.e. peer already running)
289 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
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().
297 * @param peer peer to stop
298 * @return #GNUNET_OK on success,
299 * #GNUNET_SYSERR on error (i.e. peer not running)
302 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
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).
310 * @param peer peer to destroy
313 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
317 * Sends SIGTERM to the peer's main process
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
324 GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer);
328 * Waits for a peer to terminate. The peer's main process will also be destroyed.
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
335 GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer);
339 * Callback to inform whether the peer is running or stopped.
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
347 (*GNUNET_TESTING_PeerStopCallback) (void *cls,
348 struct GNUNET_TESTING_Peer *peer,
353 * Stop a peer asynchronously using ARM API. Peer's shutdown is signaled
354 * through the GNUNET_TESTING_PeerStopCallback().
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
364 GNUNET_TESTING_peer_stop_async (struct GNUNET_TESTING_Peer *peer,
365 GNUNET_TESTING_PeerStopCallback cb,
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
375 * @param peer the peer on which GNUNET_TESTING_peer_stop_async() was called
379 GNUNET_TESTING_peer_stop_async_cancel (struct GNUNET_TESTING_Peer *peer);
383 * Signature of the 'main' function for a (single-peer) testcase that
384 * is run using #GNUNET_TESTING_peer_run().
387 * @param cfg configuration of the peer that was started
388 * @param peer identity of the peer that was created
391 (*GNUNET_TESTING_TestMain) (void *cls,
392 const struct GNUNET_CONFIGURATION_Handle *cfg,
393 struct GNUNET_TESTING_Peer *peer);
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'.
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
413 GNUNET_TESTING_peer_run (const char *testdir,
414 const char *cfgfilename,
415 GNUNET_TESTING_TestMain tm,
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'.
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.
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
441 GNUNET_TESTING_service_run (const char *testdir,
442 const char *service_name,
443 const char *cfgfilename,
444 GNUNET_TESTING_TestMain tm,
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.
454 * This function returns the respective substring, taking care
455 * of issues such as binaries ending in '.exe' on W32.
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 '_'
462 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
465 #if 0 /* keep Emacsens' auto-indent happy */
474 /** @} */ /* end of group */