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