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