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