extend API to enalbe exclusive port ranges to be specified for testing-system objects
[oweals/gnunet.git] / src / include / gnunet_testing_lib-new.h
1 /*
2       This file is part of GNUnet
3       (C) 2008, 2009, 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
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
31  */
32
33 #ifndef GNUNET_TESTING_LIB_NEW_H
34 #define GNUNET_TESTING_LIB_NEW_H
35
36 #include "gnunet_util_lib.h"
37 #include "gnunet_statistics_service.h"
38
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #if 0                           /* keep Emacsens' auto-indent happy */
43 }
44 #endif
45 #endif
46
47
48 /**
49  * Handle for a system on which GNUnet peers are executed;
50  * a system is used for reserving unique paths and ports.
51  */
52 struct GNUNET_TESTING_System;
53
54
55 /**
56  * Handle for a GNUnet peer controlled by testing.
57  */
58 struct GNUNET_TESTING_Peer;
59
60
61 /**
62  * Create a system handle.  There must only be one system
63  * handle per operating system.  Uses a default range for allowed ports.
64  * Ports are still tested for availability.
65  *
66  * @param testdir only the directory name without any path. This is used for
67  *          all service homes; the directory will be created in a temporary
68  *          location depending on the underlying OS
69  * @param controller hostname of the controlling host, 
70  *        service configurations are modified to allow 
71  *        control connections from this host; can be NULL
72  * @return handle to this system, NULL on error
73  */
74 struct GNUNET_TESTING_System *
75 GNUNET_TESTING_system_create (const char *testdir,
76                               const char *controller);
77
78
79 /**
80  * Create a system handle.  There must only be one system
81  * handle per operating system.  Use this function directly
82  * if multiple system objects are created for the same host
83  * (only really useful when testing --- or to make the port
84  * range configureable).
85  *
86  * @param testdir only the directory name without any path. This is used for
87  *          all service homes; the directory will be created in a temporary
88  *          location depending on the underlying OS
89  * @param controller hostname of the controlling host, 
90  *        service configurations are modified to allow 
91  *        control connections from this host; can be NULL
92  * @param lowport lowest port number this system is allowed to allocate (inclusive)
93  * @param highport highest port number this system is allowed to allocate (exclusive)
94  * @return handle to this system, NULL on error
95  */
96 struct GNUNET_TESTING_System *
97 GNUNET_TESTING_system_create_with_portrange (const char *testdir,
98                                              const char *controller,
99                                              uint16_t lowport,
100                                              uint16_t highport);
101
102
103 /**
104  * Free system resources.
105  *
106  * @param system system to be freed
107  * @param remove_paths should the 'testdir' and all subdirectories
108  *        be removed (clean up on shutdown)?
109  */
110 void
111 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
112                                int remove_paths);
113
114
115 /**
116  * Testing includes a number of pre-created hostkeys for
117  * faster peer startup.  This function can be used to
118  * access the n-th key of those pre-created hostkeys; note
119  * that these keys are ONLY useful for testing and not
120  * secure as the private keys are part of the public 
121  * GNUnet source code.
122  *
123  * This is primarily a helper function used internally
124  * by 'GNUNET_TESTING_peer_configure'.
125  *
126  * @param system the testing system handle
127  * @param key_number desired pre-created hostkey to obtain
128  * @param id set to the peer's identity (hash of the public
129  *        key; if NULL, GNUNET_SYSERR is returned immediately
130  * @return NULL on error (not enough keys)
131  */
132 struct GNUNET_CRYPTO_RsaPrivateKey *
133 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
134                             uint32_t key_number,
135                             struct GNUNET_PeerIdentity *id);
136
137
138 /**
139  * Reserve a TCP or UDP port for a peer.
140  *
141  * @param system system to use for reservation tracking
142  * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
143  * @return 0 if no free port was available
144  */
145 uint16_t 
146 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
147                              int is_tcp);
148
149
150 /**
151  * Release reservation of a TCP or UDP port for a peer
152  * (used during GNUNET_TESTING_peer_destroy).
153  *
154  * @param system system to use for reservation tracking
155  * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
156  * @param port reserved port to release
157  */
158 void
159 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
160                              int is_tcp,
161                              uint16_t port);
162
163
164 /**
165  * Create a new configuration using the given configuration as a template;
166  * ports and paths will be modified to select available ports on the local
167  * system. The default configuration will be available in PATHS section under
168  * the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS
169  * section to the temporary directory specific to this configuration. If we run
170  * out of "*port" numbers, return SYSERR.
171  *
172  * This is primarily a helper function used internally
173  * by 'GNUNET_TESTING_peer_configure'.
174  *
175  * @param system system to use to coordinate resource usage
176  * @param cfg template configuration to update
177  * @return GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will
178  *           be incomplete and should not be used there upon
179  */
180 int
181 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
182                                      struct GNUNET_CONFIGURATION_Handle *cfg);
183 // FIXME: add dual to 'release' ports again...
184
185
186 /**
187  * Configure a GNUnet peer.  GNUnet must be installed on the local
188  * system and available in the PATH. 
189  *
190  * @param system system to use to coordinate resource usage
191  * @param cfg configuration to use; will be UPDATED (to reflect needed
192  *            changes in port numbers and paths)
193  * @param key_number number of the hostkey to use for the peer
194  * @param id identifier for the daemon, will be set, can be NULL
195  * @param emsg set to freshly allocated error message (set to NULL on success), 
196  *          can be NULL
197  * @return handle to the peer, NULL on error
198  */
199 struct GNUNET_TESTING_Peer *
200 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
201                                struct GNUNET_CONFIGURATION_Handle *cfg,
202                                uint32_t key_number,
203                                struct GNUNET_PeerIdentity *id,
204                                char **emsg);
205
206
207 /**
208  * Obtain the peer identity from a peer handle.
209  *
210  * @param peer peer handle for which we want the peer's identity
211  * @param id identifier for the daemon, will be set
212  */
213 void
214 GNUNET_TESTING_peer_get_identity (const struct GNUNET_TESTING_Peer *peer,
215                                   struct GNUNET_PeerIdentity *id);
216
217
218 /**
219  * Start the peer. 
220  *
221  * @param peer peer to start
222  * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
223  */
224 int
225 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
226
227
228 /**
229  * Stop the peer. 
230  *
231  * @param peer peer to stop
232  * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
233  */
234 int
235 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
236
237
238 /**
239  * Destroy the peer.  Releases resources locked during peer configuration.
240  * If the peer is still running, it will be stopped AND a warning will be
241  * printed (users of the API should stop the peer explicitly first).
242  *
243  * @param peer peer to destroy
244  */
245 void
246 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
247
248
249 /**
250  * Signature of the 'main' function for a (single-peer) testcase that
251  * is run using 'GNUNET_TESTING_peer_run'.
252  * 
253  * @param cls closure
254  * @param cfg configuration of the peer that was started
255  * @param peer identity of the peer that was created
256  */
257 typedef void (*GNUNET_TESTING_TestMain)(void *cls,
258                                         const struct GNUNET_CONFIGURATION_Handle *cfg,
259                                         struct GNUNET_TESTING_Peer *peer);
260
261
262 /**
263  * Start a single peer and run a test using the testing library.
264  * Starts a peer using the given configuration and then invokes the
265  * given callback.  This function ALSO initializes the scheduler loop
266  * and should thus be called directly from "main".  The testcase
267  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
268  *
269  * @param testdir only the directory name without any path. This is used for
270  *          all service homes; the directory will be created in a temporary
271  *          location depending on the underlying OS
272  * @param cfgfilename name of the configuration file to use;
273  *         use NULL to only run with defaults
274  * @param tm main function of the testcase
275  * @param tm_cls closure for 'tm'
276  * @return 0 on success, 1 on error
277  */
278 int
279 GNUNET_TESTING_peer_run (const char *testdir,
280                          const char *cfgfilename,
281                          GNUNET_TESTING_TestMain tm,
282                          void *tm_cls);
283
284
285 /**
286  * Start a single service (no ARM, except of course if the given
287  * service name is 'arm') and run a test using the testing library.
288  * Starts a service using the given configuration and then invokes the
289  * given callback.  This function ALSO initializes the scheduler loop
290  * and should thus be called directly from "main".  The testcase
291  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
292  *
293  * This function is useful if the testcase is for a single service
294  * and if that service doesn't itself depend on other services.
295  *
296  * @param testdir only the directory name without any path. This is used for
297  *          all service homes; the directory will be created in a temporary
298  *          location depending on the underlying OS
299  * @param service_name name of the service to run
300  * @param cfgfilename name of the configuration file to use;
301  *         use NULL to only run with defaults
302  * @param tm main function of the testcase
303  * @param tm_cls closure for 'tm'
304  * @return 0 on success, 1 on error
305  */
306 int
307 GNUNET_TESTING_service_run (const char *testdir,
308                             const char *service_name,
309                             const char *cfgfilename,
310                             GNUNET_TESTING_TestMain tm,
311                             void *tm_cls);
312
313
314 /**
315  * Sometimes we use the binary name to determine which specific
316  * test to run.  In those cases, the string after the last "_"
317  * in 'argv[0]' specifies a string that determines the configuration
318  * file or plugin to use.  
319  *
320  * This function returns the respective substring, taking care
321  * of issues such as binaries ending in '.exe' on W32.
322  *
323  * @param argv0 the name of the binary
324  * @return string between the last '_' and the '.exe' (or the end of the string),
325  *         NULL if argv0 has no '_' 
326  */
327 char *
328 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
329
330
331 #if 0                           /* keep Emacsens' auto-indent happy */
332 {
333 #endif
334 #ifdef __cplusplus
335 }
336 #endif
337
338 #endif