540d3b7290bf4a546b15560563235c60d2405a68
[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
84 peers_connect ();
85
86 static void
87 end ()
88 {
89   if (GNUNET_SCHEDULER_NO_TASK != die_task)
90     GNUNET_SCHEDULER_cancel (die_task);
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
92   if (th != NULL)
93     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
94   th = NULL;
95
96   if (p1.th != NULL)
97     GNUNET_TRANSPORT_disconnect (p1.th);
98
99   if (p2.th != NULL)
100     GNUNET_TRANSPORT_disconnect (p2.th);
101   die_task = GNUNET_SCHEDULER_NO_TASK;
102   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
103               "Transports disconnected, returning success!\n");
104   ok = 0;
105 }
106
107 static void
108 stop_arm (struct PeerContext *p)
109 {
110 #if START_ARM
111   if (NULL != p->arm_proc)
112   {
113     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
114       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
115     GNUNET_OS_process_wait (p->arm_proc);
116     GNUNET_OS_process_close (p->arm_proc);
117     p->arm_proc = NULL;
118   }
119 #endif
120   GNUNET_CONFIGURATION_destroy (p->cfg);
121 }
122
123
124
125
126 static void
127 exchange_hello_last (void *cls, const struct GNUNET_MessageHeader *message)
128 {
129   struct PeerContext *me = cls;
130
131   GNUNET_assert (message != NULL);
132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133               "Exchanging HELLO of size %d with peer (%s)!\n",
134               (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
135                                        message), GNUNET_i2s (&me->id));
136   GNUNET_assert (GNUNET_OK ==
137                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
138                                       message, &me->id));
139   GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
140 }
141
142
143 static void
144 exchange_hello (void *cls, const struct GNUNET_MessageHeader *message)
145 {
146   struct PeerContext *me = cls;
147
148   GNUNET_assert (message != NULL);
149   GNUNET_assert (GNUNET_OK ==
150                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
151                                       message, &me->id));
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153               "Exchanging HELLO of size %d from peer %s!\n",
154               (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
155                                        message), GNUNET_i2s (&me->id));
156   GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
157 }
158
159
160 static void
161 end_badly ()
162 {
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
164   GNUNET_break (0);
165
166   if (th != NULL)
167   {
168     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
169     th = NULL;
170   }
171   else
172   {
173     GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
174     GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
175   }
176
177   GNUNET_TRANSPORT_disconnect (p1.th);
178   GNUNET_TRANSPORT_disconnect (p2.th);
179   if (GNUNET_SCHEDULER_NO_TASK != tct)
180   {
181     GNUNET_SCHEDULER_cancel (tct);
182     tct = GNUNET_SCHEDULER_NO_TASK;
183   }
184   ok = 1;
185 }
186
187
188 static void
189 notify_receive (void *cls, 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, const struct GNUNET_PeerIdentity *peer,
235                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
236                 uint32_t ats_count)
237 {
238   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
239               GNUNET_i2s (peer), cls);
240   peers_connected++;
241   if (cls == &p1)
242   {
243     GNUNET_assert (ok >= 2);
244     OKPP;
245     OKPP;
246     if (GNUNET_SCHEDULER_NO_TASK != die_task)
247       GNUNET_SCHEDULER_cancel (die_task);
248     if (GNUNET_SCHEDULER_NO_TASK != tct)
249       GNUNET_SCHEDULER_cancel (tct);
250     tct = GNUNET_SCHEDULER_NO_TASK;
251
252     die_task =
253         GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT, &end_badly, NULL);
254     th = GNUNET_TRANSPORT_notify_transmit_ready (p1.th, &p2.id, 256, 0, TIMEOUT,
255                                                  &notify_ready, &p1);
256   }
257 }
258
259
260 static void
261 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
262 {
263   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
264               GNUNET_i2s (peer), cls);
265   peers_connected--;
266 }
267
268
269 static void
270 setup_peer (struct PeerContext *p, const char *cfgname)
271 {
272   p->cfg = GNUNET_CONFIGURATION_create ();
273
274   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
275   if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
276     GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
277                                            &p->servicehome);
278   if (NULL != p->servicehome)
279     GNUNET_DISK_directory_remove (p->servicehome);
280
281 #if START_ARM
282   p->arm_proc =
283       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
284                                "gnunet-service-arm",
285 #if VERBOSE_ARM
286                                "-L", "DEBUG",
287 #endif
288                                "-c", cfgname, NULL);
289 #endif
290
291 }
292
293
294 static void
295 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
296 {
297   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking peers to connect...\n");
298   /* FIXME: 'pX.id' may still be all-zeros here... */
299   GNUNET_TRANSPORT_try_connect (p2.th, &p1.id);
300   GNUNET_TRANSPORT_try_connect (p1.th, &p2.id);
301   tct =
302       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect,
303                                     NULL);
304 }
305
306
307 static void
308 peers_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
309 {
310   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
311     return;
312
313   //while (peers_connected > 0);
314
315   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from Transport \n");
316
317   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
318   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
319
320   GNUNET_TRANSPORT_disconnect (p1.th);
321   p1.th = NULL;
322
323   GNUNET_TRANSPORT_disconnect (p2.th);
324   p2.th = NULL;
325
326   while (peers_connected > 0) ;
327
328   if (counter < ITERATIONS)
329   {
330     if ((counter % (ITERATIONS / 10)) == 0)
331       fprintf (stderr, "%u%%..", (counter / (ITERATIONS / 10)) * 10);
332     peers_connect ();
333   }
334   else
335   {
336     fprintf (stderr, "100%%\n");
337     end ();
338   }
339 }
340
341 static void
342 peers_connect ()
343 {
344   counter++;
345   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iteration %i of %i\n", counter,
346               ITERATIONS);
347
348   GNUNET_assert (p1.th == NULL);
349   p1.th =
350       GNUNET_TRANSPORT_connect (p1.cfg, NULL, &p1, &notify_receive,
351                                 &notify_connect, &notify_disconnect);
352
353   GNUNET_assert (p2.th == NULL);
354   p2.th =
355       GNUNET_TRANSPORT_connect (p2.cfg, NULL, &p2, &notify_receive,
356                                 &notify_connect, &notify_disconnect);
357
358   GNUNET_assert (p1.th != NULL);
359   GNUNET_assert (p2.th != NULL);
360
361   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
362   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
363   tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
364 }
365
366 static void
367 run (void *cls, char *const *args, const char *cfgfile,
368      const struct GNUNET_CONFIGURATION_Handle *cfg)
369 {
370   GNUNET_assert (ok == 1);
371   OKPP;
372   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
373
374   setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
375   setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
376
377   peers_connect ();
378 }
379
380 static int
381 check ()
382 {
383   static char *const argv[] = { "test-transport-api",
384     "-c",
385     "test_transport_api_data.conf",
386 #if VERBOSE
387     "-L", "DEBUG",
388 #endif
389     NULL
390   };
391   static struct GNUNET_GETOPT_CommandLineOption options[] = {
392     GNUNET_GETOPT_OPTION_END
393   };
394
395 #if WRITECONFIG
396   setTransportOptions ("test_transport_api_data.conf");
397 #endif
398   ok = 1;
399   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
400                       "test-transport-api", "nohelp", options, &run, &ok);
401   stop_arm (&p1);
402   stop_arm (&p2);
403
404   if (p1.servicehome != NULL)
405   {
406     GNUNET_DISK_directory_remove (p1.servicehome);
407     GNUNET_free (p1.servicehome);
408   }
409   if (p2.servicehome != NULL)
410   {
411     GNUNET_DISK_directory_remove (p2.servicehome);
412     GNUNET_free (p2.servicehome);
413   }
414   return ok;
415 }
416
417 int
418 main (int argc, char *argv[])
419 {
420   int ret;
421
422   GNUNET_log_setup ("test_transport_api_disconnect",
423 #if VERBOSE
424                     "DEBUG",
425 #else
426                     "WARNING",
427 #endif
428                     NULL);
429
430   ret = check ();
431   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Messages received:  %i\n", msgs_recv);
432   return ret;
433 }
434
435 /* end of test_transport_api_disconnect.c */