test_channel_feature_hooks.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Kinsey Moore <kmoore@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 Channel features unit tests
  21. *
  22. * \author Kinsey Moore <kmoore@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 "/channels/features/"
  40. #define CHANNEL_TECH_NAME "FeaturesTestChannel"
  41. #define TEST_BACKEND_NAME "Features Test Logging"
  42. #define TEST_CHANNEL_FORMAT ast_format_slin
  43. /*! \brief A channel technology used for the unit tests */
  44. static struct ast_channel_tech test_features_chan_tech = {
  45. .type = CHANNEL_TECH_NAME,
  46. .description = "Mock channel technology for Features tests",
  47. };
  48. static void test_nanosleep(int secs, long nanosecs)
  49. {
  50. struct timespec sleep_time = {secs, nanosecs};
  51. while ((nanosleep(&sleep_time, &sleep_time) == -1) && (errno == EINTR)) {
  52. }
  53. }
  54. /*! \brief Wait until a channel is bridged */
  55. static void wait_for_bridged(struct ast_channel *channel)
  56. {
  57. ast_channel_lock(channel);
  58. while (!ast_channel_is_bridged(channel)) {
  59. ast_channel_unlock(channel);
  60. test_nanosleep(0, 1000000);
  61. ast_channel_lock(channel);
  62. }
  63. ast_channel_unlock(channel);
  64. }
  65. /*! \brief Wait until a channel is not bridged */
  66. static void wait_for_unbridged(struct ast_channel *channel)
  67. {
  68. ast_channel_lock(channel);
  69. while (ast_channel_is_bridged(channel)) {
  70. ast_channel_unlock(channel);
  71. test_nanosleep(0, 1000000);
  72. ast_channel_lock(channel);
  73. }
  74. ast_channel_unlock(channel);
  75. }
  76. /*! \brief Create a \ref test_features_chan_tech for Alice. */
  77. #define START_ALICE(channel) START_CHANNEL(channel, "Alice", "100")
  78. /*! \brief Create a \ref test_features_chan_tech for Bob. */
  79. #define START_BOB(channel) START_CHANNEL(channel, "Bob", "200")
  80. #define START_CHANNEL(channel, name, number) do { \
  81. channel = ast_channel_alloc(0, AST_STATE_UP, number, name, number, number, \
  82. "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/" name); \
  83. ast_channel_nativeformats_set(channel, test_features_chan_tech.capabilities); \
  84. ast_channel_set_rawwriteformat(channel, TEST_CHANNEL_FORMAT); \
  85. ast_channel_set_rawreadformat(channel, TEST_CHANNEL_FORMAT); \
  86. ast_channel_set_writeformat(channel, TEST_CHANNEL_FORMAT); \
  87. ast_channel_set_readformat(channel, TEST_CHANNEL_FORMAT); \
  88. ast_channel_unlock(channel); \
  89. } while (0)
  90. /*! \brief Hang up a test channel safely */
  91. #define HANGUP_CHANNEL(channel) do { \
  92. ao2_ref(channel, +1); \
  93. ast_hangup((channel)); \
  94. ao2_cleanup(channel); \
  95. channel = NULL; \
  96. } while (0)
  97. static void safe_channel_release(struct ast_channel *chan)
  98. {
  99. if (!chan) {
  100. return;
  101. }
  102. ast_channel_release(chan);
  103. }
  104. static void safe_bridge_destroy(struct ast_bridge *bridge)
  105. {
  106. if (!bridge) {
  107. return;
  108. }
  109. ast_bridge_destroy(bridge, 0);
  110. }
  111. static int feature_callback(struct ast_bridge_channel *bridge_channel, void *obj)
  112. {
  113. int *callback_executed = obj;
  114. (*callback_executed)++;
  115. return 0;
  116. }
  117. /* Need to post null frames periodically so DTMF emulation can work. */
  118. static void stream_periodic_frames(struct ast_channel *chan, int ms, int interval_ms)
  119. {
  120. long nanosecs;
  121. ast_assert(chan != NULL);
  122. ast_assert(0 < ms);
  123. ast_assert(0 < interval_ms);
  124. nanosecs = interval_ms * 1000000L;
  125. while (0 < ms) {
  126. ast_queue_frame(chan, &ast_null_frame);
  127. if (interval_ms < ms) {
  128. ms -= interval_ms;
  129. } else {
  130. nanosecs = ms * 1000000L;
  131. ms = 0;
  132. }
  133. test_nanosleep(0, nanosecs);
  134. }
  135. }
  136. AST_TEST_DEFINE(test_features_channel_dtmf)
  137. {
  138. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  139. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  140. RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
  141. RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
  142. struct ast_bridge_features features;
  143. int callback_executed = 0;
  144. struct ast_frame f = { AST_FRAME_DTMF, };
  145. switch (cmd) {
  146. case TEST_INIT:
  147. info->name = __func__;
  148. info->category = TEST_CATEGORY;
  149. info->summary = "Test running DTMF hooks on a channel via the feature hooks mechanism";
  150. info->description =
  151. "This test creates two channels, adds a DTMF hook to one, places them into\n"
  152. "a bridge, and verifies that the DTMF hook added to the channel feature\n"
  153. "hooks can be triggered once the channel is bridged.";
  154. return AST_TEST_NOT_RUN;
  155. case TEST_EXECUTE:
  156. break;
  157. }
  158. /* Create the bridges */
  159. bridge1 = ast_bridge_basic_new();
  160. ast_test_validate(test, bridge1 != NULL);
  161. bridge2 = ast_bridge_basic_new();
  162. ast_test_validate(test, bridge2 != NULL);
  163. /* Create channels that will go into the bridge */
  164. START_ALICE(chan_alice);
  165. START_BOB(chan_bob);
  166. /* Setup the features and add them to alice */
  167. ast_bridge_features_init(&features);
  168. ast_test_validate(test, !ast_bridge_dtmf_hook(&features, "##**", feature_callback, &callback_executed, NULL, 0));
  169. ast_test_validate(test, !ast_channel_feature_hooks_append(chan_alice, &features));
  170. ast_bridge_features_cleanup(&features);
  171. /* Bridge the channels */
  172. ast_test_validate(test, !ast_bridge_impart(bridge1, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
  173. ast_test_validate(test, !ast_bridge_impart(bridge1, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
  174. wait_for_bridged(chan_alice);
  175. /* Execute the feature */
  176. f.len = 100;
  177. f.subclass.integer = '#';
  178. ast_queue_frame(chan_alice, &f);
  179. ast_queue_frame(chan_alice, &f);
  180. f.subclass.integer = '*';
  181. ast_queue_frame(chan_alice, &f);
  182. ast_queue_frame(chan_alice, &f);
  183. stream_periodic_frames(chan_alice, 1000, 20);
  184. /* Remove the channels from the bridge */
  185. ast_test_validate(test, !ast_bridge_depart(chan_alice));
  186. ast_test_validate(test, !ast_bridge_depart(chan_bob));
  187. wait_for_unbridged(chan_alice);
  188. /* Bridge the channels again to ensure that the feature hook remains on the channel */
  189. ast_test_validate(test, !ast_bridge_impart(bridge2, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
  190. ast_test_validate(test, !ast_bridge_impart(bridge2, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
  191. wait_for_bridged(chan_alice);
  192. /* Execute the feature */
  193. f.len = 100;
  194. f.subclass.integer = '#';
  195. ast_queue_frame(chan_alice, &f);
  196. ast_queue_frame(chan_alice, &f);
  197. f.subclass.integer = '*';
  198. ast_queue_frame(chan_alice, &f);
  199. ast_queue_frame(chan_alice, &f);
  200. stream_periodic_frames(chan_alice, 1000, 20);
  201. /* Remove the channels from the bridge */
  202. ast_test_validate(test, !ast_bridge_depart(chan_alice));
  203. ast_test_validate(test, !ast_bridge_depart(chan_bob));
  204. /* Hangup the channels */
  205. HANGUP_CHANNEL(chan_alice);
  206. HANGUP_CHANNEL(chan_bob);
  207. ast_test_validate(test, callback_executed == 2);
  208. return AST_TEST_PASS;
  209. }
  210. AST_TEST_DEFINE(test_features_channel_interval)
  211. {
  212. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  213. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  214. RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
  215. RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
  216. struct ast_bridge_features features;
  217. int callback_executed = 0;
  218. switch (cmd) {
  219. case TEST_INIT:
  220. info->name = __func__;
  221. info->category = TEST_CATEGORY;
  222. info->summary = "Test running interval hooks on a channel via the feature hooks mechanism";
  223. info->description =
  224. "This test creates two channels, adds an interval hook to one, places them\n"
  225. "into a bridge, and verifies that the interval hook added to the channel\n"
  226. "feature hooks is triggered once the channel is bridged.";
  227. return AST_TEST_NOT_RUN;
  228. case TEST_EXECUTE:
  229. break;
  230. }
  231. /* Create the bridges */
  232. bridge1 = ast_bridge_basic_new();
  233. ast_test_validate(test, bridge1 != NULL);
  234. bridge2 = ast_bridge_basic_new();
  235. ast_test_validate(test, bridge2 != NULL);
  236. /* Create channels that will go into the bridge */
  237. START_ALICE(chan_alice);
  238. START_BOB(chan_bob);
  239. /* Setup the features and add them to alice */
  240. ast_bridge_features_init(&features);
  241. ast_test_validate(test, !ast_bridge_interval_hook(&features, 0, 1000, feature_callback, &callback_executed, NULL, 0));
  242. ast_test_validate(test, !ast_channel_feature_hooks_append(chan_alice, &features));
  243. ast_bridge_features_cleanup(&features);
  244. /* Bridge the channels */
  245. ast_test_validate(test, !ast_bridge_impart(bridge1, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
  246. ast_test_validate(test, !ast_bridge_impart(bridge1, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
  247. wait_for_bridged(chan_alice);
  248. /* Let the interval hook execute once */
  249. test_nanosleep(1, 500000000);
  250. /* Remove the channels from the bridge */
  251. ast_test_validate(test, !ast_bridge_depart(chan_alice));
  252. ast_test_validate(test, !ast_bridge_depart(chan_bob));
  253. wait_for_unbridged(chan_alice);
  254. ast_test_validate(test, callback_executed >= 1);
  255. callback_executed = 0;
  256. /* Bridge the channels again to ensure that the feature hook remains on the channel */
  257. ast_test_validate(test, !ast_bridge_impart(bridge2, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
  258. ast_test_validate(test, !ast_bridge_impart(bridge2, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
  259. wait_for_bridged(chan_alice);
  260. /* Let the interval hook execute once */
  261. test_nanosleep(1, 500000000);
  262. /* Remove the channels from the bridge */
  263. ast_test_validate(test, !ast_bridge_depart(chan_alice));
  264. ast_test_validate(test, !ast_bridge_depart(chan_bob));
  265. /* Hangup the channels */
  266. HANGUP_CHANNEL(chan_alice);
  267. HANGUP_CHANNEL(chan_bob);
  268. ast_test_validate(test, callback_executed >= 1);
  269. return AST_TEST_PASS;
  270. }
  271. static int unload_module(void)
  272. {
  273. AST_TEST_UNREGISTER(test_features_channel_dtmf);
  274. AST_TEST_UNREGISTER(test_features_channel_interval);
  275. ast_channel_unregister(&test_features_chan_tech);
  276. ao2_cleanup(test_features_chan_tech.capabilities);
  277. test_features_chan_tech.capabilities = NULL;
  278. return 0;
  279. }
  280. static int load_module(void)
  281. {
  282. test_features_chan_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  283. if (!test_features_chan_tech.capabilities) {
  284. return AST_MODULE_LOAD_DECLINE;
  285. }
  286. ast_format_cap_append(test_features_chan_tech.capabilities, TEST_CHANNEL_FORMAT, 0);
  287. ast_channel_register(&test_features_chan_tech);
  288. AST_TEST_REGISTER(test_features_channel_dtmf);
  289. AST_TEST_REGISTER(test_features_channel_interval);
  290. return AST_MODULE_LOAD_SUCCESS;
  291. }
  292. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Bridge Features Unit Tests");