- prefix env variable for specifying the tmp dir testbed/testing uses for dumping...
[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 #include "gnunet_arm_service.h"
39
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #if 0                           /* keep Emacsens' auto-indent happy */
44 }
45 #endif
46 #endif
47
48 /**
49  * Size of each hostkey in the hostkey file (in BYTES).
50  */
51 #define GNUNET_TESTING_HOSTKEYFILESIZE sizeof (struct GNUNET_CRYPTO_EccPrivateKey)
52
53 /**
54  * Handle for a system on which GNUnet peers are executed;
55  * a system is used for reserving unique paths and ports.
56  */
57 struct GNUNET_TESTING_System;
58
59
60 /**
61  * Handle for a GNUnet peer controlled by testing.
62  */
63 struct GNUNET_TESTING_Peer;
64
65
66 /**
67  * Specification of a service that is to be shared among peers
68  */
69 struct GNUNET_TESTING_SharedService
70 {
71   /**
72    * The name of the service.
73    */
74   const char *service;
75
76   /**
77    * The configuration template for the service.  Cannot be NULL
78    */
79   const struct GNUNET_CONFIGURATION_Handle *cfg;
80
81   /**
82    * The number of peers which share an instance of the service.  0 for sharing
83    * among all peers
84    */
85   unsigned int share;
86 };
87
88
89 /**
90  * Create a system handle.  There must only be one system handle per operating
91  * system.  Uses a default range for allowed ports.  Ports are still tested for
92  * availability.
93  *
94  * @param testdir only the directory name without any path. This is used for all
95  *          service homes; the directory will be created in a temporary location
96  *          depending on the underlying OS.  This variable will be
97  *          overridden with the value of the environmental variable
98  * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
99  *          service configurations generated to allow control connections from
100  *          this ip. This can either be a single ip address or a network address
101  *          in CIDR notation.
102  * @param hostname the hostname of the system we are using for testing; NULL for
103  *          localhost
104  * @param shared_services NULL terminated array describing services that are to
105  *          be shared among peers
106  * @return handle to this system, NULL on error
107  */
108 struct GNUNET_TESTING_System *
109 GNUNET_TESTING_system_create (const char *testdir,
110                               const char *trusted_ip,
111                               const char *hostname,
112                               const struct GNUNET_TESTING_SharedService *
113                               shared_services);
114
115
116 /**
117  * Create a system handle.  There must only be one system
118  * handle per operating system.  Use this function directly
119  * if multiple system objects are created for the same host
120  * (only really useful when testing --- or to make the port
121  * range configureable).
122  *
123  * @param testdir only the directory name without any path. This is used for
124  *          all service homes; the directory will be created in a temporary
125  *          location depending on the underlying OS.  This variable will be
126  *          overridden with the value of the environmental variable
127  * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
128  *          service configurations generated to allow control connections from
129  *          this ip. This can either be a single ip address or a network address
130  *          in CIDR notation.
131  * @param hostname the hostname of the system we are using for testing; NULL for
132  *          localhost
133  * @param shared_services NULL terminated array describing services that are to
134  *          be shared among peers
135  * @param lowport lowest port number this system is allowed to allocate (inclusive)
136  * @param highport highest port number this system is allowed to allocate (exclusive)
137  * @return handle to this system, NULL on error
138  */
139 struct GNUNET_TESTING_System *
140 GNUNET_TESTING_system_create_with_portrange (const char *testdir,
141                                              const char *trusted_ip,
142                                              const char *hostname,
143                                              const struct
144                                              GNUNET_TESTING_SharedService *
145                                              shared_services,
146                                              uint16_t lowport,
147                                              uint16_t highport);
148
149
150 /**
151  * Free system resources.
152  *
153  * @param system system to be freed
154  * @param remove_paths should the 'testdir' and all subdirectories
155  *        be removed (clean up on shutdown)?
156  */
157 void
158 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
159                                int remove_paths);
160
161
162 /**
163  * Testing includes a number of pre-created hostkeys for
164  * faster peer startup.  This function can be used to
165  * access the n-th key of those pre-created hostkeys; note
166  * that these keys are ONLY useful for testing and not
167  * secure as the private keys are part of the public
168  * GNUnet source code.
169  *
170  * This is primarily a helper function used internally
171  * by 'GNUNET_TESTING_peer_configure'.
172  *
173  * @param system the testing system handle
174  * @param key_number desired pre-created hostkey to obtain
175  * @param id set to the peer's identity (hash of the public
176  *        key; if NULL, GNUNET_SYSERR is returned immediately
177  * @return NULL on error (not enough keys)
178  */
179 struct GNUNET_CRYPTO_EccPrivateKey *
180 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
181                             uint32_t key_number,
182                             struct GNUNET_PeerIdentity *id);
183
184
185 /**
186  * Reserve a port for a peer.
187  *
188  * @param system system to use for reservation tracking
189  * @return 0 if no free port was available
190  */
191 uint16_t
192 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system);
193
194
195 /**
196  * Release reservation of a TCP or UDP port for a peer
197  * (used during GNUNET_TESTING_peer_destroy).
198  *
199  * @param system system to use for reservation tracking
200  * @param port reserved port to release
201  */
202 void
203 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
204                              uint16_t port);
205
206
207 /**
208  * Create a new configuration using the given configuration as a template;
209  * ports and paths will be modified to select available ports on the local
210  * system. The default configuration will be available in PATHS section under
211  * the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS
212  * section to the temporary directory specific to this configuration. If we run
213  * out of "*port" numbers, return SYSERR.
214  *
215  * This is primarily a helper function used internally
216  * by 'GNUNET_TESTING_peer_configure'.
217  *
218  * @param system system to use to coordinate resource usage
219  * @param cfg template configuration to update
220  * @return GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will
221  *           be incomplete and should not be used there upon
222  */
223 int
224 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
225                                      struct GNUNET_CONFIGURATION_Handle *cfg);
226 // FIXME: add dual to 'release' ports again...
227
228
229 /**
230  * Configure a GNUnet peer.  GNUnet must be installed on the local
231  * system and available in the PATH.
232  *
233  * @param system system to use to coordinate resource usage
234  * @param cfg configuration to use; will be UPDATED (to reflect needed
235  *            changes in port numbers and paths)
236  * @param key_number number of the hostkey to use for the peer
237  * @param id identifier for the daemon, will be set, can be NULL
238  * @param emsg set to freshly allocated error message (set to NULL on success),
239  *          can be NULL
240  * @return handle to the peer, NULL on error
241  */
242 struct GNUNET_TESTING_Peer *
243 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
244                                struct GNUNET_CONFIGURATION_Handle *cfg,
245                                uint32_t key_number,
246                                struct GNUNET_PeerIdentity *id,
247                                char **emsg);
248
249
250 /**
251  * Obtain the peer identity from a peer handle.
252  *
253  * @param peer peer handle for which we want the peer's identity
254  * @param id identifier for the daemon, will be set
255  */
256 void
257 GNUNET_TESTING_peer_get_identity (struct GNUNET_TESTING_Peer *peer,
258                                   struct GNUNET_PeerIdentity *id);
259
260
261 /**
262  * Start the peer.
263  *
264  * @param peer peer to start
265  * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
266  */
267 int
268 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
269
270
271 /**
272  * Stop the peer. This call is blocking as it kills the peer's main ARM process
273  * by sending a SIGTERM and waits on it.  For asynchronous shutdown of peer, see
274  * GNUNET_TESTING_peer_stop_async().
275  *
276  * @param peer peer to stop
277  * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
278  */
279 int
280 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
281
282
283 /**
284  * Destroy the peer.  Releases resources locked during peer configuration.
285  * If the peer is still running, it will be stopped AND a warning will be
286  * printed (users of the API should stop the peer explicitly first).
287  *
288  * @param peer peer to destroy
289  */
290 void
291 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
292
293
294 /**
295  * Sends SIGTERM to the peer's main process
296  *
297  * @param peer the handle to the peer
298  * @return GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL
299  *           or upon any error while sending SIGTERM
300  */
301 int
302 GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer);
303
304
305 /**
306  * Waits for a peer to terminate. The peer's main process will also be destroyed.
307  *
308  * @param peer the handle to the peer
309  * @return GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL
310  *           or upon any error while waiting
311  */
312 int
313 GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer);
314
315
316 /**
317  * Callback to inform whether the peer is running or stopped.
318  *
319  * @param cls the closure given to GNUNET_TESTING_peer_stop_async()
320  * @param peer the respective peer whose status is being reported
321  * @param success GNUNET_YES if the peer is stopped; GNUNET_SYSERR upon any
322  *          error
323  */
324 typedef void (*GNUNET_TESTING_PeerStopCallback) (void *cls,
325                                                  struct GNUNET_TESTING_Peer *
326                                                  peer,
327                                                  int success);
328
329
330 /**
331  * Stop a peer asynchronously using ARM API.  Peer's shutdown is signaled
332  * through the GNUNET_TESTING_PeerStopCallback().
333  *
334  * @param peer the peer to stop
335  * @param cb the callback to signal peer shutdown
336  * @param cb_cls closure for the above callback
337  * @return GNUNET_OK upon successfully giving the request to the ARM API (this
338  *           does not mean that the peer is successfully stopped); GNUNET_SYSERR
339  *           upon any error.
340  */
341 int
342 GNUNET_TESTING_peer_stop_async (struct GNUNET_TESTING_Peer *peer,
343                                 GNUNET_TESTING_PeerStopCallback cb,
344                                 void *cb_cls);
345
346
347 /**
348  * Cancel a previous asynchronous peer stop request.
349  * GNUNET_TESTING_peer_stop_async() should have been called before on the given
350  * peer.  It is an error to call this function if the peer stop callback was
351  * already called
352  *
353  * @param peer the peer on which GNUNET_TESTING_peer_stop_async() was called
354  *          before.
355  */
356 void
357 GNUNET_TESTING_peer_stop_async_cancel (struct GNUNET_TESTING_Peer *peer);
358
359
360 /**
361  * Signature of the 'main' function for a (single-peer) testcase that
362  * is run using 'GNUNET_TESTING_peer_run'.
363  *
364  * @param cls closure
365  * @param cfg configuration of the peer that was started
366  * @param peer identity of the peer that was created
367  */
368 typedef void (*GNUNET_TESTING_TestMain) (void *cls,
369                                          const struct GNUNET_CONFIGURATION_Handle *cfg,
370                                          struct GNUNET_TESTING_Peer *peer);
371
372
373 /**
374  * Start a single peer and run a test using the testing library.
375  * Starts a peer using the given configuration and then invokes the
376  * given callback.  This function ALSO initializes the scheduler loop
377  * and should thus be called directly from "main".  The testcase
378  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
379  *
380  * @param testdir only the directory name without any path. This is used for
381  *          all service homes; the directory will be created in a temporary
382  *          location depending on the underlying OS
383  * @param cfgfilename name of the configuration file to use;
384  *         use NULL to only run with defaults
385  * @param tm main function of the testcase
386  * @param tm_cls closure for 'tm'
387  * @return 0 on success, 1 on error
388  */
389 int
390 GNUNET_TESTING_peer_run (const char *testdir,
391                          const char *cfgfilename,
392                          GNUNET_TESTING_TestMain tm,
393                          void *tm_cls);
394
395
396 /**
397  * Start a single service (no ARM, except of course if the given
398  * service name is 'arm') and run a test using the testing library.
399  * Starts a service using the given configuration and then invokes the
400  * given callback.  This function ALSO initializes the scheduler loop
401  * and should thus be called directly from "main".  The testcase
402  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
403  *
404  * This function is useful if the testcase is for a single service
405  * and if that service doesn't itself depend on other services.
406  *
407  * @param testdir only the directory name without any path. This is used for
408  *          all service homes; the directory will be created in a temporary
409  *          location depending on the underlying OS
410  * @param service_name name of the service to run
411  * @param cfgfilename name of the configuration file to use;
412  *         use NULL to only run with defaults
413  * @param tm main function of the testcase
414  * @param tm_cls closure for 'tm'
415  * @return 0 on success, 1 on error
416  */
417 int
418 GNUNET_TESTING_service_run (const char *testdir,
419                             const char *service_name,
420                             const char *cfgfilename,
421                             GNUNET_TESTING_TestMain tm,
422                             void *tm_cls);
423
424
425 /**
426  * Sometimes we use the binary name to determine which specific
427  * test to run.  In those cases, the string after the last "_"
428  * in 'argv[0]' specifies a string that determines the configuration
429  * file or plugin to use.
430  *
431  * This function returns the respective substring, taking care
432  * of issues such as binaries ending in '.exe' on W32.
433  *
434  * @param argv0 the name of the binary
435  * @return string between the last '_' and the '.exe' (or the end of the string),
436  *         NULL if argv0 has no '_'
437  */
438 char *
439 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
440
441
442 #if 0                           /* keep Emacsens' auto-indent happy */
443 {
444 #endif
445 #ifdef __cplusplus
446 }
447 #endif
448
449 #endif