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_mesh2_local.c
23 * @brief test mesh2 local: test of mesh2 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_mesh2_service.h"
33 struct GNUNET_TESTING_Peer *me;
35 static struct GNUNET_MESH_Handle *mesh_peer_1;
37 static struct GNUNET_MESH_Handle *mesh_peer_2;
39 static struct GNUNET_MESH_Tunnel *t;
41 static int result = GNUNET_OK;
43 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
45 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
52 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
54 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
57 GNUNET_SCHEDULER_cancel (abort_task);
61 GNUNET_MESH_tunnel_destroy(t);
63 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D1\n");
64 if (NULL != mesh_peer_1)
66 GNUNET_MESH_disconnect (mesh_peer_1);
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D2\n");
69 if (NULL != mesh_peer_2)
71 GNUNET_MESH_disconnect (mesh_peer_2);
77 * Something went wrong and timed out. Kill everything and set error flag
80 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
83 result = GNUNET_SYSERR;
84 abort_task = GNUNET_SCHEDULER_NO_TASK;
85 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
87 GNUNET_SCHEDULER_cancel (shutdown_task);
88 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
90 do_shutdown (cls, tc);
95 * Function is called whenever a message is received.
97 * @param cls closure (set from GNUNET_MESH_connect)
98 * @param tunnel connection to the other end
99 * @param tunnel_ctx place to store local state associated with the tunnel
100 * @param sender who sent the message
101 * @param message the actual message
103 * @return GNUNET_OK to keep the connection open,
104 * GNUNET_SYSERR to close it (signal serious error)
107 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
108 const struct GNUNET_PeerIdentity *sender,
109 const struct GNUNET_MessageHeader *message)
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Data callback\n");
112 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
113 GNUNET_SCHEDULER_cancel (shutdown_task);
115 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown,
122 * Method called whenever another peer has added us to a tunnel
123 * the other peer initiated.
126 * @param tunnel new handle to the tunnel
127 * @param initiator peer that started the tunnel
128 * @param port port number
129 * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
132 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
133 const struct GNUNET_PeerIdentity *initiator,
136 long id = (long) cls;
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
139 "test: received incoming tunnel on peer %d, port %u\n",
143 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
144 "test: wrong peer\n");
145 result = GNUNET_SYSERR;
152 * Function called whenever an inbound tunnel is destroyed. Should clean up
153 * any associated state.
155 * @param cls closure (set from GNUNET_MESH_connect)
156 * @param tunnel connection to the other end (henceforth invalid)
157 * @param tunnel_ctx place where local state associated
158 * with the tunnel is stored
161 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
164 long id = (long) cls;
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: incoming tunnel closed\n");
169 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
170 "test: received closing tunnel on peer != 2\n");
171 result = GNUNET_SYSERR;
177 * Handler array for traffic received on peer1
179 static struct GNUNET_MESH_MessageHandler handlers1[] = {
180 {&data_callback, 1, 0},
186 * Handler array for traffic received on peer2 (none expected)
188 static struct GNUNET_MESH_MessageHandler handlers2[] = { {NULL, 0, 0} };
192 do_send (void *cls, size_t size, void *buf)
194 struct GNUNET_MessageHeader *m = buf;
199 result = GNUNET_SYSERR;
202 m->size = htons (sizeof (struct GNUNET_MessageHeader));
204 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
205 return sizeof (struct GNUNET_MessageHeader);
209 * Start looking for a peer by type
212 do_find (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
214 struct GNUNET_PeerIdentity id;
216 GNUNET_TESTING_peer_get_identity (me, &id);
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: CONNECT BY PORT\n");
218 t = GNUNET_MESH_tunnel_create (mesh_peer_1, NULL, &id, 1);
219 GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
220 GNUNET_TIME_UNIT_FOREVER_REL,
221 sizeof (struct GNUNET_MessageHeader),
227 * Initialize framework and start test
231 const struct GNUNET_CONFIGURATION_Handle *cfg,
232 struct GNUNET_TESTING_Peer *peer)
234 static uint32_t ports[] = {1, 0};
238 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
239 (GNUNET_TIME_UNIT_SECONDS, 15), &do_abort,
241 mesh_peer_1 = GNUNET_MESH_connect (cfg, /* configuration */
242 (void *) 1L, /* cls */
243 &inbound_tunnel, /* inbound new hndlr */
244 &inbound_end, /* inbound end hndlr */
245 handlers1, /* traffic handlers */
246 NULL); /* ports offered */
248 mesh_peer_2 = GNUNET_MESH_connect (cfg, /* configuration */
249 (void *) 2L, /* cls */
250 &inbound_tunnel, /* inbound new hndlr */
251 &inbound_end, /* inbound end hndlr */
252 handlers2, /* traffic handlers */
253 ports); /* ports offered */
254 if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
257 result = GNUNET_SYSERR;
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
264 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_find, NULL);
272 main (int argc, char *argv[])
274 if (0 != GNUNET_TESTING_peer_run ("test-mesh-local-1",
278 return (result == GNUNET_OK) ? 0 : 1;
281 /* end of test_mesh_local_1.c */