test_gnunet_helper_testbed.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008--2013 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 testbed/test_gnunet_helper_testbed.c
  18. * @brief Testcase for testing gnunet-helper-testbed.c
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testbed_service.h"
  24. #include <zlib.h>
  25. #include "testbed_api.h"
  26. #include "testbed_helper.h"
  27. #include "testbed_api_hosts.h"
  28. /**
  29. * Generic logging shortcut
  30. */
  31. #define LOG(kind,...) \
  32. GNUNET_log (kind, __VA_ARGS__)
  33. /**
  34. * Handle to the helper process
  35. */
  36. static struct GNUNET_HELPER_Handle *helper;
  37. /**
  38. * Message to helper
  39. */
  40. static struct GNUNET_TESTBED_HelperInit *msg;
  41. /**
  42. * Message send handle
  43. */
  44. static struct GNUNET_HELPER_SendHandle *shandle;
  45. /**
  46. * Abort task identifier
  47. */
  48. static struct GNUNET_SCHEDULER_Task * abort_task;
  49. /**
  50. * Shutdown task identifier
  51. */
  52. static struct GNUNET_SCHEDULER_Task * shutdown_task;
  53. /**
  54. * Configuratin handler
  55. */
  56. static struct GNUNET_CONFIGURATION_Handle *cfg;
  57. /**
  58. * Global testing status
  59. */
  60. static int result;
  61. /**
  62. * Shutdown nicely
  63. *
  64. * @param cls NULL
  65. */
  66. static void
  67. do_shutdown (void *cls)
  68. {
  69. if (NULL != abort_task)
  70. GNUNET_SCHEDULER_cancel (abort_task);
  71. if (NULL != helper)
  72. GNUNET_HELPER_stop (helper, GNUNET_NO);
  73. GNUNET_free_non_null (msg);
  74. if (NULL != cfg)
  75. GNUNET_CONFIGURATION_destroy (cfg);
  76. }
  77. /**
  78. * abort task to run on test timed out
  79. *
  80. * @param cls NULL
  81. */
  82. static void
  83. do_abort (void *cls)
  84. {
  85. abort_task = NULL;
  86. LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
  87. result = GNUNET_SYSERR;
  88. if (NULL != shandle)
  89. GNUNET_HELPER_send_cancel (shandle);
  90. if (NULL == shutdown_task)
  91. shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  92. }
  93. /**
  94. * Continuation function.
  95. *
  96. * @param cls closure
  97. * @param result #GNUNET_OK on success,
  98. * #GNUNET_NO if helper process died
  99. * #GNUNET_SYSERR during GNUNET_HELPER_stop()
  100. */
  101. static void
  102. cont_cb (void *cls,
  103. int result)
  104. {
  105. shandle = NULL;
  106. LOG (GNUNET_ERROR_TYPE_DEBUG,
  107. "Message sent\n");
  108. GNUNET_assert (GNUNET_OK == result);
  109. }
  110. /**
  111. * Functions with this signature are called whenever a
  112. * complete message is received by the tokenizer.
  113. *
  114. * Do not call GNUNET_SERVER_mst_destroy in callback
  115. *
  116. * @param cls closure
  117. * @param client identification of the client
  118. * @param message the actual message
  119. * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
  120. */
  121. static int
  122. mst_cb (void *cls,
  123. const struct GNUNET_MessageHeader *message)
  124. {
  125. const struct GNUNET_TESTBED_HelperReply *msg;
  126. char *config;
  127. uLongf config_size;
  128. uLongf xconfig_size;
  129. msg = (const struct GNUNET_TESTBED_HelperReply *) message;
  130. config_size = 0;
  131. xconfig_size = 0;
  132. GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
  133. ntohs (msg->header.size));
  134. GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
  135. ntohs (msg->header.type));
  136. config_size = (uLongf) ntohs (msg->config_size);
  137. xconfig_size =
  138. (uLongf) (ntohs (msg->header.size) -
  139. sizeof (struct GNUNET_TESTBED_HelperReply));
  140. config = GNUNET_malloc (config_size);
  141. GNUNET_assert (Z_OK ==
  142. uncompress ((Bytef *) config, &config_size,
  143. (const Bytef *) &msg[1], xconfig_size));
  144. GNUNET_free (config);
  145. if (NULL == shutdown_task)
  146. shutdown_task =
  147. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  148. (GNUNET_TIME_UNIT_SECONDS, 1),
  149. &do_shutdown, NULL);
  150. return GNUNET_OK;
  151. }
  152. /**
  153. * Callback that will be called when the helper process dies. This is not called
  154. * when the helper process is stoped using GNUNET_HELPER_stop()
  155. *
  156. * @param cls the closure from GNUNET_HELPER_start()
  157. */
  158. static void
  159. exp_cb (void *cls)
  160. {
  161. helper = NULL;
  162. result = GNUNET_SYSERR;
  163. }
  164. /**
  165. * Main function that will be run.
  166. *
  167. * @param cls closure
  168. * @param args remaining command-line arguments
  169. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  170. * @param cfg configuration
  171. */
  172. static void
  173. run (void *cls, char *const *args, const char *cfgfile,
  174. const struct GNUNET_CONFIGURATION_Handle *cfg2)
  175. {
  176. static char *const binary_argv[] = {
  177. "gnunet-helper-testbed",
  178. NULL
  179. };
  180. const char *trusted_ip = "127.0.0.1";
  181. helper =
  182. GNUNET_HELPER_start (GNUNET_YES,
  183. "gnunet-helper-testbed",
  184. binary_argv,
  185. &mst_cb,
  186. &exp_cb,
  187. NULL);
  188. GNUNET_assert (NULL != helper);
  189. cfg = GNUNET_CONFIGURATION_dup (cfg2);
  190. msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, NULL, cfg);
  191. shandle =
  192. GNUNET_HELPER_send (helper, &msg->header, GNUNET_NO, &cont_cb, NULL);
  193. GNUNET_assert (NULL != shandle);
  194. abort_task =
  195. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  196. (GNUNET_TIME_UNIT_MINUTES, 1), &do_abort,
  197. NULL);
  198. }
  199. /**
  200. * Main function
  201. *
  202. * @param argc the number of command line arguments
  203. * @param argv command line arg array
  204. * @return return code
  205. */
  206. int
  207. main (int argc, char **argv)
  208. {
  209. struct GNUNET_GETOPT_CommandLineOption options[] = {
  210. GNUNET_GETOPT_OPTION_END
  211. };
  212. result = GNUNET_OK;
  213. if (GNUNET_OK !=
  214. GNUNET_PROGRAM_run (argc, argv, "test_gnunet_helper_testbed",
  215. "Testcase for testing gnunet-helper-testbed.c",
  216. options, &run, NULL))
  217. return 1;
  218. return (GNUNET_OK == result) ? 0 : 1;
  219. }
  220. /* end of test_gnunet_helper_testbed.c */