test_transport_plugin_cmd_udp_backchannel.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2021 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/plugin_cmd_simple_send.c
  18. * @brief a plugin to provide the API for running test cases.
  19. * @author t3sserakt
  20. */
  21. #include "platform.h"
  22. #include "gnunet_testing_ng_lib.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_transport_application_service.h"
  25. #include "transport-testing2.h"
  26. #include "transport-testing-cmds.h"
  27. /**
  28. * Generic logging shortcut
  29. */
  30. #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
  31. #define BASE_DIR "testdir"
  32. #define TOPOLOGY_CONFIG "test_transport_udp_backchannel_topo.conf"
  33. /**
  34. * The name for a specific test environment directory.
  35. *
  36. */
  37. char *testdir;
  38. /**
  39. * The name for the configuration file of the specific node.
  40. *
  41. */
  42. char *cfgname;
  43. /**
  44. * Flag indicating if all peers have been started.
  45. *
  46. */
  47. unsigned int are_all_peers_started;
  48. /**
  49. * Flag indicating a received message.
  50. */
  51. unsigned int message_received;
  52. /**
  53. * Function called to check a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE being
  54. * received.
  55. *
  56. */
  57. static int
  58. check_test (void *cls,
  59. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  60. {
  61. return GNUNET_OK;
  62. }
  63. /**
  64. * Function called to handle a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE
  65. * being received.
  66. *
  67. */
  68. static void
  69. handle_test (void *cls,
  70. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  71. {
  72. message_received = GNUNET_YES;
  73. }
  74. /**
  75. * Callback to set the flag indicating all peers started. Will be called via the plugin api.
  76. *
  77. */
  78. static void
  79. all_peers_started ()
  80. {
  81. are_all_peers_started = GNUNET_YES;
  82. LOG (GNUNET_ERROR_TYPE_ERROR,
  83. "setting are_all_peers_started: %d\n",
  84. are_all_peers_started);
  85. }
  86. /**
  87. * Function to start a local test case.
  88. *
  89. * @param write_message Callback to send a message to the master loop.
  90. * @param router_ip Global address of the network namespace.
  91. * @param node_ip Local address of a node i a network namespace.
  92. * @param m The number of the node in a network namespace.
  93. * @param n The number of the network namespace.
  94. * @param local_m The number of nodes in a network namespace.
  95. */
  96. static void
  97. start_testcase (TESTING_CMD_HELPER_write_cb write_message, char *router_ip,
  98. char *node_ip,
  99. char *m,
  100. char *n,
  101. char *local_m)
  102. {
  103. unsigned int n_int, m_int, local_m_int, num;
  104. struct GNUNET_TESTING_NetjailTopology *topology =
  105. GNUNET_TESTING_get_topo_from_file (TOPOLOGY_CONFIG);
  106. sscanf (m, "%u", &m_int);
  107. sscanf (n, "%u", &n_int);
  108. sscanf (local_m, "%u", &local_m_int);
  109. if (0 == n_int)
  110. num = m_int;
  111. else
  112. num = (n_int - 1) * local_m_int + m_int + topology->nodes_x;
  113. GNUNET_asprintf (&cfgname,
  114. "test_transport_api2_tcp_node1.conf");
  115. LOG (GNUNET_ERROR_TYPE_ERROR,
  116. "plugin cfgname: %s\n",
  117. cfgname);
  118. LOG (GNUNET_ERROR_TYPE_ERROR,
  119. "node ip: %s\n",
  120. node_ip);
  121. GNUNET_asprintf (&testdir,
  122. "%s%s%s",
  123. BASE_DIR,
  124. m,
  125. n);
  126. struct GNUNET_MQ_MessageHandler handlers[] = {
  127. GNUNET_MQ_hd_var_size (test,
  128. GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
  129. struct GNUNET_TRANSPORT_TESTING_TestMessage,
  130. NULL),
  131. GNUNET_MQ_handler_end ()
  132. };
  133. struct GNUNET_TESTING_Command commands[] = {
  134. GNUNET_TESTING_cmd_system_create ("system-create",
  135. testdir),
  136. GNUNET_TRANSPORT_cmd_start_peer_v3 ("start-peer",
  137. "system-create",
  138. num,
  139. node_ip,
  140. handlers,
  141. cfgname),
  142. GNUNET_TESTING_cmd_send_peer_ready ("send-peer-ready",
  143. write_message),
  144. GNUNET_TESTING_cmd_block_until_all_peers_started ("block",
  145. &are_all_peers_started),
  146. GNUNET_TRANSPORT_cmd_connect_peers_v3 ("connect-peers",
  147. "start-peer",
  148. "system-create",
  149. num,
  150. topology),
  151. GNUNET_TRANSPORT_cmd_send_simple_v2 ("send-simple",
  152. "start-peer",
  153. num),
  154. GNUNET_TESTING_cmd_block_until_external_trigger ("block-receive",
  155. &message_received),
  156. GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer",
  157. "start-peer"),
  158. GNUNET_TESTING_cmd_system_destroy ("system-destroy",
  159. "system-create"),
  160. GNUNET_TESTING_cmd_local_test_finished ("local-test-finished",
  161. write_message)
  162. };
  163. GNUNET_TESTING_run (NULL,
  164. commands,
  165. GNUNET_TIME_UNIT_FOREVER_REL);
  166. }
  167. /**
  168. * Entry point for the plugin.
  169. *
  170. * @param cls NULL
  171. * @return the exported block API
  172. */
  173. void *
  174. libgnunet_test_transport_plugin_cmd_udp_backchannel_init (void *cls)
  175. {
  176. struct GNUNET_TESTING_PluginFunctions *api;
  177. GNUNET_log_setup ("udp-backchannel",
  178. "DEBUG",
  179. NULL);
  180. api = GNUNET_new (struct GNUNET_TESTING_PluginFunctions);
  181. api->start_testcase = &start_testcase;
  182. api->all_peers_started = &all_peers_started;
  183. return api;
  184. }
  185. /**
  186. * Exit point from the plugin.
  187. *
  188. * @param cls the return value from #libgnunet_test_transport_plugin_block_test_init
  189. * @return NULL
  190. */
  191. void *
  192. libgnunet_test_transport_plugin_cmd_udp_backchannel_done (void *cls)
  193. {
  194. struct GNUNET_TESTING_PluginFunctions *api = cls;
  195. GNUNET_free (api);
  196. GNUNET_free (testdir);
  197. GNUNET_free (cfgname);
  198. return NULL;
  199. }
  200. /* end of plugin_cmd_simple_send.c */