97be8211c166ad852668753be5c0077665f36c58
[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  * Signature of the 'main' function for a (single-peer) testcase that
257  * is run using 'GNUNET_TESTING_peer_run'.
258  * 
259  * @param cls closure
260  * @param cfg configuration of the peer that was started
261  * @param peer identity of the peer that was created
262  */
263 typedef void (*GNUNET_TESTING_TestMain)(void *cls,
264                                         const struct GNUNET_CONFIGURATION_Handle *cfg,
265                                         struct GNUNET_TESTING_Peer *peer);
266
267
268 /**
269  * Start a single peer and run a test using the testing library.
270  * Starts a peer using the given configuration and then invokes the
271  * given callback.  This function ALSO initializes the scheduler loop
272  * and should thus be called directly from "main".  The testcase
273  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
274  *
275  * @param testdir only the directory name without any path. This is used for
276  *          all service homes; the directory will be created in a temporary
277  *          location depending on the underlying OS
278  * @param cfgfilename name of the configuration file to use;
279  *         use NULL to only run with defaults
280  * @param tm main function of the testcase
281  * @param tm_cls closure for 'tm'
282  * @return 0 on success, 1 on error
283  */
284 int
285 GNUNET_TESTING_peer_run (const char *testdir,
286                          const char *cfgfilename,
287                          GNUNET_TESTING_TestMain tm,
288                          void *tm_cls);
289
290
291 /**
292  * Start a single service (no ARM, except of course if the given
293  * service name is 'arm') and run a test using the testing library.
294  * Starts a service using the given configuration and then invokes the
295  * given callback.  This function ALSO initializes the scheduler loop
296  * and should thus be called directly from "main".  The testcase
297  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
298  *
299  * This function is useful if the testcase is for a single service
300  * and if that service doesn't itself depend on other services.
301  *
302  * @param testdir only the directory name without any path. This is used for
303  *          all service homes; the directory will be created in a temporary
304  *          location depending on the underlying OS
305  * @param service_name name of the service to run
306  * @param cfgfilename name of the configuration file to use;
307  *         use NULL to only run with defaults
308  * @param tm main function of the testcase
309  * @param tm_cls closure for 'tm'
310  * @return 0 on success, 1 on error
311  */
312 int
313 GNUNET_TESTING_service_run (const char *testdir,
314                             const char *service_name,
315                             const char *cfgfilename,
316                             GNUNET_TESTING_TestMain tm,
317                             void *tm_cls);
318
319
320 /**
321  * Sometimes we use the binary name to determine which specific
322  * test to run.  In those cases, the string after the last "_"
323  * in 'argv[0]' specifies a string that determines the configuration
324  * file or plugin to use.  
325  *
326  * This function returns the respective substring, taking care
327  * of issues such as binaries ending in '.exe' on W32.
328  *
329  * @param argv0 the name of the binary
330  * @return string between the last '_' and the '.exe' (or the end of the string),
331  *         NULL if argv0 has no '_' 
332  */
333 char *
334 GNUNET_TESTING_get_testname_from_underscore (const char *argv0);
335
336
337 #if 0                           /* keep Emacsens' auto-indent happy */
338 {
339 #endif
340 #ifdef __cplusplus
341 }
342 #endif
343
344 #endif