-do not export set_incoming_quota past file, it is not used beyond the scope
[oweals/gnunet.git] / src / transport / test_transport_api_restart_2peers.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010 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_SCHEDULER_Task * reconnect_task;
52
53 static struct PeerContext *p1;
54
55 static struct PeerContext *p2;
56
57 static 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 (send_task != NULL)
75     GNUNET_SCHEDULER_cancel (send_task);
76   send_task = NULL;
77
78   if (reconnect_task != NULL)
79     GNUNET_SCHEDULER_cancel (reconnect_task);
80   reconnect_task = NULL;
81
82   if (die_task != NULL)
83     GNUNET_SCHEDULER_cancel (die_task);
84   die_task = NULL;
85
86   if (th != NULL)
87     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
88   th = NULL;
89
90   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
91   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
92 }
93
94
95 static void
96 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 {
98   die_task = NULL;
99
100   if (restarted == GNUNET_YES)
101     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted, but communication did not resume\n");
102
103   if (restarted == GNUNET_NO)
104     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT (even) restarted\n");
105
106   if (reconnect_task != NULL)
107     GNUNET_SCHEDULER_cancel (reconnect_task);
108   reconnect_task = NULL;
109
110   if (send_task != NULL)
111     GNUNET_SCHEDULER_cancel (send_task);
112   send_task = NULL;
113
114   if (cc != NULL)
115   {
116     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
117     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
118     cc = NULL;
119   }
120
121   if (th != NULL)
122     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
123   th = NULL;
124
125   if (p1 != NULL)
126     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
127   if (p2 != NULL)
128     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
129
130   ok = GNUNET_SYSERR;
131 }
132
133
134 static void
135 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 {
137   struct PeerContext *p = cls;
138
139   reconnect_task = NULL;
140   GNUNET_TRANSPORT_try_connect (p1->th, &p2->id, NULL, NULL); /*FIXME TRY_CONNECT change */
141   reconnect_task =
142       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
143 }
144
145
146 static void
147 restart_cb (struct PeerContext *p, void *cls)
148 {
149   static int c;
150
151   c++;
152   if (c != 2)
153     return;
154   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155               "Restarted peer %u (`%4s'), issuing reconnect\n", p->no,
156               GNUNET_i2s (&p->id));
157   reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, p);
158 }
159
160
161 static void
162 restart (struct PeerContext *p, char *cfg_file)
163 {
164   GNUNET_assert (p != NULL);
165   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166               "Restarting peer %u (`%4s')\n",
167               p->no,
168               GNUNET_i2s (&p->id));
169   GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p);
170 }
171
172
173 static void
174 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
175                 const struct GNUNET_MessageHeader *message)
176 {
177   struct PeerContext *p = cls;
178   struct PeerContext *t = NULL;
179
180   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
181     t = p1;
182   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
183     t = p2;
184   GNUNET_assert (t != NULL);
185
186   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
187
188   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
190               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
191               GNUNET_i2s (&t->id));
192   GNUNET_free (ps);
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, size_t size, void *buf)
225 {
226   struct PeerContext *p = cls;
227   struct GNUNET_MessageHeader *hdr;
228
229   th = NULL;
230   if (buf == NULL)
231   {
232     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233                 "Timeout occurred while waiting for transmit_ready\n");
234     if (NULL != die_task)
235       GNUNET_SCHEDULER_cancel (die_task);
236     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
237     ok = 42;
238     return 0;
239   }
240
241   GNUNET_assert (size >= 256);
242
243   if (buf != NULL)
244   {
245     hdr = buf;
246     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
247     hdr->type = htons (MTYPE);
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, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
254               GNUNET_i2s (&p->id));
255   GNUNET_free (ps);
256   return sizeof (struct GNUNET_MessageHeader);
257 }
258
259
260 static void
261 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
262 {
263   send_task = NULL;
264
265   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
266     return;
267   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
268
269   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
271               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
272   GNUNET_free (receiver_s);
273
274   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256,
275                                                TIMEOUT_TRANSMIT, &notify_ready,
276                                                p1);
277 }
278
279
280 static void
281 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
282 {
283   static int c;
284
285   c++;
286   struct PeerContext *p = cls;
287   struct PeerContext *t = NULL;
288
289   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
290     t = p1;
291   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
292     t = p2;
293   GNUNET_assert (t != NULL);
294
295   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
296
297   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
299               t->no, GNUNET_i2s (peer));
300   GNUNET_free (ps);
301
302   if ((restarted == GNUNET_YES) && (c == 4))
303   {
304     send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
305   }
306 }
307
308
309 static void
310 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
311 {
312   struct PeerContext *p = cls;
313
314
315   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
316
317   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
319               GNUNET_i2s (peer));
320   GNUNET_free (ps);
321
322   if (th != NULL)
323     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
324   th = NULL;
325  if (NULL != send_task)
326     GNUNET_SCHEDULER_cancel (send_task);
327   send_task = NULL;
328 }
329
330
331 static void
332 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
333 {
334   cc = NULL;
335   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
336
337   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
338               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
339   GNUNET_free (p1_c);
340
341   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
342 }
343
344
345 static void
346 start_cb (struct PeerContext *p, void *cls)
347 {
348   static int started;
349
350   started++;
351
352   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
353               GNUNET_i2s (&p->id));
354
355   if (started != 2)
356     return;
357
358   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
359
360   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
362               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
363   GNUNET_free (sender_c);
364
365   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
366                                                NULL);
367
368 }
369
370
371 static void
372 run (void *cls, char *const *args, const char *cfgfile,
373      const struct GNUNET_CONFIGURATION_Handle *cfg)
374 {
375   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
376
377   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
378                                             &notify_receive, &notify_connect,
379                                             &notify_disconnect, &start_cb,
380                                             NULL);
381
382   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
383                                             &notify_receive, &notify_connect,
384                                             &notify_disconnect, &start_cb,
385                                             NULL);
386
387   if ((p1 == NULL) || (p2 == NULL))
388   {
389     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
390     if (die_task != NULL)
391       GNUNET_SCHEDULER_cancel (die_task);
392     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
393     return;
394   }
395 }
396
397
398 static int
399 check ()
400 {
401   static char *const argv[] = { "test-transport-api",
402     "-c",
403     "test_transport_api_data.conf",
404     NULL
405   };
406   static struct GNUNET_GETOPT_CommandLineOption options[] = {
407     GNUNET_GETOPT_OPTION_END
408   };
409
410   send_task = NULL;
411
412   ok = 1;
413   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
414                       "nohelp", options, &run, NULL);
415
416   return ok;
417 }
418
419
420 int
421 main (int argc, char *argv[])
422 {
423   int ret;
424
425   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
426   GNUNET_log_setup (test_name,
427                     "WARNING",
428                     NULL);
429   tth = GNUNET_TRANSPORT_TESTING_init ();
430   GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
431   GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
432   ret = check ();
433   GNUNET_free (cfg_file_p1);
434   GNUNET_free (cfg_file_p2);
435   GNUNET_free (test_name);
436   GNUNET_TRANSPORT_TESTING_done (tth);
437   return ret;
438 }
439
440 /* end of test_transport_api_restart_2peers.c */