added helper for handling the "view" in rps
[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 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., 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            const struct GNUNET_SCHEDULER_TaskContext *tc)
110 {
111   die_task = NULL;
112
113   if (restarted == GNUNET_YES)
114     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115                 "Peer was restarted, but communication did not resume\n");
116
117   if (restarted == GNUNET_NO)
118     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119                 "Peer was NOT (even) restarted\n");
120   if (cc != NULL)
121   {
122     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
123                 _("Fail! Could not connect peers\n"));
124     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
125     cc = NULL;
126   }
127   end ();
128   ok = GNUNET_SYSERR;
129 }
130
131
132 static void
133 restart_cb (struct PeerContext *p,
134             void *cls)
135 {
136   static int c;
137
138   c++;
139   if (c != 2)
140     return;
141   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142               "Restarted peer %u (`%4s'), issuing reconnect\n",
143               p->no,
144               GNUNET_i2s (&p->id));
145   ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
146                                             &p2->id,
147                                             1);
148 }
149
150
151 static void
152 restart (struct PeerContext *p,
153          const char *cfg_file)
154 {
155   GNUNET_assert (NULL != p);
156   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157               "Restarting peer %u (`%4s')\n",
158               p->no,
159               GNUNET_i2s (&p->id));
160   GNUNET_TRANSPORT_TESTING_restart_peer (p,
161                                          cfg_file,
162                                          &restart_cb,
163                                          p);
164 }
165
166
167 static void
168 notify_receive (void *cls,
169                 const struct GNUNET_PeerIdentity *peer,
170                 const struct GNUNET_MessageHeader *message)
171 {
172   struct PeerContext *p = cls;
173   struct PeerContext *t = NULL;
174
175   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
176     t = p1;
177   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
178     t = p2;
179   GNUNET_assert (t != NULL);
180
181   {
182     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
183
184     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185                 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
186                 p->no,
187                 ps,
188                 ntohs (message->type),
189                 ntohs (message->size),
190                 t->no,
191                 GNUNET_i2s (&t->id));
192     GNUNET_free (ps);
193   }
194
195   if ((MTYPE == ntohs (message->type)) &&
196       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
197   {
198     if (restarted == GNUNET_NO)
199     {
200       restarted = GNUNET_YES;
201       restart (p1, cfg_file_p1);
202       restart (p2, cfg_file_p2);
203       return;
204     }
205     else
206     {
207       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208                   "Restarted peers connected, stopping test...\n");
209       ok = 0;
210       end ();
211     }
212   }
213   else
214   {
215     GNUNET_break (0);
216     ok = 1;
217     if (die_task != NULL)
218       GNUNET_SCHEDULER_cancel (die_task);
219     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
220   }
221 }
222
223
224 static size_t
225 notify_ready (void *cls,
226               size_t size,
227               void *buf)
228 {
229   struct PeerContext *p = cls;
230   struct GNUNET_MessageHeader *hdr;
231
232   th = NULL;
233   if (NULL == buf)
234   {
235     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236                 "Timeout occurred while waiting for transmit_ready\n");
237     if (NULL != die_task)
238       GNUNET_SCHEDULER_cancel (die_task);
239     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
240     ok = 42;
241     return 0;
242   }
243
244   GNUNET_assert (size >= 256);
245   hdr = buf;
246   hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
247   hdr->type = htons (MTYPE);
248
249   {
250     char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
251
252     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253                 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
254                 p2->no,
255                 ps,
256                 ntohs (hdr->type),
257                 ntohs (hdr->size),
258                 p->no,
259                 GNUNET_i2s (&p->id));
260     GNUNET_free (ps);
261   }
262
263   return sizeof (struct GNUNET_MessageHeader);
264 }
265
266
267 static void
268 sendtask (void *cls,
269           const struct GNUNET_SCHEDULER_TaskContext *tc)
270 {
271   send_task = NULL;
272
273   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
274     return;
275
276   {
277     char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
278
279     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280                 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
281                 p2->no,
282                 GNUNET_i2s (&p2->id),
283                 p1->no,
284                 receiver_s);
285     GNUNET_free (receiver_s);
286   }
287
288   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
289                                                &p1->id,
290                                                256,
291                                                TIMEOUT_TRANSMIT,
292                                                &notify_ready,
293                                                p1);
294 }
295
296
297 static void
298 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
299 {
300   static int c;
301   struct PeerContext *p = cls;
302   struct PeerContext *t = NULL;
303
304   c++;
305   if (0 == memcmp (peer,
306                    &p1->id,
307                    sizeof (struct GNUNET_PeerIdentity)))
308     t = p1;
309   if (0 == memcmp (peer,
310                    &p2->id,
311                    sizeof (struct GNUNET_PeerIdentity)))
312     t = p2;
313   GNUNET_assert (t != NULL);
314
315   {
316     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
317
318     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319                 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
320                 p->no,
321                 ps,
322                 t->no,
323                 GNUNET_i2s (peer));
324     GNUNET_free (ps);
325   }
326
327   if ((restarted == GNUNET_YES) && (c == 4))
328   {
329     send_task = GNUNET_SCHEDULER_add_now (&sendtask,
330                                           NULL);
331   }
332 }
333
334
335 static void
336 notify_disconnect (void *cls,
337                    const struct GNUNET_PeerIdentity *peer)
338 {
339   struct PeerContext *p = cls;
340
341   {
342     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
343
344     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345                 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
346                 p->no,
347                 ps,
348                 GNUNET_i2s (peer));
349     GNUNET_free (ps);
350   }
351
352   if (th != NULL)
353   {
354     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
355     th = NULL;
356   }
357   if (NULL != send_task)
358   {
359     GNUNET_SCHEDULER_cancel (send_task);
360     send_task = NULL;
361   }
362 }
363
364
365 static void
366 testing_connect_cb (struct PeerContext *p1,
367                     struct PeerContext *p2,
368                     void *cls)
369 {
370   cc = NULL;
371
372   {
373     char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
374
375     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376                 "Peers connected: %u (%s) <-> %u (%s)\n",
377                 p1->no,
378                 p1_c,
379                 p2->no,
380                 GNUNET_i2s (&p2->id));
381     GNUNET_free (p1_c);
382   }
383   send_task = GNUNET_SCHEDULER_add_now (&sendtask,
384                                         NULL);
385 }
386
387
388 static void
389 start_cb (struct PeerContext *p, void *cls)
390 {
391   static int started;
392
393   started++;
394   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395               "Peer %u (`%s') started\n",
396               p->no,
397               GNUNET_i2s (&p->id));
398   if (started != 2)
399     return;
400
401   {
402     char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
403
404     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405                 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
406                 p1->no,
407                 sender_c,
408                 p2->no,
409                 GNUNET_i2s (&p2->id));
410     GNUNET_free (sender_c);
411   }
412
413   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth,
414                                                p1,
415                                                p2,
416                                                &testing_connect_cb,
417                                                NULL);
418 }
419
420
421 static void
422 run (void *cls,
423      char *const *args,
424      const char *cfgfile,
425      const struct GNUNET_CONFIGURATION_Handle *cfg)
426 {
427   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
428                                            &end_badly,
429                                            NULL);
430   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
431                                             cfg_file_p1,
432                                             1,
433                                             &notify_receive,
434                                             &notify_connect,
435                                             &notify_disconnect,
436                                             &start_cb,
437                                             NULL);
438
439   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
440                                             cfg_file_p2,
441                                             2,
442                                             &notify_receive,
443                                             &notify_connect,
444                                             &notify_disconnect,
445                                             &start_cb,
446                                             NULL);
447
448   if ((p1 == NULL) || (p2 == NULL))
449   {
450     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
451     if (die_task != NULL)
452       GNUNET_SCHEDULER_cancel (die_task);
453     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
454     return;
455   }
456 }
457
458
459 static int
460 check ()
461 {
462   static char *const argv[] = { "test-transport-api",
463     "-c",
464     "test_transport_api_data.conf",
465     NULL
466   };
467   static struct GNUNET_GETOPT_CommandLineOption options[] = {
468     GNUNET_GETOPT_OPTION_END
469   };
470
471   send_task = NULL;
472
473   ok = 1;
474   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
475                       "nohelp", options, &run, NULL);
476
477   return ok;
478 }
479
480
481 int
482 main (int argc, char *argv[])
483 {
484   int ret;
485
486   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
487   GNUNET_log_setup (test_name,
488                     "WARNING",
489                     NULL);
490   tth = GNUNET_TRANSPORT_TESTING_init ();
491   GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
492   GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
493   ret = check ();
494   GNUNET_free (cfg_file_p1);
495   GNUNET_free (cfg_file_p2);
496   GNUNET_free (test_name);
497   GNUNET_TRANSPORT_TESTING_done (tth);
498   return ret;
499 }
500
501 /* end of test_transport_api_restart_2peers.c */