1c4a6d2fe3150bf34575ea1548cc644bea48208d
[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_NO
46
47 #define VERBOSE_ARM GNUNET_NO
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", "-q", 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", "-q", NULL);
133
134   GNUNET_OS_process_wait (p->arm_pid);
135 #endif
136
137 #if START_ARM
138   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
139                                         "gnunet-arm",
140 #if VERBOSE
141                                         "-L", "DEBUG",
142 #endif
143                                         "-c", p->cfg_file, "-i", "transport", "-q", NULL);
144
145   GNUNET_OS_process_wait (p->arm_pid);
146 #endif
147 }
148
149
150 static void
151 end_badly ()
152 {
153   /* do work here */
154 #if VERBOSE
155   fprintf(stderr, "Ending on an unhappy note.\n");
156 #endif
157
158   GNUNET_TRANSPORT_disconnect (p1.th);
159   GNUNET_TRANSPORT_disconnect (p2.th);
160   GNUNET_TRANSPORT_disconnect (p3.th);
161
162   ok = 1;
163   return;
164 }
165
166 static void
167 notify_receive (void *cls,
168                 const struct GNUNET_PeerIdentity *peer,
169                 const struct GNUNET_MessageHeader *message,
170                 struct GNUNET_TIME_Relative latency,
171                 uint32_t distance)
172 {
173
174   if (ntohs(message->type) != MTYPE)
175     return;
176
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p) distance %d!\n",
178                 ntohs(message->type), cls, distance);
179
180   GNUNET_assert (MTYPE == ntohs (message->type));
181   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
182                  ntohs (message->size));
183   end ();
184 }
185
186
187 static size_t
188 notify_ready (void *cls, size_t size, void *buf)
189 {
190   struct GNUNET_MessageHeader *hdr;
191
192   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193               "Transmitting message to peer (%p) - %u!\n", cls, size);
194   GNUNET_assert (size >= 256);
195
196   if (buf != NULL)
197   {
198     hdr = buf;
199     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
200     hdr->type = htons (MTYPE);
201   }
202
203   return sizeof (struct GNUNET_MessageHeader);
204 }
205
206
207 static void
208 notify_connect (void *cls,
209                 const struct GNUNET_PeerIdentity *peer,
210                 struct GNUNET_TIME_Relative latency,
211                 uint32_t distance)
212 {
213   int peer_num = 0;
214   int connect_num = 0;
215
216   if (cls == &p1)
217     peer_num = 1;
218   else if (cls == &p2)
219     peer_num = 2;
220   else if (cls == &p3)
221     peer_num = 3;
222
223   if (memcmp(peer, &p1.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
224     connect_num = 1;
225   else if (memcmp(peer, &p2.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
226     connect_num = 2;
227   else if (memcmp(peer, &p3.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
228     connect_num = 3;
229
230   if ((cls == &p1) && (memcmp(peer, &p3.id, sizeof(struct GNUNET_PeerIdentity)) == 0))
231     {
232
233       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234                  "Peer 1 notified about connection to peer 3, distance %u!\n", GNUNET_i2s (peer), cls, distance);
235
236       GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
237                                               &p3.id,
238                                               256, 0, TIMEOUT, &notify_ready,
239                                               &p1);
240     }
241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242               "Peer `%d' connected to peer `%d' distance %d!\n", peer_num, connect_num, distance);
243 }
244
245
246 static void
247 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
248 {
249   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250               "Peer `%4s' disconnected (%p)!\n",
251               GNUNET_i2s (peer), cls);
252 }
253
254
255 static void
256 setup_peer (struct PeerContext *p, const char *cfgname)
257 {
258   p->cfg = GNUNET_CONFIGURATION_create ();
259   p->cfg_file = strdup(cfgname);
260 #if START_ARM
261   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
262                                         "gnunet-arm",
263 #if VERBOSE_ARM
264                                         "-L", "DEBUG",
265 #endif
266                                         "-c", cfgname, "-s", "-q", NULL);
267 #endif
268   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
269
270   /*p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
271                                     p,
272                                     &notify_receive,
273                                     &notify_connect, &notify_disconnect);*/
274   /*GNUNET_assert (p->th != NULL);*/
275 }
276
277
278 static void
279 exchange_hello_last (void *cls,
280                 const struct GNUNET_MessageHeader *message)
281 {
282   struct PeerContext *me = cls;
283
284   GNUNET_TRANSPORT_get_hello_cancel (p3.th, &exchange_hello_last, me);
285
286   GNUNET_assert (message != NULL);
287   GNUNET_assert (GNUNET_OK ==
288                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
289                                       message, &me->id));
290
291   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
293
294   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295               "Finished exchanging HELLOs, now waiting for transmission!\n");
296
297 }
298
299
300 static void
301 exchange_hello_next (void *cls,
302                 const struct GNUNET_MessageHeader *message)
303 {
304   struct PeerContext *me = cls;
305
306   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_next, me);
307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308               "Exchanging HELLO with peer (%p)!\n", cls);
309
310   GNUNET_assert (message != NULL);
311   GNUNET_assert (GNUNET_OK ==
312                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
313                                       message, &me->id));
314
315   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
317
318   GNUNET_TRANSPORT_offer_hello (p3.th, message);
319
320   GNUNET_TRANSPORT_get_hello (p3.th, &exchange_hello_last, &p3);
321
322
323 }
324
325
326 static void
327 exchange_hello (void *cls,
328                 const struct GNUNET_MessageHeader *message)
329 {
330   struct PeerContext *me = cls;
331
332   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
333   p2.th = GNUNET_TRANSPORT_connect (sched, p2.cfg,
334                                     &p2,
335                                     &notify_receive,
336                                     &notify_connect, &notify_disconnect);
337
338   GNUNET_assert(p2.th != NULL);
339
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341               "Exchanging HELLO with peer (%p)!\n", cls);
342
343   GNUNET_assert (message != NULL);
344   GNUNET_assert (GNUNET_OK ==
345                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
346                                       message, &me->id));
347
348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
350
351   GNUNET_TRANSPORT_offer_hello (p2.th, message);
352   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_next, &p2);
353 }
354
355 static void
356 blacklist_setup_third (void *cls,
357                 const struct GNUNET_MessageHeader *message)
358 {
359   struct PeerContext *me = cls;
360   char *blacklist_filename;
361   struct GNUNET_DISK_FileHandle *file;
362   struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
363   char *buf;
364   size_t size;
365
366   GNUNET_TRANSPORT_get_hello_cancel (p3.th, &blacklist_setup_third, &p3);
367
368   GNUNET_assert (message != NULL);
369   GNUNET_assert (GNUNET_OK ==
370                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
371                                       message, &me->id));
372
373   GNUNET_asprintf(&blacklist_filename, "/tmp/test-gnunetd-transport-peer-1/blacklist");
374   if (blacklist_filename != NULL)
375     {
376       file = GNUNET_DISK_file_open(blacklist_filename, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
377                                    GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
378       GNUNET_free(blacklist_filename);
379
380       if (file == NULL)
381         {
382           GNUNET_SCHEDULER_cancel(sched, die_task);
383           GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
384           return;
385         }
386       GNUNET_CRYPTO_hash_to_enc(&me->id.hashPubKey, &peer_enc);
387       size = GNUNET_asprintf(&buf, "%s:%s\n", "tcp", (char *)&peer_enc);
388       GNUNET_DISK_file_write(file, buf, size);
389       GNUNET_free_non_null(buf);
390     }
391
392   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393                 "Restarting transport service (%p) with gnunet-arm -c %s -L DEBUG -k transport!\n", cls, p1.cfg_file);
394
395   restart_transport(&p1);
396
397   p1.th = GNUNET_TRANSPORT_connect (sched, p1.cfg,
398                                     &p1,
399                                     &notify_receive,
400                                     &notify_connect, &notify_disconnect);
401
402   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
403 }
404
405 static void
406 blacklist_setup_first (void *cls,
407                 const struct GNUNET_MessageHeader *message)
408 {
409   struct PeerContext *me = cls;
410   char *blacklist_filename;
411   struct GNUNET_DISK_FileHandle *file;
412   struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
413   char *buf;
414   size_t size;
415
416   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &blacklist_setup_first, me);
417   sleep(2);
418
419   GNUNET_assert (message != NULL);
420   GNUNET_assert (GNUNET_OK ==
421                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
422                                       message, &me->id));
423
424   GNUNET_asprintf(&blacklist_filename, "/tmp/test-gnunetd-transport-peer-3/blacklist");
425   if (blacklist_filename != NULL)
426     {
427       file = GNUNET_DISK_file_open(blacklist_filename, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
428                                    GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
429       GNUNET_free(blacklist_filename);
430
431       if (file == NULL)
432         {
433           GNUNET_SCHEDULER_cancel(sched, die_task);
434           GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
435           return;
436         }
437       GNUNET_CRYPTO_hash_to_enc(&me->id.hashPubKey, &peer_enc);
438       size = GNUNET_asprintf(&buf, "%s:%s\n", "tcp", (char *)&peer_enc);
439       GNUNET_DISK_file_write(file, buf, size);
440       GNUNET_free_non_null(buf);
441     }
442
443   GNUNET_TRANSPORT_disconnect(p1.th);
444
445   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
446                 "Restarting transport service (%p) with gnunet-arm -c %s -L DEBUG -k transport!\n", cls, p3.cfg_file);
447   restart_transport(&p3);
448
449   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450               "reconnecting to transport (%p)!\n", cls);
451   p3.th = GNUNET_TRANSPORT_connect (sched, p3.cfg,
452                                     &p3,
453                                     &notify_receive,
454                                     &notify_connect, &notify_disconnect);
455   if (p3.th != NULL)
456     GNUNET_TRANSPORT_get_hello (p3.th, &blacklist_setup_third, &p3);
457   else
458     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459                   "reconnecting to transport (%p) failed.!\n", cls);
460   //GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
461 }
462
463
464 static void
465 run (void *cls,
466      struct GNUNET_SCHEDULER_Handle *s,
467      char *const *args,
468      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
469 {
470   GNUNET_assert (ok == 1);
471   OKPP;
472   sched = s;
473
474   die_task = GNUNET_SCHEDULER_add_delayed (sched,
475       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5), &end_badly, NULL);
476
477   setup_peer (&p1, "test_transport_api_dv_peer1.conf");
478   setup_peer (&p2, "test_transport_api_dv_peer2.conf");
479   setup_peer (&p3, "test_transport_api_dv_peer3.conf");
480
481   p1.th = GNUNET_TRANSPORT_connect (sched, p1.cfg,
482                                     &p1,
483                                     &notify_receive,
484                                     &notify_connect, &notify_disconnect);
485   GNUNET_assert(p1.th != NULL);
486   /*GNUNET_assert(p2.th != NULL);
487   GNUNET_assert(p3.th != NULL);*/
488
489   GNUNET_TRANSPORT_get_hello (p1.th, &blacklist_setup_first, &p1);
490 }
491
492 static int
493 check ()
494 {
495
496   char *const argv[] = { "test-transport-api",
497     "-c",
498     "test_transport_api_data.conf",
499 #if VERBOSE
500     "-L", "DEBUG",
501 #endif
502     NULL
503   };
504
505   struct GNUNET_GETOPT_CommandLineOption options[] = {
506     GNUNET_GETOPT_OPTION_END
507   };
508
509   ok = 1;
510   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
511                       argv, "test-transport-api", "nohelp",
512                       options, &run, &ok);
513   stop_arm (&p1);
514   stop_arm (&p2);
515   stop_arm (&p3);
516   return ok;
517 }
518
519
520 int
521 main (int argc, char *argv[])
522 {
523   int ret;
524 #ifdef MINGW
525   return GNUNET_SYSERR;
526 #endif
527
528   GNUNET_log_setup ("test-transport-api-dv",
529 #if VERBOSE
530                     "DEBUG",
531 #else
532                     "WARNING",
533 #endif
534                     NULL);
535   ret = check ();
536   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
537   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
538   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-3");
539   return ret;
540 }
541
542 /* end of test_transport_api_dv.c */