test_gnunet_daemon_hostlist_reconnect.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 hostlist/test_gnunet_daemon_hostlist_reconnect.c
  18. * @brief test for gnunet-daemon-hostslist.c; tries to re-start the peers
  19. * and connect a second time
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_arm_service.h"
  25. #include "gnunet_transport_service.h"
  26. #include "gnunet_transport_hello_service.h"
  27. /**
  28. * How long until we give up on transmitting the message?
  29. */
  30. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
  31. static int ok;
  32. static struct GNUNET_SCHEDULER_Task *timeout_task;
  33. struct PeerContext
  34. {
  35. struct GNUNET_CONFIGURATION_Handle *cfg;
  36. struct GNUNET_TRANSPORT_CoreHandle *th;
  37. struct GNUNET_MessageHeader *hello;
  38. struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
  39. struct GNUNET_OS_Process *arm_proc;
  40. };
  41. static struct PeerContext p1;
  42. static struct PeerContext p2;
  43. /**
  44. * Timeout, give up.
  45. */
  46. static void
  47. timeout_error (void *cls)
  48. {
  49. timeout_task = NULL;
  50. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  51. "Timeout trying to connect peers, test failed.\n");
  52. GNUNET_SCHEDULER_shutdown ();
  53. }
  54. /**
  55. * Function called to notify transport users that another
  56. * peer connected to us.
  57. *
  58. * @param cls closure
  59. * @param peer the peer that connected
  60. * @param mq message queue to send to @a peer
  61. * @return NULL
  62. */
  63. static void *
  64. notify_connect (void *cls,
  65. const struct GNUNET_PeerIdentity *peer,
  66. struct GNUNET_MQ_Handle *mq)
  67. {
  68. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected, shutting down.\n");
  69. ok = 0;
  70. GNUNET_SCHEDULER_shutdown ();
  71. return NULL;
  72. }
  73. static void
  74. process_hello (void *cls, const struct GNUNET_MessageHeader *message)
  75. {
  76. struct PeerContext *p = cls;
  77. GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
  78. p->ghh = NULL;
  79. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  80. "Received HELLO, starting hostlist service.\n");
  81. }
  82. static void
  83. setup_peer (struct PeerContext *p, const char *cfgname)
  84. {
  85. char *binary;
  86. binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
  87. p->cfg = GNUNET_CONFIGURATION_create ();
  88. p->arm_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR
  89. | GNUNET_OS_USE_PIPE_CONTROL,
  90. NULL,
  91. NULL,
  92. NULL,
  93. binary,
  94. "gnunet-service-arm",
  95. "-c",
  96. cfgname,
  97. NULL);
  98. GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
  99. p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
  100. NULL,
  101. NULL,
  102. p,
  103. &notify_connect,
  104. NULL,
  105. NULL);
  106. GNUNET_assert (NULL != p->th);
  107. p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
  108. GNUNET_TRANSPORT_AC_ANY,
  109. &process_hello,
  110. p);
  111. GNUNET_free (binary);
  112. }
  113. static void
  114. waitpid_task (void *cls)
  115. {
  116. struct PeerContext *p = cls;
  117. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Killing ARM process.\n");
  118. if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
  119. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  120. if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
  121. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  122. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  123. "ARM process %u stopped\n",
  124. GNUNET_OS_process_get_pid (p->arm_proc));
  125. GNUNET_OS_process_destroy (p->arm_proc);
  126. p->arm_proc = NULL;
  127. GNUNET_CONFIGURATION_destroy (p->cfg);
  128. }
  129. static void
  130. stop_arm (struct PeerContext *p)
  131. {
  132. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking ARM to stop core service\n");
  133. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &waitpid_task, p);
  134. }
  135. /**
  136. * Try again to connect to transport service.
  137. */
  138. static void
  139. shutdown_task (void *cls)
  140. {
  141. if (NULL != timeout_task)
  142. {
  143. GNUNET_SCHEDULER_cancel (timeout_task);
  144. timeout_task = NULL;
  145. }
  146. if (NULL != p1.ghh)
  147. {
  148. GNUNET_TRANSPORT_hello_get_cancel (p1.ghh);
  149. p1.ghh = NULL;
  150. }
  151. if (NULL != p1.th)
  152. {
  153. GNUNET_TRANSPORT_core_disconnect (p1.th);
  154. p1.th = NULL;
  155. }
  156. if (NULL != p2.ghh)
  157. {
  158. GNUNET_TRANSPORT_hello_get_cancel (p2.ghh);
  159. p2.ghh = NULL;
  160. }
  161. if (NULL != p2.th)
  162. {
  163. GNUNET_TRANSPORT_core_disconnect (p2.th);
  164. p2.th = NULL;
  165. }
  166. stop_arm (&p1);
  167. stop_arm (&p2);
  168. }
  169. static void
  170. run (void *cls,
  171. char *const *args,
  172. const char *cfgfile,
  173. const struct GNUNET_CONFIGURATION_Handle *cfg)
  174. {
  175. GNUNET_assert (ok == 1);
  176. ok++;
  177. timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
  178. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  179. setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
  180. setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
  181. }
  182. int
  183. main (int argcx, char *argvx[])
  184. {
  185. static char *const argv[] = { "test-gnunet-daemon-hostlist",
  186. "-c",
  187. "test_gnunet_daemon_hostlist_data.conf",
  188. NULL };
  189. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  190. GNUNET_GETOPT_OPTION_END
  191. };
  192. GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer1.conf",
  193. "GNUNET_TEST_HOME");
  194. GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer2.conf",
  195. "GNUNET_TEST_HOME");
  196. GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_data.conf",
  197. "GNUNET_TEST_HOME");
  198. GNUNET_log_setup ("test-gnunet-daemon-hostlist", "WARNING", NULL);
  199. ok = 1;
  200. GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1,
  201. argv,
  202. "test-gnunet-daemon-hostlist",
  203. "nohelp",
  204. options,
  205. &run,
  206. &ok);
  207. if (0 == ok)
  208. {
  209. fprintf (stderr, "%s", ".");
  210. /* now do it again */
  211. ok = 1;
  212. GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1,
  213. argv,
  214. "test-gnunet-daemon-hostlist",
  215. "nohelp",
  216. options,
  217. &run,
  218. &ok);
  219. fprintf (stderr, "%s", ".\n");
  220. }
  221. GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer1.conf",
  222. "GNUNET_TEST_HOME");
  223. GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer2.conf",
  224. "GNUNET_TEST_HOME");
  225. GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_data.conf",
  226. "GNUNET_TEST_HOME");
  227. return ok;
  228. }
  229. /* end of test_gnunet_daemon_hostlist_reconnect.c */