- rename
[oweals/gnunet.git] / src / include / gnunet_testing_lib.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.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_H
34 #define GNUNET_TESTING_LIB_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 #define GNUNET_TESTING_HOSTKEYFILESIZE 914
48
49 /**
50  * Handle for a system on which GNUnet peers are executed;
51  * a system is used for reserving unique paths and ports.
52  */
53 struct GNUNET_TESTING_System;
54
55
56 /**
57  * Handle for a GNUnet peer controlled by testing.
58  */
59 struct GNUNET_TESTING_Peer;
60
61
62 /**
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
65  * availability.
66  *
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
73  *          in CIDR notation.
74  * @param hostname the hostname of the system we are using for testing; NULL for
75  *          localhost
76  * @return handle to this system, NULL on error
77  */
78 struct GNUNET_TESTING_System *
79 GNUNET_TESTING_system_create (const char *testdir,
80                               const char *trusted_ip,
81                               const char *hostname);
82
83
84 /**
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).
90  *
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
97  *          in CIDR notation.
98  * @param hostname the hostname of the system we are using for testing; NULL for
99  *          localhost
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
103  */
104 struct GNUNET_TESTING_System *
105 GNUNET_TESTING_system_create_with_portrange (const char *testdir,
106                                              const char *trusted_ip,
107                                              const char *hostname,
108                                              uint16_t lowport,
109                                              uint16_t highport);
110
111
112 /**
113  * Free system resources.
114  *
115  * @param system system to be freed
116  * @param remove_paths should the 'testdir' and all subdirectories
117  *        be removed (clean up on shutdown)?
118  */
119 void
120 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
121                                int remove_paths);
122
123
124 /**
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.
131  *
132  * This is primarily a helper function used internally
133  * by 'GNUNET_TESTING_peer_configure'.
134  *
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)
140  */
141 struct GNUNET_CRYPTO_RsaPrivateKey *
142 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
143                             uint32_t key_number,
144                             struct GNUNET_PeerIdentity *id);
145
146
147 /**
148  * Reserve a TCP or UDP port for a peer.
149  *
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
153  */
154 uint16_t 
155 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
156                              int is_tcp);
157
158
159 /**
160  * Release reservation of a TCP or UDP port for a peer
161  * (used during GNUNET_TESTING_peer_destroy).
162  *
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
166  */
167 void
168 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
169                              int is_tcp,
170                              uint16_t port);
171
172
173 /**
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.
180  *
181  * This is primarily a helper function used internally
182  * by 'GNUNET_TESTING_peer_configure'.
183  *
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
188  */
189 int
190 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
191                                      struct GNUNET_CONFIGURATION_Handle *cfg);
192 // FIXME: add dual to 'release' ports again...
193
194
195 /**
196  * Configure a GNUnet peer.  GNUnet must be installed on the local
197  * system and available in the PATH. 
198  *
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), 
205  *          can be NULL
206  * @return handle to the peer, NULL on error
207  */
208 struct GNUNET_TESTING_Peer *
209 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
210                                struct GNUNET_CONFIGURATION_Handle *cfg,
211                                uint32_t key_number,
212                                struct GNUNET_PeerIdentity *id,
213                                char **emsg);
214
215
216 /**
217  * Obtain the peer identity from a peer handle.
218  *
219  * @param peer peer handle for which we want the peer's identity
220  * @param id identifier for the daemon, will be set
221  */
222 void
223 GNUNET_TESTING_peer_get_identity (const struct GNUNET_TESTING_Peer *peer,
224                                   struct GNUNET_PeerIdentity *id);
225
226
227 /**
228  * Start the peer. 
229  *
230  * @param peer peer to start
231  * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
232  */
233 int
234 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
235
236
237 /**
238  * Stop the peer. 
239  *
240  * @param peer peer to stop
241  * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
242  */
243 int
244 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
245
246
247 /**
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).
251  *
252  * @param peer peer to destroy
253  */
254 void
255 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
256
257
258 /**
259  * Sends SIGTERM to the peer's main process
260  *
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
264  */
265 int
266 GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer);
267
268
269 /**
270  * Waits for a peer to terminate. The peer's main process will also be destroyed.
271  *
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
275  */
276 int
277 GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer);
278
279
280 /**
281  * Signature of the 'main' function for a (single-peer) testcase that
282  * is run using 'GNUNET_TESTING_peer_run'.
283  * 
284  * @param cls closure
285  * @param cfg configuration of the peer that was started
286  * @param peer identity of the peer that was created
287  */
288 typedef void (*GNUNET_TESTING_TestMain)(void *cls,
289                                         const struct GNUNET_CONFIGURATION_Handle *cfg,
290                                         struct GNUNET_TESTING_Peer *peer);
291
292
293 /**
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'.
299  *
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
308  */
309 int
310 GNUNET_TESTING_peer_run (const char *testdir,
311                          const char *cfgfilename,
312                          GNUNET_TESTING_TestMain tm,
313                          void *tm_cls);
314
315
316 /**
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'.
323  *
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.
326  *
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
336  */
337 int
338 GNUNET_TESTING_service_run (const char *testdir,
339                             const char *service_name,
340                             const char *cfgfilename,
341                             GNUNET_TESTING_TestMain tm,
342                             void *tm_cls);
343
344
345 /**
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.  
350  *
351  * This function returns the respective substring, taking care
352  * of issues such as binaries ending in '.exe' on W32.
353  *
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 '_' 
357  */
358 char *
359 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
360
361
362 #if 0                           /* keep Emacsens' auto-indent happy */
363 {
364 #endif
365 #ifdef __cplusplus
366 }
367 #endif
368
369 #endif