test_arm_api.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2011 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 arm/test_arm_api.c
  19. * @brief testcase for arm_api.c
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_arm_service.h"
  24. #include "gnunet_resolver_service.h"
  25. #define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
  26. #define START_ARM GNUNET_YES
  27. #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1500)
  28. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
  29. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  30. static struct GNUNET_ARM_Handle *arm;
  31. static int ok = 1;
  32. static int phase = 0;
  33. static void
  34. arm_stop_cb (void *cls,
  35. enum GNUNET_ARM_RequestStatus status,
  36. const char *servicename,
  37. enum GNUNET_ARM_Result result)
  38. {
  39. /* (6), a stop request should be sent to ARM successfully */
  40. /* ARM should report that it is stopping */
  41. GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
  42. GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
  43. GNUNET_break (phase == 6);
  44. phase++;
  45. LOG ("Sent 'STOP' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
  46. }
  47. static void
  48. resolver_stop_cb (void *cls,
  49. enum GNUNET_ARM_RequestStatus status,
  50. const char *servicename, enum GNUNET_ARM_Result result)
  51. {
  52. /* (5), a stop request should be sent to ARM successfully.
  53. * ARM should report that resolver is stopped.
  54. */
  55. GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
  56. GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
  57. GNUNET_break (phase == 5);
  58. LOG ("Sent 'STOP' request for resolver to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
  59. phase++;
  60. #if START_ARM
  61. GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
  62. #else
  63. arm_stop_cb (NULL, GNUNET_ARM_STATUS_SENT_OK, "arm", GNUNET_ARM_SERVICE_STOPPING);
  64. arm_conn (NULL, GNUNET_NO);
  65. #endif
  66. }
  67. static void
  68. dns_notify (void *cls, const struct sockaddr *addr, socklen_t addrlen)
  69. {
  70. if (addr == NULL)
  71. {
  72. /* (4), resolver should finish resolving localhost */
  73. GNUNET_break (phase == 4);
  74. phase++;
  75. LOG ("Finished resolving localhost\n");
  76. if (ok != 0)
  77. ok = 2;
  78. GNUNET_ARM_request_service_stop (arm, "resolver", TIMEOUT, resolver_stop_cb, NULL);
  79. return;
  80. }
  81. /* (3), resolver should resolve localhost */
  82. GNUNET_break (phase == 3);
  83. LOG ("Resolved localhost\n");
  84. phase++;
  85. GNUNET_break (addr != NULL);
  86. ok = 0;
  87. }
  88. static void
  89. resolver_start_cb (void *cls,
  90. enum GNUNET_ARM_RequestStatus status,
  91. const char *servicename,
  92. enum GNUNET_ARM_Result result)
  93. {
  94. /* (2), the start request for resolver should be sent successfully
  95. * ARM should report that resolver service is starting.
  96. */
  97. GNUNET_assert (status == GNUNET_ARM_REQUEST_SENT_OK);
  98. GNUNET_break (phase == 2);
  99. GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
  100. LOG ("Sent 'START' request for resolver to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
  101. phase++;
  102. GNUNET_RESOLVER_ip_get ("localhost", AF_INET, TIMEOUT, &dns_notify, NULL);
  103. }
  104. static void
  105. trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  106. {
  107. GNUNET_ARM_disconnect_and_free ((struct GNUNET_ARM_Handle *) cls);
  108. }
  109. static void
  110. arm_conn (void *cls,
  111. int connected)
  112. {
  113. if (GNUNET_SYSERR == connected)
  114. {
  115. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  116. _("Fatal error initializing ARM API.\n"));
  117. GNUNET_SCHEDULER_shutdown ();
  118. GNUNET_assert (0);
  119. return;
  120. }
  121. if (GNUNET_YES == connected)
  122. {
  123. /* (1), arm connection should be established */
  124. LOG ("Connected to ARM\n");
  125. GNUNET_break (phase == 1);
  126. phase++;
  127. GNUNET_ARM_request_service_start (arm, "resolver", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, resolver_start_cb, NULL);
  128. }
  129. else
  130. {
  131. /* (7), ARM should stop (we disconnect from it) */
  132. LOG ("Disconnected from ARM\n");
  133. GNUNET_break (phase == 7);
  134. if (phase != 7)
  135. ok = 3;
  136. else if (ok == 1)
  137. ok = 0;
  138. GNUNET_SCHEDULER_add_now (trigger_disconnect, arm);
  139. }
  140. }
  141. static void
  142. arm_start_cb (void *cls,
  143. enum GNUNET_ARM_RequestStatus status,
  144. const char *servicename,
  145. enum GNUNET_ARM_Result result)
  146. {
  147. /* (0) The request should be "sent" successfully
  148. * ("sent", because it isn't going anywhere, ARM API starts ARM service
  149. * by itself).
  150. * ARM API should report that ARM service is starting.
  151. */
  152. GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
  153. GNUNET_break (phase == 0);
  154. LOG ("Sent 'START' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
  155. GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
  156. phase++;
  157. }
  158. static void
  159. task (void *cls, char *const *args, const char *cfgfile,
  160. const struct GNUNET_CONFIGURATION_Handle *c)
  161. {
  162. char *armconfig;
  163. cfg = c;
  164. if (NULL != cfgfile)
  165. {
  166. if (GNUNET_OK !=
  167. GNUNET_CONFIGURATION_get_value_filename (cfg, "arm", "CONFIG",
  168. &armconfig))
  169. {
  170. GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
  171. *) cfg, "arm", "CONFIG",
  172. cfgfile);
  173. }
  174. else
  175. GNUNET_free (armconfig);
  176. }
  177. arm = GNUNET_ARM_connect (cfg, &arm_conn, NULL);
  178. if (NULL == arm)
  179. return;
  180. #if START_ARM
  181. GNUNET_ARM_request_service_start (arm, "arm", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, arm_start_cb, NULL);
  182. #else
  183. arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_STARTING);
  184. arm_conn (NULL, arm, GNUNET_YES);
  185. #endif
  186. }
  187. int
  188. main (int argc, char *argvx[])
  189. {
  190. char *const argv[] = {
  191. "test-arm-api",
  192. "-c", "test_arm_api_data.conf",
  193. NULL
  194. };
  195. struct GNUNET_GETOPT_CommandLineOption options[] = {
  196. GNUNET_GETOPT_OPTION_END
  197. };
  198. GNUNET_log_setup ("test-arm-api",
  199. "WARNING",
  200. NULL);
  201. GNUNET_assert (GNUNET_OK ==
  202. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  203. argv, "test-arm-api", "nohelp", options,
  204. &task, NULL));
  205. return ok;
  206. }
  207. /* end of test_arm_api.c */