test_peerinfo_api.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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,
  77. GNUNET_NO);
  78. GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
  79. GNUNET_free (h2);
  80. }
  81. static void
  82. process (void *cls, const struct GNUNET_PeerIdentity *peer,
  83. const struct GNUNET_HELLO_Message *hello, const char *err_msg)
  84. {
  85. unsigned int agc;
  86. if (err_msg != NULL)
  87. {
  88. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  89. _ ("Error in communication with PEERINFO service\n"));
  90. }
  91. if (peer == NULL)
  92. {
  93. ic = NULL;
  94. if ((3 == global_ret) && (retries < 50))
  95. {
  96. /* try again */
  97. retries++;
  98. add_peer ();
  99. ic = GNUNET_PEERINFO_iterate (h, GNUNET_NO, NULL,
  100. &process,
  101. cls);
  102. return;
  103. }
  104. GNUNET_assert (peer == NULL);
  105. GNUNET_assert (2 == global_ret);
  106. GNUNET_PEERINFO_disconnect (h);
  107. h = NULL;
  108. global_ret = 0;
  109. return;
  110. }
  111. if (hello != NULL)
  112. {
  113. GNUNET_assert (3 == global_ret);
  114. agc = 3;
  115. GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
  116. &check_it, &agc);
  117. GNUNET_assert (agc == 0);
  118. global_ret = 2;
  119. }
  120. }
  121. static void
  122. run (void *cls,
  123. const struct GNUNET_CONFIGURATION_Handle *cfg,
  124. struct GNUNET_TESTING_Peer *peer)
  125. {
  126. h = GNUNET_PEERINFO_connect (cfg);
  127. GNUNET_assert (NULL != h);
  128. add_peer ();
  129. ic = GNUNET_PEERINFO_iterate (h, GNUNET_NO, &pid,
  130. &process, cls);
  131. }
  132. int
  133. main (int argc, char *argv[])
  134. {
  135. global_ret = 3;
  136. if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerinfo",
  137. "peerinfo",
  138. "test_peerinfo_api_data.conf",
  139. &run, NULL))
  140. return 1;
  141. return global_ret;
  142. }
  143. /* end of test_peerinfo_api.c */