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