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