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