test_arm_api.c 7.1 KB

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