test_peerinfo_api.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_api.c
  18. * @brief testcase for peerinfo_api.c
  19. * @author Christian Grothoff
  20. *
  21. * TODO:
  22. * - test merging of HELLOs (add same peer twice...)
  23. */
  24. #include "platform.h"
  25. #include "gnunet_hello_lib.h"
  26. #include "gnunet_util_lib.h"
  27. #include "gnunet_peerinfo_service.h"
  28. #include "gnunet_testing_lib.h"
  29. #include "peerinfo.h"
  30. static struct GNUNET_PEERINFO_IteratorContext *ic;
  31. static struct GNUNET_PEERINFO_Handle *h;
  32. static unsigned int retries;
  33. static int global_ret;
  34. static int
  35. check_it (void *cls, const struct GNUNET_HELLO_Address *address,
  36. struct GNUNET_TIME_Absolute expiration)
  37. {
  38. unsigned int *agc = cls;
  39. if (address != NULL)
  40. {
  41. GNUNET_assert (0 == strcmp ("peerinfotest", address->transport_name));
  42. GNUNET_assert (0 ==
  43. strncmp ("Address", address->address,
  44. address->address_length));
  45. (*agc) -= (1 << (address->address_length - 1));
  46. }
  47. return GNUNET_OK;
  48. }
  49. static ssize_t
  50. address_generator (void *cls, size_t max, void *buf)
  51. {
  52. size_t *agc = cls;
  53. ssize_t ret;
  54. struct GNUNET_HELLO_Address address;
  55. if (0 == *agc)
  56. return GNUNET_SYSERR; /* Done */
  57. memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
  58. address.address = "Address";
  59. address.transport_name = "peerinfotest";
  60. address.address_length = *agc;
  61. ret =
  62. GNUNET_HELLO_add_address (&address,
  63. GNUNET_TIME_relative_to_absolute
  64. (GNUNET_TIME_UNIT_HOURS), buf, max);
  65. (*agc)--;
  66. return ret;
  67. }
  68. struct GNUNET_PeerIdentity pid;
  69. static void
  70. add_peer ()
  71. {
  72. struct GNUNET_HELLO_Message *h2;
  73. size_t agc;
  74. agc = 2;
  75. memset (&pid, 32, sizeof (pid));
  76. h2 = GNUNET_HELLO_create (&pid.public_key, &address_generator, &agc, GNUNET_NO);
  77. GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
  78. GNUNET_free (h2);
  79. }
  80. static void
  81. process (void *cls, const struct GNUNET_PeerIdentity *peer,
  82. const struct GNUNET_HELLO_Message *hello, const char *err_msg)
  83. {
  84. unsigned int agc;
  85. if (err_msg != NULL)
  86. {
  87. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  88. _("Error in communication with PEERINFO service\n"));
  89. }
  90. if (peer == NULL)
  91. {
  92. ic = NULL;
  93. if ((3 == global_ret) && (retries < 50))
  94. {
  95. /* try again */
  96. retries++;
  97. add_peer ();
  98. ic = GNUNET_PEERINFO_iterate (h, GNUNET_NO, NULL,
  99. &process,
  100. cls);
  101. return;
  102. }
  103. GNUNET_assert (peer == NULL);
  104. GNUNET_assert (2 == global_ret);
  105. GNUNET_PEERINFO_disconnect (h);
  106. h = NULL;
  107. global_ret = 0;
  108. return;
  109. }
  110. if (hello != NULL)
  111. {
  112. GNUNET_assert (3 == global_ret);
  113. agc = 3;
  114. GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
  115. &check_it, &agc);
  116. GNUNET_assert (agc == 0);
  117. global_ret = 2;
  118. }
  119. }
  120. static void
  121. run (void *cls,
  122. const struct GNUNET_CONFIGURATION_Handle *cfg,
  123. struct GNUNET_TESTING_Peer *peer)
  124. {
  125. h = GNUNET_PEERINFO_connect (cfg);
  126. GNUNET_assert (NULL != h);
  127. add_peer ();
  128. ic = GNUNET_PEERINFO_iterate (h, GNUNET_NO, &pid,
  129. &process, cls);
  130. }
  131. int
  132. main (int argc, char *argv[])
  133. {
  134. global_ret = 3;
  135. if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerinfo",
  136. "peerinfo",
  137. "test_peerinfo_api_data.conf",
  138. &run, NULL))
  139. return 1;
  140. return global_ret;
  141. }
  142. /* end of test_peerinfo_api.c */