kill all peers first and wait for them later
[oweals/gnunet.git] / src / include / gnunet_testing_lib-new.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-new.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_NEW_H
34 #define GNUNET_TESTING_LIB_NEW_H
35
36 #include "gnunet_util_lib.h"
37 #include "gnunet_statistics_service.h"
38
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #if 0                           /* keep Emacsens' auto-indent happy */
43 }
44 #endif
45 #endif
46
47 #define GNUNET_TESTING_HOSTKEYFILESIZE 914
48
49 /**
50  * Handle for a system on which GNUnet peers are executed;
51  * a system is used for reserving unique paths and ports.
52  */
53 struct GNUNET_TESTING_System;
54
55
56 /**
57  * Handle for a GNUnet peer controlled by testing.
58  */
59 struct GNUNET_TESTING_Peer;
60
61
62 /**
63  * Create a system handle.  There must only be one system handle per operating
64  * system.  Uses a default range for allowed ports.  Ports are still tested for
65  * availability.
66  *
67  * @param testdir only the directory name without any path. This is used for all
68  *          service homes; the directory will be created in a temporary location
69  *          depending on the underlying OS
70  * @param controller hostname of the controlling host, service configurations
71  *        are modified to allow control connections from this host; can be NULL
72  * @param hostname the hostname of the system we are using for testing; NULL for
73  *          localhost
74  * @return handle to this system, NULL on error
75  */
76 struct GNUNET_TESTING_System *
77 GNUNET_TESTING_system_create (const char *testdir,
78                               const char *controller,
79                               const char *hostname);
80
81
82 /**
83  * Create a system handle.  There must only be one system
84  * handle per operating system.  Use this function directly
85  * if multiple system objects are created for the same host
86  * (only really useful when testing --- or to make the port
87  * range configureable).
88  *
89  * @param testdir only the directory name without any path. This is used for
90  *          all service homes; the directory will be created in a temporary
91  *          location depending on the underlying OS
92  * @param controller hostname of the controlling host, 
93  *        service configurations are modified to allow 
94  *        control connections from this host; can be NULL
95  * @param hostname the hostname of the system we are using for testing; NULL for
96  *          localhost
97  * @param lowport lowest port number this system is allowed to allocate (inclusive)
98  * @param highport highest port number this system is allowed to allocate (exclusive)
99  * @return handle to this system, NULL on error
100  */
101 struct GNUNET_TESTING_System *
102 GNUNET_TESTING_system_create_with_portrange (const char *testdir,
103                                              const char *controller,
104                                              const char *hostname,
105                                              uint16_t lowport,
106                                              uint16_t highport);
107
108
109 /**
110  * Free system resources.
111  *
112  * @param system system to be freed
113  * @param remove_paths should the 'testdir' and all subdirectories
114  *        be removed (clean up on shutdown)?
115  */
116 void
117 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
118                                int remove_paths);
119
120
121 /**
122  * Testing includes a number of pre-created hostkeys for
123  * faster peer startup.  This function can be used to
124  * access the n-th key of those pre-created hostkeys; note
125  * that these keys are ONLY useful for testing and not
126  * secure as the private keys are part of the public 
127  * GNUnet source code.
128  *
129  * This is primarily a helper function used internally
130  * by 'GNUNET_TESTING_peer_configure'.
131  *
132  * @param system the testing system handle
133  * @param key_number desired pre-created hostkey to obtain
134  * @param id set to the peer's identity (hash of the public
135  *        key; if NULL, GNUNET_SYSERR is returned immediately
136  * @return NULL on error (not enough keys)
137  */
138 struct GNUNET_CRYPTO_RsaPrivateKey *
139 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
140                             uint32_t key_number,
141                             struct GNUNET_PeerIdentity *id);
142
143
144 /**
145  * Reserve a TCP or UDP port for a peer.
146  *
147  * @param system system to use for reservation tracking
148  * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
149  * @return 0 if no free port was available
150  */
151 uint16_t 
152 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
153                              int is_tcp);
154
155
156 /**
157  * Release reservation of a TCP or UDP port for a peer
158  * (used during GNUNET_TESTING_peer_destroy).
159  *
160  * @param system system to use for reservation tracking
161  * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
162  * @param port reserved port to release
163  */
164 void
165 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
166                              int is_tcp,
167                              uint16_t port);
168
169
170 /**
171  * Create a new configuration using the given configuration as a template;
172  * ports and paths will be modified to select available ports on the local
173  * system. The default configuration will be available in PATHS section under
174  * the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS
175  * section to the temporary directory specific to this configuration. If we run
176  * out of "*port" numbers, return SYSERR.
177  *
178  * This is primarily a helper function used internally
179  * by 'GNUNET_TESTING_peer_configure'.
180  *
181  * @param system system to use to coordinate resource usage
182  * @param cfg template configuration to update
183  * @return GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will
184  *           be incomplete and should not be used there upon
185  */
186 int
187 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
188                                      struct GNUNET_CONFIGURATION_Handle *cfg);
189 // FIXME: add dual to 'release' ports again...
190
191
192 /**
193  * Configure a GNUnet peer.  GNUnet must be installed on the local
194  * system and available in the PATH. 
195  *
196  * @param system system to use to coordinate resource usage
197  * @param cfg configuration to use; will be UPDATED (to reflect needed
198  *            changes in port numbers and paths)
199  * @param key_number number of the hostkey to use for the peer
200  * @param id identifier for the daemon, will be set, can be NULL
201  * @param emsg set to freshly allocated error message (set to NULL on success), 
202  *          can be NULL
203  * @return handle to the peer, NULL on error
204  */
205 struct GNUNET_TESTING_Peer *
206 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
207                                struct GNUNET_CONFIGURATION_Handle *cfg,
208                                uint32_t key_number,
209                                struct GNUNET_PeerIdentity *id,
210                                char **emsg);
211
212
213 /**
214  * Obtain the peer identity from a peer handle.
215  *
216  * @param peer peer handle for which we want the peer's identity
217  * @param id identifier for the daemon, will be set
218  */
219 void
220 GNUNET_TESTING_peer_get_identity (const struct GNUNET_TESTING_Peer *peer,
221                                   struct GNUNET_PeerIdentity *id);
222
223
224 /**
225  * Start the peer. 
226  *
227  * @param peer peer to start
228  * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
229  */
230 int
231 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
232
233
234 /**
235  * Stop the peer. 
236  *
237  * @param peer peer to stop
238  * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
239  */
240 int
241 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
242
243
244 /**
245  * Destroy the peer.  Releases resources locked during peer configuration.
246  * If the peer is still running, it will be stopped AND a warning will be
247  * printed (users of the API should stop the peer explicitly first).
248  *
249  * @param peer peer to destroy
250  */
251 void
252 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
253
254
255 /**
256  * Sends SIGTERM to the peer's main process
257  *
258  * @param peer the handle to the peer
259  * @return GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL
260  *           or upon any error while sending SIGTERM
261  */
262 int
263 GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer);
264
265
266 /**
267  * Waits for a peer to terminate. The peer's main process will also be destroyed.
268  *
269  * @param peer the handle to the peer
270  * @return GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL
271  *           or upon any error while waiting
272  */
273 int
274 GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer);
275
276
277 /**
278  * Signature of the 'main' function for a (single-peer) testcase that
279  * is run using 'GNUNET_TESTING_peer_run'.
280  * 
281  * @param cls closure
282  * @param cfg configuration of the peer that was started
283  * @param peer identity of the peer that was created
284  */
285 typedef void (*GNUNET_TESTING_TestMain)(void *cls,
286                                         const struct GNUNET_CONFIGURATION_Handle *cfg,
287                                         struct GNUNET_TESTING_Peer *peer);
288
289
290 /**
291  * Start a single peer and run a test using the testing library.
292  * Starts a peer using the given configuration and then invokes the
293  * given callback.  This function ALSO initializes the scheduler loop
294  * and should thus be called directly from "main".  The testcase
295  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
296  *
297  * @param testdir only the directory name without any path. This is used for
298  *          all service homes; the directory will be created in a temporary
299  *          location depending on the underlying OS
300  * @param cfgfilename name of the configuration file to use;
301  *         use NULL to only run with defaults
302  * @param tm main function of the testcase
303  * @param tm_cls closure for 'tm'
304  * @return 0 on success, 1 on error
305  */
306 int
307 GNUNET_TESTING_peer_run (const char *testdir,
308                          const char *cfgfilename,
309                          GNUNET_TESTING_TestMain tm,
310                          void *tm_cls);
311
312
313 /**
314  * Start a single service (no ARM, except of course if the given
315  * service name is 'arm') and run a test using the testing library.
316  * Starts a service using the given configuration and then invokes the
317  * given callback.  This function ALSO initializes the scheduler loop
318  * and should thus be called directly from "main".  The testcase
319  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
320  *
321  * This function is useful if the testcase is for a single service
322  * and if that service doesn't itself depend on other services.
323  *
324  * @param testdir only the directory name without any path. This is used for
325  *          all service homes; the directory will be created in a temporary
326  *          location depending on the underlying OS
327  * @param service_name name of the service to run
328  * @param cfgfilename name of the configuration file to use;
329  *         use NULL to only run with defaults
330  * @param tm main function of the testcase
331  * @param tm_cls closure for 'tm'
332  * @return 0 on success, 1 on error
333  */
334 int
335 GNUNET_TESTING_service_run (const char *testdir,
336                             const char *service_name,
337                             const char *cfgfilename,
338                             GNUNET_TESTING_TestMain tm,
339                             void *tm_cls);
340
341
342 /**
343  * Sometimes we use the binary name to determine which specific
344  * test to run.  In those cases, the string after the last "_"
345  * in 'argv[0]' specifies a string that determines the configuration
346  * file or plugin to use.  
347  *
348  * This function returns the respective substring, taking care
349  * of issues such as binaries ending in '.exe' on W32.
350  *
351  * @param argv0 the name of the binary
352  * @return string between the last '_' and the '.exe' (or the end of the string),
353  *         NULL if argv0 has no '_' 
354  */
355 char *
356 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
357
358
359 #if 0                           /* keep Emacsens' auto-indent happy */
360 {
361 #endif
362 #ifdef __cplusplus
363 }
364 #endif
365
366 #endif