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