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