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