aboutsummaryrefslogtreecommitdiff
path: root/src/hostlist/test_gnunet_daemon_hostlist_learning.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hostlist/test_gnunet_daemon_hostlist_learning.c')
-rw-r--r--src/hostlist/test_gnunet_daemon_hostlist_learning.c224
1 files changed, 140 insertions, 84 deletions
diff --git a/src/hostlist/test_gnunet_daemon_hostlist_learning.c b/src/hostlist/test_gnunet_daemon_hostlist_learning.c
index 54f219ad8..041898abd 100644
--- a/src/hostlist/test_gnunet_daemon_hostlist_learning.c
+++ b/src/hostlist/test_gnunet_daemon_hostlist_learning.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2009, 2010, 2011, 2012 GNUnet e.V. 3 Copyright (C) 2009, 2010, 2011, 2012, 2016 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -63,9 +63,9 @@ static char *current_adv_uri;
63 63
64static const struct GNUNET_CONFIGURATION_Handle *cfg; 64static const struct GNUNET_CONFIGURATION_Handle *cfg;
65 65
66static struct GNUNET_SCHEDULER_Task * timeout_task; 66static struct GNUNET_SCHEDULER_Task *timeout_task;
67 67
68static struct GNUNET_SCHEDULER_Task * check_task; 68static struct GNUNET_SCHEDULER_Task *check_task;
69 69
70static struct PeerContext adv_peer; 70static struct PeerContext adv_peer;
71 71
@@ -81,8 +81,9 @@ static struct GNUNET_STATISTICS_GetHandle *advsent_stat;
81static void 81static void
82shutdown_testcase () 82shutdown_testcase ()
83{ 83{
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown testcase....\n"); 84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 if (timeout_task != NULL) 85 "Shutdown testcase....\n");
86 if (NULL != timeout_task)
86 { 87 {
87 GNUNET_SCHEDULER_cancel (timeout_task); 88 GNUNET_SCHEDULER_cancel (timeout_task);
88 timeout_task = NULL; 89 timeout_task = NULL;
@@ -112,7 +113,7 @@ shutdown_testcase ()
112 GNUNET_STATISTICS_destroy (learn_peer.stats, GNUNET_NO); 113 GNUNET_STATISTICS_destroy (learn_peer.stats, GNUNET_NO);
113 learn_peer.stats = NULL; 114 learn_peer.stats = NULL;
114 } 115 }
115 if (check_task != NULL) 116 if (NULL != check_task)
116 { 117 {
117 GNUNET_SCHEDULER_cancel (check_task); 118 GNUNET_SCHEDULER_cancel (check_task);
118 check_task = NULL; 119 check_task = NULL;
@@ -124,20 +125,24 @@ shutdown_testcase ()
124 } 125 }
125 if (NULL != adv_peer.core) 126 if (NULL != adv_peer.core)
126 { 127 {
127 GNUNET_CORE_disconnect (adv_peer.core); 128 GNUNET_CORE_disconnecT (adv_peer.core);
128 adv_peer.core = NULL; 129 adv_peer.core = NULL;
129 } 130 }
130 if (NULL != learn_peer.core) 131 if (NULL != learn_peer.core)
131 { 132 {
132 GNUNET_CORE_disconnect (learn_peer.core); 133 GNUNET_CORE_disconnecT (learn_peer.core);
133 learn_peer.core = NULL; 134 learn_peer.core = NULL;
134 } 135 }
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 "Killing hostlist server ARM process.\n"); 137 "Killing hostlist server ARM process.\n");
137 if (0 != GNUNET_OS_process_kill (adv_peer.arm_proc, GNUNET_TERM_SIG)) 138 if (0 != GNUNET_OS_process_kill (adv_peer.arm_proc,
138 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 139 GNUNET_TERM_SIG))
139 if (GNUNET_OS_process_wait (adv_peer.arm_proc) != GNUNET_OK) 140 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
140 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); 141 "kill");
142 if (GNUNET_OK !=
143 GNUNET_OS_process_wait (adv_peer.arm_proc))
144 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
145 "waitpid");
141 GNUNET_OS_process_destroy (adv_peer.arm_proc); 146 GNUNET_OS_process_destroy (adv_peer.arm_proc);
142 adv_peer.arm_proc = NULL; 147 adv_peer.arm_proc = NULL;
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -156,6 +161,7 @@ shutdown_testcase ()
156 "Shutdown complete....\n"); 161 "Shutdown complete....\n");
157} 162}
158 163
164
159/** 165/**
160 * Timeout, give up. 166 * Timeout, give up.
161 */ 167 */
@@ -185,10 +191,14 @@ do_shutdown (void *cls)
185 191
186 192
187static int 193static int
188process_downloads (void *cls, const char *subsystem, const char *name, 194process_downloads (void *cls,
189 uint64_t value, int is_persistent) 195 const char *subsystem,
196 const char *name,
197 uint64_t value,
198 int is_persistent)
190{ 199{
191 if ((value >= 2) && (learned_hostlist_downloaded == GNUNET_NO)) 200 if ( (value >= 2) &&
201 (GNUNET_NO == learned_hostlist_downloaded) )
192 { 202 {
193 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 203 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
194 "Peer has successfully downloaded advertised URI\n"); 204 "Peer has successfully downloaded advertised URI\n");
@@ -210,18 +220,25 @@ process_uris_recv_done (void *cls, int success)
210 220
211 221
212static int 222static int
213process_uris_recv (void *cls, const char *subsystem, const char *name, 223process_uris_recv (void *cls,
214 uint64_t value, int is_persistent) 224 const char *subsystem,
225 const char *name,
226 uint64_t value,
227 int is_persistent)
215{ 228{
216 if (((struct PeerContext *) cls == &learn_peer) && (value == 1) && 229 struct PeerContext *pc = cls;
217 (learned_hostlist_saved == GNUNET_NO)) 230 if ( (pc == &learn_peer) &&
231 (value == 1) &&
232 (learned_hostlist_saved == GNUNET_NO) )
218 { 233 {
219 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
220 "Peer has successfully saved advertised URI\n"); 235 "Peer has successfully saved advertised URI\n");
221 learned_hostlist_saved = GNUNET_YES; 236 learned_hostlist_saved = GNUNET_YES;
222 if ((learned_hostlist_downloaded == GNUNET_YES) && (adv_sent == GNUNET_YES)) 237 if ( (learned_hostlist_downloaded == GNUNET_YES) &&
238 (adv_sent == GNUNET_YES) )
223 { 239 {
224 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 240 GNUNET_SCHEDULER_add_now (&do_shutdown,
241 NULL);
225 } 242 }
226 } 243 }
227 return GNUNET_OK; 244 return GNUNET_OK;
@@ -239,7 +256,8 @@ static int
239process_adv_sent (void *cls, 256process_adv_sent (void *cls,
240 const char *subsystem, 257 const char *subsystem,
241 const char *name, 258 const char *name,
242 uint64_t value, int is_persistent) 259 uint64_t value,
260 int is_persistent)
243{ 261{
244 if ((value >= 1) && (adv_sent == GNUNET_NO)) 262 if ((value >= 1) && (adv_sent == GNUNET_NO))
245 { 263 {
@@ -249,7 +267,8 @@ process_adv_sent (void *cls,
249 if ((learned_hostlist_downloaded == GNUNET_YES) && 267 if ((learned_hostlist_downloaded == GNUNET_YES) &&
250 (learned_hostlist_saved == GNUNET_YES)) 268 (learned_hostlist_saved == GNUNET_YES))
251 { 269 {
252 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 270 GNUNET_SCHEDULER_add_now (&do_shutdown,
271 NULL);
253 } 272 }
254 } 273 }
255 return GNUNET_OK; 274 return GNUNET_OK;
@@ -273,8 +292,11 @@ check_statistics (void *cls)
273 if (NULL != download_stats) 292 if (NULL != download_stats)
274 GNUNET_STATISTICS_get_cancel (download_stats); 293 GNUNET_STATISTICS_get_cancel (download_stats);
275 download_stats = 294 download_stats =
276 GNUNET_STATISTICS_get (learn_peer.stats, "hostlist", stat, 295 GNUNET_STATISTICS_get (learn_peer.stats,
277 &process_downloads_done, &process_downloads, 296 "hostlist",
297 stat,
298 &process_downloads_done,
299 &process_downloads,
278 &learn_peer); 300 &learn_peer);
279 if (NULL != urisrecv_stat) 301 if (NULL != urisrecv_stat)
280 GNUNET_STATISTICS_get_cancel (urisrecv_stat); 302 GNUNET_STATISTICS_get_cancel (urisrecv_stat);
@@ -293,55 +315,64 @@ check_statistics (void *cls)
293 GNUNET_STATISTICS_get (adv_peer.stats, "hostlist", 315 GNUNET_STATISTICS_get (adv_peer.stats, "hostlist",
294 gettext_noop ("# hostlist advertisements send"), 316 gettext_noop ("# hostlist advertisements send"),
295 &process_adv_sent_done, 317 &process_adv_sent_done,
296 &process_adv_sent, NULL); 318 &process_adv_sent,
319 NULL);
297 } 320 }
298 check_task = 321 check_task =
299 GNUNET_SCHEDULER_add_delayed (CHECK_INTERVAL, &check_statistics, NULL); 322 GNUNET_SCHEDULER_add_delayed (CHECK_INTERVAL,
323 &check_statistics,
324 NULL);
300} 325}
301 326
302 327
303/**
304 * Core handler for p2p hostlist advertisements
305 */
306static int 328static int
307ad_arrive_handler (void *cls, 329check_ad_arrive (void *cls,
308 const struct GNUNET_PeerIdentity *peer, 330 const struct GNUNET_MessageHeader *message)
331{
332 const char *end = (const char *) &message[1];
333 if ('\0' != end[ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - 1])
334 {
335 GNUNET_break (0);
336 return GNUNET_SYSERR;
337 }
338 return GNUNET_OK;
339}
340
341
342static void
343handle_ad_arrive (void *cls,
309 const struct GNUNET_MessageHeader *message) 344 const struct GNUNET_MessageHeader *message)
310{ 345{
311 char *hostname; 346 char *hostname;
312 char *expected_uri; 347 char *expected_uri;
313 unsigned long long port; 348 unsigned long long port;
314 const struct GNUNET_MessageHeader *incoming;
315 const char *end; 349 const char *end;
316 350
317 if (-1 == 351 if (GNUNET_SYSERR ==
318 GNUNET_CONFIGURATION_get_value_number (adv_peer.cfg, "HOSTLIST", 352 GNUNET_CONFIGURATION_get_value_number (adv_peer.cfg,
319 "HTTPPORT", &port)) 353 "HOSTLIST",
354 "HTTPPORT",
355 &port))
320 { 356 {
321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
322 "Could not read advertising server's configuration\n"); 358 "Could not read advertising server's configuration\n");
323 return GNUNET_SYSERR; 359 return;
324 } 360 }
325 361
326 if (GNUNET_SYSERR == 362 if (GNUNET_SYSERR ==
327 GNUNET_CONFIGURATION_get_value_string (adv_peer.cfg, "HOSTLIST", 363 GNUNET_CONFIGURATION_get_value_string (adv_peer.cfg,
328 "EXTERNAL_DNS_NAME", &hostname)) 364 "HOSTLIST",
365 "EXTERNAL_DNS_NAME",
366 &hostname))
329 hostname = GNUNET_RESOLVER_local_fqdn_get (); 367 hostname = GNUNET_RESOLVER_local_fqdn_get ();
330 GNUNET_asprintf (&expected_uri, "http://%s:%u/", 368 GNUNET_asprintf (&expected_uri,
369 "http://%s:%u/",
331 hostname != NULL ? hostname : "localhost", 370 hostname != NULL ? hostname : "localhost",
332 (unsigned int) port); 371 (unsigned int) port);
333 incoming = (const struct GNUNET_MessageHeader *) message; 372 end = (const char *) &message[1];
334 end = (const char *) &incoming[1];
335 if ('\0' !=
336 end[ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - 1])
337 {
338 GNUNET_break (0);
339 GNUNET_free (expected_uri);
340 GNUNET_free_non_null (hostname);
341 return GNUNET_SYSERR;
342 }
343 current_adv_uri = GNUNET_strdup (end); 373 current_adv_uri = GNUNET_strdup (end);
344 if (0 == strcmp (expected_uri, current_adv_uri)) 374 if (0 == strcmp (expected_uri,
375 current_adv_uri))
345 { 376 {
346 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 377 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
347 "Received hostlist advertisement with URI `%s' as expected\n", 378 "Received hostlist advertisement with URI `%s' as expected\n",
@@ -352,25 +383,24 @@ ad_arrive_handler (void *cls,
352 else 383 else
353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
354 "Expected URI `%s' and received URI `%s' differ\n", 385 "Expected URI `%s' and received URI `%s' differ\n",
355 expected_uri, current_adv_uri); 386 expected_uri,
387 current_adv_uri);
356 GNUNET_free (expected_uri); 388 GNUNET_free (expected_uri);
357 GNUNET_free_non_null (hostname); 389 GNUNET_free_non_null (hostname);
358 return GNUNET_OK;
359} 390}
360 391
361 392
362/**
363 * List of handlers if we are learning.
364 */
365static struct GNUNET_CORE_MessageHandler learn_handlers[] = {
366 {&ad_arrive_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
367 {NULL, 0, 0}
368};
369
370
371static void 393static void
372setup_learn_peer (struct PeerContext *p, const char *cfgname) 394setup_learn_peer (struct PeerContext *p,
395 const char *cfgname)
373{ 396{
397 GNUNET_MQ_hd_var_size (ad_arrive,
398 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT,
399 struct GNUNET_MessageHeader);
400 struct GNUNET_MQ_MessageHandler learn_handlers[] = {
401 make_ad_arrive_handler (NULL),
402 GNUNET_MQ_handler_end ()
403 };
374 char *filename; 404 char *filename;
375 unsigned int result; 405 unsigned int result;
376 char *binary; 406 char *binary;
@@ -383,9 +413,13 @@ setup_learn_peer (struct PeerContext *p, const char *cfgname)
383 binary, 413 binary,
384 "gnunet-service-arm", 414 "gnunet-service-arm",
385 "-c", cfgname, NULL); 415 "-c", cfgname, NULL);
386 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); 416 GNUNET_assert (GNUNET_OK ==
417 GNUNET_CONFIGURATION_load (p->cfg,
418 cfgname));
387 if (GNUNET_OK == 419 if (GNUNET_OK ==
388 GNUNET_CONFIGURATION_get_value_string (p->cfg, "HOSTLIST", "HOSTLISTFILE", 420 GNUNET_CONFIGURATION_get_value_string (p->cfg,
421 "HOSTLIST",
422 "HOSTLISTFILE",
389 &filename)) 423 &filename))
390 { 424 {
391 if (GNUNET_YES == GNUNET_DISK_file_test (filename)) 425 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
@@ -393,34 +427,43 @@ setup_learn_peer (struct PeerContext *p, const char *cfgname)
393 result = UNLINK (filename); 427 result = UNLINK (filename);
394 if (result == 0) 428 if (result == 0)
395 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 429 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
396 _("Hostlist file `%s' was removed\n"), filename); 430 _("Hostlist file `%s' was removed\n"),
431 filename);
397 } 432 }
398 GNUNET_free (filename); 433 GNUNET_free (filename);
399 } 434 }
400 p->core = 435 p->core = GNUNET_CORE_connecT (p->cfg,
401 GNUNET_CORE_connect (p->cfg, NULL, NULL, NULL, NULL, NULL, GNUNET_NO, 436 NULL,
402 NULL, GNUNET_NO, learn_handlers); 437 NULL,
438 NULL,
439 NULL,
440 learn_handlers);
403 GNUNET_assert (NULL != p->core); 441 GNUNET_assert (NULL != p->core);
404 p->stats = GNUNET_STATISTICS_create ("hostlist", p->cfg); 442 p->stats = GNUNET_STATISTICS_create ("hostlist",
443 p->cfg);
405 GNUNET_assert (NULL != p->stats); 444 GNUNET_assert (NULL != p->stats);
406 GNUNET_free (binary); 445 GNUNET_free (binary);
407} 446}
408 447
409 448
410static void 449static void
411setup_adv_peer (struct PeerContext *p, const char *cfgname) 450setup_adv_peer (struct PeerContext *p,
451 const char *cfgname)
412{ 452{
413 char *binary; 453 char *binary;
414 454
415 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm"); 455 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
416 p->cfg = GNUNET_CONFIGURATION_create (); 456 p->cfg = GNUNET_CONFIGURATION_create ();
417 p->arm_proc = 457 p->arm_proc =
418 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 458 GNUNET_OS_start_process (GNUNET_YES,
459 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
419 NULL, NULL, NULL, 460 NULL, NULL, NULL,
420 binary, 461 binary,
421 "gnunet-service-arm", 462 "gnunet-service-arm",
422 "-c", cfgname, NULL); 463 "-c", cfgname, NULL);
423 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); 464 GNUNET_assert (GNUNET_OK ==
465 GNUNET_CONFIGURATION_load (p->cfg,
466 cfgname));
424 p->stats = GNUNET_STATISTICS_create ("hostlist", p->cfg); 467 p->stats = GNUNET_STATISTICS_create ("hostlist", p->cfg);
425 GNUNET_assert (NULL != p->stats); 468 GNUNET_assert (NULL != p->stats);
426 GNUNET_free (binary); 469 GNUNET_free (binary);
@@ -428,7 +471,9 @@ setup_adv_peer (struct PeerContext *p, const char *cfgname)
428 471
429 472
430static void 473static void
431run (void *cls, char *const *args, const char *cfgfile, 474run (void *cls,
475 char *const *args,
476 const char *cfgfile,
432 const struct GNUNET_CONFIGURATION_Handle *c) 477 const struct GNUNET_CONFIGURATION_Handle *c)
433{ 478{
434 timeout = GNUNET_NO; 479 timeout = GNUNET_NO;
@@ -440,12 +485,17 @@ run (void *cls, char *const *args, const char *cfgfile,
440 485
441 cfg = c; 486 cfg = c;
442 487
443 setup_adv_peer (&adv_peer, "test_learning_adv_peer.conf"); 488 setup_adv_peer (&adv_peer,
444 setup_learn_peer (&learn_peer, "test_learning_learn_peer.conf"); 489 "test_learning_adv_peer.conf");
445 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL); 490 setup_learn_peer (&learn_peer,
446 491 "test_learning_learn_peer.conf");
492 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
493 &timeout_error,
494 NULL);
447 check_task = 495 check_task =
448 GNUNET_SCHEDULER_add_delayed (CHECK_INTERVAL, &check_statistics, NULL); 496 GNUNET_SCHEDULER_add_delayed (CHECK_INTERVAL,
497 &check_statistics,
498 NULL);
449} 499}
450 500
451 501
@@ -463,13 +513,18 @@ check ()
463 GNUNET_GETOPT_OPTION_END 513 GNUNET_GETOPT_OPTION_END
464 }; 514 };
465 515
466 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, 516 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
467 "test-gnunet-daemon-hostlist-learning", "nohelp", options, 517 argv,
468 &run, NULL); 518 "test-gnunet-daemon-hostlist-learning",
519 "nohelp",
520 options,
521 &run,
522 NULL);
469 failed = GNUNET_NO; 523 failed = GNUNET_NO;
470 if (timeout == GNUNET_YES) 524 if (timeout == GNUNET_YES)
471 { 525 {
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n"); 526 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
527 "Testcase timeout\n");
473 failed = GNUNET_YES; 528 failed = GNUNET_YES;
474 } 529 }
475 if (adv_arrived != GNUNET_YES) 530 if (adv_arrived != GNUNET_YES)
@@ -522,7 +577,8 @@ main (int argc, char *argv[])
522 ret = check (); 577 ret = check ();
523 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1"); 578 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
524 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2"); 579 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
525 if (GNUNET_YES == GNUNET_DISK_file_test ("hostlists_learn_peer.file")) 580 if (GNUNET_YES ==
581 GNUNET_DISK_file_test ("hostlists_learn_peer.file"))
526 { 582 {
527 if (0 == UNLINK ("hostlists_learn_peer.file")) 583 if (0 == UNLINK ("hostlists_learn_peer.file"))
528 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 584 GNUNET_log (GNUNET_ERROR_TYPE_INFO,