8e5d55dcd13cc9ab34726c2a18acaac8c825413b
[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
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 struct GNUNET_TRANSPORT_TESTING_handle;
62
63 /**
64  * Context for a single peer
65  */
66 struct PeerContext
67 {
68   struct PeerContext *next;
69   struct PeerContext *prev;
70
71   struct GNUNET_TRANSPORT_TESTING_handle *tth;
72
73   struct GNUNET_CONFIGURATION_Handle *cfg;
74
75   struct GNUNET_TRANSPORT_Handle *th;
76
77   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
78
79   struct GNUNET_PeerIdentity id;
80
81   struct GNUNET_OS_Process *arm_proc;
82
83   GNUNET_TRANSPORT_ReceiveCallback rec;
84
85   GNUNET_TRANSPORT_NotifyConnect nc;
86
87   GNUNET_TRANSPORT_NotifyDisconnect nd;
88
89   GNUNET_TRANSPORT_TESTING_start_cb start_cb;
90
91   struct GNUNET_HELLO_Message *hello;
92
93   void *cb_cls;
94
95   char *servicehome;
96
97   unsigned int no;
98 };
99
100
101 struct ConnectingContext
102 {
103   struct ConnectingContext *next;
104   struct ConnectingContext *prev;
105   struct PeerContext *p1;
106   struct PeerContext *p2;
107   GNUNET_SCHEDULER_TaskIdentifier tct;
108   GNUNET_TRANSPORT_TESTING_connect_cb cb;
109   void *cb_cls;
110   struct GNUNET_TRANSPORT_Handle *th_p1;
111   struct GNUNET_TRANSPORT_Handle *th_p2;
112   int p1_c;
113   int p2_c;
114 };
115
116 struct GNUNET_TRANSPORT_TESTING_handle
117 {
118   struct ConnectingContext *cc_head;
119   struct ConnectingContext *cc_tail;
120
121   struct PeerContext *p_head;
122   struct PeerContext *p_tail;
123 };
124
125
126
127 /**
128  * Start a peer with the given configuration
129  * @param rec receive callback
130  * @param nc connect callback
131  * @param nd disconnect callback
132  * @param cb_cls closure for callback
133  *   if NULL passed the PeerContext * will be used!
134  * @return the peer context
135  */
136 struct PeerContext *
137 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
138                                      *tth, const char *cfgname, int peer_id,
139                                      GNUNET_TRANSPORT_ReceiveCallback rec,
140                                      GNUNET_TRANSPORT_NotifyConnect nc,
141                                      GNUNET_TRANSPORT_NotifyDisconnect nd,
142                                      GNUNET_TRANSPORT_TESTING_start_cb start_cb,
143                                      void *cb_cls);
144
145
146 /**
147  * shutdown the given peer
148  * @param p the peer
149  */
150
151 void
152 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
153                                     struct PeerContext *pc);
154
155
156 /**
157  * Connect the given peers and call the callback when both peers report the
158  * inbound connection. Remarks: start_peer's notify_connect callback can be called
159  * before.
160  * @param p1 peer 1
161  * @param p2 peer 2
162  * @param cb the callback to call when both peers notified that they are connected
163  * @param cb_cls callback cls
164  * @return a connect request handle
165  */
166 GNUNET_TRANSPORT_TESTING_ConnectRequest
167 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle
168                                         *tth, struct PeerContext *p1,
169                                         struct PeerContext *p2,
170                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
171                                         void *cls);
172
173 /**
174  * Cancel the request to connect two peers
175  * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
176  * @param cc a connect request handle
177  */
178 void
179 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
180                                                GNUNET_TRANSPORT_TESTING_handle
181                                                *, void *cc);
182
183 /**
184  * Clean up the transport testing
185  * @param tth transport testing handle
186  */
187 void
188 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth);
189
190 /**
191  * Initialize the transport testing
192  * @return transport testing handle
193  */
194 struct GNUNET_TRANSPORT_TESTING_handle *
195 GNUNET_TRANSPORT_TESTING_init ();
196
197 /*
198  * Some utility functions
199  */
200
201 /**
202  * Extracts the test filename from an absolute file name and removes the extension
203  * @param file absolute file name
204  * @param dest where to store result
205  */
206 void
207 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest);
208
209 /**
210  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
211  * if existing ".exe"-prefix and adds the peer-number
212  * @param file filename of the test, e.g. argv[0]
213  * @param cfgname where to write the result
214  * @param count peer number
215  */
216 void
217 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **cfgname,
218                                           int count);
219
220
221 /**
222  * Extracts the plugin anme from an absolute file name and the test name
223  * @param file absolute file name
224  * @param test test name
225  * @param dest where to store result
226  */
227 void
228 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
229                                                const char *testname,
230                                                char **pluginname);
231
232
233 /**
234  * Extracts the filename from an absolute file name and removes the extenstion
235  * @param file absolute file name
236  * @param dest where to store result
237  */
238 void
239 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file,
240                                                char **testname);
241
242 /* end of transport_testing.h */