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