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 tmppath prefix path to use for all service homes
66 * @param controller hostname of the controlling host,
67 * service configurations are modified to allow
68 * control connections from this host; can be NULL
69 * @return handle to this system, NULL on error
71 struct GNUNET_TESTING_System *
72 GNUNET_TESTING_system_create (const char *tmppath,
73 const char *controller);
78 * Free system resources.
80 * @param system system to be freed
81 * @param remove_paths should the 'tmppath' and all subdirectories
82 * be removed (clean up on shutdown)?
85 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
90 * Testing includes a number of pre-created hostkeys for
91 * faster peer startup. This function can be used to
92 * access the n-th key of those pre-created hostkeys; note
93 * that these keys are ONLY useful for testing and not
94 * secure as the private keys are part of the public
97 * This is primarily a helper function used internally
98 * by 'GNUNET_TESTING_peer_configure'.
100 * @param key_number desired pre-created hostkey to obtain
101 * @param filename where to store the hostkey (file will
102 * be created, or overwritten if it already exists)
103 * @param id set to the peer's identity (hash of the public
104 * key; if NULL, GNUNET_SYSERR is returned immediately
105 * @return GNUNET_SYSERR on error (not enough keys)
108 GNUNET_TESTING_hostkey_get (uint32_t key_number,
109 const char *filename,
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
141 * as a template; ports and paths will be modified to select
142 * available ports on the local system. If we run
143 * out of "*port" numbers, return SYSERR.
145 * This is primarily a helper function used internally
146 * by 'GNUNET_TESTING_peer_configure'.
148 * @param system system to use to coordinate resource usage
149 * @param cfg template configuration to update
150 * @return GNUNET_OK on success, GNUNET_SYSERR on error
153 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
154 struct GNUNET_CONFIGURATION_Handle *cfg);
155 // FIXME: add dual to 'release' ports again...
159 * Configure a GNUnet peer. GNUnet must be installed on the local
160 * system and available in the PATH.
162 * @param system system to use to coordinate resource usage
163 * @param cfg configuration to use; will be UPDATED (to reflect needed
164 * changes in port numbers and paths)
165 * @param key_number number of the hostkey to use for the peer
166 * @param id identifier for the daemon, will be set, can be NULL
167 * @param emsg set to error message (set to NULL on success), can be NULL
168 * @return handle to the peer, NULL on error
170 struct GNUNET_TESTING_Peer *
171 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
172 struct GNUNET_CONFIGURATION_Handle *cfg,
174 struct GNUNET_PeerIdentity *id,
181 * @param peer peer to start
182 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
185 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
191 * @param peer peer to stop
192 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
195 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
199 * Destroy the peer. Releases resources locked during peer configuration.
200 * If the peer is still running, it will be stopped AND a warning will be
201 * printed (users of the API should stop the peer explicitly first).
203 * @param peer peer to destroy
206 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
210 * Signature of the 'main' function for a (single-peer) testcase that
211 * is run using 'GNUNET_TESTING_peer_run'.
214 * @param cfg configuration of the peer that was started
216 typedef void (*GNUNET_TESTING_TestMain)(void *cls,
217 const struct GNUNET_CONFIGURATION_Handle *cfg);
221 * Start a single peer and run a test using the testing library.
222 * Starts a peer using the given configuration and then invokes the
223 * given callback. This function ALSO initializes the scheduler loop
224 * and should thus be called directly from "main". The testcase
225 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
227 * @param tmppath path for storing temporary data for the test
228 * @param cfgfilename name of the configuration file to use;
229 * use NULL to only run with defaults
230 * @param tm main function of the testcase
231 * @param tm_cls closure for 'tm'
232 * @return 0 on success, 1 on error
235 GNUNET_TESTING_peer_run (const char *tmppath,
236 const char *cfgfilename,
237 GNUNET_TESTING_TestMain tm,
243 * Start a single service (no ARM, except of course if the given
244 * service name is 'arm') and run a test using the testing library.
245 * Starts a service using the given configuration and then invokes the
246 * given callback. This function ALSO initializes the scheduler loop
247 * and should thus be called directly from "main". The testcase
248 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
250 * This function is useful if the testcase is for a single service
251 * and if that service doesn't itself depend on other services.
253 * @param tmppath path for storing temporary data for the test
254 * @param service_name name of the service to run
255 * @param cfgfilename name of the configuration file to use;
256 * use NULL to only run with defaults
257 * @param tm main function of the testcase
258 * @param tm_cls closure for 'tm'
259 * @return 0 on success, 1 on error
262 GNUNET_TESTING_service_run (const char *tmppath,
263 const char *service_name,
264 const char *cfgfilename,
265 GNUNET_TESTING_TestMain tm,
270 #if 0 /* keep Emacsens' auto-indent happy */