test_arm_api.c 6.9 KB

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