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