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