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