-draft of new low-level testing API
[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  * Free system resources.
78  *
79  * @param system system to be freed
80  */
81 void
82 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system);
83
84
85 /**
86  * Testing includes a number of pre-created hostkeys for
87  * faster peer startup.  This function can be used to
88  * access the n-th key of those pre-created hostkeys; note
89  * that these keys are ONLY useful for testing and not
90  * secure as the private keys are part of the public 
91  * GNUnet source code.
92  *
93  * This is primarily a helper function used internally
94  * by 'GNUNET_TESTING_peer_configure'.
95  *
96  * @param key_number desired pre-created hostkey to obtain
97  * @param filename where to store the hostkey (file will
98  *        be created, or overwritten if it already exists)
99  * @param id set to the peer's identity (hash of the public
100  *        key; can be NULL
101  * @return GNUNET_SYSERR on error (not enough keys)
102  */
103 int
104 GNUNET_TESTING_hostkey_get (uint32_t key_number,
105                             const char *filename,
106                             struct GNUNET_PeerIdentity *id);
107
108
109
110 /**
111  * Create a new configuration using the given configuration
112  * as a template; ports and paths will be modified to select
113  * available ports on the local system.  If we run
114  * out of "*port" numbers, return SYSERR.
115  *
116  * This is primarily a helper function used internally
117  * by 'GNUNET_TESTING_peer_configure'.
118  *
119  * @param system system to use to coordinate resource usage
120  * @param cfg template configuration to update
121  * @return GNUNET_OK on success, GNUNET_SYSERR on error
122  */
123 int
124 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
125                                      struct GNUNET_CONFIGURATION_Handle *cfg);
126
127
128 /**
129  * Configure a GNUnet peer.  GNUnet must be installed on the local
130  * system and available in the PATH. 
131  *
132  * @param system system to use to coordinate resource usage
133  * @param cfg configuration to use; will be UPDATED (to reflect needed
134  *            changes in port numbers and paths)
135  * @param key_number number of the hostkey to use for the peer
136  * @param id identifier for the daemon, will be set, can be NULL
137  * @param emsg set to error message (set to NULL on success), can be NULL
138  * @return handle to the peer, NULL on error
139  */
140 struct GNUNET_TESTING_Peer *
141 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
142                                struct GNUNET_CONFIGURATION_Handle *cfg,
143                                uint32_t key_number,
144                                struct GNUNET_PeerIdentity *id,
145                                char **emsg);
146
147
148 /**
149  * Start the peer. 
150  *
151  * @param peer peer to start
152  * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
153  */
154 int
155 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer);
156
157
158 /**
159  * Stop the peer. 
160  *
161  * @param peer peer to stop
162  * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
163  */
164 int
165 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer);
166
167
168 /**
169  * Destroy the peer.  Releases resources locked during peer configuration.
170  * If the peer is still running, it will be stopped AND a warning will be
171  * printed (users of the API should stop the peer explicitly first).
172  *
173  * @param peer peer to destroy
174  */
175 void
176 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer);
177
178
179 /**
180  * Signature of the 'main' function for a (single-peer) testcase that
181  * is run using 'GNUNET_TESTING_peer_run'.
182  * 
183  * @param cls closure
184  * @param cfg configuration of the peer that was started
185  */
186 typedef void (*GNUNET_TESTING_TestMain)(void *cls,
187                                         const struct GNUNET_CONFIGURATION_Handle *cfg);
188
189
190 /**
191  * Start a single peer and run a test using the testing library.
192  * Starts a peer using the given configuration and then invokes the
193  * given callback.  This function ALSO initializes the scheduler loop
194  * and should thus be called directly from "main".  The testcase
195  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
196  *
197  * @param tmppath path for storing temporary data for the test
198  * @param cfgfilename name of the configuration file to use;
199  *         use NULL to only run with defaults
200  * @param tm main function of the testcase
201  * @param tm_cls closure for 'tm'
202  * @return 0 on success, 1 on error
203  */
204 int
205 GNUNET_TESTING_peer_run (const char *tmppath,
206                          const char *cfgfilename,
207                          GNUNET_TESTING_TestMain tm,
208                          void *tm_cls);
209
210
211
212 /**
213  * Start a single service (no ARM, except of course if the given
214  * service name is 'arm') and run a test using the testing library.
215  * Starts a service using the given configuration and then invokes the
216  * given callback.  This function ALSO initializes the scheduler loop
217  * and should thus be called directly from "main".  The testcase
218  * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
219  *
220  * This function is useful if the testcase is for a single service
221  * and if that service doesn't itself depend on other services.
222  *
223  * @param tmppath path for storing temporary data for the test
224  * @param service_name name of the service to run
225  * @param cfgfilename name of the configuration file to use;
226  *         use NULL to only run with defaults
227  * @param tm main function of the testcase
228  * @param tm_cls closure for 'tm'
229  * @return 0 on success, 1 on error
230  */
231 int
232 GNUNET_TESTING_service_run (const char *tmppath,
233                             const char *service_name,
234                             const char *cfgfilename,
235                             GNUNET_TESTING_TestMain tm,
236                             void *tm_cls);
237
238
239
240 #if 0                           /* keep Emacsens' auto-indent happy */
241 {
242 #endif
243 #ifdef __cplusplus
244 }
245 #endif
246
247 #endif