2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file mesh/test_mesh_local_1.c
23 * @brief test mesh local: test of tunnels with just one peer
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_mesh_service.h"
34 static struct GNUNET_MESH_Handle *mesh_peer_1;
36 static struct GNUNET_MESH_Handle *mesh_peer_2;
38 static struct GNUNET_MESH_Tunnel *t;
40 static unsigned int one = 1;
42 static unsigned int two = 2;
44 static int result = GNUNET_OK;
46 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
48 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
55 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
57 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
60 GNUNET_SCHEDULER_cancel (abort_task);
64 GNUNET_MESH_tunnel_destroy(t);
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D1\n");
67 if (NULL != mesh_peer_1)
69 GNUNET_MESH_disconnect (mesh_peer_1);
71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D2\n");
72 if (NULL != mesh_peer_2)
74 GNUNET_MESH_disconnect (mesh_peer_2);
80 * Something went wrong and timed out. Kill everything and set error flag
83 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
86 result = GNUNET_SYSERR;
88 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
90 GNUNET_SCHEDULER_cancel (shutdown_task);
91 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
93 do_shutdown (cls, tc);
98 * Function is called whenever a message is received.
100 * @param cls closure (set from GNUNET_MESH_connect)
101 * @param tunnel connection to the other end
102 * @param tunnel_ctx place to store local state associated with the tunnel
103 * @param sender who sent the message
104 * @param message the actual message
105 * @param atsi performance data for the connection
106 * @return GNUNET_OK to keep the connection open,
107 * GNUNET_SYSERR to close it (signal serious error)
110 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
111 const struct GNUNET_PeerIdentity *sender,
112 const struct GNUNET_MessageHeader *message,
113 const struct GNUNET_ATS_Information *atsi)
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Data callback\n");
116 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
117 GNUNET_SCHEDULER_cancel (shutdown_task);
119 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
120 (GNUNET_TIME_UNIT_SECONDS, 2), &do_shutdown,
127 * Method called whenever another peer has added us to a tunnel
128 * the other peer initiated.
131 * @param tunnel new handle to the tunnel
132 * @param initiator peer that started the tunnel
133 * @param atsi performance information for the tunnel
134 * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
137 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
138 const struct GNUNET_PeerIdentity *initiator,
139 const struct GNUNET_ATS_Information *atsi)
141 unsigned int id = *(unsigned int *) cls;
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: received incoming tunnel\n");
146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
147 "test: received incoming tunnel on peer 2\n");
148 result = GNUNET_SYSERR;
155 * Function called whenever an inbound tunnel is destroyed. Should clean up
156 * any associated state.
158 * @param cls closure (set from GNUNET_MESH_connect)
159 * @param tunnel connection to the other end (henceforth invalid)
160 * @param tunnel_ctx place where local state associated
161 * with the tunnel is stored
164 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
167 unsigned int id = *(unsigned int *) cls;
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: incoming tunnel closed\n");
172 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
173 "test: received closing tunnel on peer 2\n");
174 result = GNUNET_SYSERR;
180 * Method called whenever a peer has connected to the tunnel.
183 * @param peer peer identity the tunnel stopped working with
186 peer_conected (void *cls, const struct GNUNET_PeerIdentity *peer,
187 const struct GNUNET_ATS_Information *atsi)
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer connected\n");
190 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
191 GNUNET_SCHEDULER_cancel(shutdown_task);
192 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
198 * Method called whenever a peer has connected to the tunnel.
201 * @param peer peer identity the tunnel was created to, NULL on timeout
202 * @param atsi performance data for the connection
205 peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer disconnected\n");
212 * Handler array for traffic received on peer1
214 static struct GNUNET_MESH_MessageHandler handlers1[] = {
215 {&data_callback, 1, 0},
221 * Handler array for traffic received on peer2 (none expected)
223 static struct GNUNET_MESH_MessageHandler handlers2[] = { {NULL, 0, 0} };
227 * Start looking for a peer by type
230 do_find (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: CONNECT BY TYPE\n");
233 GNUNET_MESH_peer_request_connect_by_type (t, 1);
238 * Initialize framework and start test
242 const struct GNUNET_CONFIGURATION_Handle *cfg,
243 struct GNUNET_TESTING_Peer *peer)
245 static const GNUNET_MESH_ApplicationType app1[] = { 1, 0 };
246 static const GNUNET_MESH_ApplicationType app2[] = { 0 };
249 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
250 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
252 mesh_peer_1 = GNUNET_MESH_connect (cfg, /* configuration */
253 (void *) &one, /* cls */
254 &inbound_tunnel, /* inbound new hndlr */
255 &inbound_end, /* inbound end hndlr */
256 handlers1, /* traffic handlers */
257 app1); /* apps offered */
259 mesh_peer_2 = GNUNET_MESH_connect (cfg, /* configuration */
260 (void *) &two, /* cls */
261 &inbound_tunnel, /* inbound new hndlr */
262 &inbound_end, /* inbound end hndlr */
263 handlers2, /* traffic handlers */
264 app2); /* apps offered */
265 if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
268 result = GNUNET_SYSERR;
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
275 t = GNUNET_MESH_tunnel_create (mesh_peer_2, NULL, &peer_conected,
276 &peer_disconnected, (void *) &two);
277 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_find, NULL);
285 main (int argc, char *argv[])
287 if (0 != GNUNET_TESTING_peer_run ("test-mesh-local-1",
291 return (result == GNUNET_OK) ? 0 : 1;
294 /* end of test_mesh_local_1.c */