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