639eff018e931e98b0971474824db034df7d1f81
[oweals/gnunet.git] / src / transport / transport-testing.h
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2009 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 2, 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 transport-testing.h
23  * @brief testing lib for transport service
24  *
25  * @author Matthias Wachs
26  */
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_container_lib.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_testing_lib-new.h"
37
38
39 #define GNUNET_TRANSPORT_TESTING_ConnectRequest void *
40
41
42 /**
43  * Context for a single peer
44  */
45 struct PeerContext;
46
47 /**
48  * Callback when two peers are connected and both have called the connect callback
49  * to notify clients about a new peer
50  */
51 typedef void (*GNUNET_TRANSPORT_TESTING_start_cb) (struct PeerContext * p,
52                                                    void *cls);
53
54 /**
55  * Callback when two peers are connected and both have called the connect callback
56  * to notify clients about a new peer
57  */
58 typedef void (*GNUNET_TRANSPORT_TESTING_connect_cb) (struct PeerContext * p1,
59                                                      struct PeerContext * p2,
60                                                      void *cls);
61
62
63 /**
64  * Definition for a transport testing handle
65  */
66 struct GNUNET_TRANSPORT_TESTING_handle;
67
68 /**
69  * Context for a single peer
70  */
71 struct PeerContext
72 {
73   /**
74    * Next element in the DLL
75    */
76   struct PeerContext *next;
77
78   /**
79    * Previous element in the DLL
80    */
81   struct PeerContext *prev;
82
83   /**
84    * Transport testing handle this peer belongs to
85    */
86   struct GNUNET_TRANSPORT_TESTING_handle *tth;
87
88   /**
89    * Peer's configuration
90    */
91   struct GNUNET_CONFIGURATION_Handle *cfg;
92
93   /**
94    * Peer's transport service handle
95    */
96   struct GNUNET_TRANSPORT_Handle *th;
97
98   /**
99    * Peer's transport get hello handle to retrieve peer's HELLO message
100    */
101   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
102
103   /**
104    * Peer's testing handle
105    */
106   struct GNUNET_TESTING_Peer *peer;
107
108   /**
109    * Peer identity
110    */
111   struct GNUNET_PeerIdentity id;
112
113   /**
114    * Handle for the peer's ARM process
115    */
116   struct GNUNET_OS_Process *arm_proc;
117
118   /**
119    * Receive callback
120    */
121   GNUNET_TRANSPORT_ReceiveCallback rec;
122
123   /**
124    * Notify connect callback
125    */
126   GNUNET_TRANSPORT_NotifyConnect nc;
127
128   /**
129    * Notify disconnect callback
130    */
131   GNUNET_TRANSPORT_NotifyDisconnect nd;
132
133   /**
134    * Startup completed callback
135    */
136   GNUNET_TRANSPORT_TESTING_start_cb start_cb;
137
138   /**
139    * Peers HELLO Message
140    */
141   struct GNUNET_HELLO_Message *hello;
142
143   /**
144    * Closure for the callbacks
145    */
146   void *cb_cls;
147
148   /**
149    * Peer's service home directory
150    */
151   char *servicehome;
152
153   /**
154    * Hostkey file
155    */
156   char *hostkeyfile;
157
158   /**
159    * An unique number to identify the peer
160    */
161   unsigned int no;
162 };
163
164
165 struct ConnectingContext
166 {
167   struct ConnectingContext *next;
168   struct ConnectingContext *prev;
169   struct PeerContext *p1;
170   struct PeerContext *p2;
171   GNUNET_SCHEDULER_TaskIdentifier tct;
172   GNUNET_TRANSPORT_TESTING_connect_cb cb;
173   void *cb_cls;
174   struct GNUNET_TRANSPORT_Handle *th_p1;
175   struct GNUNET_TRANSPORT_Handle *th_p2;
176   int p1_c;
177   int p2_c;
178 };
179
180 struct GNUNET_TRANSPORT_TESTING_handle
181 {
182   struct ConnectingContext *cc_head;
183   struct ConnectingContext *cc_tail;
184
185   char *hostkey_data;
186   int hostkeys_total;
187   int hostkeys_last;
188
189   struct PeerContext *p_head;
190   struct PeerContext *p_tail;
191 };
192
193
194 /**
195 * Start a peer with the given configuration
196 * @param tth the testing handle
197 * @param cfgname configuration file
198 * @param peer_id the peer_id
199 * @param rec receive callback
200 * @param nc connect callback
201 * @param nd disconnect callback
202 * @param start_cb start callback
203 * @param cb_cls closure for callback
204 * @return the peer context
205 */
206 struct PeerContext *
207 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
208                                      *tth, const char *cfgname, int peer_id,
209                                      GNUNET_TRANSPORT_ReceiveCallback rec,
210                                      GNUNET_TRANSPORT_NotifyConnect nc,
211                                      GNUNET_TRANSPORT_NotifyDisconnect nd,
212                                      GNUNET_TRANSPORT_TESTING_start_cb start_cb,
213                                      void *cb_cls);
214
215
216 /**
217  * shutdown the given peer
218  * @param tth the testing handle
219  * @param p the peer
220  */
221
222 void
223 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
224                                     struct PeerContext *pc);
225
226
227 /**
228 * Restart the given peer
229 * @param tth testing handle
230 * @param p the peer
231 * @param cfgname the cfg file used to restart
232 * @param restart_cb restart callback
233 * @param cb_cls callback closure
234 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
235 */
236 int
237 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
238                                        *tth, struct PeerContext *p,
239                                        const char *cfgname,
240                                        GNUNET_TRANSPORT_TESTING_start_cb
241                                        restart_cb, void *cb_cls);
242
243 /**
244  * Connect the given peers and call the callback when both peers report the
245  * inbound connection. Remarks: start_peer's notify_connect callback can be called
246  * before.
247  *
248  * @param tth transport testing handle
249  * @param p1 peer 1
250  * @param p2 peer 2
251  * @param cb the callback to call when both peers notified that they are connected
252  * @param cls callback cls
253  * @return a connect request handle
254  */
255 GNUNET_TRANSPORT_TESTING_ConnectRequest
256 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
257                                         struct PeerContext *p1,
258                                         struct PeerContext *p2,
259                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
260                                         void *cls);
261
262 /**
263  * Cancel the request to connect two peers
264  * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
265  * @param tth testing
266  * @param ccr a connect request handle
267  */
268 void
269 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
270                                                GNUNET_TRANSPORT_TESTING_handle
271                                                *tth,
272                                                GNUNET_TRANSPORT_TESTING_ConnectRequest
273                                                ccr);
274
275 /**
276  * Clean up the transport testing
277  * @param tth transport testing handle
278  */
279 void
280 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth);
281
282 /**
283  * Initialize the transport testing
284  * @return transport testing handle
285  */
286 struct GNUNET_TRANSPORT_TESTING_handle *
287 GNUNET_TRANSPORT_TESTING_init ();
288
289 /*
290  * Some utility functions
291  */
292
293 /**
294  * Extracts the test filename from an absolute file name and removes the extension
295  * @param file absolute file name
296  * @param dest where to store result
297  */
298 void
299 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest);
300
301 /**
302  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
303  * if existing ".exe"-prefix and adds the peer-number
304  *
305  * @param file filename of the test, e.g. argv[0]
306  * @param dest where to write the filename
307  * @param count peer number
308  */
309 void
310 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
311                                           int count);
312
313
314 /**
315  * Extracts the plugin anme from an absolute file name and the test name
316  * @param file absolute file name
317  * @param test test name
318  * @param dest where to store result
319  */
320 void
321 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
322                                                const char *testname,
323                                                char **pluginname);
324
325
326 /**
327  * Extracts the filename from an absolute file name and removes the extenstion
328  * @param file absolute file name
329  * @param dest where to store result
330  */
331 void
332 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file,
333                                                char **testname);
334
335 /* end of transport_testing.h */