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