test_core_api_send_to_self.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010, 2016 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 core/test_core_api_send_to_self.c
  18. * @brief test that sending a message to ourselves via CORE works
  19. * @author Philipp Toelke
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_testing_lib.h"
  25. #include "gnunet_protocols.h"
  26. #include "gnunet_core_service.h"
  27. #include "gnunet_constants.h"
  28. /**
  29. * Final status code.
  30. */
  31. static int ret;
  32. /**
  33. * Handle to the cleanup task.
  34. */
  35. static struct GNUNET_SCHEDULER_Task *die_task;
  36. /**
  37. * Identity of this peer.
  38. */
  39. static struct GNUNET_PeerIdentity myself;
  40. /**
  41. * The handle to core
  42. */
  43. static struct GNUNET_CORE_Handle *core;
  44. /**
  45. * Function scheduled as very last function, cleans up after us
  46. */
  47. static void
  48. cleanup (void *cls)
  49. {
  50. if (NULL != die_task)
  51. {
  52. GNUNET_SCHEDULER_cancel (die_task);
  53. die_task = NULL;
  54. }
  55. if (NULL != core)
  56. {
  57. GNUNET_CORE_disconnect (core);
  58. core = NULL;
  59. }
  60. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  61. "Ending test.\n");
  62. }
  63. /**
  64. * Function scheduled as very last function, cleans up after us
  65. */
  66. static void
  67. do_timeout (void *cls)
  68. {
  69. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  70. "Test timeout.\n");
  71. die_task = NULL;
  72. GNUNET_SCHEDULER_shutdown ();
  73. }
  74. static void
  75. handle_test (void *cls,
  76. const struct GNUNET_MessageHeader *message)
  77. {
  78. GNUNET_SCHEDULER_shutdown ();
  79. ret = 0;
  80. }
  81. static void
  82. init (void *cls,
  83. const struct GNUNET_PeerIdentity *my_identity)
  84. {
  85. if (NULL == my_identity)
  86. {
  87. GNUNET_break (0);
  88. return;
  89. }
  90. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  91. "Correctly connected to CORE; we are the peer %s.\n",
  92. GNUNET_i2s (my_identity));
  93. GNUNET_memcpy (&myself,
  94. my_identity,
  95. sizeof (struct GNUNET_PeerIdentity));
  96. }
  97. static void *
  98. connect_cb (void *cls,
  99. const struct GNUNET_PeerIdentity *peer,
  100. struct GNUNET_MQ_Handle *mq)
  101. {
  102. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  103. "Connected to peer %s.\n",
  104. GNUNET_i2s (peer));
  105. if (0 == memcmp (peer,
  106. &myself,
  107. sizeof (struct GNUNET_PeerIdentity)))
  108. {
  109. struct GNUNET_MQ_Envelope *env;
  110. struct GNUNET_MessageHeader *msg;
  111. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  112. "Connected to myself; sending message!\n");
  113. env = GNUNET_MQ_msg (msg,
  114. GNUNET_MESSAGE_TYPE_DUMMY);
  115. GNUNET_MQ_send (mq,
  116. env);
  117. }
  118. return NULL;
  119. }
  120. /**
  121. * Main function that will be run by the scheduler.
  122. *
  123. * @param cls closure
  124. * @param cfg configuration
  125. */
  126. static void
  127. run (void *cls,
  128. const struct GNUNET_CONFIGURATION_Handle *cfg,
  129. struct GNUNET_TESTING_Peer *peer)
  130. {
  131. struct GNUNET_MQ_MessageHandler handlers[] = {
  132. GNUNET_MQ_hd_fixed_size (test,
  133. GNUNET_MESSAGE_TYPE_DUMMY,
  134. struct GNUNET_MessageHeader,
  135. NULL),
  136. GNUNET_MQ_handler_end ()
  137. };
  138. core =
  139. GNUNET_CORE_connect (cfg,
  140. NULL,
  141. &init,
  142. &connect_cb,
  143. NULL,
  144. handlers);
  145. GNUNET_SCHEDULER_add_shutdown (&cleanup,
  146. NULL);
  147. die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
  148. &do_timeout,
  149. NULL);
  150. }
  151. /**
  152. * The main function to test sending a message to the local peer via core
  153. *
  154. * @param argc number of arguments from the command line
  155. * @param argv command line arguments
  156. * @return 0 ok, 1 on error
  157. */
  158. int
  159. main (int argc, char *argv[])
  160. {
  161. ret = 1;
  162. if (0 != GNUNET_TESTING_peer_run ("test-core-api-send-to-self",
  163. "test_core_api_peer1.conf",
  164. &run, NULL))
  165. return 1;
  166. return ret;
  167. }
  168. /* end of test_core_api_send_to_self.c */