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