test_peerinfo_api_friend_only.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 (GNUNET_TIME_UNIT_HOURS),
  53. buf,
  54. max);
  55. (*agc)--;
  56. return ret;
  57. }
  58. static void
  59. add_peer ()
  60. {
  61. struct GNUNET_HELLO_Message *h2;
  62. size_t agc;
  63. agc = 2;
  64. memset (&pid, 32, sizeof (pid));
  65. h2 = GNUNET_HELLO_create (&pid.public_key,
  66. &address_generator,
  67. &agc,
  68. GNUNET_YES);
  69. GNUNET_PEERINFO_add_peer (h,
  70. h2,
  71. NULL,
  72. NULL);
  73. GNUNET_free (h2);
  74. }
  75. static void
  76. process (void *cls,
  77. const struct GNUNET_PeerIdentity *peer,
  78. const struct GNUNET_HELLO_Message *hello,
  79. const char *err_msg)
  80. {
  81. if (NULL != err_msg)
  82. {
  83. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  84. "Error in communication with PEERINFO service: %s\n",
  85. err_msg);
  86. }
  87. if (NULL == peer)
  88. {
  89. ic = NULL;
  90. if ((3 == global_ret) && (retries < 50))
  91. {
  92. /* try again */
  93. retries++;
  94. add_peer ();
  95. ic = GNUNET_PEERINFO_iterate (h,
  96. GNUNET_NO,
  97. NULL,
  98. &process,
  99. cls);
  100. return;
  101. }
  102. GNUNET_assert (peer == NULL);
  103. GNUNET_PEERINFO_disconnect (h);
  104. h = NULL;
  105. global_ret = 0;
  106. return;
  107. }
  108. if ( (NULL != hello) &&
  109. (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello)) )
  110. {
  111. fprintf (stderr,
  112. "Received friend-only HELLO\n");
  113. global_ret = 1;
  114. GNUNET_PEERINFO_disconnect (h);
  115. h = NULL;
  116. return;
  117. }
  118. }
  119. static void
  120. run (void *cls,
  121. const struct GNUNET_CONFIGURATION_Handle *cfg,
  122. struct GNUNET_TESTING_Peer *peer)
  123. {
  124. h = GNUNET_PEERINFO_connect (cfg);
  125. GNUNET_assert (NULL != h);
  126. add_peer ();
  127. ic = GNUNET_PEERINFO_iterate (h,
  128. GNUNET_NO,
  129. &pid,
  130. &process,
  131. NULL);
  132. }
  133. int
  134. main (int argc,
  135. char *argv[])
  136. {
  137. global_ret = 3;
  138. if (0 != GNUNET_TESTING_service_run ("test-peerinfo-api-friend-only",
  139. "peerinfo",
  140. "test_peerinfo_api_data.conf",
  141. &run, NULL))
  142. return 1;
  143. return global_ret;
  144. }
  145. /* end of test_peerinfo_api_friend_only */