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 */
47 #define GNUNET_TESTING_HOSTKEYFILESIZE 914
50 * Handle for a system on which GNUnet peers are executed;
51 * a system is used for reserving unique paths and ports.
53 struct GNUNET_TESTING_System;
57 * Handle for a GNUnet peer controlled by testing.
59 struct GNUNET_TESTING_Peer;
63 * Create a system handle. There must only be one system handle per operating
64 * system. Uses a default range for allowed ports. Ports are still tested for
67 * @param testdir only the directory name without any path. This is used for all
68 * service homes; the directory will be created in a temporary location
69 * depending on the underlying OS
70 * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
71 * service configurations generated to allow control connections from
72 * this ip. This can either be a single ip address or a network address
74 * @param hostname the hostname of the system we are using for testing; NULL for
76 * @return handle to this system, NULL on error
78 struct GNUNET_TESTING_System *
79 GNUNET_TESTING_system_create (const char *testdir,
80 const char *trusted_ip,
81 const char *hostname);
85 * Create a system handle. There must only be one system
86 * handle per operating system. Use this function directly
87 * if multiple system objects are created for the same host
88 * (only really useful when testing --- or to make the port
89 * range configureable).
91 * @param testdir only the directory name without any path. This is used for
92 * all service homes; the directory will be created in a temporary
93 * location depending on the underlying OS
94 * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
95 * service configurations generated to allow control connections from
96 * this ip. This can either be a single ip address or a network address
98 * @param hostname the hostname of the system we are using for testing; NULL for
100 * @param lowport lowest port number this system is allowed to allocate (inclusive)
101 * @param highport highest port number this system is allowed to allocate (exclusive)
102 * @return handle to this system, NULL on error
104 struct GNUNET_TESTING_System *
105 GNUNET_TESTING_system_create_with_portrange (const char *testdir,
106 const char *trusted_ip,
107 const char *hostname,
113 * Free system resources.
115 * @param system system to be freed
116 * @param remove_paths should the 'testdir' and all subdirectories
117 * be removed (clean up on shutdown)?
120 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
125 * Testing includes a number of pre-created hostkeys for
126 * faster peer startup. This function can be used to
127 * access the n-th key of those pre-created hostkeys; note
128 * that these keys are ONLY useful for testing and not
129 * secure as the private keys are part of the public
130 * GNUnet source code.
132 * This is primarily a helper function used internally
133 * by 'GNUNET_TESTING_peer_configure'.
135 * @param system the testing system handle
136 * @param key_number desired pre-created hostkey to obtain
137 * @param id set to the peer's identity (hash of the public
138 * key; if NULL, GNUNET_SYSERR is returned immediately
139 * @return NULL on error (not enough keys)
141 struct GNUNET_CRYPTO_RsaPrivateKey *
142 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
144 struct GNUNET_PeerIdentity *id);
148 * Reserve a TCP or UDP port for a peer.
150 * @param system system to use for reservation tracking
151 * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
152 * @return 0 if no free port was available
155 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
160 * Release reservation of a TCP or UDP port for a peer
161 * (used during GNUNET_TESTING_peer_destroy).
163 * @param system system to use for reservation tracking
164 * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
165 * @param port reserved port to release
168 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
174 * Create a new configuration using the given configuration as a template;
175 * ports and paths will be modified to select available ports on the local
176 * system. The default configuration will be available in PATHS section under
177 * the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS
178 * section to the temporary directory specific to this configuration. If we run
179 * out of "*port" numbers, return SYSERR.
181 * This is primarily a helper function used internally
182 * by 'GNUNET_TESTING_peer_configure'.
184 * @param system system to use to coordinate resource usage
185 * @param cfg template configuration to update
186 * @return GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will
187 * be incomplete and should not be used there upon
190 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
191 struct GNUNET_CONFIGURATION_Handle *cfg);
192 // FIXME: add dual to 'release' ports again...
196 * Configure a GNUnet peer. GNUnet must be installed on the local
197 * system and available in the PATH.
199 * @param system system to use to coordinate resource usage
200 * @param cfg configuration to use; will be UPDATED (to reflect needed
201 * changes in port numbers and paths)
202 * @param key_number number of the hostkey to use for the peer
203 * @param id identifier for the daemon, will be set, can be NULL
204 * @param emsg set to freshly allocated error message (set to NULL on success),
206 * @return handle to the peer, NULL on error
208 struct GNUNET_TESTING_Peer *
209 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
210 struct GNUNET_CONFIGURATION_Handle *cfg,
212 struct GNUNET_PeerIdentity *id,
217 * Obtain the peer identity from a peer handle.
219 * @param peer peer handle for which we want the peer's identity
220 * @param id identifier for the daemon, will be set
223 GNUNET_TESTING_peer_get_identity (const struct GNUNET_TESTING_Peer *peer,
224 struct GNUNET_PeerIdentity *id);
230 * @param peer peer to start
231 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
234 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
240 * @param peer peer to stop
241 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
244 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
248 * Destroy the peer. Releases resources locked during peer configuration.
249 * If the peer is still running, it will be stopped AND a warning will be
250 * printed (users of the API should stop the peer explicitly first).
252 * @param peer peer to destroy
255 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
259 * Sends SIGTERM to the peer's main process
261 * @param peer the handle to the peer
262 * @return GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL
263 * or upon any error while sending SIGTERM
266 GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer);
270 * Waits for a peer to terminate. The peer's main process will also be destroyed.
272 * @param peer the handle to the peer
273 * @return GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL
274 * or upon any error while waiting
277 GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer);
281 * Signature of the 'main' function for a (single-peer) testcase that
282 * is run using 'GNUNET_TESTING_peer_run'.
285 * @param cfg configuration of the peer that was started
286 * @param peer identity of the peer that was created
288 typedef void (*GNUNET_TESTING_TestMain)(void *cls,
289 const struct GNUNET_CONFIGURATION_Handle *cfg,
290 struct GNUNET_TESTING_Peer *peer);
294 * Start a single peer and run a test using the testing library.
295 * Starts a peer using the given configuration and then invokes the
296 * given callback. This function ALSO initializes the scheduler loop
297 * and should thus be called directly from "main". The testcase
298 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
300 * @param testdir only the directory name without any path. This is used for
301 * all service homes; the directory will be created in a temporary
302 * location depending on the underlying OS
303 * @param cfgfilename name of the configuration file to use;
304 * use NULL to only run with defaults
305 * @param tm main function of the testcase
306 * @param tm_cls closure for 'tm'
307 * @return 0 on success, 1 on error
310 GNUNET_TESTING_peer_run (const char *testdir,
311 const char *cfgfilename,
312 GNUNET_TESTING_TestMain tm,
317 * Start a single service (no ARM, except of course if the given
318 * service name is 'arm') and run a test using the testing library.
319 * Starts a service using the given configuration and then invokes the
320 * given callback. This function ALSO initializes the scheduler loop
321 * and should thus be called directly from "main". The testcase
322 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
324 * This function is useful if the testcase is for a single service
325 * and if that service doesn't itself depend on other services.
327 * @param testdir only the directory name without any path. This is used for
328 * all service homes; the directory will be created in a temporary
329 * location depending on the underlying OS
330 * @param service_name name of the service to run
331 * @param cfgfilename name of the configuration file to use;
332 * use NULL to only run with defaults
333 * @param tm main function of the testcase
334 * @param tm_cls closure for 'tm'
335 * @return 0 on success, 1 on error
338 GNUNET_TESTING_service_run (const char *testdir,
339 const char *service_name,
340 const char *cfgfilename,
341 GNUNET_TESTING_TestMain tm,
346 * Sometimes we use the binary name to determine which specific
347 * test to run. In those cases, the string after the last "_"
348 * in 'argv[0]' specifies a string that determines the configuration
349 * file or plugin to use.
351 * This function returns the respective substring, taking care
352 * of issues such as binaries ending in '.exe' on W32.
354 * @param argv0 the name of the binary
355 * @return string between the last '_' and the '.exe' (or the end of the string),
356 * NULL if argv0 has no '_'
359 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
362 #if 0 /* keep Emacsens' auto-indent happy */