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