test_peerinfo_api_notify_friend_only.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_notify_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. #include "peerinfo.h"
  31. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  32. static struct GNUNET_PEERINFO_Handle *h;
  33. static struct GNUNET_PEERINFO_NotifyContext *pnc_w_fo;
  34. static struct GNUNET_PEERINFO_NotifyContext *pnc_wo_fo;
  35. static const struct GNUNET_CONFIGURATION_Handle *mycfg;
  36. static int global_ret;
  37. /**
  38. * Did we get a HELLO callback for notification handle with friend HELLOS
  39. * (expected)
  40. */
  41. static int res_cb_w_fo;
  42. /**
  43. * Did we get a HELLO callback for notification handle without friend HELLOS
  44. * (not expected)
  45. */
  46. static int res_cb_wo_fo;
  47. struct GNUNET_PeerIdentity pid;
  48. struct GNUNET_SCHEDULER_Task *timeout_task;
  49. static void
  50. end_badly (void *cls)
  51. {
  52. timeout_task = NULL;
  53. GNUNET_break(0);
  54. if (NULL != pnc_wo_fo)
  55. {
  56. GNUNET_PEERINFO_notify_cancel (pnc_wo_fo);
  57. pnc_wo_fo = NULL;
  58. }
  59. if (NULL != pnc_w_fo)
  60. {
  61. GNUNET_PEERINFO_notify_cancel (pnc_w_fo);
  62. pnc_w_fo = NULL;
  63. }
  64. if (NULL != h)
  65. {
  66. GNUNET_PEERINFO_disconnect (h);
  67. h = NULL;
  68. }
  69. global_ret = 255;
  70. }
  71. static void
  72. done (void *cls)
  73. {
  74. if (NULL != pnc_w_fo)
  75. GNUNET_PEERINFO_notify_cancel (pnc_w_fo);
  76. pnc_w_fo = NULL;
  77. if (NULL != pnc_wo_fo)
  78. GNUNET_PEERINFO_notify_cancel (pnc_wo_fo);
  79. pnc_wo_fo = NULL;
  80. GNUNET_PEERINFO_disconnect (h);
  81. h = NULL;
  82. if (NULL != timeout_task)
  83. {
  84. GNUNET_SCHEDULER_cancel (timeout_task);
  85. timeout_task = NULL;
  86. }
  87. if ((GNUNET_YES == res_cb_w_fo) && (GNUNET_NO == res_cb_wo_fo))
  88. global_ret = 0;
  89. else
  90. GNUNET_break(0);
  91. }
  92. static ssize_t
  93. address_generator (void *cls, size_t max, void *buf)
  94. {
  95. size_t *agc = cls;
  96. ssize_t ret;
  97. struct GNUNET_HELLO_Address address;
  98. if (0 == *agc)
  99. return GNUNET_SYSERR; /* Done */
  100. memset (&address.peer, 0, sizeof(struct GNUNET_PeerIdentity));
  101. address.address = "Address";
  102. address.transport_name = "peerinfotest";
  103. address.address_length = *agc;
  104. ret = GNUNET_HELLO_add_address (&address,
  105. GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), buf, max);
  106. (*agc)--;
  107. return ret;
  108. }
  109. static void
  110. process_w_fo (void *cls,
  111. const struct GNUNET_PeerIdentity *peer,
  112. const struct GNUNET_HELLO_Message *hello,
  113. const char *err_msg)
  114. {
  115. if (err_msg != NULL)
  116. {
  117. GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
  118. _("Error in communication with PEERINFO service\n"));
  119. GNUNET_SCHEDULER_add_now (&done, NULL);
  120. return;
  121. }
  122. if (NULL != peer)
  123. {
  124. GNUNET_log(GNUNET_ERROR_TYPE_INFO,
  125. "Received callback for peer `%s' %s HELLO\n", GNUNET_i2s (peer),
  126. (NULL != hello) ? "with" : "without");
  127. if (NULL == hello)
  128. return;
  129. if (GNUNET_NO == GNUNET_HELLO_is_friend_only (hello))
  130. {
  131. GNUNET_break(0);
  132. return;
  133. }
  134. GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received %s HELLO for peer `%s'\n",
  135. (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello)) ? "friend only" : "public",
  136. GNUNET_i2s (peer));
  137. if (0 == memcmp (&pid, peer, sizeof(pid)))
  138. {
  139. res_cb_w_fo = GNUNET_YES;
  140. GNUNET_SCHEDULER_add_now (&done, NULL);
  141. }
  142. return;
  143. }
  144. }
  145. static void
  146. process_wo_fo (void *cls, const struct GNUNET_PeerIdentity *peer,
  147. const struct GNUNET_HELLO_Message *hello, const char *err_msg)
  148. {
  149. if (err_msg != NULL)
  150. {
  151. GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
  152. _("Error in communication with PEERINFO service\n"));
  153. GNUNET_SCHEDULER_add_now (&done, NULL);
  154. return;
  155. }
  156. if (NULL != peer)
  157. {
  158. GNUNET_log(GNUNET_ERROR_TYPE_INFO,
  159. "Received callback for peer `%s' %s HELLO\n", GNUNET_i2s (peer),
  160. (NULL != hello) ? "with" : "without");
  161. if (NULL == hello)
  162. return;
  163. if (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello))
  164. {
  165. GNUNET_break(0);
  166. return;
  167. }
  168. GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received %s HELLO for peer `%s'\n",
  169. (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello)) ? "friend only" : "public",
  170. GNUNET_i2s (peer));
  171. if (0 == memcmp (&pid, peer, sizeof(pid)))
  172. {
  173. GNUNET_break(0);
  174. res_cb_wo_fo = GNUNET_YES;
  175. }
  176. }
  177. }
  178. static void
  179. add_peer ()
  180. {
  181. struct GNUNET_HELLO_Message *h2;
  182. size_t agc;
  183. agc = 2;
  184. memset (&pid, 32, sizeof(pid));
  185. h2 = GNUNET_HELLO_create (&pid.public_key, &address_generator, &agc,
  186. GNUNET_YES);
  187. GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
  188. GNUNET_free(h2);
  189. }
  190. static void
  191. run (void *cls,
  192. const struct GNUNET_CONFIGURATION_Handle *cfg,
  193. struct GNUNET_TESTING_Peer *peer)
  194. {
  195. timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
  196. mycfg = cfg;
  197. pnc_w_fo = GNUNET_PEERINFO_notify (mycfg, GNUNET_YES, &process_w_fo, NULL);
  198. pnc_wo_fo = GNUNET_PEERINFO_notify (mycfg, GNUNET_NO, &process_wo_fo, NULL);
  199. h = GNUNET_PEERINFO_connect (cfg);
  200. GNUNET_assert(NULL != h);
  201. add_peer ();
  202. }
  203. int
  204. main (int argc, char *argv[])
  205. {
  206. res_cb_w_fo = GNUNET_NO;
  207. res_cb_wo_fo = GNUNET_NO;
  208. global_ret = 3;
  209. if (0 != GNUNET_TESTING_service_run ("test-peerinfo-api-friend-only",
  210. "peerinfo",
  211. "test_peerinfo_api_data.conf",
  212. &run, NULL))
  213. return 1;
  214. return global_ret;
  215. }
  216. /* end of test_peerinfo_api_notify_friend_only.c */