indentation
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist_learning.c
1 /*
2      This file is part of GNUnet
3      (C) 2009, 2010, 2011 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 hostlist/test_gnunet_daemon_hostlist.c
22  * @brief test for gnunet_daemon_hostslist.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_resolver_service.h"
31 #include "gnunet_statistics_service.h"
32
33 #define VERBOSE GNUNET_NO
34
35 #define START_ARM GNUNET_YES
36
37 #define MAX_URL_LEN 1000
38
39 /**
40  * How long until wait until testcases fails
41  */
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 180)
43
44 #define CHECK_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
45
46
47 struct PeerContext
48 {
49   struct GNUNET_CONFIGURATION_Handle *cfg;
50   struct GNUNET_TRANSPORT_Handle *th;
51   struct GNUNET_MessageHeader *hello;
52   struct GNUNET_CORE_Handle *core;
53   struct GNUNET_STATISTICS_Handle *stats;
54 #if START_ARM
55   struct GNUNET_OS_Process *arm_proc;
56 #endif
57 };
58
59 static int timeout;
60
61 static int adv_sent;
62
63 static int adv_arrived;
64
65 static int learned_hostlist_saved;
66
67 static int learned_hostlist_downloaded;
68
69 static char *current_adv_uri;
70
71 static const struct GNUNET_CONFIGURATION_Handle *cfg;
72
73 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
74
75 static GNUNET_SCHEDULER_TaskIdentifier check_task;
76
77 static struct PeerContext adv_peer;
78
79 static struct PeerContext learn_peer;
80
81 static struct GNUNET_STATISTICS_GetHandle *download_stats;
82
83 static struct GNUNET_STATISTICS_GetHandle *urisrecv_stat;
84
85 static struct GNUNET_STATISTICS_GetHandle *advsent_stat;
86
87
88 static void
89 shutdown_testcase ()
90 {
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown testcase....\n");
92   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
93   {
94     GNUNET_SCHEDULER_cancel (timeout_task);
95     timeout_task = GNUNET_SCHEDULER_NO_TASK;
96   }
97   if (NULL != download_stats)
98   {
99     GNUNET_STATISTICS_get_cancel (download_stats);
100     download_stats = NULL;
101   }
102   if (NULL != urisrecv_stat)
103   {
104     GNUNET_STATISTICS_get_cancel (urisrecv_stat);
105     urisrecv_stat = NULL;
106   }
107   if (NULL != advsent_stat)
108   {
109     GNUNET_STATISTICS_get_cancel (advsent_stat);
110     advsent_stat = NULL;
111   }
112   if (check_task != GNUNET_SCHEDULER_NO_TASK)
113   {
114     GNUNET_SCHEDULER_cancel (check_task);
115     check_task = GNUNET_SCHEDULER_NO_TASK;
116   }
117   if (NULL != current_adv_uri)
118   {
119     GNUNET_free (current_adv_uri);
120     current_adv_uri = NULL;
121   }
122   if (adv_peer.th != NULL)
123   {
124     GNUNET_TRANSPORT_disconnect (adv_peer.th);
125     adv_peer.th = NULL;
126   }
127   if (learn_peer.th != NULL)
128   {
129     GNUNET_TRANSPORT_disconnect (learn_peer.th);
130     learn_peer.th = NULL;
131   }
132   if (adv_peer.core != NULL)
133   {
134     GNUNET_CORE_disconnect (adv_peer.core);
135     adv_peer.core = NULL;
136   }
137   if (learn_peer.core != NULL)
138   {
139     GNUNET_CORE_disconnect (learn_peer.core);
140     learn_peer.core = NULL;
141   }
142 #if START_ARM
143   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144               "Killing hostlist server ARM process.\n");
145   if (0 != GNUNET_OS_process_kill (adv_peer.arm_proc, SIGTERM))
146     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
147   if (GNUNET_OS_process_wait (adv_peer.arm_proc) != GNUNET_OK)
148     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
149   GNUNET_OS_process_close (adv_peer.arm_proc);
150   adv_peer.arm_proc = NULL;
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152               "Killing hostlist client ARM process.\n");
153   if (0 != GNUNET_OS_process_kill (learn_peer.arm_proc, SIGTERM))
154     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
155   if (GNUNET_OS_process_wait (learn_peer.arm_proc) != GNUNET_OK)
156     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
157   GNUNET_OS_process_close (learn_peer.arm_proc);
158   learn_peer.arm_proc = NULL;
159 #endif
160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown complete....\n");
161 }
162
163 /**
164  * Timeout, give up.
165  */
166 static void
167 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
168 {
169   timeout_task = GNUNET_SCHEDULER_NO_TASK;
170   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171               "Timeout while executing testcase, test failed.\n");
172   timeout = GNUNET_YES;
173   shutdown_testcase ();
174 }
175
176
177 static void
178 process_downloads_done (void *cls, int success)
179 {
180   download_stats = NULL;
181 }
182
183
184 static int
185 process_downloads (void *cls,
186                    const char *subsystem,
187                    const char *name, uint64_t value, int is_persistent)
188 {
189   if ((value >= 2) && (learned_hostlist_downloaded == GNUNET_NO))
190   {
191     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
192                 "Peer has successfully downloaded advertised URI\n");
193     learned_hostlist_downloaded = GNUNET_YES;
194     if ((learned_hostlist_saved == GNUNET_YES) && (adv_sent == GNUNET_YES))
195       shutdown_testcase ();
196   }
197   return GNUNET_OK;
198 }
199
200
201 static void
202 process_uris_recv_done (void *cls, int success)
203 {
204   urisrecv_stat = NULL;
205 }
206
207
208 static int
209 process_uris_recv (void *cls,
210                    const char *subsystem,
211                    const char *name, uint64_t value, int is_persistent)
212 {
213   if (((struct PeerContext *) cls == &learn_peer) &&
214       (value == 1) && (learned_hostlist_saved == GNUNET_NO))
215   {
216     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
217                 "Peer has successfully saved advertised URI\n");
218     learned_hostlist_saved = GNUNET_YES;
219     if ((learned_hostlist_downloaded == GNUNET_YES) && (adv_sent == GNUNET_YES))
220       shutdown_testcase ();
221   }
222   return GNUNET_OK;
223 }
224
225
226 static void
227 process_adv_sent_done (void *cls, int success)
228 {
229   advsent_stat = NULL;
230 }
231
232
233 static int
234 process_adv_sent (void *cls,
235                   const char *subsystem,
236                   const char *name, uint64_t value, int is_persistent)
237 {
238   if ((value >= 1) && (adv_sent == GNUNET_NO))
239   {
240     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
241                 "Server has successfully sent advertisement\n");
242     adv_sent = GNUNET_YES;
243     if ((learned_hostlist_downloaded == GNUNET_YES) &&
244         (learned_hostlist_saved == GNUNET_YES))
245       shutdown_testcase ();
246   }
247   return GNUNET_OK;
248 }
249
250
251 /**
252  * Check the server statistics regularly
253  */
254 static void
255 check_statistics (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
256 {
257   char *stat;
258
259   check_task = GNUNET_SCHEDULER_NO_TASK;
260   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
261     return;
262   GNUNET_asprintf (&stat,
263                    gettext_noop ("# advertised URI `%s' downloaded"),
264                    current_adv_uri);
265   if (NULL != learn_peer.stats)
266   {
267     if (NULL != download_stats)
268       GNUNET_STATISTICS_get_cancel (download_stats);
269     download_stats = GNUNET_STATISTICS_get (learn_peer.stats,
270                                             "hostlist",
271                                             stat,
272                                             GNUNET_TIME_UNIT_MINUTES,
273                                             &process_downloads_done,
274                                             &process_downloads, &learn_peer);
275     if (NULL != urisrecv_stat)
276       GNUNET_STATISTICS_get_cancel (urisrecv_stat);
277     urisrecv_stat = GNUNET_STATISTICS_get (learn_peer.stats,
278                                            "hostlist",
279                                            gettext_noop
280                                            ("# advertised hostlist URIs"),
281                                            GNUNET_TIME_UNIT_MINUTES,
282                                            &process_uris_recv_done,
283                                            &process_uris_recv, &learn_peer);
284   }
285   GNUNET_free (stat);
286   if (NULL != adv_peer.stats)
287   {
288     if (NULL != advsent_stat)
289       GNUNET_STATISTICS_get_cancel (advsent_stat);
290     advsent_stat = GNUNET_STATISTICS_get (adv_peer.stats,
291                                           "hostlist",
292                                           gettext_noop
293                                           ("# hostlist advertisements send"),
294                                           GNUNET_TIME_UNIT_MINUTES,
295                                           &process_adv_sent_done,
296                                           &process_adv_sent, NULL);
297   }
298   check_task = GNUNET_SCHEDULER_add_delayed (CHECK_INTERVALL,
299                                              &check_statistics, NULL);
300 }
301
302
303 /**
304  * Core handler for p2p hostlist advertisements
305  */
306 static int
307 ad_arrive_handler (void *cls,
308                    const struct GNUNET_PeerIdentity *peer,
309                    const struct GNUNET_MessageHeader *message,
310                    const struct GNUNET_TRANSPORT_ATS_Information *atsi)
311 {
312   char *hostname;
313   char *expected_uri;
314   unsigned long long port;
315   const struct GNUNET_MessageHeader *incoming;
316   const char *end;
317
318   if (-1 == GNUNET_CONFIGURATION_get_value_number (adv_peer.cfg,
319                                                    "HOSTLIST",
320                                                    "HTTPPORT", &port))
321   {
322     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
323                 "Could not read advertising server's configuration\n");
324     return GNUNET_SYSERR;
325   }
326
327   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (adv_peer.cfg,
328                                                               "HOSTLIST",
329                                                               "EXTERNAL_DNS_NAME",
330                                                               &hostname))
331     hostname = GNUNET_RESOLVER_local_fqdn_get ();
332   GNUNET_asprintf (&expected_uri,
333                    "http://%s:%u/",
334                    hostname != NULL ? hostname : "localhost",
335                    (unsigned int) port);
336   incoming = (const struct GNUNET_MessageHeader *) message;
337   end = (const char *) &incoming[1];
338   if ('\0' !=
339       end[ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - 1])
340   {
341     GNUNET_break (0);
342     GNUNET_free (expected_uri);
343     GNUNET_free_non_null (hostname);
344     return GNUNET_SYSERR;
345   }
346   current_adv_uri = GNUNET_strdup (end);
347   if (0 == strcmp (expected_uri, current_adv_uri))
348   {
349     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
350                 "Received hostlist advertisement with URI `%s' as expected\n",
351                 current_adv_uri);
352     adv_arrived = GNUNET_YES;
353     adv_sent = GNUNET_YES;
354   }
355   else
356     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
357                 "Expected URI `%s' and recieved URI `%s' differ\n",
358                 expected_uri, current_adv_uri);
359   GNUNET_free (expected_uri);
360   GNUNET_free_non_null (hostname);
361   return GNUNET_OK;
362 }
363
364
365 /**
366  * List of handlers if we are learning.
367  */
368 static struct GNUNET_CORE_MessageHandler learn_handlers[] = {
369   {&ad_arrive_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
370   {NULL, 0, 0}
371 };
372
373
374 static void
375 setup_learn_peer (struct PeerContext *p, const char *cfgname)
376 {
377   char *filename;
378   unsigned int result;
379
380   p->cfg = GNUNET_CONFIGURATION_create ();
381 #if START_ARM
382   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
383                                          "gnunet-service-arm",
384 #if VERBOSE
385                                          "-L", "DEBUG",
386 #endif
387                                          "-c", cfgname, NULL);
388 #endif
389   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
390   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (p->cfg,
391                                                           "HOSTLIST",
392                                                           "HOSTLISTFILE",
393                                                           &filename))
394   {
395     if (GNUNET_YES == GNUNET_DISK_file_test (filename))
396     {
397       result = UNLINK (filename);
398       if (result == 0)
399         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
400                     _("Hostlist file `%s' was removed\n"), filename);
401     }
402     GNUNET_free (filename);
403   }
404   p->core = GNUNET_CORE_connect (p->cfg,
405                                  1,
406                                  NULL,
407                                  NULL,
408                                  NULL, NULL, NULL,
409                                  NULL, GNUNET_NO,
410                                  NULL, GNUNET_NO, learn_handlers);
411   GNUNET_assert (NULL != p->core);
412   p->stats = GNUNET_STATISTICS_create ("hostlist", p->cfg);
413   GNUNET_assert (NULL != p->stats);
414 }
415
416
417 static void
418 setup_adv_peer (struct PeerContext *p, const char *cfgname)
419 {
420   p->cfg = GNUNET_CONFIGURATION_create ();
421 #if START_ARM
422   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
423                                          "gnunet-service-arm",
424 #if VERBOSE
425                                          "-L", "DEBUG",
426 #endif
427                                          "-c", cfgname, NULL);
428 #endif
429   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
430   p->stats = GNUNET_STATISTICS_create ("hostlist", p->cfg);
431   GNUNET_assert (NULL != p->stats);
432 }
433
434
435 static void
436 run (void *cls,
437      char *const *args,
438      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
439 {
440   timeout = GNUNET_NO;
441   adv_sent = GNUNET_NO;
442
443   adv_arrived = 0;
444   learned_hostlist_saved = GNUNET_NO;
445   learned_hostlist_downloaded = GNUNET_NO;
446
447   cfg = c;
448
449   setup_adv_peer (&adv_peer, "test_learning_adv_peer.conf");
450   setup_learn_peer (&learn_peer, "test_learning_learn_peer.conf");
451   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
452
453   check_task = GNUNET_SCHEDULER_add_delayed (CHECK_INTERVALL,
454                                              &check_statistics, NULL);
455 }
456
457
458 static int
459 check ()
460 {
461   unsigned int failed;
462
463   char *const argv[] = {
464     "test-gnunet-daemon-hostlist-learning",
465     "-c", "learning_data.conf",
466 #if VERBOSE
467     "-L", "DEBUG",
468 #endif
469     NULL
470   };
471   struct GNUNET_GETOPT_CommandLineOption options[] = {
472     GNUNET_GETOPT_OPTION_END
473   };
474
475   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
476                       argv, "test-gnunet-daemon-hostlist-learning",
477                       "nohelp", options, &run, NULL);
478   failed = GNUNET_NO;
479   if (timeout == GNUNET_YES)
480   {
481     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n");
482     failed = GNUNET_YES;
483   }
484   if (adv_arrived != GNUNET_YES)
485   {
486     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
487                 "Learning peer did not receive advertisement from server\n");
488     failed = GNUNET_YES;
489   }
490   if (learned_hostlist_saved == GNUNET_NO)
491   {
492     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
493                 "Advertised hostlist was not saved in datastore\n");
494     failed = GNUNET_YES;
495   }
496   if (learned_hostlist_downloaded == GNUNET_NO)
497   {
498     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
499                 "Advertised hostlist could not be downloaded from server\n");
500     failed = GNUNET_YES;
501   }
502   if (adv_sent == GNUNET_NO)
503   {
504     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
505                 "Advertised was not sent from server to client\n");
506     failed = GNUNET_YES;
507   }
508   if (GNUNET_YES == failed)
509     return GNUNET_YES;
510   return GNUNET_NO;
511 }
512
513
514 int
515 main (int argc, char *argv[])
516 {
517   int ret;
518
519   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
520   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
521   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
522 #if VERBOSE
523                     "DEBUG",
524 #else
525                     "WARNING",
526 #endif
527                     NULL);
528 #if !WINDOWS
529   system ("gnunet-peerinfo -s -c test_learning_adv_peer.conf > /dev/null");
530   system ("gnunet-peerinfo -s -c test_learning_learn_peer.conf > /dev/null");
531 #else
532   system ("gnunet-peerinfo -s -c test_learning_adv_peer.conf > NUL");
533   system ("gnunet-peerinfo -s -c test_learning_learn_peer.conf > NUL");
534 #endif
535   ret = check ();
536   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
537   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
538   if (GNUNET_YES == GNUNET_DISK_file_test ("hostlists_learn_peer.file"))
539   {
540     if (0 == UNLINK ("hostlists_learn_peer.file"))
541       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
542                   "Hostlist file hostlists_learn_peer.file was removed\n");
543   }
544   return ret;
545 }
546
547 /* end of test_gnunet_daemon_hostlist.c */