test_peerinfo_shipped_hellos.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2004, 2009 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file peerinfo/test_peerinfo_shipped_hellos.c
  18. * @brief testcase for shipped HELLOs getting parsed
  19. * @author Christian Grothoff
  20. * @author Matthias Wachs
  21. *
  22. */
  23. #include "platform.h"
  24. #include "gnunet_hello_lib.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_peerinfo_service.h"
  27. #include "gnunet_testing_lib.h"
  28. #include "peerinfo.h"
  29. static struct GNUNET_PEERINFO_IteratorContext *ic;
  30. static struct GNUNET_PEERINFO_Handle *h;
  31. static int global_ret;
  32. static int
  33. addr_cb (void *cls,
  34. const struct GNUNET_HELLO_Address *address,
  35. struct GNUNET_TIME_Absolute expiration)
  36. {
  37. unsigned int *addr = cls;
  38. (*addr) ++;
  39. return GNUNET_OK;
  40. }
  41. static void
  42. process (void *cls,
  43. const struct GNUNET_PeerIdentity *peer,
  44. const struct GNUNET_HELLO_Message *hello,
  45. const char *err_msg)
  46. {
  47. static unsigned int calls = 0;
  48. unsigned int addr;
  49. if (NULL != err_msg)
  50. {
  51. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  52. "Error in communication with PEERINFO service: %s\n",
  53. err_msg);
  54. }
  55. if (NULL != peer)
  56. {
  57. addr = 0;
  58. if (NULL != hello)
  59. {
  60. GNUNET_HELLO_iterate_addresses (hello,
  61. GNUNET_NO,
  62. &addr_cb,
  63. &addr);
  64. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  65. "Got information about peer %s with %u addresses\n",
  66. GNUNET_i2s (peer),
  67. addr);
  68. calls++;
  69. }
  70. else
  71. {
  72. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  73. "Got no HELLP for peer %s\n",
  74. GNUNET_i2s (peer));
  75. }
  76. }
  77. else
  78. {
  79. if (0 == calls)
  80. {
  81. fprintf (stderr,
  82. "Failed: got no callbacks!\n");
  83. global_ret = 1;
  84. GNUNET_PEERINFO_disconnect (h);
  85. h = NULL;
  86. }
  87. else
  88. {
  89. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  90. "Got %u HELLOs in total\n",
  91. calls);
  92. global_ret = 0;
  93. GNUNET_PEERINFO_disconnect (h);
  94. h = NULL;
  95. }
  96. }
  97. }
  98. static void
  99. run (void *cls,
  100. const struct GNUNET_CONFIGURATION_Handle *cfg,
  101. struct GNUNET_TESTING_Peer *peer)
  102. {
  103. h = GNUNET_PEERINFO_connect (cfg);
  104. GNUNET_assert (NULL != h);
  105. ic = GNUNET_PEERINFO_iterate (h,
  106. GNUNET_YES,
  107. NULL,
  108. &process,
  109. cls);
  110. GNUNET_assert (NULL != ic);
  111. }
  112. int
  113. main (int argc,
  114. char *argv[])
  115. {
  116. global_ret = 3;
  117. if (0 != GNUNET_TESTING_service_run ("test_peerinfo_shipped_hellos",
  118. "peerinfo",
  119. "test_peerinfo_api_data.conf",
  120. &run, NULL))
  121. return 1;
  122. return global_ret;
  123. }
  124. /* end of test_peerinfo_shipped_hellos.c */