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