-ensure stats queues do not grow too big
[oweals/gnunet.git] / src / transport / test_transport_api_restart_2peers.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file transport/test_transport_api_restart_2peers.c
22  * @brief base test case for transport implementations
23  *
24  * This test case starts 2 peers, connects and exchanges a message
25  * boths peer are restarted and tested if peers reconnect
26  */
27 #include "platform.h"
28 #include "gnunet_transport_service.h"
29 #include "transport-testing.h"
30
31 /**
32  * How long until we give up on transmitting the message?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 900)
35
36 /**
37  * How long until we give up on transmitting the message?
38  */
39 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
40
41 #define MTYPE 12345
42
43 static char *test_name;
44
45 static int ok;
46
47 static struct GNUNET_SCHEDULER_Task *die_task;
48
49 static struct GNUNET_SCHEDULER_Task *send_task;
50
51 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
52
53 static struct PeerContext *p1;
54
55 static struct PeerContext *p2;
56
57 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
58
59 static struct GNUNET_TRANSPORT_TransmitHandle *th;
60
61 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
62
63 static char *cfg_file_p1;
64
65 static char *cfg_file_p2;
66
67 static int restarted;
68
69
70 static void
71 end ()
72 {
73   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
74   if (NULL != send_task)
75   {
76     GNUNET_SCHEDULER_cancel (send_task);
77     send_task = NULL;
78   }
79   if (NULL != ats_sh)
80   {
81     GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
82     ats_sh = NULL;
83   }
84   if (NULL != die_task)
85   {
86     GNUNET_SCHEDULER_cancel (die_task);
87     die_task = NULL;
88   }
89   if (NULL != th)
90   {
91     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
92     th = NULL;
93   }
94   if (NULL != p1)
95   {
96     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
97     p1 = NULL;
98   }
99   if (NULL != p2)
100   {
101     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
102     p2 = NULL;
103   }
104 }
105
106
107 static void
108 end_badly (void *cls)
109 {
110   die_task = NULL;
111
112   if (restarted == GNUNET_YES)
113     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
114                 "Peer was restarted, but communication did not resume\n");
115
116   if (restarted == GNUNET_NO)
117     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118                 "Peer was NOT (even) restarted\n");
119   if (cc != NULL)
120   {
121     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
122                 _("Fail! Could not connect peers\n"));
123     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
124     cc = NULL;
125   }
126   end ();
127   ok = GNUNET_SYSERR;
128 }
129
130
131 static void
132 restart_cb (struct PeerContext *p,
133             void *cls)
134 {
135   static int c;
136
137   c++;
138   if (c != 2)
139     return;
140   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141               "Restarted peer %u (`%4s'), issuing reconnect\n",
142               p->no,
143               GNUNET_i2s (&p->id));
144   ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
145                                             &p2->id,
146                                             1);
147 }
148
149
150 static void
151 restart (struct PeerContext *p,
152          const char *cfg_file)
153 {
154   GNUNET_assert (NULL != p);
155   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156               "Restarting peer %u (`%4s')\n",
157               p->no,
158               GNUNET_i2s (&p->id));
159   GNUNET_TRANSPORT_TESTING_restart_peer (p,
160                                          cfg_file,
161                                          &restart_cb,
162                                          p);
163 }
164
165
166 static void
167 notify_receive (void *cls,
168                 const struct GNUNET_PeerIdentity *peer,
169                 const struct GNUNET_MessageHeader *message)
170 {
171   struct PeerContext *p = cls;
172   struct PeerContext *t = NULL;
173
174   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
175     t = p1;
176   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
177     t = p2;
178   GNUNET_assert (t != NULL);
179
180   {
181     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
182
183     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184                 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
185                 p->no,
186                 ps,
187                 ntohs (message->type),
188                 ntohs (message->size),
189                 t->no,
190                 GNUNET_i2s (&t->id));
191     GNUNET_free (ps);
192   }
193
194   if ((MTYPE == ntohs (message->type)) &&
195       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
196   {
197     if (restarted == GNUNET_NO)
198     {
199       restarted = GNUNET_YES;
200       restart (p1, cfg_file_p1);
201       restart (p2, cfg_file_p2);
202       return;
203     }
204     else
205     {
206       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207                   "Restarted peers connected, stopping test...\n");
208       ok = 0;
209       end ();
210     }
211   }
212   else
213   {
214     GNUNET_break (0);
215     ok = 1;
216     if (die_task != NULL)
217       GNUNET_SCHEDULER_cancel (die_task);
218     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
219   }
220 }
221
222
223 static size_t
224 notify_ready (void *cls,
225               size_t size,
226               void *buf)
227 {
228   struct PeerContext *p = cls;
229   struct GNUNET_MessageHeader *hdr;
230
231   th = NULL;
232   if (NULL == buf)
233   {
234     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235                 "Timeout occurred while waiting for transmit_ready\n");
236     if (NULL != die_task)
237       GNUNET_SCHEDULER_cancel (die_task);
238     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
239     ok = 42;
240     return 0;
241   }
242
243   GNUNET_assert (size >= 256);
244   hdr = buf;
245   hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
246   hdr->type = htons (MTYPE);
247
248   {
249     char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
250
251     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252                 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
253                 p2->no,
254                 ps,
255                 ntohs (hdr->type),
256                 ntohs (hdr->size),
257                 p->no,
258                 GNUNET_i2s (&p->id));
259     GNUNET_free (ps);
260   }
261
262   return sizeof (struct GNUNET_MessageHeader);
263 }
264
265
266 static void
267 sendtask (void *cls)
268 {
269   send_task = NULL;
270   {
271     char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
272
273     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274                 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
275                 p2->no,
276                 GNUNET_i2s (&p2->id),
277                 p1->no,
278                 receiver_s);
279     GNUNET_free (receiver_s);
280   }
281
282   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
283                                                &p1->id,
284                                                256,
285                                                TIMEOUT_TRANSMIT,
286                                                &notify_ready,
287                                                p1);
288 }
289
290
291 static void
292 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
293 {
294   static int c;
295   struct PeerContext *p = cls;
296   struct PeerContext *t = NULL;
297
298   c++;
299   if (0 == memcmp (peer,
300                    &p1->id,
301                    sizeof (struct GNUNET_PeerIdentity)))
302     t = p1;
303   if (0 == memcmp (peer,
304                    &p2->id,
305                    sizeof (struct GNUNET_PeerIdentity)))
306     t = p2;
307   GNUNET_assert (t != NULL);
308
309   {
310     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
311
312     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313                 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
314                 p->no,
315                 ps,
316                 t->no,
317                 GNUNET_i2s (peer));
318     GNUNET_free (ps);
319   }
320
321   if ((restarted == GNUNET_YES) && (c == 4))
322   {
323     send_task = GNUNET_SCHEDULER_add_now (&sendtask,
324                                           NULL);
325   }
326 }
327
328
329 static void
330 notify_disconnect (void *cls,
331                    const struct GNUNET_PeerIdentity *peer)
332 {
333   struct PeerContext *p = cls;
334
335   {
336     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
337
338     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339                 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
340                 p->no,
341                 ps,
342                 GNUNET_i2s (peer));
343     GNUNET_free (ps);
344   }
345
346   if (th != NULL)
347   {
348     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
349     th = NULL;
350   }
351   if (NULL != send_task)
352   {
353     GNUNET_SCHEDULER_cancel (send_task);
354     send_task = NULL;
355   }
356 }
357
358
359 static void
360 testing_connect_cb (struct PeerContext *p1,
361                     struct PeerContext *p2,
362                     void *cls)
363 {
364   cc = NULL;
365
366   {
367     char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
368
369     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370                 "Peers connected: %u (%s) <-> %u (%s)\n",
371                 p1->no,
372                 p1_c,
373                 p2->no,
374                 GNUNET_i2s (&p2->id));
375     GNUNET_free (p1_c);
376   }
377   send_task = GNUNET_SCHEDULER_add_now (&sendtask,
378                                         NULL);
379 }
380
381
382 static void
383 start_cb (struct PeerContext *p, void *cls)
384 {
385   static int started;
386
387   started++;
388   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389               "Peer %u (`%s') started\n",
390               p->no,
391               GNUNET_i2s (&p->id));
392   if (started != 2)
393     return;
394
395   {
396     char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
397
398     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399                 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
400                 p1->no,
401                 sender_c,
402                 p2->no,
403                 GNUNET_i2s (&p2->id));
404     GNUNET_free (sender_c);
405   }
406
407   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth,
408                                                p1,
409                                                p2,
410                                                &testing_connect_cb,
411                                                NULL);
412 }
413
414
415 static void
416 run (void *cls,
417      char *const *args,
418      const char *cfgfile,
419      const struct GNUNET_CONFIGURATION_Handle *cfg)
420 {
421   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
422                                            &end_badly,
423                                            NULL);
424   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
425                                             cfg_file_p1,
426                                             1,
427                                             &notify_receive,
428                                             &notify_connect,
429                                             &notify_disconnect,
430                                             &start_cb,
431                                             NULL);
432
433   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
434                                             cfg_file_p2,
435                                             2,
436                                             &notify_receive,
437                                             &notify_connect,
438                                             &notify_disconnect,
439                                             &start_cb,
440                                             NULL);
441
442   if ((p1 == NULL) || (p2 == NULL))
443   {
444     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
445     if (die_task != NULL)
446       GNUNET_SCHEDULER_cancel (die_task);
447     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
448     return;
449   }
450 }
451
452
453 static int
454 check ()
455 {
456   static char *const argv[] = { "test-transport-api",
457     "-c",
458     "test_transport_api_data.conf",
459     NULL
460   };
461   static struct GNUNET_GETOPT_CommandLineOption options[] = {
462     GNUNET_GETOPT_OPTION_END
463   };
464
465   send_task = NULL;
466
467   ok = 1;
468   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
469                       "nohelp", options, &run, NULL);
470
471   return ok;
472 }
473
474
475 int
476 main (int argc, char *argv[])
477 {
478   int ret;
479
480   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
481   GNUNET_log_setup (test_name,
482                     "WARNING",
483                     NULL);
484   tth = GNUNET_TRANSPORT_TESTING_init ();
485   GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
486   GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
487   ret = check ();
488   GNUNET_free (cfg_file_p1);
489   GNUNET_free (cfg_file_p2);
490   GNUNET_free (test_name);
491   GNUNET_TRANSPORT_TESTING_done (tth);
492   return ret;
493 }
494
495 /* end of test_transport_api_restart_2peers.c */