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