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.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_mesh_service_new.h"
35 static struct GNUNET_OS_Process *arm_pid;
36 static struct GNUNET_MESH_Handle *mesh_peer_1;
37 static struct GNUNET_MESH_Handle *mesh_peer_2;
38 static struct GNUNET_MESH_Tunnel *t;
41 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
42 static GNUNET_SCHEDULER_TaskIdentifier test_task;
49 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
51 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
54 GNUNET_SCHEDULER_cancel (abort_task);
56 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D1\n");
57 if (NULL != mesh_peer_1)
59 GNUNET_MESH_disconnect (mesh_peer_1);
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D2\n");
62 if (NULL != mesh_peer_2)
64 GNUNET_MESH_disconnect (mesh_peer_2);
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
67 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
69 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
72 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
73 GNUNET_OS_process_close (arm_pid);
78 * Something went wrong and timed out. Kill everything and set error flag
81 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
86 GNUNET_SCHEDULER_cancel (test_task);
88 result = GNUNET_SYSERR;
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
102 * @param atsi performance data for the connection
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,
110 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Data callback\n");
113 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
114 (GNUNET_TIME_UNIT_SECONDS, 2), &do_shutdown,
121 * Method called whenever another peer has added us to a tunnel
122 * the other peer initiated.
125 * @param tunnel new handle to the tunnel
126 * @param initiator peer that started the tunnel
127 * @param atsi performance information for the tunnel
128 * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
131 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
132 const struct GNUNET_PeerIdentity *initiator,
133 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
135 unsigned int id = (unsigned int) cls;
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: received incoming tunnel\n");
140 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
141 "test: received incoming tunnel on peer 2\n");
142 result = GNUNET_SYSERR;
149 * Function called whenever an inbound tunnel is destroyed. Should clean up
150 * any associated state.
152 * @param cls closure (set from GNUNET_MESH_connect)
153 * @param tunnel connection to the other end (henceforth invalid)
154 * @param tunnel_ctx place where local state associated
155 * with the tunnel is stored
158 inbound_end (void *cls,
159 const struct GNUNET_MESH_Tunnel * tunnel,
162 unsigned int id = (unsigned int) cls;
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: incoming tunnel closed\n");
167 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
168 "test: received closing tunnel on peer 2\n");
169 result = GNUNET_SYSERR;
175 * Method called whenever a peer has disconnected from the tunnel.
178 * @param peer peer identity the tunnel stopped working with
180 static void peer_conected (
182 const struct GNUNET_PeerIdentity * peer)
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer connected\n");
185 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, &do_shutdown, NULL);
190 * Method called whenever a peer has connected to the tunnel.
193 * @param peer peer identity the tunnel was created to, NULL on timeout
194 * @param atsi performance data for the connection
196 static void peer_disconnected (
198 const struct GNUNET_PeerIdentity * peer,
199 const struct GNUNET_TRANSPORT_ATS_Information * atsi)
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer disconnected\n");
206 * Handler array for traffic received on peer1
208 static struct GNUNET_MESH_MessageHandler handlers1[] = {
209 {&data_callback, 1, 0},
215 * Handler array for traffic received on peer2 (none expected)
217 static struct GNUNET_MESH_MessageHandler handlers2[] = { {NULL, 0, 0} };
221 * Start looking for a peer by type
224 do_connect_peer_1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
227 static const GNUNET_MESH_ApplicationType app1[] = { 1, 0 };
229 test_task = GNUNET_SCHEDULER_NO_TASK;
230 mesh_peer_1 = GNUNET_MESH_connect (cfg, /* configuration */
232 (void *) 1, /* cls */
233 &inbound_tunnel, /* inbound new hndlr */
234 &inbound_end, /* inbound end hndlr */
235 handlers1, /* traffic handlers */
236 app1); /* apps offered */
244 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
246 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
247 static const GNUNET_MESH_ApplicationType app2[] = { 0 };
249 test_task = GNUNET_SCHEDULER_NO_TASK;
251 mesh_peer_2 = GNUNET_MESH_connect (cfg, /* configuration */
253 (void *) 2, /* cls */
254 &inbound_tunnel, /* inbound new hndlr */
255 &inbound_end, /* inbound end hndlr */
256 handlers2, /* traffic handlers */
257 app2); /* apps offered */
258 if (NULL == mesh_peer_2)
260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
268 t = GNUNET_MESH_tunnel_create (mesh_peer_2,
273 GNUNET_MESH_peer_request_connect_by_type(t, 1);
274 test_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
281 * Initialize framework and start test
284 run (void *cls, char *const *args, const char *cfgfile,
285 const struct GNUNET_CONFIGURATION_Handle *cfg)
287 GNUNET_log_setup ("test_mesh_local",
295 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
296 "gnunet-service-arm",
300 "-c", "test_mesh.conf", NULL);
303 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
304 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
307 test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
316 main (int argc, char *argv[])
320 char *const argv2[] = { "test-mesh-local",
321 "-c", "test_mesh.conf",
327 struct GNUNET_GETOPT_CommandLineOption options[] = {
328 GNUNET_GETOPT_OPTION_END
332 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
333 "test-mesh-local", "nohelp", options, &run, NULL);
335 if (GNUNET_OK != ret)
337 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
341 if (GNUNET_SYSERR == result)
343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");