-makefile for new test_stream_local (commented)
[oweals/gnunet.git] / src / transport / test_transport_api_disconnect.c
1 /*
2      This file is part of GNUnet.
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file transport/test_transport_api_disconnect.c
22  * @brief base test case for transport implementations
23  *
24  * This test case tests disconnect notifications in peer shutdown
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "transport.h"
35 #include "transport-testing.h"
36
37 #define VERBOSE GNUNET_NO
38 #define VERBOSE_ARM GNUNET_NO
39
40 #define START_ARM GNUNET_YES
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
46
47 /**
48  * How long until we give up on transmitting the message?
49  */
50 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
51
52 #define MTYPE 12345
53
54 static char *test_source;
55
56 static char *test_plugin;
57
58 static char *test_name;
59
60 static int ok;
61
62 static GNUNET_SCHEDULER_TaskIdentifier die_task;
63
64 static GNUNET_SCHEDULER_TaskIdentifier send_task;
65
66 struct PeerContext *p1;
67
68 struct PeerContext *p2;
69
70 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
71
72 struct GNUNET_TRANSPORT_TransmitHandle *th;
73
74 struct GNUNET_TRANSPORT_TESTING_handle *tth;
75
76 int shutdown_;
77
78 char *cfg_file_p1;
79
80 char *cfg_file_p2;
81
82 #if VERBOSE
83 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
84 #else
85 #define OKPP do { ok++; } while (0)
86 #endif
87
88
89 static void
90 end ()
91 {
92   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
93
94   if (send_task != GNUNET_SCHEDULER_NO_TASK)
95   {
96     GNUNET_SCHEDULER_cancel (send_task);
97     send_task = GNUNET_SCHEDULER_NO_TASK;
98   }
99
100   if (die_task != GNUNET_SCHEDULER_NO_TASK)
101   {
102     GNUNET_SCHEDULER_cancel (die_task);
103     die_task = GNUNET_SCHEDULER_NO_TASK;
104   }
105
106   if (th != NULL)
107     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
108   th = NULL;
109
110   if (p1 != NULL)
111     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
112   p1 = NULL;
113
114   if (p2 != NULL)
115     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
116   p2 = NULL;
117
118   /* success */
119   ok = 0;
120 }
121
122 static void
123 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
124 {
125   die_task = GNUNET_SCHEDULER_NO_TASK;
126
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
128
129   if (cc != NULL)
130   {
131     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
132     cc = NULL;
133   }
134
135   if (send_task != GNUNET_SCHEDULER_NO_TASK)
136   {
137     GNUNET_SCHEDULER_cancel (send_task);
138     send_task = GNUNET_SCHEDULER_NO_TASK;
139   }
140
141   if (th != NULL)
142     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
143   th = NULL;
144
145   if (p1 != NULL)
146     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
147   if (p2 != NULL)
148     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
149
150   ok = GNUNET_SYSERR;
151 }
152
153 static void
154 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
155 {
156   struct PeerContext *p = cls;
157   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
158
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
161               GNUNET_i2s (peer));
162
163   if (th != NULL)
164     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
165   th = NULL;
166
167   if (shutdown_ == GNUNET_YES)
168   {
169     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Complete, shutting down...\n");
170     GNUNET_SCHEDULER_add_now (&end, NULL);
171   }
172 }
173
174
175 static void
176 stop_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
177 {
178   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
179     return;
180
181   struct PeerContext *p = cls;
182
183   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down peer %u (`%s')\n", p->no,
184               GNUNET_i2s (&p->id));
185   shutdown_ = GNUNET_YES;
186   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
187   p2 = NULL;
188   GNUNET_assert (p2 == NULL);
189 }
190
191 static void
192 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
193                 const struct GNUNET_MessageHeader *message,
194                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
195 {
196   struct PeerContext *p = cls;
197   struct PeerContext *t = NULL;
198
199   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
200     t = p1;
201   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
202     t = p2;
203   GNUNET_assert (t != NULL);
204
205   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
206
207   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
209               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
210               GNUNET_i2s (&t->id));
211
212   if ((MTYPE == ntohs (message->type)) &&
213       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
214   {
215     ok = 1;
216     GNUNET_SCHEDULER_add_now (stop_peer, p2);
217     return;
218   }
219 }
220
221
222 static size_t
223 notify_ready (void *cls, size_t size, void *buf)
224 {
225   struct PeerContext *p = cls;
226   struct GNUNET_MessageHeader *hdr;
227
228   th = NULL;
229
230   if (buf == NULL)
231   {
232     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233                 "Timeout occurred while waiting for transmit_ready\n");
234     if (GNUNET_SCHEDULER_NO_TASK != 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 = GNUNET_SCHEDULER_NO_TASK;
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, 0,
275                                                TIMEOUT_TRANSMIT, &notify_ready,
276                                                p1);
277 }
278
279
280 static void
281 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
282                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
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     t = p1;
292   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
293     t = p2;
294   GNUNET_assert (t != NULL);
295
296   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
297
298   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
300               t->no, GNUNET_i2s (peer));
301   GNUNET_free (ps);
302 }
303
304
305 static void
306 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
307 {
308   cc = NULL;
309   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
310
311   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
312               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
313   GNUNET_free (p1_c);
314
315   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
316 }
317
318
319
320 void
321 start_cb (struct PeerContext *p, void *cls)
322 {
323   static int started;
324
325   started++;
326
327   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
328               GNUNET_i2s (&p->id));
329
330   if (started != 2)
331     return;
332
333   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
334
335   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
337               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
338
339   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
340                                                NULL);
341
342 }
343
344 static void
345 run (void *cls, char *const *args, const char *cfgfile,
346      const struct GNUNET_CONFIGURATION_Handle *cfg)
347 {
348   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
349
350   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
351                                             &notify_receive, &notify_connect,
352                                             &notify_disconnect, &start_cb,
353                                             NULL);
354
355   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
356                                             &notify_receive, &notify_connect,
357                                             &notify_disconnect, &start_cb,
358                                             NULL);
359
360   if ((p1 == NULL) || (p2 == NULL))
361   {
362     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
363     if (die_task != GNUNET_SCHEDULER_NO_TASK)
364       GNUNET_SCHEDULER_cancel (die_task);
365     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
366     return;
367   }
368 }
369
370
371 static int
372 check ()
373 {
374   static char *const argv[] = { "test-transport-api",
375     "-c",
376     "test_transport_api_data.conf",
377 #if VERBOSE
378     "-L", "DEBUG",
379 #endif
380     NULL
381   };
382   static struct GNUNET_GETOPT_CommandLineOption options[] = {
383     GNUNET_GETOPT_OPTION_END
384   };
385
386 #if WRITECONFIG
387   setTransportOptions ("test_transport_api_data.conf");
388 #endif
389   send_task = GNUNET_SCHEDULER_NO_TASK;
390
391   ok = 1;
392   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
393                       "nohelp", options, &run, &ok);
394
395   return ok;
396 }
397
398 int
399 main (int argc, char *argv[])
400 {
401   int ret;
402
403   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
404
405   GNUNET_log_setup (test_name,
406 #if VERBOSE
407                     "DEBUG",
408 #else
409                     "WARNING",
410 #endif
411                     NULL);
412
413   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
414   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
415                                                  &test_plugin);
416
417   tth = GNUNET_TRANSPORT_TESTING_init ();
418
419   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
420   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
421
422   ret = check ();
423
424   GNUNET_free (cfg_file_p1);
425   GNUNET_free (cfg_file_p2);
426
427   GNUNET_free (test_source);
428   GNUNET_free (test_plugin);
429   GNUNET_free (test_name);
430
431   GNUNET_TRANSPORT_TESTING_done (tth);
432
433   return ret;
434 }
435
436 /* end of test_transport_api.c */