- leaks
[oweals/gnunet.git] / src / mesh / test_mesh_local_2.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file mesh/test_mesh_local.c
23  * @brief test mesh local: test of tunnels with just one peer
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib-new.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_mesh_service.h"
32
33 static struct GNUNET_MESH_Handle *mesh_peer_1;
34
35 static struct GNUNET_MESH_Handle *mesh_peer_2;
36
37 static struct GNUNET_MESH_Tunnel *t;
38
39 static unsigned int one = 1;
40
41 static unsigned int two = 2;
42
43 static int result = GNUNET_OK;
44
45 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
46
47 static GNUNET_SCHEDULER_TaskIdentifier test_task;
48
49
50 /**
51  * Shutdown nicely
52  */
53 static void
54 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
55 {
56   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
57   if (0 != abort_task)
58   {
59     GNUNET_SCHEDULER_cancel (abort_task);
60   }
61   if (NULL != t)
62   {
63     GNUNET_MESH_tunnel_destroy(t);
64   }
65   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D1\n");
66   if (NULL != mesh_peer_1)
67   {
68     GNUNET_MESH_disconnect (mesh_peer_1);
69   }
70   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D2\n");
71   if (NULL != mesh_peer_2)
72   {
73     GNUNET_MESH_disconnect (mesh_peer_2);
74   }
75 }
76
77
78 /**
79  * Something went wrong and timed out. Kill everything and set error flag
80  */
81 static void
82 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 {
84   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
85   if (0 != test_task)
86   {
87     GNUNET_SCHEDULER_cancel (test_task);
88   }
89   result = GNUNET_SYSERR;
90   abort_task = 0;
91   do_shutdown (cls, tc);
92 }
93
94
95 /**
96  * Function is called whenever a message is received.
97  *
98  * @param cls closure (set from GNUNET_MESH_connect)
99  * @param tunnel connection to the other end
100  * @param tunnel_ctx place to store local state associated with the tunnel
101  * @param sender who sent the message
102  * @param message the actual message
103  * @param atsi performance data for the connection
104  * @return GNUNET_OK to keep the connection open,
105  *         GNUNET_SYSERR to close it (signal serious error)
106  */
107 static int
108 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
109                const struct GNUNET_PeerIdentity *sender,
110                const struct GNUNET_MessageHeader *message,
111                const struct GNUNET_ATS_Information *atsi)
112 {
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Data callback\n");
114   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
115                                 (GNUNET_TIME_UNIT_SECONDS, 2), &do_shutdown,
116                                 NULL);
117   return GNUNET_OK;
118 }
119
120
121 /**
122  * Method called whenever another peer has added us to a tunnel
123  * the other peer initiated.
124  *
125  * @param cls closure
126  * @param tunnel new handle to the tunnel
127  * @param initiator peer that started the tunnel
128  * @param atsi performance information for the tunnel
129  * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
130  */
131 static void *
132 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
133                 const struct GNUNET_PeerIdentity *initiator,
134                 const struct GNUNET_ATS_Information *atsi)
135 {
136   unsigned int id = *(unsigned int *) cls;
137
138   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: received incoming tunnel\n");
139   if (id != 1)
140   {
141     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
142                 "test: received incoming tunnel on peer 2\n");
143     result = GNUNET_SYSERR;
144   }
145   return NULL;
146 }
147
148
149 /**
150  * Function called whenever an inbound tunnel is destroyed.  Should clean up
151  * any associated state.
152  *
153  * @param cls closure (set from GNUNET_MESH_connect)
154  * @param tunnel connection to the other end (henceforth invalid)
155  * @param tunnel_ctx place where local state associated
156  *                   with the tunnel is stored
157  */
158 static void
159 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
160              void *tunnel_ctx)
161 {
162   unsigned int id = *(unsigned int *) cls;
163
164   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: incoming tunnel closed\n");
165   if (id != 1)
166   {
167     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
168                 "test: received closing tunnel on peer 2\n");
169     result = GNUNET_SYSERR;
170   }
171 }
172
173
174 /**
175  * Method called whenever a peer has disconnected from the tunnel.
176  *
177  * @param cls closure
178  * @param peer peer identity the tunnel stopped working with
179  */
180 static void
181 peer_conected (void *cls, const struct GNUNET_PeerIdentity *peer,
182                const struct GNUNET_ATS_Information *atsi)
183 {
184   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer connected\n");
185   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown, NULL);
186 }
187
188
189 /**
190  * Method called whenever a peer has connected to the tunnel.
191  *
192  * @param cls closure
193  * @param peer peer identity the tunnel was created to, NULL on timeout
194  * @param atsi performance data for the connection
195  */
196 static void
197 peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
198 {
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer disconnected\n");
200 }
201
202
203 /**
204  * Handler array for traffic received on peer1
205  */
206 static struct GNUNET_MESH_MessageHandler handlers1[] = {
207   {&data_callback, 1, 0},
208   {NULL, 0, 0}
209 };
210
211
212 /**
213  * Handler array for traffic received on peer2 (none expected)
214  */
215 static struct GNUNET_MESH_MessageHandler handlers2[] = { {NULL, 0, 0} };
216
217
218 /**
219  * Start looking for a peer by type
220  */
221 static void
222 do_connect_peer_1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 {
224   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
225   static const GNUNET_MESH_ApplicationType app1[] = { 1, 0 };
226
227   test_task = GNUNET_SCHEDULER_NO_TASK;
228   mesh_peer_1 = GNUNET_MESH_connect (cfg,       /* configuration */
229                                      (void *) &one,     /* cls */
230                                      &inbound_tunnel,   /* inbound new hndlr */
231                                      &inbound_end,      /* inbound end hndlr */
232                                      handlers1, /* traffic handlers */
233                                      app1);     /* apps offered */
234 }
235
236
237 /**
238  * Initialize framework and start test
239  */
240 static void
241 run (void *cls, 
242      const struct GNUNET_CONFIGURATION_Handle *cfg,
243      struct GNUNET_TESTING_Peer *peer)
244 {
245   static const GNUNET_MESH_ApplicationType app2[] = { 0 };
246
247   abort_task =
248       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
249                                     (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
250                                     NULL);
251   mesh_peer_2 = GNUNET_MESH_connect (cfg,       /* configuration */
252                                      (void *) &two,     /* cls */
253                                      &inbound_tunnel,   /* inbound new hndlr */
254                                      &inbound_end,      /* inbound end hndlr */
255                                      handlers2, /* traffic handlers */
256                                      app2);     /* apps offered */
257   if (NULL == mesh_peer_2)
258   {
259     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
260     result = GNUNET_SYSERR;
261     return;
262   }
263   else
264   {
265     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
266   }
267   t = GNUNET_MESH_tunnel_create (mesh_peer_2, NULL, &peer_conected,
268                                  &peer_disconnected, (void *) &two);
269   GNUNET_MESH_peer_request_connect_by_type (t, 1);
270   test_task =
271       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
272                                     (GNUNET_TIME_UNIT_SECONDS, 5),
273                                     &do_connect_peer_1, (void*) cfg);
274 }
275
276
277 /**
278  * Main
279  */
280 int
281 main (int argc, char *argv[])
282 {
283   if (0 != GNUNET_TESTING_peer_run ("test-mesh-local-2",
284                                     "test_mesh.conf",
285                                     &run, NULL))
286     return 1;
287   return (result == GNUNET_OK) ? 0 : 1;
288 }
289
290 /* end of test_mesh_local_2.c */