- fix coverity
[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   send_task = NULL;
264   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
265
266   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
268               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
269   GNUNET_free (receiver_s);
270
271   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
272                                                &p1->id,
273                                                256,
274                                                TIMEOUT_TRANSMIT,
275                                                &notify_ready,
276                                                p1);
277 }
278
279
280 static void
281 notify_connect (void *cls,
282                 const struct GNUNET_PeerIdentity *peer)
283 {
284   static int c;
285
286   c++;
287   struct PeerContext *p = cls;
288   struct PeerContext *t = NULL;
289
290   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
291   {
292     p1_connected = GNUNET_YES;
293     t = p1;
294   }
295   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
296   {
297     p2_connected = GNUNET_YES;
298     t = p2;
299   }
300   GNUNET_assert (t != NULL);
301
302   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
303
304   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
306               t->no, GNUNET_i2s (peer));
307   GNUNET_free (ps);
308
309   if ((restarted == GNUNET_YES) && ((p1_connected == GNUNET_YES) && (p2_connected == GNUNET_YES)))
310   {
311     /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
312     send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
313   }
314 }
315
316
317 static void
318 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
319 {
320   struct PeerContext *p = cls;
321
322   if ( (NULL != p1) &&
323        (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))))
324   {
325     p1_connected = GNUNET_NO;
326   }
327   if ( (NULL != p2) &&
328        (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))))
329   {
330     p2_connected = GNUNET_NO;
331   }
332   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
333
334   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
336               GNUNET_i2s (peer));
337   GNUNET_free (ps);
338
339   if (th != NULL)
340     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
341   th = NULL;
342   if (NULL != send_task)
343     GNUNET_SCHEDULER_cancel (send_task);
344   send_task = NULL;
345 }
346
347 static void
348 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
349 {
350   cc = NULL;
351   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
352
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
354               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
355   GNUNET_free (p1_c);
356
357   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
358 }
359
360
361
362 static void
363 start_cb (struct PeerContext *p, void *cls)
364 {
365   static int started;
366
367   started++;
368
369   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
370               GNUNET_i2s (&p->id));
371
372   if (started != 2)
373     return;
374
375   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
376
377   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
379               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
380   GNUNET_free (sender_c);
381
382   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
383                                                NULL);
384
385 }
386
387 static void
388 run (void *cls, char *const *args, const char *cfgfile,
389      const struct GNUNET_CONFIGURATION_Handle *cfg)
390 {
391   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
392   p1_connected = GNUNET_NO;
393   p2_connected = GNUNET_NO;
394   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
395                                             &notify_receive, &notify_connect,
396                                             &notify_disconnect, &start_cb,
397                                             NULL);
398
399   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
400                                             &notify_receive, &notify_connect,
401                                             &notify_disconnect, &start_cb,
402                                             NULL);
403
404   if ((p1 == NULL) || (p2 == NULL))
405   {
406     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
407     if (die_task != NULL)
408       GNUNET_SCHEDULER_cancel (die_task);
409     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
410     return;
411   }
412 }
413
414
415 static int
416 check ()
417 {
418   static char *const argv[] = { "test-transport-api",
419     "-c",
420     "test_transport_api_data.conf",
421     NULL
422   };
423   static struct GNUNET_GETOPT_CommandLineOption options[] = {
424     GNUNET_GETOPT_OPTION_END
425   };
426
427   send_task = NULL;
428
429   ok = 1;
430   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
431                       "nohelp", options, &run, &ok);
432
433   return ok;
434 }
435
436 int
437 main (int argc, char *argv[])
438 {
439   int ret;
440
441   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
442   GNUNET_log_setup (test_name,
443                     "WARNING",
444                     NULL);
445   tth = GNUNET_TRANSPORT_TESTING_init ();
446   GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
447   GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
448   ret = check ();
449   GNUNET_free (cfg_file_p1);
450   GNUNET_free (cfg_file_p2);
451   GNUNET_free (test_name);
452   GNUNET_TRANSPORT_TESTING_done (tth);
453   return ret;
454 }
455
456 /* end of test_transport_api_restart_1peer.c */