test_bridging.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2017, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief Bridging unit tests
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/test.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/time.h"
  35. #include "asterisk/bridge.h"
  36. #include "asterisk/bridge_basic.h"
  37. #include "asterisk/features.h"
  38. #include "asterisk/format_cache.h"
  39. #define TEST_CATEGORY "/main/bridging/"
  40. #define CHANNEL_TECH_NAME "BridgingTestChannel"
  41. #define TEST_CHANNEL_FORMAT ast_format_slin
  42. /*! \brief A private structure for the test channel */
  43. struct test_bridging_chan_pvt {
  44. /* \brief The expected indication */
  45. int condition;
  46. /*! \brief The number of indicated things */
  47. unsigned int indicated;
  48. };
  49. /*! \brief Callback function for when a frame is written to a channel */
  50. static int test_bridging_chan_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
  51. {
  52. struct test_bridging_chan_pvt *test_pvt = ast_channel_tech_pvt(chan);
  53. if (condition == test_pvt->condition) {
  54. test_pvt->indicated++;
  55. }
  56. return 0;
  57. }
  58. /*! \brief Callback function for when a channel is hung up */
  59. static int test_bridging_chan_hangup(struct ast_channel *chan)
  60. {
  61. struct test_bridging_chan_pvt *test_pvt = ast_channel_tech_pvt(chan);
  62. ast_free(test_pvt);
  63. ast_channel_tech_pvt_set(chan, NULL);
  64. return 0;
  65. }
  66. /*! \brief A channel technology used for the unit tests */
  67. static struct ast_channel_tech test_bridging_chan_tech = {
  68. .type = CHANNEL_TECH_NAME,
  69. .description = "Mock channel technology for bridge tests",
  70. .indicate = test_bridging_chan_indicate,
  71. .hangup = test_bridging_chan_hangup,
  72. .properties = AST_CHAN_TP_INTERNAL,
  73. };
  74. static void test_nanosleep(int secs, long nanosecs)
  75. {
  76. struct timespec sleep_time = {secs, nanosecs};
  77. while ((nanosleep(&sleep_time, &sleep_time) == -1) && (errno == EINTR)) {
  78. }
  79. }
  80. /*! \brief Wait until a channel is bridged */
  81. static void wait_for_bridged(struct ast_channel *channel)
  82. {
  83. ast_channel_lock(channel);
  84. while (!ast_channel_is_bridged(channel)) {
  85. ast_channel_unlock(channel);
  86. test_nanosleep(0, 1000000);
  87. ast_channel_lock(channel);
  88. }
  89. ast_channel_unlock(channel);
  90. }
  91. /*! \brief Wait until a channel is not bridged */
  92. static void wait_for_unbridged(struct ast_channel *channel)
  93. {
  94. ast_channel_lock(channel);
  95. while (ast_channel_is_bridged(channel)) {
  96. ast_channel_unlock(channel);
  97. test_nanosleep(0, 1000000);
  98. ast_channel_lock(channel);
  99. }
  100. ast_channel_unlock(channel);
  101. }
  102. /*! \brief Wait until a channel has no frames on its read queue */
  103. static void wait_for_empty_queue(struct ast_channel *channel)
  104. {
  105. ast_channel_lock(channel);
  106. while (!AST_LIST_EMPTY(ast_channel_readq(channel))) {
  107. ast_channel_unlock(channel);
  108. test_nanosleep(0, 1000000);
  109. ast_channel_lock(channel);
  110. }
  111. ast_channel_unlock(channel);
  112. }
  113. /*! \brief Create a \ref test_bridging_chan_tech for Alice. */
  114. #define START_ALICE(channel, pvt) START_CHANNEL(channel, pvt, "Alice", "100")
  115. /*! \brief Create a \ref test_bridging_chan_tech for Bob. */
  116. #define START_BOB(channel, pvt) START_CHANNEL(channel, pvt, "Bob", "200")
  117. #define START_CHANNEL(channel, pvt, name, number) do { \
  118. channel = ast_channel_alloc(0, AST_STATE_UP, number, name, number, number, \
  119. "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/" name); \
  120. pvt = ast_calloc(1, sizeof(*pvt)); \
  121. ast_channel_tech_pvt_set(channel, pvt); \
  122. ast_channel_nativeformats_set(channel, test_bridging_chan_tech.capabilities); \
  123. ast_channel_set_rawwriteformat(channel, TEST_CHANNEL_FORMAT); \
  124. ast_channel_set_rawreadformat(channel, TEST_CHANNEL_FORMAT); \
  125. ast_channel_set_writeformat(channel, TEST_CHANNEL_FORMAT); \
  126. ast_channel_set_readformat(channel, TEST_CHANNEL_FORMAT); \
  127. ast_channel_unlock(channel); \
  128. } while (0)
  129. /*! \brief Hang up a test channel safely */
  130. #define HANGUP_CHANNEL(channel) do { \
  131. ao2_ref(channel, +1); \
  132. ast_hangup((channel)); \
  133. ao2_cleanup(channel); \
  134. channel = NULL; \
  135. } while (0)
  136. static void safe_channel_release(struct ast_channel *chan)
  137. {
  138. if (!chan) {
  139. return;
  140. }
  141. ast_channel_release(chan);
  142. }
  143. static void safe_bridge_destroy(struct ast_bridge *bridge)
  144. {
  145. if (!bridge) {
  146. return;
  147. }
  148. ast_bridge_destroy(bridge, 0);
  149. }
  150. static void stream_periodic_frames(struct ast_channel *chan, int ms, int interval_ms)
  151. {
  152. long nanosecs;
  153. ast_assert(chan != NULL);
  154. ast_assert(0 < ms);
  155. ast_assert(0 < interval_ms);
  156. nanosecs = interval_ms * 1000000L;
  157. while (0 < ms) {
  158. ast_queue_frame(chan, &ast_null_frame);
  159. if (interval_ms < ms) {
  160. ms -= interval_ms;
  161. } else {
  162. nanosecs = ms * 1000000L;
  163. ms = 0;
  164. }
  165. test_nanosleep(0, nanosecs);
  166. }
  167. }
  168. AST_TEST_DEFINE(test_bridging_deferred_queue)
  169. {
  170. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  171. struct test_bridging_chan_pvt *alice_pvt;
  172. struct ast_control_t38_parameters t38_parameters = {
  173. .request_response = AST_T38_REQUEST_NEGOTIATE,
  174. };
  175. struct ast_frame frame = {
  176. .frametype = AST_FRAME_CONTROL,
  177. .subclass.integer = AST_CONTROL_T38_PARAMETERS,
  178. .data.ptr = &t38_parameters,
  179. .datalen = sizeof(t38_parameters),
  180. };
  181. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  182. struct test_bridging_chan_pvt *bob_pvt;
  183. RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
  184. switch (cmd) {
  185. case TEST_INIT:
  186. info->name = __func__;
  187. info->category = TEST_CATEGORY;
  188. info->summary = "Test that deferred frames from a channel in a bridge get written";
  189. info->description =
  190. "This test creates two channels, queues a deferrable frame on one, places it into\n"
  191. "a bridge, confirms the frame was read by the bridge, adds the second channel to the\n"
  192. "bridge, and makes sure the deferred frame is written to it.";
  193. return AST_TEST_NOT_RUN;
  194. case TEST_EXECUTE:
  195. break;
  196. }
  197. /* Create the bridges */
  198. bridge1 = ast_bridge_basic_new();
  199. ast_test_validate(test, bridge1 != NULL);
  200. /* Create channels that will go into the bridge */
  201. START_ALICE(chan_alice, alice_pvt);
  202. START_BOB(chan_bob, bob_pvt);
  203. bob_pvt->condition = AST_CONTROL_T38_PARAMETERS;
  204. /* Bridge alice and wait for the frame to be deferred */
  205. ast_test_validate(test, !ast_bridge_impart(bridge1, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
  206. wait_for_bridged(chan_alice);
  207. ast_queue_frame(chan_alice, &frame);
  208. wait_for_empty_queue(chan_alice);
  209. /* Bridge bob for a second so it can receive the deferred T.38 request negotiate frame */
  210. ast_test_validate(test, !ast_bridge_impart(bridge1, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
  211. wait_for_bridged(chan_bob);
  212. stream_periodic_frames(chan_alice, 1000, 20);
  213. ast_test_validate(test, !ast_bridge_depart(chan_bob));
  214. wait_for_unbridged(chan_bob);
  215. /* Ensure that we received the expected indications while it was in there (request to negotiate, and to terminate) */
  216. ast_test_validate(test, bob_pvt->indicated == 2);
  217. /* Now remove alice since we are done */
  218. ast_test_validate(test, !ast_bridge_depart(chan_alice));
  219. wait_for_unbridged(chan_alice);
  220. /* Hangup the channels */
  221. HANGUP_CHANNEL(chan_alice);
  222. HANGUP_CHANNEL(chan_bob);
  223. return AST_TEST_PASS;
  224. }
  225. static int unload_module(void)
  226. {
  227. AST_TEST_UNREGISTER(test_bridging_deferred_queue);
  228. ast_channel_unregister(&test_bridging_chan_tech);
  229. ao2_cleanup(test_bridging_chan_tech.capabilities);
  230. test_bridging_chan_tech.capabilities = NULL;
  231. return 0;
  232. }
  233. static int load_module(void)
  234. {
  235. test_bridging_chan_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  236. if (!test_bridging_chan_tech.capabilities) {
  237. return AST_MODULE_LOAD_DECLINE;
  238. }
  239. ast_format_cap_append(test_bridging_chan_tech.capabilities, TEST_CHANNEL_FORMAT, 0);
  240. ast_channel_register(&test_bridging_chan_tech);
  241. AST_TEST_REGISTER(test_bridging_deferred_queue);
  242. return AST_MODULE_LOAD_SUCCESS;
  243. }
  244. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Bridging Unit Tests");