-ftbfs
[oweals/gnunet.git] / src / mesh / test_mesh_local.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 mesh channels with just one peer
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_mesh_service_enc.h"
32
33 struct GNUNET_TESTING_Peer *me;
34
35 static struct GNUNET_MESH_Handle *mesh_peer_1;
36
37 static struct GNUNET_MESH_Handle *mesh_peer_2;
38
39 static struct GNUNET_MESH_Channel *ch;
40
41 static int result = GNUNET_OK;
42
43 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
44
45 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
46
47
48 /**
49  * Shutdown nicely
50  */
51 static void
52 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
53 {
54   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
55   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
56   {
57     GNUNET_SCHEDULER_cancel (abort_task);
58   }
59   if (NULL != ch)
60   {
61     GNUNET_MESH_channel_destroy (ch);
62   }
63   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 1\n");
64   if (NULL != mesh_peer_1)
65   {
66     GNUNET_MESH_disconnect (mesh_peer_1);
67   }
68   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 2\n");
69   if (NULL != mesh_peer_2)
70   {
71     GNUNET_MESH_disconnect (mesh_peer_2);
72   }
73 }
74
75
76 /**
77  * Something went wrong and timed out. Kill everything and set error flag
78  */
79 static void
80 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 {
82   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
83   result = GNUNET_SYSERR;
84   abort_task = GNUNET_SCHEDULER_NO_TASK;
85   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
86   {
87     GNUNET_SCHEDULER_cancel (shutdown_task);
88     shutdown_task = GNUNET_SCHEDULER_NO_TASK;
89   }
90   do_shutdown (cls, tc);
91 }
92
93
94 /**
95  * Function is called whenever a message is received.
96  *
97  * @param cls closure (set from GNUNET_MESH_connect)
98  * @param channel connection to the other end
99  * @param channel_ctx place to store local state associated with the channel
100  * @param message the actual message
101  *
102  * @return GNUNET_OK to keep the connection open,
103  *         GNUNET_SYSERR to close it (signal serious error)
104  */
105 static int
106 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
107                void **channel_ctx,
108                const struct GNUNET_MessageHeader *message)
109 {
110   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data callback! Shutting down.\n");
111   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
112     GNUNET_SCHEDULER_cancel (shutdown_task);
113   shutdown_task =
114     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown,
115                                   NULL);
116   GNUNET_MESH_receive_done (channel);
117   return GNUNET_OK;
118 }
119
120
121 /**
122  * Method called whenever another peer has added us to a channel
123  * the other peer initiated.
124  *
125  * @param cls closure
126  * @param channel new handle to the channel
127  * @param initiator peer that started the channel
128  * @param port port number
129  * @return initial channel context for the channel
130  *         (can be NULL -- that's not an error)
131  */
132 static void *
133 inbound_channel (void *cls, struct GNUNET_MESH_Channel *channel,
134                 const struct GNUNET_PeerIdentity *initiator,
135                 uint32_t port)
136 {
137   long id = (long) cls;
138
139   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140               "received incoming channel on peer %d, port %u\n",
141               id, port);
142   if (id != 2L)
143   {
144     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
145                 "wrong peer\n");
146     result = GNUNET_SYSERR;
147   }
148   return NULL;
149 }
150
151
152 /**
153  * Function called whenever an inbound channel is destroyed.  Should clean up
154  * any associated state.
155  *
156  * @param cls closure (set from GNUNET_MESH_connect)
157  * @param channel connection to the other end (henceforth invalid)
158  * @param channel_ctx place where local state associated
159  *                    with the channel is stored
160  */
161 static void
162 inbound_end (void *cls, const struct GNUNET_MESH_Channel *channel,
163              void *channel_ctx)
164 {
165   long id = (long) cls;
166
167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168               "incoming channel closed at peer %ld\n",
169               id);
170 }
171
172
173 /**
174  * Handler array for traffic received on peer1
175  */
176 static struct GNUNET_MESH_MessageHandler handlers1[] = {
177   {&data_callback, 1, 0},
178   {NULL, 0, 0}
179 };
180
181
182 /**
183  * Handler array for traffic received on peer2 (none expected)
184  */
185 static struct GNUNET_MESH_MessageHandler handlers2[] = {
186   {&data_callback, 1, 0},
187   {NULL, 0, 0}
188 };
189
190
191 /**
192  * Data send callback: fillbuffer with test packet.
193  *
194  * @param cls Closure (unused).
195  * @param size Buffer size.
196  * @param buf Buffer to fill.
197  *
198  * @return size of test packet.
199  */
200 static size_t
201 do_send (void *cls, size_t size, void *buf)
202 {
203   struct GNUNET_MessageHeader *m = buf;
204
205   if (NULL == buf)
206   {
207     GNUNET_break (0);
208     result = GNUNET_SYSERR;
209     return 0;
210   }
211   m->size = htons (sizeof (struct GNUNET_MessageHeader));
212   m->type = htons (1);
213   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
214   return sizeof (struct GNUNET_MessageHeader);
215 }
216
217 /**
218  * Connect to other client and send data
219  *
220  * @param cls Closue (unused).
221  * @param tc TaskContext.
222  */
223 static void
224 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
225 {
226   struct GNUNET_PeerIdentity id;
227
228   GNUNET_TESTING_peer_get_identity (me, &id);
229   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
230   ch = GNUNET_MESH_channel_create (mesh_peer_1, NULL, &id, 1,
231                                   GNUNET_YES, GNUNET_NO);
232   GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
233                                      GNUNET_TIME_UNIT_FOREVER_REL,
234                                      sizeof (struct GNUNET_MessageHeader),
235                                      &do_send, NULL);
236 }
237
238
239 /**
240  * Initialize framework and start test
241  *
242  * @param cls Closure (unused).
243  * @param cfg Configuration handle.
244  * @param peer Testing peer handle.
245  */
246 static void
247 run (void *cls,
248      const struct GNUNET_CONFIGURATION_Handle *cfg,
249      struct GNUNET_TESTING_Peer *peer)
250 {
251   static uint32_t ports[] = {1, 0};
252
253   me = peer;
254   abort_task =
255       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
256                                     (GNUNET_TIME_UNIT_SECONDS, 15), &do_abort,
257                                     NULL);
258   mesh_peer_1 = GNUNET_MESH_connect (cfg,       /* configuration */
259                                      (void *) 1L,     /* cls */
260                                      &inbound_channel,   /* inbound new hndlr */
261                                      &inbound_end,      /* inbound end hndlr */
262                                      handlers1, /* traffic handlers */
263                                      NULL);     /* ports offered */
264
265   mesh_peer_2 = GNUNET_MESH_connect (cfg,       /* configuration */
266                                      (void *) 2L,     /* cls */
267                                      &inbound_channel,   /* inbound new hndlr */
268                                      &inbound_end,      /* inbound end hndlr */
269                                      handlers2, /* traffic handlers */
270                                      ports);     /* ports offered */
271   if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
272   {
273     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh :(\n");
274     result = GNUNET_SYSERR;
275     return;
276   }
277   else
278   {
279     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO MESH :D\n");
280   }
281   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
282                                   GNUNET_TIME_UNIT_SECONDS,
283                                   2),
284                                 &do_connect, NULL);
285 }
286
287
288 /**
289  * Main
290  */
291 int
292 main (int argc, char *argv[])
293 {
294   if (0 != GNUNET_TESTING_peer_run ("test-mesh-local",
295                                     "test_mesh.conf",
296                                 &run, NULL))
297   {
298     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
299     return 2;
300   }
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
302   return (result == GNUNET_OK) ? 0 : 1;
303 }
304
305 /* end of test_mesh_local_1.c */