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