make weakness more explicit
[oweals/gnunet.git] / src / dv / test_transport_api_dv.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 2, 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_dv.c
22  * @brief base test case for dv transport (separated from other transport
23  * testcases for two reasons. 1) dv-service relies on core and other
24  * transport plugins, dv plugin relies on dv-service, so dv-plugin needs
25  * to live here, and 2) a dv plugin testcase is different from other
26  * tranport plugin testcases because we need at least three peer to test
27  * it.
28  *
29  * This test case tests DV functionality.  Specifically it starts three
30  * peers connected in a line (1 <-> 2 <-> 3).  Then a message is transmitted
31  * from peer 1 to peer 3.  Assuming that DV is working, peer 2 should have
32  * gossiped about peer 3 to 1, and should then forward a message from one
33  * to 3.
34  */
35 #include "platform.h"
36 #include "gnunet_common.h"
37 #include "gnunet_hello_lib.h"
38 #include "gnunet_getopt_lib.h"
39 #include "gnunet_os_lib.h"
40 #include "gnunet_program_lib.h"
41 #include "gnunet_scheduler_lib.h"
42 #include "gnunet_transport_service.h"
43 #include "../transport/transport.h"
44
45 #define VERBOSE GNUNET_YES
46
47 #define VERBOSE_ARM GNUNET_YES
48
49 #define START_ARM GNUNET_YES
50
51 /**
52  * How long until we give up on transmitting the message?
53  */
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
55
56 #define MTYPE 12345
57
58 struct PeerContext
59 {
60   struct GNUNET_CONFIGURATION_Handle *cfg;
61   struct GNUNET_TRANSPORT_Handle *th;
62   struct GNUNET_PeerIdentity id;
63   const char *cfg_file;
64   const struct GNUNET_HELLO_Message *hello;
65 #if START_ARM
66   pid_t arm_pid;
67 #endif
68 };
69
70 static struct PeerContext p1;
71
72 static struct PeerContext p2;
73
74 static struct PeerContext p3;
75
76 static struct GNUNET_SCHEDULER_Handle *sched;
77
78 static int ok;
79
80 GNUNET_SCHEDULER_TaskIdentifier die_task;
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   /* do work here */
93   GNUNET_SCHEDULER_cancel (sched, die_task);
94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transport 1!\n");
95   GNUNET_TRANSPORT_disconnect (p1.th);
96   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transport 2!\n");
97   GNUNET_TRANSPORT_disconnect (p2.th);
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transport 3!\n");
99   GNUNET_TRANSPORT_disconnect (p3.th);
100
101   die_task = GNUNET_SCHEDULER_NO_TASK;
102   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
103   ok = 0;
104 }
105
106 static void
107 stop_arm (struct PeerContext *p)
108 {
109 #if START_ARM
110   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
111                                         "gnunet-arm",
112 #if VERBOSE
113                                         "-L", "DEBUG",
114 #endif
115                                         "-c", p->cfg_file, "-e", NULL);
116
117   GNUNET_OS_process_wait (p->arm_pid);
118 #endif
119   GNUNET_CONFIGURATION_destroy (p->cfg);
120 }
121
122
123 static void
124 restart_transport (struct PeerContext *p)
125 {
126 #if START_ARM
127   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
128                                         "gnunet-arm",
129 #if VERBOSE
130                                         "-L", "DEBUG",
131 #endif
132                                         "-c", p->cfg_file, "-k", "transport", NULL);
133
134   fprintf(stderr, "stop arm command returned %d\n", p->arm_pid);
135   GNUNET_OS_process_wait (p->arm_pid);
136 #endif
137
138 #if START_ARM
139   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
140                                         "gnunet-arm",
141 #if VERBOSE
142                                         "-L", "DEBUG",
143 #endif
144                                         "-c", p->cfg_file, "-i", "transport", NULL);
145
146   GNUNET_OS_process_wait (p->arm_pid);
147 #endif
148 }
149
150
151 static void
152 end_badly ()
153 {
154   /* do work here */
155 #if VERBOSE
156   fprintf(stderr, "Ending on an unhappy note.\n");
157 #endif
158
159   GNUNET_TRANSPORT_disconnect (p1.th);
160   GNUNET_TRANSPORT_disconnect (p2.th);
161   GNUNET_TRANSPORT_disconnect (p3.th);
162
163   ok = 1;
164   return;
165 }
166
167 static void
168 notify_receive (void *cls,
169                 const struct GNUNET_PeerIdentity *peer,
170                 const struct GNUNET_MessageHeader *message,
171                 struct GNUNET_TIME_Relative latency,
172                 uint32_t distance)
173 {
174
175   if (ntohs(message->type) != MTYPE)
176     return;
177
178   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p) distance %d!\n",
179                 ntohs(message->type), cls, distance);
180
181   GNUNET_assert (MTYPE == ntohs (message->type));
182   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
183                  ntohs (message->size));
184   end ();
185 }
186
187
188 static size_t
189 notify_ready (void *cls, size_t size, void *buf)
190 {
191   struct GNUNET_MessageHeader *hdr;
192
193   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194               "Transmitting message to peer (%p) - %u!\n", cls, size);
195   GNUNET_assert (size >= 256);
196
197   if (buf != NULL)
198   {
199     hdr = buf;
200     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
201     hdr->type = htons (MTYPE);
202   }
203
204   return sizeof (struct GNUNET_MessageHeader);
205 }
206
207
208 static void
209 notify_connect (void *cls,
210                 const struct GNUNET_PeerIdentity *peer,
211                 struct GNUNET_TIME_Relative latency,
212                 uint32_t distance)
213 {
214   int peer_num = 0;
215   int connect_num = 0;
216
217   if (cls == &p1)
218     peer_num = 1;
219   else if (cls == &p2)
220     peer_num = 2;
221   else if (cls == &p3)
222     peer_num = 3;
223
224   if (memcmp(peer, &p1.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
225     connect_num = 1;
226   else if (memcmp(peer, &p2.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
227     connect_num = 2;
228   else if (memcmp(peer, &p3.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
229     connect_num = 3;
230
231   if ((cls == &p1) && (memcmp(peer, &p3.id, sizeof(struct GNUNET_PeerIdentity)) == 0))
232     {
233
234       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235                  "Peer 1 notified about connection to peer 3, distance %d!\n", GNUNET_i2s (peer), cls, distance);
236
237       GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
238                                               &p3.id,
239                                               256, 0, TIMEOUT, &notify_ready,
240                                               &p1);
241     }
242   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243               "Peer `%d' connected to peer `%d' distance %d!\n", peer_num, connect_num, distance);
244 }
245
246
247 static void
248 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
249 {
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251               "Peer `%4s' disconnected (%p)!\n",
252               GNUNET_i2s (peer), cls);
253 }
254
255
256 static void
257 setup_peer (struct PeerContext *p, const char *cfgname)
258 {
259   p->cfg = GNUNET_CONFIGURATION_create ();
260   p->cfg_file = strdup(cfgname);
261 #if START_ARM
262   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
263                                         "gnunet-arm",
264 #if VERBOSE_ARM
265                                         "-L", "DEBUG",
266 #endif
267                                         "-c", cfgname, "-s", NULL);
268 #endif
269   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
270
271   /*p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
272                                     p,
273                                     &notify_receive,
274                                     &notify_connect, &notify_disconnect);*/
275   /*GNUNET_assert (p->th != NULL);*/
276 }
277
278
279 static void
280 exchange_hello_last (void *cls,
281                 const struct GNUNET_MessageHeader *message)
282 {
283   struct PeerContext *me = cls;
284
285   GNUNET_TRANSPORT_get_hello_cancel (p3.th, &exchange_hello_last, me);
286
287   GNUNET_assert (message != NULL);
288   GNUNET_assert (GNUNET_OK ==
289                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
290                                       message, &me->id));
291
292   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
294
295   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296               "Finished exchanging HELLOs, now waiting for transmission!\n");
297
298 }
299
300
301 static void
302 exchange_hello_next (void *cls,
303                 const struct GNUNET_MessageHeader *message)
304 {
305   struct PeerContext *me = cls;
306
307   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_next, me);
308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309               "Exchanging HELLO with peer (%p)!\n", cls);
310
311   GNUNET_assert (message != NULL);
312   GNUNET_assert (GNUNET_OK ==
313                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
314                                       message, &me->id));
315
316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
318
319   GNUNET_TRANSPORT_offer_hello (p3.th, message);
320
321   GNUNET_TRANSPORT_get_hello (p3.th, &exchange_hello_last, &p3);
322
323
324 }
325
326
327 static void
328 exchange_hello (void *cls,
329                 const struct GNUNET_MessageHeader *message)
330 {
331   struct PeerContext *me = cls;
332
333   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
334   p2.th = GNUNET_TRANSPORT_connect (sched, p2.cfg,
335                                     &p2,
336                                     &notify_receive,
337                                     &notify_connect, &notify_disconnect);
338
339   GNUNET_assert(p2.th != NULL);
340
341   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342               "Exchanging HELLO with peer (%p)!\n", cls);
343
344   GNUNET_assert (message != NULL);
345   GNUNET_assert (GNUNET_OK ==
346                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
347                                       message, &me->id));
348
349   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
351
352   GNUNET_TRANSPORT_offer_hello (p2.th, message);
353   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_next, &p2);
354 }
355
356 static void
357 blacklist_setup_third (void *cls,
358                 const struct GNUNET_MessageHeader *message)
359 {
360   struct PeerContext *me = cls;
361   char *blacklist_filename;
362   struct GNUNET_DISK_FileHandle *file;
363   struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
364   char *buf;
365   size_t size;
366
367   GNUNET_TRANSPORT_get_hello_cancel (p3.th, &blacklist_setup_third, &p3);
368
369   GNUNET_assert (message != NULL);
370   GNUNET_assert (GNUNET_OK ==
371                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
372                                       message, &me->id));
373
374   GNUNET_asprintf(&blacklist_filename, "/tmp/test-gnunetd-transport-peer-1/blacklist");
375   if (blacklist_filename != NULL)
376     {
377       file = GNUNET_DISK_file_open(blacklist_filename, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
378                                    GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
379       GNUNET_free(blacklist_filename);
380
381       if (file == NULL)
382         {
383           GNUNET_SCHEDULER_cancel(sched, die_task);
384           GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
385           return;
386         }
387       GNUNET_CRYPTO_hash_to_enc(&me->id.hashPubKey, &peer_enc);
388       size = GNUNET_asprintf(&buf, "%s:%s\n", "tcp", (char *)&peer_enc);
389       GNUNET_DISK_file_write(file, buf, size);
390       GNUNET_free_non_null(buf);
391     }
392
393   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394                 "Restarting transport service (%p) with gnunet-arm -c %s -L DEBUG -k transport!\n", cls, p1.cfg_file);
395
396   restart_transport(&p1);
397
398   p1.th = GNUNET_TRANSPORT_connect (sched, p1.cfg,
399                                     &p1,
400                                     &notify_receive,
401                                     &notify_connect, &notify_disconnect);
402
403   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
404 }
405
406 static void
407 blacklist_setup_first (void *cls,
408                 const struct GNUNET_MessageHeader *message)
409 {
410   struct PeerContext *me = cls;
411   char *blacklist_filename;
412   struct GNUNET_DISK_FileHandle *file;
413   struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
414   char *buf;
415   size_t size;
416
417   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &blacklist_setup_first, me);
418   sleep(2);
419
420   GNUNET_assert (message != NULL);
421   GNUNET_assert (GNUNET_OK ==
422                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
423                                       message, &me->id));
424
425   GNUNET_asprintf(&blacklist_filename, "/tmp/test-gnunetd-transport-peer-3/blacklist");
426   if (blacklist_filename != NULL)
427     {
428       file = GNUNET_DISK_file_open(blacklist_filename, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
429                                    GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
430       GNUNET_free(blacklist_filename);
431
432       if (file == NULL)
433         {
434           GNUNET_SCHEDULER_cancel(sched, die_task);
435           GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
436           return;
437         }
438       GNUNET_CRYPTO_hash_to_enc(&me->id.hashPubKey, &peer_enc);
439       size = GNUNET_asprintf(&buf, "%s:%s\n", "tcp", (char *)&peer_enc);
440       GNUNET_DISK_file_write(file, buf, size);
441       GNUNET_free_non_null(buf);
442     }
443
444   GNUNET_TRANSPORT_disconnect(p1.th);
445
446   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447                 "Restarting transport service (%p) with gnunet-arm -c %s -L DEBUG -k transport!\n", cls, p3.cfg_file);
448   restart_transport(&p3);
449
450   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451               "reconnecting to transport (%p)!\n", cls);
452   p3.th = GNUNET_TRANSPORT_connect (sched, p3.cfg,
453                                     &p3,
454                                     &notify_receive,
455                                     &notify_connect, &notify_disconnect);
456   if (p3.th != NULL)
457     GNUNET_TRANSPORT_get_hello (p3.th, &blacklist_setup_third, &p3);
458   else
459     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460                   "reconnecting to transport (%p) failed.!\n", cls);
461   //GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
462 }
463
464
465 static void
466 run (void *cls,
467      struct GNUNET_SCHEDULER_Handle *s,
468      char *const *args,
469      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
470 {
471   GNUNET_assert (ok == 1);
472   OKPP;
473   sched = s;
474
475   die_task = GNUNET_SCHEDULER_add_delayed (sched,
476       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5), &end_badly, NULL);
477
478   setup_peer (&p1, "test_transport_api_dv_peer1.conf");
479   setup_peer (&p2, "test_transport_api_dv_peer2.conf");
480   setup_peer (&p3, "test_transport_api_dv_peer3.conf");
481
482   p1.th = GNUNET_TRANSPORT_connect (sched, p1.cfg,
483                                     &p1,
484                                     &notify_receive,
485                                     &notify_connect, &notify_disconnect);
486   GNUNET_assert(p1.th != NULL);
487   /*GNUNET_assert(p2.th != NULL);
488   GNUNET_assert(p3.th != NULL);*/
489
490   GNUNET_TRANSPORT_get_hello (p1.th, &blacklist_setup_first, &p1);
491 }
492
493 static int
494 check ()
495 {
496
497   char *const argv[] = { "test-transport-api",
498     "-c",
499     "test_transport_api_data.conf",
500 #if VERBOSE
501     "-L", "DEBUG",
502 #endif
503     NULL
504   };
505
506   struct GNUNET_GETOPT_CommandLineOption options[] = {
507     GNUNET_GETOPT_OPTION_END
508   };
509
510   ok = 1;
511   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
512                       argv, "test-transport-api", "nohelp",
513                       options, &run, &ok);
514   stop_arm (&p1);
515   stop_arm (&p2);
516   stop_arm (&p3);
517   return ok;
518 }
519
520
521 int
522 main (int argc, char *argv[])
523 {
524   int ret;
525 #ifdef MINGW
526   return GNUNET_SYSERR;
527 #endif
528
529   GNUNET_log_setup ("test-transport-api-dv",
530 #if VERBOSE
531                     "DEBUG",
532 #else
533                     "WARNING",
534 #endif
535                     NULL);
536   ret = check ();
537   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
538   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
539   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-3");
540   return ret;
541 }
542
543 /* end of test_transport_api_dv.c */