2 This file is part of GNUnet
3 (C) 2008, 2009, 2012 Christian Grothoff (and other contributing authors)
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.
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.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_testing_lib.h
23 * @brief convenience API for writing testcases for GNUnet;
24 * can start/stop one or more peers on a system;
25 * testing is responsible for managing private keys,
26 * ports and paths; it is a low-level library that
27 * does not support higher-level functions such as
28 * P2P connection, topology management or distributed
29 * testbed maintenance (those are in gnunet_testbed_service.h)
30 * @author Christian Grothoff
33 #ifndef GNUNET_TESTING_LIB_H
34 #define GNUNET_TESTING_LIB_H
36 #include "gnunet_util_lib.h"
37 #include "gnunet_statistics_service.h"
42 #if 0 /* keep Emacsens' auto-indent happy */
48 * Size of each hostkey in the hostkey file (in BYTES). This is the
49 * maximum length of the S-expressions generated by libgcrypt for the
50 * curves (rounded up to the next full KB to make IO nicer); it is NOT
51 * the number of bits in the key.
53 #define GNUNET_TESTING_HOSTKEYFILESIZE 1024
56 * Handle for a system on which GNUnet peers are executed;
57 * a system is used for reserving unique paths and ports.
59 struct GNUNET_TESTING_System;
63 * Handle for a GNUnet peer controlled by testing.
65 struct GNUNET_TESTING_Peer;
69 * Create a system handle. There must only be one system handle per operating
70 * system. Uses a default range for allowed ports. Ports are still tested for
73 * @param testdir only the directory name without any path. This is used for all
74 * service homes; the directory will be created in a temporary location
75 * depending on the underlying OS
76 * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
77 * service configurations generated to allow control connections from
78 * this ip. This can either be a single ip address or a network address
80 * @param hostname the hostname of the system we are using for testing; NULL for
82 * @return handle to this system, NULL on error
84 struct GNUNET_TESTING_System *
85 GNUNET_TESTING_system_create (const char *testdir,
86 const char *trusted_ip,
87 const char *hostname);
91 * Create a system handle. There must only be one system
92 * handle per operating system. Use this function directly
93 * if multiple system objects are created for the same host
94 * (only really useful when testing --- or to make the port
95 * range configureable).
97 * @param testdir only the directory name without any path. This is used for
98 * all service homes; the directory will be created in a temporary
99 * location depending on the underlying OS
100 * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
101 * service configurations generated to allow control connections from
102 * this ip. This can either be a single ip address or a network address
104 * @param hostname the hostname of the system we are using for testing; NULL for
106 * @param lowport lowest port number this system is allowed to allocate (inclusive)
107 * @param highport highest port number this system is allowed to allocate (exclusive)
108 * @return handle to this system, NULL on error
110 struct GNUNET_TESTING_System *
111 GNUNET_TESTING_system_create_with_portrange (const char *testdir,
112 const char *trusted_ip,
113 const char *hostname,
119 * Free system resources.
121 * @param system system to be freed
122 * @param remove_paths should the 'testdir' and all subdirectories
123 * be removed (clean up on shutdown)?
126 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
131 * Testing includes a number of pre-created hostkeys for
132 * faster peer startup. This function can be used to
133 * access the n-th key of those pre-created hostkeys; note
134 * that these keys are ONLY useful for testing and not
135 * secure as the private keys are part of the public
136 * GNUnet source code.
138 * This is primarily a helper function used internally
139 * by 'GNUNET_TESTING_peer_configure'.
141 * @param system the testing system handle
142 * @param key_number desired pre-created hostkey to obtain
143 * @param id set to the peer's identity (hash of the public
144 * key; if NULL, GNUNET_SYSERR is returned immediately
145 * @return NULL on error (not enough keys)
147 struct GNUNET_CRYPTO_EccPrivateKey *
148 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
150 struct GNUNET_PeerIdentity *id);
154 * Reserve a TCP or UDP port for a peer.
156 * @param system system to use for reservation tracking
157 * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
158 * @return 0 if no free port was available
161 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
166 * Release reservation of a TCP or UDP port for a peer
167 * (used during GNUNET_TESTING_peer_destroy).
169 * @param system system to use for reservation tracking
170 * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
171 * @param port reserved port to release
174 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
180 * Create a new configuration using the given configuration as a template;
181 * ports and paths will be modified to select available ports on the local
182 * system. The default configuration will be available in PATHS section under
183 * the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS
184 * section to the temporary directory specific to this configuration. If we run
185 * out of "*port" numbers, return SYSERR.
187 * This is primarily a helper function used internally
188 * by 'GNUNET_TESTING_peer_configure'.
190 * @param system system to use to coordinate resource usage
191 * @param cfg template configuration to update
192 * @return GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will
193 * be incomplete and should not be used there upon
196 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
197 struct GNUNET_CONFIGURATION_Handle *cfg);
198 // FIXME: add dual to 'release' ports again...
202 * Configure a GNUnet peer. GNUnet must be installed on the local
203 * system and available in the PATH.
205 * @param system system to use to coordinate resource usage
206 * @param cfg configuration to use; will be UPDATED (to reflect needed
207 * changes in port numbers and paths)
208 * @param key_number number of the hostkey to use for the peer
209 * @param id identifier for the daemon, will be set, can be NULL
210 * @param emsg set to freshly allocated error message (set to NULL on success),
212 * @return handle to the peer, NULL on error
214 struct GNUNET_TESTING_Peer *
215 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
216 struct GNUNET_CONFIGURATION_Handle *cfg,
218 struct GNUNET_PeerIdentity *id,
223 * Obtain the peer identity from a peer handle.
225 * @param peer peer handle for which we want the peer's identity
226 * @param id identifier for the daemon, will be set
229 GNUNET_TESTING_peer_get_identity (struct GNUNET_TESTING_Peer *peer,
230 struct GNUNET_PeerIdentity *id);
236 * @param peer peer to start
237 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
240 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
246 * @param peer peer to stop
247 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
250 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
254 * Destroy the peer. Releases resources locked during peer configuration.
255 * If the peer is still running, it will be stopped AND a warning will be
256 * printed (users of the API should stop the peer explicitly first).
258 * @param peer peer to destroy
261 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
265 * Sends SIGTERM to the peer's main process
267 * @param peer the handle to the peer
268 * @return GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL
269 * or upon any error while sending SIGTERM
272 GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer);
276 * Waits for a peer to terminate. The peer's main process will also be destroyed.
278 * @param peer the handle to the peer
279 * @return GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL
280 * or upon any error while waiting
283 GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer);
287 * Signature of the 'main' function for a (single-peer) testcase that
288 * is run using 'GNUNET_TESTING_peer_run'.
291 * @param cfg configuration of the peer that was started
292 * @param peer identity of the peer that was created
294 typedef void (*GNUNET_TESTING_TestMain)(void *cls,
295 const struct GNUNET_CONFIGURATION_Handle *cfg,
296 struct GNUNET_TESTING_Peer *peer);
300 * Start a single peer and run a test using the testing library.
301 * Starts a peer using the given configuration and then invokes the
302 * given callback. This function ALSO initializes the scheduler loop
303 * and should thus be called directly from "main". The testcase
304 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
306 * @param testdir only the directory name without any path. This is used for
307 * all service homes; the directory will be created in a temporary
308 * location depending on the underlying OS
309 * @param cfgfilename name of the configuration file to use;
310 * use NULL to only run with defaults
311 * @param tm main function of the testcase
312 * @param tm_cls closure for 'tm'
313 * @return 0 on success, 1 on error
316 GNUNET_TESTING_peer_run (const char *testdir,
317 const char *cfgfilename,
318 GNUNET_TESTING_TestMain tm,
323 * Start a single service (no ARM, except of course if the given
324 * service name is 'arm') and run a test using the testing library.
325 * Starts a service using the given configuration and then invokes the
326 * given callback. This function ALSO initializes the scheduler loop
327 * and should thus be called directly from "main". The testcase
328 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
330 * This function is useful if the testcase is for a single service
331 * and if that service doesn't itself depend on other services.
333 * @param testdir only the directory name without any path. This is used for
334 * all service homes; the directory will be created in a temporary
335 * location depending on the underlying OS
336 * @param service_name name of the service to run
337 * @param cfgfilename name of the configuration file to use;
338 * use NULL to only run with defaults
339 * @param tm main function of the testcase
340 * @param tm_cls closure for 'tm'
341 * @return 0 on success, 1 on error
344 GNUNET_TESTING_service_run (const char *testdir,
345 const char *service_name,
346 const char *cfgfilename,
347 GNUNET_TESTING_TestMain tm,
352 * Sometimes we use the binary name to determine which specific
353 * test to run. In those cases, the string after the last "_"
354 * in 'argv[0]' specifies a string that determines the configuration
355 * file or plugin to use.
357 * This function returns the respective substring, taking care
358 * of issues such as binaries ending in '.exe' on W32.
360 * @param argv0 the name of the binary
361 * @return string between the last '_' and the '.exe' (or the end of the string),
362 * NULL if argv0 has no '_'
365 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
368 #if 0 /* keep Emacsens' auto-indent happy */