test_core_api_start_only.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 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 transport/test_core_api_start_only.c
  18. * @brief testcase for core_api.c that only starts two peers,
  19. * connects to the core service and shuts down again
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_arm_service.h"
  24. #include "gnunet_core_service.h"
  25. #include "gnunet_util_lib.h"
  26. #define TIMEOUT 5
  27. #define MTYPE 12345
  28. struct PeerContext
  29. {
  30. struct GNUNET_CONFIGURATION_Handle *cfg;
  31. struct GNUNET_CORE_Handle *ch;
  32. struct GNUNET_PeerIdentity id;
  33. struct GNUNET_OS_Process *arm_proc;
  34. };
  35. static struct PeerContext p1;
  36. static struct PeerContext p2;
  37. static struct GNUNET_SCHEDULER_Task *timeout_task_id;
  38. static int ok;
  39. static void *
  40. connect_notify (void *cls,
  41. const struct GNUNET_PeerIdentity *peer,
  42. struct GNUNET_MQ_Handle *mq)
  43. {
  44. return NULL;
  45. }
  46. static void
  47. disconnect_notify (void *cls,
  48. const struct GNUNET_PeerIdentity *peer,
  49. void *internal_cls)
  50. {
  51. }
  52. static struct GNUNET_MQ_MessageHandler handlers[] = {
  53. GNUNET_MQ_handler_end ()
  54. };
  55. static void
  56. shutdown_task (void *cls)
  57. {
  58. GNUNET_CORE_disconnect (p1.ch);
  59. p1.ch = NULL;
  60. GNUNET_CORE_disconnect (p2.ch);
  61. p2.ch = NULL;
  62. ok = 0;
  63. }
  64. static void
  65. init_notify (void *cls,
  66. const struct GNUNET_PeerIdentity *my_identity)
  67. {
  68. struct PeerContext *p = cls;
  69. if (p == &p1)
  70. {
  71. /* connect p2 */
  72. p2.ch = GNUNET_CORE_connect (p2.cfg,
  73. &p2,
  74. &init_notify,
  75. &connect_notify,
  76. &disconnect_notify,
  77. handlers);
  78. }
  79. else
  80. {
  81. GNUNET_assert (p == &p2);
  82. GNUNET_SCHEDULER_cancel (timeout_task_id);
  83. timeout_task_id = NULL;
  84. GNUNET_SCHEDULER_add_now (&shutdown_task,
  85. NULL);
  86. }
  87. }
  88. static void
  89. setup_peer (struct PeerContext *p,
  90. const char *cfgname)
  91. {
  92. char *binary;
  93. binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
  94. p->cfg = GNUNET_CONFIGURATION_create ();
  95. p->arm_proc =
  96. GNUNET_OS_start_process (GNUNET_YES,
  97. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  98. NULL, NULL, NULL,
  99. binary,
  100. "gnunet-service-arm",
  101. "-c", cfgname,
  102. NULL);
  103. GNUNET_assert (GNUNET_OK ==
  104. GNUNET_CONFIGURATION_load (p->cfg,
  105. cfgname));
  106. GNUNET_free (binary);
  107. }
  108. static void
  109. timeout_task (void *cls)
  110. {
  111. FPRINTF (stderr,
  112. "%s",
  113. "Timeout.\n");
  114. if (NULL != p1.ch)
  115. {
  116. GNUNET_CORE_disconnect (p1.ch);
  117. p1.ch = NULL;
  118. }
  119. if (NULL != p2.ch)
  120. {
  121. GNUNET_CORE_disconnect (p2.ch);
  122. p2.ch = NULL;
  123. }
  124. ok = 42;
  125. }
  126. static void
  127. run (void *cls,
  128. char *const *args,
  129. const char *cfgfile,
  130. const struct GNUNET_CONFIGURATION_Handle *cfg)
  131. {
  132. GNUNET_assert (ok == 1);
  133. ok++;
  134. setup_peer (&p1, "test_core_api_peer1.conf");
  135. setup_peer (&p2, "test_core_api_peer2.conf");
  136. timeout_task_id =
  137. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  138. (GNUNET_TIME_UNIT_MINUTES,
  139. TIMEOUT),
  140. &timeout_task,
  141. NULL);
  142. p1.ch = GNUNET_CORE_connect (p1.cfg,
  143. &p1,
  144. &init_notify,
  145. &connect_notify,
  146. &disconnect_notify,
  147. handlers);
  148. }
  149. static void
  150. stop_arm (struct PeerContext *p)
  151. {
  152. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  153. "Stopping peer\n");
  154. if (0 != GNUNET_OS_process_kill (p->arm_proc,
  155. GNUNET_TERM_SIG))
  156. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
  157. "kill");
  158. if (GNUNET_OK !=
  159. GNUNET_OS_process_wait (p->arm_proc))
  160. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
  161. "waitpid");
  162. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  163. "ARM process %u stopped\n",
  164. (unsigned int) GNUNET_OS_process_get_pid (p->arm_proc));
  165. GNUNET_OS_process_destroy (p->arm_proc);
  166. p->arm_proc = NULL;
  167. GNUNET_CONFIGURATION_destroy (p->cfg);
  168. }
  169. static int
  170. check ()
  171. {
  172. char *const argv[] = {
  173. "test-core-api-start-only",
  174. "-c",
  175. "test_core_api_data.conf",
  176. NULL
  177. };
  178. struct GNUNET_GETOPT_CommandLineOption options[] = {
  179. GNUNET_GETOPT_OPTION_END
  180. };
  181. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
  182. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
  183. ok = 1;
  184. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  185. argv,
  186. "test-core-api-start-only",
  187. "nohelp",
  188. options,
  189. &run,
  190. &ok);
  191. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  192. "Test finished\n");
  193. stop_arm (&p1);
  194. stop_arm (&p2);
  195. return ok;
  196. }
  197. int
  198. main (int argc,
  199. char *argv[])
  200. {
  201. int ret;
  202. GNUNET_log_setup ("test-core-api-start-only",
  203. "WARNING",
  204. NULL);
  205. ret = check ();
  206. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
  207. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
  208. return ret;
  209. }
  210. /* end of test_core_api_start_only.c */