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-new.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_NEW_H
34 #define GNUNET_TESTING_LIB_NEW_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 controller hostname of the controlling host, service configurations
71 * are modified to allow control connections from this host; can be NULL
72 * @param hostname the hostname of the system we are using for testing; NULL for
74 * @return handle to this system, NULL on error
76 struct GNUNET_TESTING_System *
77 GNUNET_TESTING_system_create (const char *testdir,
78 const char *controller,
79 const char *hostname);
83 * Create a system handle. There must only be one system
84 * handle per operating system. Use this function directly
85 * if multiple system objects are created for the same host
86 * (only really useful when testing --- or to make the port
87 * range configureable).
89 * @param testdir only the directory name without any path. This is used for
90 * all service homes; the directory will be created in a temporary
91 * location depending on the underlying OS
92 * @param controller hostname of the controlling host,
93 * service configurations are modified to allow
94 * control connections from this host; can be NULL
95 * @param hostname the hostname of the system we are using for testing; NULL for
97 * @param lowport lowest port number this system is allowed to allocate (inclusive)
98 * @param highport highest port number this system is allowed to allocate (exclusive)
99 * @return handle to this system, NULL on error
101 struct GNUNET_TESTING_System *
102 GNUNET_TESTING_system_create_with_portrange (const char *testdir,
103 const char *controller,
104 const char *hostname,
110 * Free system resources.
112 * @param system system to be freed
113 * @param remove_paths should the 'testdir' and all subdirectories
114 * be removed (clean up on shutdown)?
117 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
122 * Testing includes a number of pre-created hostkeys for
123 * faster peer startup. This function can be used to
124 * access the n-th key of those pre-created hostkeys; note
125 * that these keys are ONLY useful for testing and not
126 * secure as the private keys are part of the public
127 * GNUnet source code.
129 * This is primarily a helper function used internally
130 * by 'GNUNET_TESTING_peer_configure'.
132 * @param system the testing system handle
133 * @param key_number desired pre-created hostkey to obtain
134 * @param id set to the peer's identity (hash of the public
135 * key; if NULL, GNUNET_SYSERR is returned immediately
136 * @return NULL on error (not enough keys)
138 struct GNUNET_CRYPTO_RsaPrivateKey *
139 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
141 struct GNUNET_PeerIdentity *id);
145 * Reserve a TCP or UDP port for a peer.
147 * @param system system to use for reservation tracking
148 * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
149 * @return 0 if no free port was available
152 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
157 * Release reservation of a TCP or UDP port for a peer
158 * (used during GNUNET_TESTING_peer_destroy).
160 * @param system system to use for reservation tracking
161 * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
162 * @param port reserved port to release
165 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
171 * Create a new configuration using the given configuration as a template;
172 * ports and paths will be modified to select available ports on the local
173 * system. The default configuration will be available in PATHS section under
174 * the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS
175 * section to the temporary directory specific to this configuration. If we run
176 * out of "*port" numbers, return SYSERR.
178 * This is primarily a helper function used internally
179 * by 'GNUNET_TESTING_peer_configure'.
181 * @param system system to use to coordinate resource usage
182 * @param cfg template configuration to update
183 * @return GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will
184 * be incomplete and should not be used there upon
187 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
188 struct GNUNET_CONFIGURATION_Handle *cfg);
189 // FIXME: add dual to 'release' ports again...
193 * Configure a GNUnet peer. GNUnet must be installed on the local
194 * system and available in the PATH.
196 * @param system system to use to coordinate resource usage
197 * @param cfg configuration to use; will be UPDATED (to reflect needed
198 * changes in port numbers and paths)
199 * @param key_number number of the hostkey to use for the peer
200 * @param id identifier for the daemon, will be set, can be NULL
201 * @param emsg set to freshly allocated error message (set to NULL on success),
203 * @return handle to the peer, NULL on error
205 struct GNUNET_TESTING_Peer *
206 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
207 struct GNUNET_CONFIGURATION_Handle *cfg,
209 struct GNUNET_PeerIdentity *id,
214 * Obtain the peer identity from a peer handle.
216 * @param peer peer handle for which we want the peer's identity
217 * @param id identifier for the daemon, will be set
220 GNUNET_TESTING_peer_get_identity (const struct GNUNET_TESTING_Peer *peer,
221 struct GNUNET_PeerIdentity *id);
227 * @param peer peer to start
228 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
231 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
237 * @param peer peer to stop
238 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
241 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
245 * Destroy the peer. Releases resources locked during peer configuration.
246 * If the peer is still running, it will be stopped AND a warning will be
247 * printed (users of the API should stop the peer explicitly first).
249 * @param peer peer to destroy
252 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
256 * Signature of the 'main' function for a (single-peer) testcase that
257 * is run using 'GNUNET_TESTING_peer_run'.
260 * @param cfg configuration of the peer that was started
261 * @param peer identity of the peer that was created
263 typedef void (*GNUNET_TESTING_TestMain)(void *cls,
264 const struct GNUNET_CONFIGURATION_Handle *cfg,
265 struct GNUNET_TESTING_Peer *peer);
269 * Start a single peer and run a test using the testing library.
270 * Starts a peer using the given configuration and then invokes the
271 * given callback. This function ALSO initializes the scheduler loop
272 * and should thus be called directly from "main". The testcase
273 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
275 * @param testdir only the directory name without any path. This is used for
276 * all service homes; the directory will be created in a temporary
277 * location depending on the underlying OS
278 * @param cfgfilename name of the configuration file to use;
279 * use NULL to only run with defaults
280 * @param tm main function of the testcase
281 * @param tm_cls closure for 'tm'
282 * @return 0 on success, 1 on error
285 GNUNET_TESTING_peer_run (const char *testdir,
286 const char *cfgfilename,
287 GNUNET_TESTING_TestMain tm,
292 * Start a single service (no ARM, except of course if the given
293 * service name is 'arm') and run a test using the testing library.
294 * Starts a service using the given configuration and then invokes the
295 * given callback. This function ALSO initializes the scheduler loop
296 * and should thus be called directly from "main". The testcase
297 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
299 * This function is useful if the testcase is for a single service
300 * and if that service doesn't itself depend on other services.
302 * @param testdir only the directory name without any path. This is used for
303 * all service homes; the directory will be created in a temporary
304 * location depending on the underlying OS
305 * @param service_name name of the service to run
306 * @param cfgfilename name of the configuration file to use;
307 * use NULL to only run with defaults
308 * @param tm main function of the testcase
309 * @param tm_cls closure for 'tm'
310 * @return 0 on success, 1 on error
313 GNUNET_TESTING_service_run (const char *testdir,
314 const char *service_name,
315 const char *cfgfilename,
316 GNUNET_TESTING_TestMain tm,
321 * Sometimes we use the binary name to determine which specific
322 * test to run. In those cases, the string after the last "_"
323 * in 'argv[0]' specifies a string that determines the configuration
324 * file or plugin to use.
326 * This function returns the respective substring, taking care
327 * of issues such as binaries ending in '.exe' on W32.
329 * @param argv0 the name of the binary
330 * @return string between the last '_' and the '.exe' (or the end of the string),
331 * NULL if argv0 has no '_'
334 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
337 #if 0 /* keep Emacsens' auto-indent happy */