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