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 */
49 * Handle for a system on which GNUnet peers are executed;
50 * a system is used for reserving unique paths and ports.
52 struct GNUNET_TESTING_System;
56 * Handle for a GNUnet peer controlled by testing.
58 struct GNUNET_TESTING_Peer;
62 * Create a system handle. There must only be one system
63 * handle per operating system.
65 * @param testdir only the directory name without any path. This is used for
66 * all service homes; the directory will be created in a temporary
67 * location depending on the underlying OS
68 * @param controller hostname of the controlling host,
69 * service configurations are modified to allow
70 * control connections from this host; can be NULL
71 * @return handle to this system, NULL on error
73 struct GNUNET_TESTING_System *
74 GNUNET_TESTING_system_create (const char *testdir,
75 const char *controller);
79 * Free system resources.
81 * @param system system to be freed
82 * @param remove_paths should the 'testdir' and all subdirectories
83 * be removed (clean up on shutdown)?
86 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
91 * Testing includes a number of pre-created hostkeys for
92 * faster peer startup. This function can be used to
93 * access the n-th key of those pre-created hostkeys; note
94 * that these keys are ONLY useful for testing and not
95 * secure as the private keys are part of the public
98 * This is primarily a helper function used internally
99 * by 'GNUNET_TESTING_peer_configure'.
101 * @param system the testing system handle
102 * @param key_number desired pre-created hostkey to obtain
103 * @param id set to the peer's identity (hash of the public
104 * key; if NULL, GNUNET_SYSERR is returned immediately
105 * @return NULL on error (not enough keys)
107 struct GNUNET_CRYPTO_RsaPrivateKey *
108 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
110 struct GNUNET_PeerIdentity *id);
114 * Reserve a TCP or UDP port for a peer.
116 * @param system system to use for reservation tracking
117 * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
118 * @return 0 if no free port was available
121 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
126 * Release reservation of a TCP or UDP port for a peer
127 * (used during GNUNET_TESTING_peer_destroy).
129 * @param system system to use for reservation tracking
130 * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
131 * @param port reserved port to release
134 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
140 * Create a new configuration using the given configuration as a template;
141 * ports and paths will be modified to select available ports on the local
142 * system. The default configuration will be available in PATHS section under
143 * the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS
144 * section to the temporary directory specific to this configuration. If we run
145 * out of "*port" numbers, return SYSERR.
147 * This is primarily a helper function used internally
148 * by 'GNUNET_TESTING_peer_configure'.
150 * @param system system to use to coordinate resource usage
151 * @param cfg template configuration to update
152 * @return GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will
153 * be incomplete and should not be used there upon
156 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
157 struct GNUNET_CONFIGURATION_Handle *cfg);
158 // FIXME: add dual to 'release' ports again...
162 * Configure a GNUnet peer. GNUnet must be installed on the local
163 * system and available in the PATH.
165 * @param system system to use to coordinate resource usage
166 * @param cfg configuration to use; will be UPDATED (to reflect needed
167 * changes in port numbers and paths)
168 * @param key_number number of the hostkey to use for the peer
169 * @param id identifier for the daemon, will be set, can be NULL
170 * @param emsg set to freshly allocated error message (set to NULL on success),
172 * @return handle to the peer, NULL on error
174 struct GNUNET_TESTING_Peer *
175 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
176 struct GNUNET_CONFIGURATION_Handle *cfg,
178 struct GNUNET_PeerIdentity *id,
183 * Obtain the peer identity from a peer handle.
185 * @param peer peer handle for which we want the peer's identity
186 * @param id identifier for the daemon, will be set
189 GNUNET_TESTING_peer_get_identity (const struct GNUNET_TESTING_Peer *peer,
190 struct GNUNET_PeerIdentity *id);
196 * @param peer peer to start
197 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
200 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
206 * @param peer peer to stop
207 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
210 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
214 * Destroy the peer. Releases resources locked during peer configuration.
215 * If the peer is still running, it will be stopped AND a warning will be
216 * printed (users of the API should stop the peer explicitly first).
218 * @param peer peer to destroy
221 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
225 * Signature of the 'main' function for a (single-peer) testcase that
226 * is run using 'GNUNET_TESTING_peer_run'.
229 * @param cfg configuration of the peer that was started
230 * @param peer identity of the peer that was created
232 typedef void (*GNUNET_TESTING_TestMain)(void *cls,
233 const struct GNUNET_CONFIGURATION_Handle *cfg,
234 struct GNUNET_TESTING_Peer *peer);
238 * Start a single peer and run a test using the testing library.
239 * Starts a peer using the given configuration and then invokes the
240 * given callback. This function ALSO initializes the scheduler loop
241 * and should thus be called directly from "main". The testcase
242 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
244 * @param testdir only the directory name without any path. This is used for
245 * all service homes; the directory will be created in a temporary
246 * location depending on the underlying OS
247 * @param cfgfilename name of the configuration file to use;
248 * use NULL to only run with defaults
249 * @param tm main function of the testcase
250 * @param tm_cls closure for 'tm'
251 * @return 0 on success, 1 on error
254 GNUNET_TESTING_peer_run (const char *testdir,
255 const char *cfgfilename,
256 GNUNET_TESTING_TestMain tm,
261 * Start a single service (no ARM, except of course if the given
262 * service name is 'arm') and run a test using the testing library.
263 * Starts a service using the given configuration and then invokes the
264 * given callback. This function ALSO initializes the scheduler loop
265 * and should thus be called directly from "main". The testcase
266 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
268 * This function is useful if the testcase is for a single service
269 * and if that service doesn't itself depend on other services.
271 * @param testdir only the directory name without any path. This is used for
272 * all service homes; the directory will be created in a temporary
273 * location depending on the underlying OS
274 * @param service_name name of the service to run
275 * @param cfgfilename name of the configuration file to use;
276 * use NULL to only run with defaults
277 * @param tm main function of the testcase
278 * @param tm_cls closure for 'tm'
279 * @return 0 on success, 1 on error
282 GNUNET_TESTING_service_run (const char *testdir,
283 const char *service_name,
284 const char *cfgfilename,
285 GNUNET_TESTING_TestMain tm,
290 * Sometimes we use the binary name to determine which specific
291 * test to run. In those cases, the string after the last "_"
292 * in 'argv[0]' specifies a string that determines the configuration
293 * file or plugin to use.
295 * This function returns the respective substring, taking care
296 * of issues such as binaries ending in '.exe' on W32.
298 * @param argv0 the name of the binary
299 * @return string between the last '_' and the '.exe' (or the end of the string),
300 * NULL if argv0 has no '_'
303 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
306 #if 0 /* keep Emacsens' auto-indent happy */