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