indentation
[oweals/gnunet.git] / src / transport / test_transport_api_disconnect.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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  * @file transport/test_transport_api_disconnect.c
22  * @brief base test case for transport implementations
23  *
24  * This test case serves as a base for tcp, udp, and udp-nat
25  * transport test cases.  Based on the executable being run
26  * the correct test case will be performed.  Conservation of
27  * C code apparently.
28  */
29 #include "platform.h"
30 #include "gnunet_common.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "transport.h"
38 #include "transport-testing.h"
39
40 #define VERBOSE GNUNET_NO
41
42 #define VERBOSE_ARM GNUNET_NO
43
44 #define START_ARM GNUNET_YES
45
46 /**
47  * How long until we give up on transmitting the message?
48  */
49 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
50
51 /**
52  * How long until we give up on transmitting the message?
53  */
54 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
55
56 #define MTYPE 12345
57
58 #define ITERATIONS 50
59
60 static struct PeerContext p1;
61
62 static struct PeerContext p2;
63
64 static int ok;
65
66 static int peers_connected = 0;
67 static int counter;
68 static int msgs_recv;
69
70 static GNUNET_SCHEDULER_TaskIdentifier die_task;
71
72 static GNUNET_SCHEDULER_TaskIdentifier tct;
73
74 struct GNUNET_TRANSPORT_TransmitHandle *th;
75
76 #if VERBOSE
77 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
78 #else
79 #define OKPP do { ok++; } while (0)
80 #endif
81
82
83 static void peers_connect ();
84
85 static void
86 end ()
87 {
88   if (GNUNET_SCHEDULER_NO_TASK != die_task)
89     GNUNET_SCHEDULER_cancel (die_task);
90   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
91   if (th != NULL)
92     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
93   th = NULL;
94
95   if (p1.th != NULL)
96     GNUNET_TRANSPORT_disconnect (p1.th);
97
98   if (p2.th != NULL)
99     GNUNET_TRANSPORT_disconnect (p2.th);
100   die_task = GNUNET_SCHEDULER_NO_TASK;
101   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
102               "Transports disconnected, returning success!\n");
103   ok = 0;
104 }
105
106 static void
107 stop_arm (struct PeerContext *p)
108 {
109 #if START_ARM
110   if (NULL != p->arm_proc)
111   {
112     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
113       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
114     GNUNET_OS_process_wait (p->arm_proc);
115     GNUNET_OS_process_close (p->arm_proc);
116     p->arm_proc = NULL;
117   }
118 #endif
119   GNUNET_CONFIGURATION_destroy (p->cfg);
120 }
121
122
123
124
125 static void
126 exchange_hello_last (void *cls, const struct GNUNET_MessageHeader *message)
127 {
128   struct PeerContext *me = cls;
129
130   GNUNET_assert (message != NULL);
131   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132               "Exchanging HELLO of size %d with peer (%s)!\n",
133               (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
134                                        message), GNUNET_i2s (&me->id));
135   GNUNET_assert (GNUNET_OK ==
136                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
137                                       message, &me->id));
138   GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
139 }
140
141
142 static void
143 exchange_hello (void *cls, const struct GNUNET_MessageHeader *message)
144 {
145   struct PeerContext *me = cls;
146
147   GNUNET_assert (message != NULL);
148   GNUNET_assert (GNUNET_OK ==
149                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
150                                       message, &me->id));
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152               "Exchanging HELLO of size %d from peer %s!\n",
153               (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
154                                        message), GNUNET_i2s (&me->id));
155   GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
156 }
157
158
159 static void
160 end_badly ()
161 {
162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
163   GNUNET_break (0);
164
165   if (th != NULL)
166   {
167     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
168     th = NULL;
169   }
170   else
171   {
172     GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
173     GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
174   }
175
176   GNUNET_TRANSPORT_disconnect (p1.th);
177   GNUNET_TRANSPORT_disconnect (p2.th);
178   if (GNUNET_SCHEDULER_NO_TASK != tct)
179   {
180     GNUNET_SCHEDULER_cancel (tct);
181     tct = GNUNET_SCHEDULER_NO_TASK;
182   }
183   ok = 1;
184 }
185
186
187 static void
188 notify_receive (void *cls,
189                 const struct GNUNET_PeerIdentity *peer,
190                 const struct GNUNET_MessageHeader *message,
191                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
192                 uint32_t ats_count)
193 {
194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195               "Received message of type %d from peer %s!\n",
196               ntohs (message->type), GNUNET_i2s (peer));
197   OKPP;
198   GNUNET_assert (MTYPE == ntohs (message->type));
199   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
200
201   msgs_recv++;
202 }
203
204 static void
205 peers_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
206
207 static size_t
208 notify_ready (void *cls, size_t size, void *buf)
209 {
210   struct PeerContext *p = cls;
211   struct GNUNET_MessageHeader *hdr;
212
213   th = NULL;
214
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216               "Transmitting message with %u bytes to peer %s\n",
217               sizeof (struct GNUNET_MessageHeader), GNUNET_i2s (&p->id));
218   GNUNET_assert (size >= 256);
219   OKPP;
220   if (buf != NULL)
221   {
222     hdr = buf;
223     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
224     hdr->type = htons (MTYPE);
225   }
226
227   GNUNET_SCHEDULER_add_now (&peers_disconnect, NULL);
228
229   return sizeof (struct GNUNET_MessageHeader);
230 }
231
232
233 static void
234 notify_connect (void *cls,
235                 const struct GNUNET_PeerIdentity *peer,
236                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
237                 uint32_t ats_count)
238 {
239   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240               "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
241   peers_connected++;
242   if (cls == &p1)
243   {
244     GNUNET_assert (ok >= 2);
245     OKPP;
246     OKPP;
247     if (GNUNET_SCHEDULER_NO_TASK != die_task)
248       GNUNET_SCHEDULER_cancel (die_task);
249     if (GNUNET_SCHEDULER_NO_TASK != tct)
250       GNUNET_SCHEDULER_cancel (tct);
251     tct = GNUNET_SCHEDULER_NO_TASK;
252
253     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT,
254                                              &end_badly, NULL);
255     th = GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
256                                                  &p2.id,
257                                                  256, 0, TIMEOUT, &notify_ready,
258                                                  &p1);
259   }
260 }
261
262
263 static void
264 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
265 {
266   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267               "Peer `%4s' disconnected (%p)!\n", GNUNET_i2s (peer), cls);
268   peers_connected--;
269 }
270
271
272 static void
273 setup_peer (struct PeerContext *p, const char *cfgname)
274 {
275   p->cfg = GNUNET_CONFIGURATION_create ();
276
277   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
278   if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
279     GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
280                                            &p->servicehome);
281   if (NULL != p->servicehome)
282     GNUNET_DISK_directory_remove (p->servicehome);
283
284 #if START_ARM
285   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
286                                          "gnunet-service-arm",
287 #if VERBOSE_ARM
288                                          "-L", "DEBUG",
289 #endif
290                                          "-c", cfgname, NULL);
291 #endif
292
293 }
294
295
296 static void
297 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
298 {
299   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking peers to connect...\n");
300   /* FIXME: 'pX.id' may still be all-zeros here... */
301   GNUNET_TRANSPORT_try_connect (p2.th, &p1.id);
302   GNUNET_TRANSPORT_try_connect (p1.th, &p2.id);
303   tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
304                                       &try_connect, NULL);
305 }
306
307
308 static void
309 peers_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
310 {
311   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
312     return;
313
314   //while (peers_connected > 0);
315
316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from Transport \n");
317
318   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
319   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
320
321   GNUNET_TRANSPORT_disconnect (p1.th);
322   p1.th = NULL;
323
324   GNUNET_TRANSPORT_disconnect (p2.th);
325   p2.th = NULL;
326
327   while (peers_connected > 0) ;
328
329   if (counter < ITERATIONS)
330     peers_connect ();
331   else
332     end ();
333 }
334
335 static void
336 peers_connect ()
337 {
338   counter++;
339   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iteration %i of %i\n", counter,
340               ITERATIONS);
341
342   GNUNET_assert (p1.th == NULL);
343   p1.th = GNUNET_TRANSPORT_connect (p1.cfg,
344                                     NULL, &p1,
345                                     &notify_receive,
346                                     &notify_connect, &notify_disconnect);
347
348   GNUNET_assert (p2.th == NULL);
349   p2.th = GNUNET_TRANSPORT_connect (p2.cfg,
350                                     NULL, &p2,
351                                     &notify_receive,
352                                     &notify_connect, &notify_disconnect);
353
354   GNUNET_assert (p1.th != NULL);
355   GNUNET_assert (p2.th != NULL);
356
357   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
358   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
359   tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
360 }
361
362 static void
363 run (void *cls,
364      char *const *args,
365      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
366 {
367   GNUNET_assert (ok == 1);
368   OKPP;
369   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
370
371   setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
372   setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
373
374   peers_connect ();
375 }
376
377 static int
378 check ()
379 {
380   static char *const argv[] = { "test-transport-api",
381     "-c",
382     "test_transport_api_data.conf",
383 #if VERBOSE
384     "-L", "DEBUG",
385 #endif
386     NULL
387   };
388   static struct GNUNET_GETOPT_CommandLineOption options[] = {
389     GNUNET_GETOPT_OPTION_END
390   };
391
392 #if WRITECONFIG
393   setTransportOptions ("test_transport_api_data.conf");
394 #endif
395   ok = 1;
396   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
397                       argv, "test-transport-api", "nohelp", options, &run, &ok);
398   stop_arm (&p1);
399   stop_arm (&p2);
400
401   if (p1.servicehome != NULL)
402   {
403     GNUNET_DISK_directory_remove (p1.servicehome);
404     GNUNET_free (p1.servicehome);
405   }
406   if (p2.servicehome != NULL)
407   {
408     GNUNET_DISK_directory_remove (p2.servicehome);
409     GNUNET_free (p2.servicehome);
410   }
411   return ok;
412 }
413
414 int
415 main (int argc, char *argv[])
416 {
417   int ret;
418
419   GNUNET_log_setup ("test_transport_api_disconnect",
420 #if VERBOSE
421                     "DEBUG",
422 #else
423                     "WARNING",
424 #endif
425                     NULL);
426
427   ret = check ();
428   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Messages received:  %i\n", msgs_recv);
429   return ret;
430 }
431
432 /* end of test_transport_api_disconnect.c */