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