dcac29185097163f54f7025a59aafab5e5242bc6
[oweals/gnunet.git] / src / transport / transport-testing.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2009, 2015 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., 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 transport get hello handle to retrieve peer's HELLO message
97    */
98   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
99
100   /**
101    * Peer's testing handle
102    */
103   struct GNUNET_TESTING_Peer *peer;
104
105   /**
106    * Peer identity
107    */
108   struct GNUNET_PeerIdentity id;
109
110   /**
111    * Handle for the peer's ARM process
112    */
113   struct GNUNET_OS_Process *arm_proc;
114
115   /**
116    * Receive callback
117    */
118   GNUNET_TRANSPORT_ReceiveCallback rec;
119
120   /**
121    * Notify connect callback
122    */
123   GNUNET_TRANSPORT_NotifyConnect nc;
124
125   /**
126    * Notify disconnect callback
127    */
128   GNUNET_TRANSPORT_NotifyDisconnect nd;
129
130   /**
131    * Startup completed callback
132    */
133   GNUNET_TRANSPORT_TESTING_start_cb start_cb;
134
135   /**
136    * Peers HELLO Message
137    */
138   struct GNUNET_HELLO_Message *hello;
139
140   /**
141    * Closure for the callbacks
142    */
143   void *cb_cls;
144
145   /**
146    * An unique number to identify the peer
147    */
148   unsigned int no;
149 };
150
151
152 struct GNUNET_TRANSPORT_TESTING_ConnectRequest
153 {
154   struct GNUNET_TRANSPORT_TESTING_ConnectRequest *next;
155   struct GNUNET_TRANSPORT_TESTING_ConnectRequest *prev;
156   struct PeerContext *p1;
157   struct PeerContext *p2;
158   struct GNUNET_SCHEDULER_Task *tct;
159   GNUNET_TRANSPORT_TESTING_connect_cb cb;
160   void *cb_cls;
161   struct GNUNET_TRANSPORT_Handle *th_p1;
162   struct GNUNET_TRANSPORT_Handle *th_p2;
163   int p1_c;
164   int p2_c;
165 };
166
167 struct GNUNET_TRANSPORT_TESTING_handle
168 {
169   /**
170    * Testing library system handle
171    */
172   struct GNUNET_TESTING_System *tl_system;
173
174   /**
175    * head DLL of connect contexts
176    */
177   struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_head;
178
179   /**
180    * head DLL of connect contexts
181    */
182   struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_tail;
183
184   /**
185    * head DLL of peers
186    */
187   struct PeerContext *p_head;
188
189   /**
190    * tail DLL of peers
191    */
192   struct PeerContext *p_tail;
193 };
194
195
196 /**
197  * Start a peer with the given configuration
198  *
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 *tth,
211                                      const char *cfgname,
212                                      int peer_id,
213                                      GNUNET_TRANSPORT_ReceiveCallback rec,
214                                      GNUNET_TRANSPORT_NotifyConnect nc,
215                                      GNUNET_TRANSPORT_NotifyDisconnect nd,
216                                      GNUNET_TRANSPORT_TESTING_start_cb start_cb,
217                                      void *cb_cls);
218
219
220 /**
221  * shutdown the given peer
222  *
223  * @param tth the testing handle
224  * @param p the peer
225  */
226 void
227 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
228                                     struct PeerContext *pc);
229
230
231 /**
232  * Restart the given peer
233  *
234  * @param tth testing handle
235  * @param p the peer
236  * @param cfgname the cfg file used to restart
237  * @param restart_cb restart callback
238  * @param cb_cls callback closure
239  * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
240  */
241 int
242 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
243                                        struct PeerContext *p,
244                                        const char *cfgname,
245                                        GNUNET_TRANSPORT_TESTING_start_cb restart_cb,
246                                        void *cb_cls);
247
248
249 /**
250  * Connect the given peers and call the callback when both peers report the
251  * inbound connection. Remarks: start_peer's notify_connect callback can be called
252  * before.
253  *
254  * @param tth transport testing handle
255  * @param p1 peer 1
256  * @param p2 peer 2
257  * @param cb the callback to call when both peers notified that they are connected
258  * @param cls callback cls
259  * @return a connect request handle
260  */
261 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
262 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
263                                         struct PeerContext *p1,
264                                         struct PeerContext *p2,
265                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
266                                         void *cls);
267
268
269 /**
270  * Cancel the request to connect two peers
271  * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
272  * @param tth testing
273  * @param cc a connect request handle
274  */
275 void
276 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_handle *tth,
277                                                struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc);
278
279 /**
280  * Clean up the transport testing
281  * @param tth transport testing handle
282  */
283 void
284 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth);
285
286 /**
287  * Initialize the transport testing
288  * @return transport testing handle
289  */
290 struct GNUNET_TRANSPORT_TESTING_handle *
291 GNUNET_TRANSPORT_TESTING_init (void);
292
293 /*
294  * Some utility functions
295  */
296
297 /**
298  * Extracts the test filename from an absolute file name and removes the extension
299  * @param file absolute file name
300  * @param dest where to store result
301  */
302 void
303 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file,
304                                         char **dest);
305
306 /**
307  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
308  * if existing ".exe"-prefix and adds the peer-number
309  *
310  * @param file filename of the test, e.g. argv[0]
311  * @param dest where to write the filename
312  * @param count peer number
313  */
314 void
315 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
316                                           char **dest,
317                                           int count);
318
319
320 /**
321  * Extracts the plugin anme from an absolute file name and the test name
322  * @param file absolute file name
323  * @param test test name
324  * @param dest where to store result
325  */
326 void
327 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
328                                                const char *testname,
329                                                char **pluginname);
330
331
332 /**
333  * Extracts the filename from an absolute file name and removes the extenstion
334  * @param file absolute file name
335  * @param dest where to store result
336  */
337 void
338 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file,
339                                                char **testname);
340
341 /* end of transport_testing.h */