test_gnunet_helper_testbed.c 6.5 KB

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