test_cel.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, 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 CEL 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 <math.h>
  32. #include "asterisk/module.h"
  33. #include "asterisk/test.h"
  34. #include "asterisk/cel.h"
  35. #include "asterisk/channel.h"
  36. #include "asterisk/format_cache.h"
  37. #include "asterisk/linkedlists.h"
  38. #include "asterisk/chanvars.h"
  39. #include "asterisk/utils.h"
  40. #include "asterisk/causes.h"
  41. #include "asterisk/time.h"
  42. #include "asterisk/bridge.h"
  43. #include "asterisk/bridge_basic.h"
  44. #include "asterisk/pickup.h"
  45. #include "asterisk/stasis_channels.h"
  46. #include "asterisk/stasis_bridges.h"
  47. #include "asterisk/json.h"
  48. #include "asterisk/features.h"
  49. #include "asterisk/core_local.h"
  50. #define TEST_CATEGORY "/main/cel/"
  51. #define CHANNEL_TECH_NAME "CELTestChannel"
  52. #define TEST_BACKEND_NAME "CEL Test Logging"
  53. /*! \brief A placeholder for Asterisk's 'real' CEL configuration */
  54. static struct ast_cel_general_config *saved_config;
  55. /*! \brief The CEL config used for CEL unit tests */
  56. static struct ast_cel_general_config *cel_test_config;
  57. /*! \brief Lock used for synchronizing test execution stages with received events */
  58. ast_mutex_t mid_test_sync_lock;
  59. /*! \brief Lock used with sync_out for checking the end of test execution */
  60. ast_mutex_t sync_lock;
  61. /*! \brief Condition used for checking the end of test execution */
  62. ast_cond_t sync_out;
  63. /*! \brief Flag used to trigger a mid-test synchronization, access controlled by mid_test_sync_lock */
  64. int do_mid_test_sync = 0;
  65. /*! \brief A channel technology used for the unit tests */
  66. static struct ast_channel_tech test_cel_chan_tech = {
  67. .type = CHANNEL_TECH_NAME,
  68. .description = "Mock channel technology for CEL tests",
  69. };
  70. /*! \brief A 1 second sleep */
  71. static struct timespec to_sleep = {1, 0};
  72. static void do_sleep(void)
  73. {
  74. while ((nanosleep(&to_sleep, &to_sleep) == -1) && (errno == EINTR)) {
  75. }
  76. }
  77. #define APPEND_EVENT(chan, ev_type, userevent, extra) do { \
  78. if (append_expected_event(chan, ev_type, userevent, extra, NULL)) { \
  79. return AST_TEST_FAIL; \
  80. } \
  81. } while (0)
  82. #define APPEND_EVENT_PEER(chan, ev_type, userevent, extra, peer) do { \
  83. if (append_expected_event(chan, ev_type, userevent, extra, peer)) { \
  84. return AST_TEST_FAIL; \
  85. } \
  86. } while (0)
  87. #define APPEND_EVENT_SNAPSHOT(snapshot, ev_type, userevent, extra, peer) do { \
  88. if (append_expected_event_snapshot(snapshot, ev_type, userevent, extra, peer)) { \
  89. return AST_TEST_FAIL; \
  90. } \
  91. } while (0)
  92. #define APPEND_DUMMY_EVENT() do { \
  93. if (append_dummy_event()) { \
  94. return AST_TEST_FAIL; \
  95. } \
  96. } while (0)
  97. #define BRIDGE_EXIT(channel, bridge) do { \
  98. ast_test_validate(test, !ast_bridge_depart(channel)); \
  99. BRIDGE_EXIT_EVENT(channel, bridge); \
  100. mid_test_sync(); \
  101. } while (0)
  102. #define BRIDGE_EXIT_EVENT(channel, bridge) do { \
  103. RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
  104. peer_str = test_cel_generate_peer_str(channel, bridge); \
  105. ast_test_validate(test, peer_str != NULL); \
  106. BRIDGE_EXIT_EVENT_PEER(channel, bridge, ast_str_buffer(peer_str)); \
  107. } while (0)
  108. #define BRIDGE_EXIT_EVENT_PEER(channel, bridge, peer) do { \
  109. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
  110. extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
  111. ast_test_validate(test, extra != NULL); \
  112. APPEND_EVENT_PEER(channel, AST_CEL_BRIDGE_EXIT, NULL, extra, peer); \
  113. } while (0)
  114. #define BRIDGE_EXIT_SNAPSHOT(channel, bridge) do { \
  115. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
  116. RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
  117. peer_str = test_cel_generate_peer_str_snapshot(channel, bridge); \
  118. ast_test_validate(test, peer_str != NULL); \
  119. extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
  120. ast_test_validate(test, extra != NULL); \
  121. APPEND_EVENT_SNAPSHOT(channel, AST_CEL_BRIDGE_EXIT, NULL, extra, ast_str_buffer(peer_str)); \
  122. } while (0)
  123. #define BRIDGE_ENTER(channel, bridge) do { \
  124. ast_test_validate(test, !ast_bridge_impart(bridge, channel, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE)); \
  125. do_sleep(); \
  126. BRIDGE_ENTER_EVENT(channel, bridge); \
  127. mid_test_sync(); \
  128. } while (0)
  129. #define BRIDGE_ENTER_EVENT(channel, bridge) do { \
  130. RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
  131. peer_str = test_cel_generate_peer_str(channel, bridge); \
  132. ast_test_validate(test, peer_str != NULL); \
  133. BRIDGE_ENTER_EVENT_PEER(channel, bridge, ast_str_buffer(peer_str)); \
  134. } while (0)
  135. #define BRIDGE_ENTER_EVENT_PEER(channel, bridge, peer) do { \
  136. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
  137. extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
  138. ast_test_validate(test, extra != NULL); \
  139. APPEND_EVENT_PEER(channel, AST_CEL_BRIDGE_ENTER, NULL, extra, peer); \
  140. } while (0)
  141. #define BLINDTRANSFER_EVENT(channel, bridge, extension, context) do { \
  142. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
  143. extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}", \
  144. "extension", extension, \
  145. "context", context, \
  146. "bridge_id", bridge->uniqueid, \
  147. "transferee_channel_name", "N/A", \
  148. "transferee_channel_uniqueid", "N/A"); \
  149. ast_test_validate(test, extra != NULL); \
  150. APPEND_EVENT(channel, AST_CEL_BLINDTRANSFER, NULL, extra); \
  151. } while (0)
  152. #define ATTENDEDTRANSFER_BRIDGE(channel1, bridge1, channel2, bridge2, channel3, channel4) do { \
  153. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
  154. extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: s, s: s}", \
  155. "bridge1_id", bridge1->uniqueid, \
  156. "channel2_name", ast_channel_name(channel2), \
  157. "channel2_uniqueid", ast_channel_uniqueid(channel2), \
  158. "bridge2_id", bridge2->uniqueid, \
  159. "transferee_channel_name", ast_channel_name(channel4), \
  160. "transferee_channel_uniqueid", ast_channel_uniqueid(channel4), \
  161. "transfer_target_channel_name", ast_channel_name(channel3), \
  162. "transfer_target_channel_uniqueid", ast_channel_uniqueid(channel3)); \
  163. ast_test_validate(test, extra != NULL); \
  164. APPEND_EVENT(channel1, AST_CEL_ATTENDEDTRANSFER, NULL, extra); \
  165. } while (0)
  166. /*! \brief Alice's Caller ID */
  167. #define ALICE_CALLERID { .id.name.str = "Alice", .id.name.valid = 1, .id.number.str = "100", .id.number.valid = 1, }
  168. /*! \brief Bob's Caller ID */
  169. #define BOB_CALLERID { .id.name.str = "Bob", .id.name.valid = 1, .id.number.str = "200", .id.number.valid = 1, }
  170. /*! \brief Charlie's Caller ID */
  171. #define CHARLIE_CALLERID { .id.name.str = "Charlie", .id.name.valid = 1, .id.number.str = "300", .id.number.valid = 1, }
  172. /*! \brief David's Caller ID */
  173. #define DAVID_CALLERID { .id.name.str = "David", .id.name.valid = 1, .id.number.str = "400", .id.number.valid = 1, }
  174. /*! \brief Set ulaw format on channel */
  175. #define SET_FORMATS(chan) do {\
  176. struct ast_format_cap *caps;\
  177. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);\
  178. ast_format_cap_append(caps, ast_format_ulaw, 0);\
  179. ast_channel_nativeformats_set((chan), caps);\
  180. ast_channel_set_writeformat((chan), ast_format_ulaw);\
  181. ast_channel_set_rawwriteformat((chan), ast_format_ulaw);\
  182. ast_channel_set_readformat((chan), ast_format_ulaw);\
  183. ast_channel_set_rawreadformat((chan), ast_format_ulaw);\
  184. ao2_ref(caps, -1);\
  185. } while (0)
  186. /*! \brief Create a \ref test_cel_chan_tech for Alice. */
  187. #define CREATE_ALICE_CHANNEL(channel_var, caller_id) do { \
  188. (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "100", "100", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Alice"); \
  189. SET_FORMATS((channel_var));\
  190. APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
  191. ast_channel_unlock((channel_var)); \
  192. } while (0)
  193. /*! \brief Create a \ref test_cel_chan_tech for Bob. */
  194. #define CREATE_BOB_CHANNEL(channel_var, caller_id) do { \
  195. (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "200", "200", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Bob"); \
  196. SET_FORMATS((channel_var));\
  197. APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
  198. ast_channel_unlock((channel_var)); \
  199. } while (0)
  200. /*! \brief Create a \ref test_cel_chan_tech for Charlie. */
  201. #define CREATE_CHARLIE_CHANNEL(channel_var, caller_id) do { \
  202. (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "300", "300", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Charlie"); \
  203. SET_FORMATS((channel_var));\
  204. APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
  205. ast_channel_unlock((channel_var)); \
  206. } while (0)
  207. /*! \brief Create a \ref test_cel_chan_tech for David. */
  208. #define CREATE_DAVID_CHANNEL(channel_var, caller_id) do { \
  209. (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "400", "400", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/David"); \
  210. SET_FORMATS((channel_var));\
  211. APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
  212. ast_channel_unlock((channel_var)); \
  213. } while (0)
  214. /*! \brief Emulate a channel entering into an application */
  215. #define EMULATE_APP_DATA(channel, priority, application, data) do { \
  216. if ((priority) > 0) { \
  217. ast_channel_priority_set((channel), (priority)); \
  218. } \
  219. ast_channel_appl_set((channel), (application)); \
  220. ast_channel_data_set((channel), (data)); \
  221. ast_channel_publish_snapshot((channel)); \
  222. } while (0)
  223. #define ANSWER_CHANNEL(chan) do { \
  224. EMULATE_APP_DATA(chan, 1, "Answer", ""); \
  225. ANSWER_NO_APP(chan); \
  226. } while (0)
  227. #define ANSWER_NO_APP(chan) do { \
  228. ast_setstate(chan, AST_STATE_UP); \
  229. APPEND_EVENT(chan, AST_CEL_ANSWER, NULL, NULL); \
  230. } while (0)
  231. /*! \brief Hang up a test channel safely */
  232. #define HANGUP_CHANNEL(channel, cause, dialstatus) do { \
  233. ast_channel_hangupcause_set((channel), (cause)); \
  234. ao2_ref(channel, +1); \
  235. ast_hangup((channel)); \
  236. HANGUP_EVENT(channel, cause, dialstatus); \
  237. APPEND_EVENT(channel, AST_CEL_CHANNEL_END, NULL, NULL); \
  238. ao2_cleanup(stasis_cache_get(ast_channel_cache(), \
  239. ast_channel_snapshot_type(), ast_channel_uniqueid(channel))); \
  240. ao2_cleanup(channel); \
  241. channel = NULL; \
  242. } while (0)
  243. #define HANGUP_EVENT(channel, cause, dialstatus) do { \
  244. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
  245. extra = ast_json_pack("{s: i, s: s, s: s}", \
  246. "hangupcause", cause, \
  247. "hangupsource", "", \
  248. "dialstatus", dialstatus); \
  249. ast_test_validate(test, extra != NULL); \
  250. APPEND_EVENT(channel, AST_CEL_HANGUP, NULL, extra); \
  251. } while (0)
  252. static void mid_test_sync(void);
  253. static int append_expected_event(
  254. struct ast_channel *chan,
  255. enum ast_cel_event_type type,
  256. const char *userdefevname,
  257. struct ast_json *extra,
  258. const char *peer);
  259. static int append_expected_event_snapshot(
  260. struct ast_channel_snapshot *snapshot,
  261. enum ast_cel_event_type type,
  262. const char *userdefevname,
  263. struct ast_json *extra,
  264. const char *peer);
  265. #ifdef RACEY_TESTS
  266. static int append_dummy_event(void);
  267. #endif
  268. static struct ast_str *__test_cel_generate_peer_str(struct ast_channel_snapshot *chan, struct ast_bridge_snapshot *bridge)
  269. {
  270. struct ast_str *peer_str = ast_str_create(32);
  271. struct ao2_iterator i;
  272. char *current_chan = NULL;
  273. if (!peer_str) {
  274. return NULL;
  275. }
  276. for (i = ao2_iterator_init(bridge->channels, 0);
  277. (current_chan = ao2_iterator_next(&i));
  278. ao2_cleanup(current_chan)) {
  279. RAII_VAR(struct ast_channel_snapshot *, current_snapshot,
  280. NULL,
  281. ao2_cleanup);
  282. /* Don't add the channel for which this message is being generated */
  283. if (!strcmp(current_chan, chan->uniqueid)) {
  284. continue;
  285. }
  286. current_snapshot = ast_channel_snapshot_get_latest(current_chan);
  287. if (!current_snapshot) {
  288. continue;
  289. }
  290. ast_str_append(&peer_str, 0, "%s,", current_snapshot->name);
  291. }
  292. ao2_iterator_destroy(&i);
  293. /* Rip off the trailing comma */
  294. ast_str_truncate(peer_str, -1);
  295. return peer_str;
  296. }
  297. static struct ast_str *test_cel_generate_peer_str_snapshot(struct ast_channel_snapshot *chan, struct ast_bridge *bridge)
  298. {
  299. RAII_VAR(struct ast_bridge_snapshot *, snapshot,
  300. ast_bridge_snapshot_get_latest(bridge->uniqueid),
  301. ao2_cleanup);
  302. if (!snapshot) {
  303. return NULL;
  304. }
  305. return __test_cel_generate_peer_str(chan, snapshot);
  306. }
  307. static struct ast_str *test_cel_generate_peer_str(struct ast_channel *chan, struct ast_bridge *bridge)
  308. {
  309. RAII_VAR(struct ast_channel_snapshot *, snapshot,
  310. ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan)),
  311. ao2_cleanup);
  312. if (!snapshot) {
  313. return NULL;
  314. }
  315. return test_cel_generate_peer_str_snapshot(snapshot, bridge);
  316. }
  317. static void safe_channel_release(struct ast_channel *chan)
  318. {
  319. if (!chan) {
  320. return;
  321. }
  322. ast_channel_release(chan);
  323. }
  324. static void safe_bridge_destroy(struct ast_bridge *bridge)
  325. {
  326. if (!bridge) {
  327. return;
  328. }
  329. ast_bridge_destroy(bridge, 0);
  330. }
  331. AST_TEST_DEFINE(test_cel_channel_creation)
  332. {
  333. RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
  334. struct ast_party_caller caller = ALICE_CALLERID;
  335. switch (cmd) {
  336. case TEST_INIT:
  337. info->name = __func__;
  338. info->category = TEST_CATEGORY;
  339. info->summary = "Test the CEL records created when a channel is created";
  340. info->description =
  341. "Test the CEL records created when a channel is created";
  342. return AST_TEST_NOT_RUN;
  343. case TEST_EXECUTE:
  344. break;
  345. }
  346. CREATE_ALICE_CHANNEL(chan, (&caller));
  347. HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
  348. return AST_TEST_PASS;
  349. }
  350. AST_TEST_DEFINE(test_cel_unanswered_inbound_call)
  351. {
  352. RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
  353. struct ast_party_caller caller = ALICE_CALLERID;
  354. switch (cmd) {
  355. case TEST_INIT:
  356. info->name = __func__;
  357. info->category = TEST_CATEGORY;
  358. info->summary = "Test inbound unanswered calls";
  359. info->description =
  360. "Test CEL records for a call that is\n"
  361. "inbound to Asterisk, executes some dialplan, but\n"
  362. "is never answered.";
  363. return AST_TEST_NOT_RUN;
  364. case TEST_EXECUTE:
  365. break;
  366. }
  367. CREATE_ALICE_CHANNEL(chan, &caller);
  368. EMULATE_APP_DATA(chan, 1, "Wait", "1");
  369. HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
  370. return AST_TEST_PASS;
  371. }
  372. AST_TEST_DEFINE(test_cel_unanswered_outbound_call)
  373. {
  374. RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
  375. struct ast_party_caller caller = {
  376. .id.name.str = "",
  377. .id.name.valid = 1,
  378. .id.number.str = "",
  379. .id.number.valid = 1, };
  380. switch (cmd) {
  381. case TEST_INIT:
  382. info->name = __func__;
  383. info->category = TEST_CATEGORY;
  384. info->summary = "Test outbound unanswered calls";
  385. info->description =
  386. "Test CEL records for a call that is\n"
  387. "outbound to Asterisk but is never answered.";
  388. return AST_TEST_NOT_RUN;
  389. case TEST_EXECUTE:
  390. break;
  391. }
  392. CREATE_ALICE_CHANNEL(chan, &caller);
  393. ast_channel_exten_set(chan, "s");
  394. ast_channel_context_set(chan, "default");
  395. ast_set_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED);
  396. EMULATE_APP_DATA(chan, 0, "AppDial", "(Outgoing Line)");
  397. HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
  398. return AST_TEST_PASS;
  399. }
  400. AST_TEST_DEFINE(test_cel_single_party)
  401. {
  402. RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
  403. struct ast_party_caller caller = ALICE_CALLERID;
  404. switch (cmd) {
  405. case TEST_INIT:
  406. info->name = __func__;
  407. info->category = TEST_CATEGORY;
  408. info->summary = "Test CEL for a single party";
  409. info->description =
  410. "Test CEL records for a call that is\n"
  411. "answered, but only involves a single channel";
  412. return AST_TEST_NOT_RUN;
  413. case TEST_EXECUTE:
  414. break;
  415. }
  416. CREATE_ALICE_CHANNEL(chan, &caller);
  417. ANSWER_CHANNEL(chan);
  418. EMULATE_APP_DATA(chan, 2, "VoiceMailMain", "1");
  419. HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
  420. return AST_TEST_PASS;
  421. }
  422. AST_TEST_DEFINE(test_cel_single_bridge)
  423. {
  424. RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
  425. RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
  426. struct ast_party_caller caller = ALICE_CALLERID;
  427. switch (cmd) {
  428. case TEST_INIT:
  429. info->name = __func__;
  430. info->category = TEST_CATEGORY;
  431. info->summary = "Test CEL for a single party entering/leaving a bridge";
  432. info->description =
  433. "Test CEL records for a call that is\n"
  434. "answered, enters a bridge, and leaves it.";
  435. return AST_TEST_NOT_RUN;
  436. case TEST_EXECUTE:
  437. break;
  438. }
  439. bridge = ast_bridge_basic_new();
  440. ast_test_validate(test, bridge != NULL);
  441. CREATE_ALICE_CHANNEL(chan, &caller);
  442. ANSWER_CHANNEL(chan);
  443. EMULATE_APP_DATA(chan, 2, "Bridge", "");
  444. do_sleep();
  445. BRIDGE_ENTER(chan, bridge);
  446. do_sleep();
  447. BRIDGE_EXIT(chan, bridge);
  448. HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
  449. return AST_TEST_PASS;
  450. }
  451. AST_TEST_DEFINE(test_cel_single_bridge_continue)
  452. {
  453. RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
  454. RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
  455. struct ast_party_caller caller = ALICE_CALLERID;
  456. switch (cmd) {
  457. case TEST_INIT:
  458. info->name = __func__;
  459. info->category = TEST_CATEGORY;
  460. info->summary = "Test CEL for a single party entering/leaving a bridge";
  461. info->description =
  462. "Test CEL records for a call that is\n"
  463. "answered, enters a bridge, and leaves it.";
  464. return AST_TEST_NOT_RUN;
  465. case TEST_EXECUTE:
  466. break;
  467. }
  468. bridge = ast_bridge_basic_new();
  469. ast_test_validate(test, bridge != NULL);
  470. CREATE_ALICE_CHANNEL(chan, &caller);
  471. ANSWER_CHANNEL(chan);
  472. EMULATE_APP_DATA(chan, 2, "Bridge", "");
  473. do_sleep();
  474. BRIDGE_ENTER(chan, bridge);
  475. do_sleep();
  476. BRIDGE_EXIT(chan, bridge);
  477. EMULATE_APP_DATA(chan, 3, "Wait", "");
  478. /* And then it hangs up */
  479. HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
  480. return AST_TEST_PASS;
  481. }
  482. AST_TEST_DEFINE(test_cel_single_twoparty_bridge_a)
  483. {
  484. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  485. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  486. RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
  487. struct ast_party_caller caller_alice = ALICE_CALLERID;
  488. struct ast_party_caller caller_bob = BOB_CALLERID;
  489. switch (cmd) {
  490. case TEST_INIT:
  491. info->name = __func__;
  492. info->category = TEST_CATEGORY;
  493. info->summary = "Test CEL for a single party entering/leaving a bridge";
  494. info->description =
  495. "Test CEL records for a call that is\n"
  496. "answered, enters a bridge, and leaves it. In this scenario, the\n"
  497. "Party A should answer the bridge first.";
  498. return AST_TEST_NOT_RUN;
  499. case TEST_EXECUTE:
  500. break;
  501. }
  502. bridge = ast_bridge_basic_new();
  503. ast_test_validate(test, bridge != NULL);
  504. CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
  505. CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
  506. ANSWER_CHANNEL(chan_alice);
  507. EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
  508. BRIDGE_ENTER(chan_alice, bridge);
  509. do_sleep();
  510. ANSWER_CHANNEL(chan_bob);
  511. EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
  512. BRIDGE_ENTER(chan_bob, bridge);
  513. BRIDGE_EXIT(chan_alice, bridge);
  514. BRIDGE_EXIT(chan_bob, bridge);
  515. HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
  516. HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
  517. return AST_TEST_PASS;
  518. }
  519. AST_TEST_DEFINE(test_cel_single_twoparty_bridge_b)
  520. {
  521. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  522. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  523. RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
  524. struct ast_party_caller caller_alice = ALICE_CALLERID;
  525. struct ast_party_caller caller_bob = BOB_CALLERID;
  526. switch (cmd) {
  527. case TEST_INIT:
  528. info->name = __func__;
  529. info->category = TEST_CATEGORY;
  530. info->summary = "Test CEL for a single party entering/leaving a bridge";
  531. info->description =
  532. "Test CEL records for a call that is\n"
  533. "answered, enters a bridge, and leaves it. In this scenario, the\n"
  534. "Party B should answer the bridge first.";
  535. return AST_TEST_NOT_RUN;
  536. case TEST_EXECUTE:
  537. break;
  538. }
  539. bridge = ast_bridge_basic_new();
  540. ast_test_validate(test, bridge != NULL);
  541. CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
  542. CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
  543. ANSWER_CHANNEL(chan_alice);
  544. EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
  545. ANSWER_CHANNEL(chan_bob);
  546. EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
  547. do_sleep();
  548. BRIDGE_ENTER(chan_bob, bridge);
  549. BRIDGE_ENTER(chan_alice, bridge);
  550. BRIDGE_EXIT(chan_alice, bridge);
  551. BRIDGE_EXIT(chan_bob, bridge);
  552. HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
  553. HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
  554. return AST_TEST_PASS;
  555. }
  556. /* XXX Validation needs to be reworked on a per-channel basis before
  557. * test_cel_single_multiparty_bridge and test_cel_dial_answer_multiparty
  558. * can operate properly. */
  559. #ifdef RACEY_TESTS
  560. AST_TEST_DEFINE(test_cel_single_multiparty_bridge)
  561. {
  562. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  563. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  564. RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
  565. RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
  566. struct ast_party_caller caller_alice = ALICE_CALLERID;
  567. struct ast_party_caller caller_bob = BOB_CALLERID;
  568. struct ast_party_caller caller_charlie = CHARLIE_CALLERID;
  569. switch (cmd) {
  570. case TEST_INIT:
  571. info->name = __func__;
  572. info->category = TEST_CATEGORY;
  573. info->summary = "Test CEL for a single party entering/leaving a multi-party bridge";
  574. info->description =
  575. "Test CEL records for a call that is\n"
  576. "answered, enters a bridge, and leaves it. A total of three\n"
  577. "parties perform this action.";
  578. return AST_TEST_NOT_RUN;
  579. case TEST_EXECUTE:
  580. break;
  581. }
  582. bridge = ast_bridge_basic_new();
  583. ast_test_validate(test, bridge != NULL);
  584. CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
  585. CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
  586. CREATE_CHARLIE_CHANNEL(chan_charlie, &caller_charlie);
  587. ANSWER_CHANNEL(chan_alice);
  588. EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
  589. do_sleep();
  590. BRIDGE_ENTER(chan_alice, bridge);
  591. ANSWER_CHANNEL(chan_bob);
  592. EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
  593. do_sleep();
  594. BRIDGE_ENTER(chan_bob, bridge);
  595. ANSWER_CHANNEL(chan_charlie);
  596. EMULATE_APP_DATA(chan_charlie, 2, "Bridge", "");
  597. do_sleep();
  598. BRIDGE_ENTER(chan_charlie, bridge);
  599. BRIDGE_EXIT(chan_alice, bridge);
  600. BRIDGE_EXIT(chan_bob, bridge);
  601. BRIDGE_EXIT(chan_charlie, bridge);
  602. HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
  603. HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
  604. HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
  605. return AST_TEST_PASS;
  606. }
  607. #endif
  608. #define EMULATE_DIAL(channel, dialstring) do { \
  609. EMULATE_APP_DATA(channel, 1, "Dial", dialstring); \
  610. if (append_expected_event(channel, AST_CEL_APP_START, NULL, NULL, NULL)) { \
  611. return AST_TEST_FAIL; \
  612. } \
  613. } while (0)
  614. #define START_DIALED(caller, callee) \
  615. START_DIALED_FULL(caller, callee, "200", "Bob")
  616. #define START_DIALED_FULL(caller, callee, number, name) do { \
  617. callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, number, NULL, NULL, NULL, caller, 0, CHANNEL_TECH_NAME "/" name); \
  618. SET_FORMATS(callee);\
  619. ast_channel_unlock(callee); \
  620. if (append_expected_event(callee, AST_CEL_CHANNEL_START, NULL, NULL, NULL)) { \
  621. return AST_TEST_FAIL; \
  622. } \
  623. ast_set_flag(ast_channel_flags(callee), AST_FLAG_OUTGOING); \
  624. EMULATE_APP_DATA(callee, 0, "AppDial", "(Outgoing Line)"); \
  625. ast_channel_publish_dial(caller, callee, name, NULL); \
  626. } while (0)
  627. AST_TEST_DEFINE(test_cel_dial_unanswered)
  628. {
  629. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  630. RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
  631. struct ast_party_caller caller = ALICE_CALLERID;
  632. switch (cmd) {
  633. case TEST_INIT:
  634. info->name = __func__;
  635. info->category = TEST_CATEGORY;
  636. info->summary = "Test CEL for a dial that isn't answered";
  637. info->description =
  638. "Test CEL records for a channel that\n"
  639. "performs a dial operation that isn't answered";
  640. return AST_TEST_NOT_RUN;
  641. case TEST_EXECUTE:
  642. break;
  643. }
  644. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  645. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
  646. START_DIALED(chan_caller, chan_callee);
  647. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  648. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOANSWER");
  649. HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ANSWER, "NOANSWER");
  650. HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ANSWER, "");
  651. return AST_TEST_PASS;
  652. }
  653. AST_TEST_DEFINE(test_cel_dial_unanswered_filter)
  654. {
  655. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  656. RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
  657. struct ast_party_caller caller = ALICE_CALLERID;
  658. switch (cmd) {
  659. case TEST_INIT:
  660. info->name = __func__;
  661. info->category = TEST_CATEGORY;
  662. info->summary = "Test CEL for a dial that isn't answered";
  663. info->description =
  664. "Test CEL records for a channel that\n"
  665. "performs a dial operation that isn't answered";
  666. return AST_TEST_NOT_RUN;
  667. case TEST_EXECUTE:
  668. break;
  669. }
  670. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  671. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
  672. START_DIALED(chan_caller, chan_callee);
  673. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  674. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOT A VALID DIAL STATUS");
  675. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOANSWER");
  676. HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ANSWER, "NOANSWER");
  677. HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ANSWER, "");
  678. return AST_TEST_PASS;
  679. }
  680. AST_TEST_DEFINE(test_cel_dial_busy)
  681. {
  682. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  683. RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
  684. struct ast_party_caller caller = ALICE_CALLERID;
  685. switch (cmd) {
  686. case TEST_INIT:
  687. info->name = __func__;
  688. info->category = TEST_CATEGORY;
  689. info->summary = "Test CEL for a dial that results in a busy";
  690. info->description =
  691. "Test CEL records for a channel that\n"
  692. "performs a dial operation to an endpoint that's busy";
  693. return AST_TEST_NOT_RUN;
  694. case TEST_EXECUTE:
  695. break;
  696. }
  697. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  698. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
  699. START_DIALED(chan_caller, chan_callee);
  700. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  701. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "BUSY");
  702. HANGUP_CHANNEL(chan_caller, AST_CAUSE_BUSY, "BUSY");
  703. HANGUP_CHANNEL(chan_callee, AST_CAUSE_BUSY, "");
  704. return AST_TEST_PASS;
  705. }
  706. AST_TEST_DEFINE(test_cel_dial_congestion)
  707. {
  708. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  709. RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
  710. struct ast_party_caller caller = ALICE_CALLERID;
  711. switch (cmd) {
  712. case TEST_INIT:
  713. info->name = __func__;
  714. info->category = TEST_CATEGORY;
  715. info->summary = "Test CEL for a dial that results in congestion";
  716. info->description =
  717. "Test CEL records for a channel that\n"
  718. "performs a dial operation to an endpoint that's congested";
  719. return AST_TEST_NOT_RUN;
  720. case TEST_EXECUTE:
  721. break;
  722. }
  723. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  724. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
  725. START_DIALED(chan_caller, chan_callee);
  726. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  727. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CONGESTION");
  728. HANGUP_CHANNEL(chan_caller, AST_CAUSE_CONGESTION, "CONGESTION");
  729. HANGUP_CHANNEL(chan_callee, AST_CAUSE_CONGESTION, "");
  730. return AST_TEST_PASS;
  731. }
  732. AST_TEST_DEFINE(test_cel_dial_unavailable)
  733. {
  734. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  735. RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
  736. struct ast_party_caller caller = ALICE_CALLERID;
  737. switch (cmd) {
  738. case TEST_INIT:
  739. info->name = __func__;
  740. info->category = TEST_CATEGORY;
  741. info->summary = "Test CEL for a dial that results in unavailable";
  742. info->description =
  743. "Test CEL records for a channel that\n"
  744. "performs a dial operation to an endpoint that's unavailable";
  745. return AST_TEST_NOT_RUN;
  746. case TEST_EXECUTE:
  747. break;
  748. }
  749. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  750. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
  751. START_DIALED(chan_caller, chan_callee);
  752. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  753. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CHANUNAVAIL");
  754. HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ROUTE_DESTINATION, "CHANUNAVAIL");
  755. HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ROUTE_DESTINATION, "");
  756. return AST_TEST_PASS;
  757. }
  758. AST_TEST_DEFINE(test_cel_dial_caller_cancel)
  759. {
  760. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  761. RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
  762. struct ast_party_caller caller = ALICE_CALLERID;
  763. switch (cmd) {
  764. case TEST_INIT:
  765. info->name = __func__;
  766. info->category = TEST_CATEGORY;
  767. info->summary = "Test CEL for a dial where the caller cancels";
  768. info->description =
  769. "Test CEL records for a channel that\n"
  770. "performs a dial operation to an endpoint but then decides\n"
  771. "to hang up, cancelling the dial";
  772. return AST_TEST_NOT_RUN;
  773. case TEST_EXECUTE:
  774. break;
  775. }
  776. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  777. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
  778. START_DIALED(chan_caller, chan_callee);
  779. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  780. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CANCEL");
  781. HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
  782. HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "CANCEL");
  783. return AST_TEST_PASS;
  784. }
  785. AST_TEST_DEFINE(test_cel_dial_parallel_failed)
  786. {
  787. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  788. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  789. RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
  790. RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
  791. struct ast_party_caller caller = ALICE_CALLERID;
  792. switch (cmd) {
  793. case TEST_INIT:
  794. info->name = __func__;
  795. info->category = TEST_CATEGORY;
  796. info->summary = "Test a parallel dial where all channels fail to answer";
  797. info->description =
  798. "This tests dialing three parties: Bob, Charlie, David. Charlie\n"
  799. "returns BUSY; David returns CONGESTION; Bob fails to answer and\n"
  800. "Alice hangs up. Three records are created for Alice as a result.";
  801. return AST_TEST_NOT_RUN;
  802. case TEST_EXECUTE:
  803. break;
  804. }
  805. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  806. /* Channel enters Dial app */
  807. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob&" CHANNEL_TECH_NAME "/Charlie&" CHANNEL_TECH_NAME "/David");
  808. /* Outbound channels are created */
  809. START_DIALED_FULL(chan_caller, chan_bob, "200", "Bob");
  810. START_DIALED_FULL(chan_caller, chan_charlie, "300", "Charlie");
  811. START_DIALED_FULL(chan_caller, chan_david, "400", "David");
  812. /* Dial starts */
  813. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  814. /* Charlie is busy */
  815. ast_channel_publish_dial(chan_caller, chan_charlie, NULL, "BUSY");
  816. HANGUP_CHANNEL(chan_charlie, AST_CAUSE_BUSY, "");
  817. /* David is congested */
  818. ast_channel_publish_dial(chan_caller, chan_david, NULL, "CONGESTION");
  819. HANGUP_CHANNEL(chan_david, AST_CAUSE_CONGESTION, "");
  820. /* Bob is canceled */
  821. ast_channel_publish_dial(chan_caller, chan_bob, NULL, "CANCEL");
  822. HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
  823. /* Alice hangs up */
  824. HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "BUSY");
  825. return AST_TEST_PASS;
  826. }
  827. AST_TEST_DEFINE(test_cel_dial_answer_no_bridge)
  828. {
  829. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  830. RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
  831. struct ast_party_caller caller = ALICE_CALLERID;
  832. switch (cmd) {
  833. case TEST_INIT:
  834. info->name = __func__;
  835. info->category = TEST_CATEGORY;
  836. info->summary = "Test dialing, answering, and not going into a bridge.";
  837. info->description =
  838. "This is a weird one, but theoretically possible. You can perform\n"
  839. "a dial, then bounce both channels to different priorities and\n"
  840. "never have them enter a bridge together. Ew. This makes sure that\n"
  841. "when we answer, we get a CEL, it gets ended at that point, and\n"
  842. "that it gets finalized appropriately.";
  843. return AST_TEST_NOT_RUN;
  844. case TEST_EXECUTE:
  845. break;
  846. }
  847. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  848. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
  849. START_DIALED(chan_caller, chan_callee);
  850. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  851. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
  852. ANSWER_NO_APP(chan_caller);
  853. ast_clear_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
  854. ANSWER_NO_APP(chan_callee);
  855. EMULATE_APP_DATA(chan_caller, 2, "Wait", "1");
  856. EMULATE_APP_DATA(chan_callee, 1, "Wait", "1");
  857. HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
  858. HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
  859. return AST_TEST_PASS;
  860. }
  861. AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_a)
  862. {
  863. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  864. RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
  865. RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
  866. struct ast_party_caller caller = ALICE_CALLERID;
  867. switch (cmd) {
  868. case TEST_INIT:
  869. info->name = __func__;
  870. info->category = TEST_CATEGORY;
  871. info->summary = "Test dialing, answering, and going into a 2-party bridge";
  872. info->description =
  873. "The most 'basic' of scenarios";
  874. return AST_TEST_NOT_RUN;
  875. case TEST_EXECUTE:
  876. break;
  877. }
  878. bridge = ast_bridge_basic_new();
  879. ast_test_validate(test, bridge != NULL);
  880. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  881. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
  882. START_DIALED(chan_caller, chan_callee);
  883. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  884. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
  885. ANSWER_NO_APP(chan_caller);
  886. ANSWER_NO_APP(chan_callee);
  887. do_sleep();
  888. BRIDGE_ENTER(chan_caller, bridge);
  889. BRIDGE_ENTER(chan_callee, bridge);
  890. BRIDGE_EXIT(chan_caller, bridge);
  891. BRIDGE_EXIT(chan_callee, bridge);
  892. HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
  893. HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
  894. return AST_TEST_PASS;
  895. }
  896. AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_b)
  897. {
  898. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  899. RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
  900. RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
  901. struct ast_party_caller caller = ALICE_CALLERID;
  902. switch (cmd) {
  903. case TEST_INIT:
  904. info->name = __func__;
  905. info->category = TEST_CATEGORY;
  906. info->summary = "Test dialing, answering, and going into a 2-party bridge";
  907. info->description =
  908. "The most 'basic' of scenarios";
  909. return AST_TEST_NOT_RUN;
  910. case TEST_EXECUTE:
  911. break;
  912. }
  913. bridge = ast_bridge_basic_new();
  914. ast_test_validate(test, bridge != NULL);
  915. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  916. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
  917. START_DIALED(chan_caller, chan_callee);
  918. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  919. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
  920. ANSWER_NO_APP(chan_caller);
  921. ANSWER_NO_APP(chan_callee);
  922. do_sleep();
  923. BRIDGE_ENTER(chan_callee, bridge);
  924. BRIDGE_ENTER(chan_caller, bridge);
  925. BRIDGE_EXIT(chan_caller, bridge);
  926. BRIDGE_EXIT(chan_callee, bridge);
  927. HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
  928. HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
  929. return AST_TEST_PASS;
  930. }
  931. #ifdef RACEY_TESTS
  932. AST_TEST_DEFINE(test_cel_dial_answer_multiparty)
  933. {
  934. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  935. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  936. RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
  937. RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
  938. RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
  939. struct ast_party_caller alice_caller = ALICE_CALLERID;
  940. struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
  941. switch (cmd) {
  942. case TEST_INIT:
  943. info->name = __func__;
  944. info->category = TEST_CATEGORY;
  945. info->summary = "Test dialing, answering, and going into a multi-party bridge";
  946. info->description =
  947. "A little tricky to get to do, but possible with some redirects.";
  948. return AST_TEST_NOT_RUN;
  949. case TEST_EXECUTE:
  950. break;
  951. }
  952. bridge = ast_bridge_basic_new();
  953. ast_test_validate(test, bridge != NULL);
  954. CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
  955. EMULATE_DIAL(chan_alice, CHANNEL_TECH_NAME "/Bob");
  956. START_DIALED(chan_alice, chan_bob);
  957. do_sleep();
  958. CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
  959. do_sleep();
  960. EMULATE_DIAL(chan_charlie, CHANNEL_TECH_NAME "/Bob");
  961. do_sleep();
  962. START_DIALED_FULL(chan_charlie, chan_david, "400", "David");
  963. ast_channel_state_set(chan_alice, AST_STATE_RINGING);
  964. do_sleep();
  965. ast_channel_state_set(chan_charlie, AST_STATE_RINGING);
  966. do_sleep();
  967. ast_channel_publish_dial(chan_alice, chan_bob, NULL, "ANSWER");
  968. do_sleep();
  969. ast_channel_publish_dial(chan_charlie, chan_david, NULL, "ANSWER");
  970. do_sleep();
  971. ANSWER_NO_APP(chan_alice);
  972. do_sleep();
  973. ANSWER_NO_APP(chan_bob);
  974. do_sleep();
  975. ANSWER_NO_APP(chan_charlie);
  976. do_sleep();
  977. ANSWER_NO_APP(chan_david);
  978. do_sleep();
  979. do_sleep();
  980. BRIDGE_ENTER(chan_charlie, bridge);
  981. BRIDGE_ENTER(chan_david, bridge);
  982. BRIDGE_ENTER(chan_bob, bridge);
  983. BRIDGE_ENTER(chan_alice, bridge);
  984. BRIDGE_EXIT(chan_alice, bridge);
  985. BRIDGE_EXIT(chan_bob, bridge);
  986. BRIDGE_EXIT(chan_charlie, bridge);
  987. BRIDGE_EXIT(chan_david, bridge);
  988. HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "ANSWER");
  989. HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
  990. HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "ANSWER");
  991. HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
  992. return AST_TEST_PASS;
  993. }
  994. #endif
  995. AST_TEST_DEFINE(test_cel_blind_transfer)
  996. {
  997. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  998. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  999. RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
  1000. RAII_VAR(struct ast_blind_transfer_message *, transfer_msg, NULL, ao2_cleanup);
  1001. struct ast_party_caller alice_caller = ALICE_CALLERID;
  1002. struct ast_party_caller bob_caller = BOB_CALLERID;
  1003. switch (cmd) {
  1004. case TEST_INIT:
  1005. info->name = __func__;
  1006. info->category = TEST_CATEGORY;
  1007. info->summary = "Test blind transfers to an extension";
  1008. info->description =
  1009. "This test creates two channels, bridges them, and then"
  1010. " blind transfers the bridge to an extension.";
  1011. return AST_TEST_NOT_RUN;
  1012. case TEST_EXECUTE:
  1013. break;
  1014. }
  1015. bridge = ast_bridge_basic_new();
  1016. ast_test_validate(test, bridge != NULL);
  1017. CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
  1018. CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
  1019. ANSWER_NO_APP(chan_alice);
  1020. ANSWER_NO_APP(chan_bob);
  1021. BRIDGE_ENTER(chan_bob, bridge);
  1022. BRIDGE_ENTER(chan_alice, bridge);
  1023. ast_bridge_lock(bridge);
  1024. transfer_msg = ast_blind_transfer_message_create(1, chan_alice,
  1025. "transfer_extension", "transfer_context");
  1026. if (!transfer_msg) {
  1027. ast_bridge_unlock(bridge);
  1028. ast_test_status_update(test, "Failed to create transfer Stasis message\n");
  1029. return AST_TEST_FAIL;
  1030. }
  1031. transfer_msg->bridge = ast_bridge_snapshot_create(bridge);
  1032. if (!transfer_msg->bridge) {
  1033. ast_bridge_unlock(bridge);
  1034. ast_test_status_update(test, "Failed to create bridge snapshot\n");
  1035. return AST_TEST_FAIL;
  1036. }
  1037. ast_bridge_unlock(bridge);
  1038. transfer_msg->result = AST_BRIDGE_TRANSFER_SUCCESS;
  1039. ast_bridge_publish_blind_transfer(transfer_msg);
  1040. BLINDTRANSFER_EVENT(chan_alice, bridge, "transfer_extension", "transfer_context");
  1041. BRIDGE_EXIT(chan_alice, bridge);
  1042. BRIDGE_EXIT(chan_bob, bridge);
  1043. HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
  1044. do_sleep();
  1045. HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
  1046. return AST_TEST_PASS;
  1047. }
  1048. /* XXX Validation needs to take into account the BRIDGE_EXIT for Alice and the
  1049. * ATTENDEDTRANSFER message are not guaranteed to be ordered
  1050. */
  1051. #ifdef RACEY_TESTS
  1052. AST_TEST_DEFINE(test_cel_attended_transfer_bridges_swap)
  1053. {
  1054. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  1055. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  1056. RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
  1057. RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
  1058. RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
  1059. RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
  1060. struct ast_party_caller alice_caller = ALICE_CALLERID;
  1061. struct ast_party_caller bob_caller = BOB_CALLERID;
  1062. struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
  1063. struct ast_party_caller david_caller = ALICE_CALLERID;
  1064. switch (cmd) {
  1065. case TEST_INIT:
  1066. info->name = __func__;
  1067. info->category = TEST_CATEGORY;
  1068. info->summary = "Test attended transfers between two pairs of bridged parties";
  1069. info->description =
  1070. "This test creates four channels, places each pair in"
  1071. " a bridge, and then attended transfers the bridges"
  1072. " together.";
  1073. return AST_TEST_NOT_RUN;
  1074. case TEST_EXECUTE:
  1075. break;
  1076. }
  1077. /* Create first set of bridged parties */
  1078. bridge1 = ast_bridge_basic_new();
  1079. ast_test_validate(test, bridge1 != NULL);
  1080. CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
  1081. CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
  1082. ANSWER_NO_APP(chan_alice);
  1083. ANSWER_NO_APP(chan_bob);
  1084. BRIDGE_ENTER(chan_bob, bridge1);
  1085. BRIDGE_ENTER(chan_alice, bridge1);
  1086. /* Create second set of bridged parties */
  1087. bridge2 = ast_bridge_basic_new();
  1088. ast_test_validate(test, bridge2 != NULL);
  1089. CREATE_DAVID_CHANNEL(chan_david, &david_caller);
  1090. CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
  1091. ANSWER_NO_APP(chan_david);
  1092. ANSWER_NO_APP(chan_charlie);
  1093. BRIDGE_ENTER(chan_charlie, bridge2);
  1094. BRIDGE_ENTER(chan_david, bridge2);
  1095. BRIDGE_EXIT_EVENT(chan_bob, bridge1);
  1096. do_sleep();
  1097. /* Perform attended transfer */
  1098. if (ast_bridge_transfer_attended(chan_alice, chan_david)) {
  1099. ast_test_status_update(test, "Attended transfer failed!\n");
  1100. return AST_TEST_FAIL;
  1101. }
  1102. do_sleep();
  1103. BRIDGE_ENTER_EVENT_PEER(chan_bob, bridge2, "CELTestChannel/David,CELTestChannel/Charlie");
  1104. BRIDGE_EXIT_EVENT(chan_david, bridge2);
  1105. BRIDGE_EXIT_EVENT(chan_alice, bridge1);
  1106. ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2, chan_charlie, chan_bob);
  1107. do_sleep();
  1108. BRIDGE_EXIT(chan_bob, bridge2);
  1109. BRIDGE_EXIT(chan_charlie, bridge2);
  1110. HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
  1111. do_sleep();
  1112. HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
  1113. do_sleep();
  1114. HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
  1115. do_sleep();
  1116. HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
  1117. return AST_TEST_PASS;
  1118. }
  1119. #endif
  1120. AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
  1121. {
  1122. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  1123. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  1124. RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
  1125. RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
  1126. RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
  1127. RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
  1128. struct ast_party_caller alice_caller = ALICE_CALLERID;
  1129. struct ast_party_caller bob_caller = BOB_CALLERID;
  1130. struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
  1131. struct ast_party_caller david_caller = ALICE_CALLERID;
  1132. switch (cmd) {
  1133. case TEST_INIT:
  1134. info->name = __func__;
  1135. info->category = TEST_CATEGORY;
  1136. info->summary = "Test attended transfers between two pairs of"
  1137. " bridged parties that results in a bridge merge";
  1138. info->description =
  1139. "This test creates four channels, places each pair"
  1140. " in a bridge, and then attended transfers the bridges"
  1141. " together causing a bridge merge.";
  1142. return AST_TEST_NOT_RUN;
  1143. case TEST_EXECUTE:
  1144. break;
  1145. }
  1146. /* Create first set of bridged parties */
  1147. bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
  1148. AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
  1149. "test_cel", "test_cel_atxfer_bridges_merge_1", NULL);
  1150. ast_test_validate(test, bridge1 != NULL);
  1151. CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
  1152. CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
  1153. ANSWER_NO_APP(chan_alice);
  1154. ANSWER_NO_APP(chan_bob);
  1155. BRIDGE_ENTER(chan_bob, bridge1);
  1156. BRIDGE_ENTER(chan_alice, bridge1);
  1157. /* Create second set of bridged parties */
  1158. bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
  1159. AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
  1160. "test_cel", "test_cel_atxfer_bridges_merge_2", NULL);
  1161. ast_test_validate(test, bridge2 != NULL);
  1162. CREATE_DAVID_CHANNEL(chan_david, &david_caller);
  1163. CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
  1164. ANSWER_NO_APP(chan_david);
  1165. ANSWER_NO_APP(chan_charlie);
  1166. BRIDGE_ENTER(chan_charlie, bridge2);
  1167. BRIDGE_ENTER(chan_david, bridge2);
  1168. /* Perform attended transfer */
  1169. if (ast_bridge_transfer_attended(chan_alice, chan_david)) {
  1170. ast_test_status_update(test, "Attended transfer failed!\n");
  1171. return AST_TEST_FAIL;
  1172. }
  1173. do_sleep();
  1174. BRIDGE_EXIT_EVENT_PEER(chan_charlie, bridge2, "CELTestChannel/David");
  1175. BRIDGE_ENTER_EVENT_PEER(chan_charlie, bridge1, "CELTestChannel/Bob,CELTestChannel/Alice");
  1176. BRIDGE_EXIT_EVENT(chan_david, bridge2);
  1177. BRIDGE_EXIT_EVENT(chan_alice, bridge1);
  1178. ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2, chan_charlie, chan_bob);
  1179. do_sleep();
  1180. BRIDGE_EXIT(chan_bob, bridge1);
  1181. BRIDGE_EXIT(chan_charlie, bridge1);
  1182. HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
  1183. do_sleep();
  1184. HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
  1185. do_sleep();
  1186. HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
  1187. do_sleep();
  1188. HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
  1189. return AST_TEST_PASS;
  1190. }
  1191. /* XXX Validation needs to take into account the BRIDGE_EXIT for David and the
  1192. * ATTENDEDTRANSFER message are not guaranteed to be ordered
  1193. */
  1194. #ifdef RACEY_TESTS
  1195. AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
  1196. {
  1197. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  1198. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  1199. RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
  1200. RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
  1201. RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
  1202. RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
  1203. struct ast_party_caller alice_caller = ALICE_CALLERID;
  1204. struct ast_party_caller bob_caller = BOB_CALLERID;
  1205. struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
  1206. struct ast_party_caller david_caller = ALICE_CALLERID;
  1207. switch (cmd) {
  1208. case TEST_INIT:
  1209. info->name = __func__;
  1210. info->category = TEST_CATEGORY;
  1211. info->summary = "Test attended transfers between two pairs of"
  1212. " bridged parties that results in a bridge merge";
  1213. info->description =
  1214. "This test creates four channels, places each pair"
  1215. " in a bridge, and then attended transfers the bridges"
  1216. " together causing a bridge link.";
  1217. return AST_TEST_NOT_RUN;
  1218. case TEST_EXECUTE:
  1219. break;
  1220. }
  1221. /* Create first set of bridged parties */
  1222. bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
  1223. AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
  1224. | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
  1225. | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
  1226. "test_cel", "test_cel_atxfer_bridges_link_1", NULL);
  1227. ast_test_validate(test, bridge1 != NULL);
  1228. CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
  1229. CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
  1230. ANSWER_NO_APP(chan_alice);
  1231. ANSWER_NO_APP(chan_bob);
  1232. BRIDGE_ENTER(chan_bob, bridge1);
  1233. BRIDGE_ENTER(chan_alice, bridge1);
  1234. /* Create second set of bridged parties */
  1235. bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
  1236. AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
  1237. | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
  1238. | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
  1239. "test_cel", "test_cel_atxfer_bridges_link_2", NULL);
  1240. ast_test_validate(test, bridge2 != NULL);
  1241. CREATE_DAVID_CHANNEL(chan_david, &david_caller);
  1242. CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
  1243. ANSWER_NO_APP(chan_david);
  1244. ANSWER_NO_APP(chan_charlie);
  1245. BRIDGE_ENTER(chan_charlie, bridge2);
  1246. BRIDGE_ENTER(chan_david, bridge2);
  1247. /* Perform attended transfer */
  1248. ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2, chan_charlie, chan_bob);
  1249. ast_bridge_transfer_attended(chan_alice, chan_david);
  1250. do_sleep();
  1251. /* BRIDGE_EXIT alice and david */
  1252. APPEND_DUMMY_EVENT();
  1253. APPEND_DUMMY_EVENT();
  1254. do_sleep();
  1255. BRIDGE_EXIT(chan_bob, bridge1);
  1256. BRIDGE_EXIT(chan_charlie, bridge2);
  1257. HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
  1258. do_sleep();
  1259. HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
  1260. do_sleep();
  1261. HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
  1262. do_sleep();
  1263. HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
  1264. do_sleep();
  1265. return AST_TEST_PASS;
  1266. }
  1267. #endif
  1268. AST_TEST_DEFINE(test_cel_dial_pickup)
  1269. {
  1270. RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
  1271. RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
  1272. RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
  1273. struct ast_party_caller caller = ALICE_CALLERID;
  1274. struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
  1275. switch (cmd) {
  1276. case TEST_INIT:
  1277. info->name = __func__;
  1278. info->category = TEST_CATEGORY;
  1279. info->summary = "Test call pickup";
  1280. info->description =
  1281. "Test CEL records for a call that is\n"
  1282. "inbound to Asterisk, executes some dialplan, and\n"
  1283. "is picked up.";
  1284. return AST_TEST_NOT_RUN;
  1285. case TEST_EXECUTE:
  1286. break;
  1287. }
  1288. CREATE_ALICE_CHANNEL(chan_caller, &caller);
  1289. EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
  1290. START_DIALED(chan_caller, chan_callee);
  1291. ast_channel_state_set(chan_caller, AST_STATE_RINGING);
  1292. CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
  1293. {
  1294. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
  1295. SCOPED_CHANNELLOCK(lock, chan_callee);
  1296. extra = ast_json_pack("{s: s, s: s}", "pickup_channel", ast_channel_name(chan_charlie),
  1297. "pickup_channel_uniqueid", ast_channel_uniqueid(chan_charlie));
  1298. ast_test_validate(test, extra != NULL);
  1299. APPEND_EVENT(chan_callee, AST_CEL_PICKUP, NULL, extra);
  1300. ast_test_validate(test, !ast_do_pickup(chan_charlie, chan_callee));
  1301. }
  1302. /* Hang up the masqueraded zombie */
  1303. HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
  1304. ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
  1305. HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
  1306. HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
  1307. return AST_TEST_PASS;
  1308. }
  1309. AST_TEST_DEFINE(test_cel_local_optimize)
  1310. {
  1311. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  1312. RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
  1313. struct ast_party_caller alice_caller = ALICE_CALLERID;
  1314. struct ast_party_caller bob_caller = BOB_CALLERID;
  1315. RAII_VAR(struct ast_multi_channel_blob *, mc_blob, NULL, ao2_cleanup);
  1316. RAII_VAR(struct ast_channel_snapshot *, alice_snapshot, NULL, ao2_cleanup);
  1317. RAII_VAR(struct ast_channel_snapshot *, bob_snapshot, NULL, ao2_cleanup);
  1318. RAII_VAR(struct stasis_message *, local_opt_begin, NULL, ao2_cleanup);
  1319. RAII_VAR(struct stasis_message *, local_opt_end, NULL, ao2_cleanup);
  1320. RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
  1321. switch (cmd) {
  1322. case TEST_INIT:
  1323. info->name = __func__;
  1324. info->category = TEST_CATEGORY;
  1325. info->summary = "Test local channel optimization record generation";
  1326. info->description =
  1327. "Test CEL records for two local channels being optimized\n"
  1328. "out by sending a messages indicating local optimization\n"
  1329. "begin and end";
  1330. return AST_TEST_NOT_RUN;
  1331. case TEST_EXECUTE:
  1332. break;
  1333. }
  1334. mc_blob = ast_multi_channel_blob_create(ast_json_null());
  1335. ast_test_validate(test, mc_blob != NULL);
  1336. CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
  1337. CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
  1338. ast_channel_lock(chan_alice);
  1339. alice_snapshot = ast_channel_snapshot_create(chan_alice);
  1340. ast_channel_unlock(chan_alice);
  1341. ast_test_validate(test, alice_snapshot != NULL);
  1342. ast_channel_lock(chan_bob);
  1343. bob_snapshot = ast_channel_snapshot_create(chan_bob);
  1344. ast_channel_unlock(chan_bob);
  1345. ast_test_validate(test, bob_snapshot != NULL);
  1346. ast_multi_channel_blob_add_channel(mc_blob, "1", alice_snapshot);
  1347. ast_multi_channel_blob_add_channel(mc_blob, "2", bob_snapshot);
  1348. local_opt_begin = stasis_message_create(ast_local_optimization_begin_type(), mc_blob);
  1349. ast_test_validate(test, local_opt_begin != NULL);
  1350. local_opt_end = stasis_message_create(ast_local_optimization_end_type(), mc_blob);
  1351. ast_test_validate(test, local_opt_end != NULL);
  1352. stasis_publish(ast_channel_topic(chan_alice), local_opt_begin);
  1353. stasis_publish(ast_channel_topic(chan_alice), local_opt_end);
  1354. extra = ast_json_pack("{s: s, s: s}", "local_two", bob_snapshot->name,
  1355. "local_two_uniqueid", bob_snapshot->uniqueid);
  1356. ast_test_validate(test, extra != NULL);
  1357. APPEND_EVENT_SNAPSHOT(alice_snapshot, AST_CEL_LOCAL_OPTIMIZE, NULL, extra, NULL);
  1358. HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
  1359. HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
  1360. return AST_TEST_PASS;
  1361. }
  1362. /*! Container for astobj2 duplicated ast_events */
  1363. static struct ao2_container *cel_received_events = NULL;
  1364. /*! Container for expected CEL events */
  1365. static struct ao2_container *cel_expected_events = NULL;
  1366. static struct ast_event *ao2_dup_event(const struct ast_event *event)
  1367. {
  1368. struct ast_event *event_dup;
  1369. uint16_t event_len;
  1370. event_len = ast_event_get_size(event);
  1371. event_dup = ao2_alloc(event_len, NULL);
  1372. if (!event_dup) {
  1373. return NULL;
  1374. }
  1375. memcpy(event_dup, event, event_len);
  1376. return event_dup;
  1377. }
  1378. static void mid_test_sync(void)
  1379. {
  1380. ast_mutex_lock(&mid_test_sync_lock);
  1381. if (ao2_container_count(cel_expected_events) <= ao2_container_count(cel_received_events)) {
  1382. ast_mutex_unlock(&mid_test_sync_lock);
  1383. return;
  1384. }
  1385. do_mid_test_sync = 1;
  1386. ast_mutex_unlock(&mid_test_sync_lock);
  1387. {
  1388. struct timeval start = ast_tvnow();
  1389. struct timespec end = {
  1390. .tv_sec = start.tv_sec + 15,
  1391. .tv_nsec = start.tv_usec * 1000
  1392. };
  1393. SCOPED_MUTEX(lock, &sync_lock);
  1394. ast_cond_timedwait(&sync_out, &sync_lock, &end);
  1395. }
  1396. }
  1397. static int append_event(struct ast_event *ev)
  1398. {
  1399. RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
  1400. ao2_ev = ao2_dup_event(ev);
  1401. if (!ao2_ev) {
  1402. return -1;
  1403. }
  1404. ao2_link(cel_expected_events, ao2_ev);
  1405. return 0;
  1406. }
  1407. #ifdef RACEY_TESTS
  1408. static int append_dummy_event(void)
  1409. {
  1410. RAII_VAR(struct ast_event *, ev, NULL, ast_free);
  1411. RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
  1412. ev = ast_event_new(AST_EVENT_CUSTOM, AST_EVENT_IE_END);
  1413. if (!ev) {
  1414. return -1;
  1415. }
  1416. return append_event(ev);
  1417. }
  1418. #endif
  1419. static int append_expected_event_snapshot(
  1420. struct ast_channel_snapshot *snapshot,
  1421. enum ast_cel_event_type type,
  1422. const char *userdefevname,
  1423. struct ast_json *extra,
  1424. const char *peer)
  1425. {
  1426. RAII_VAR(struct ast_event *, ev, NULL, ast_free);
  1427. ev = ast_cel_create_event(snapshot, type, userdefevname, extra, peer);
  1428. if (!ev) {
  1429. return -1;
  1430. }
  1431. return append_event(ev);
  1432. }
  1433. static int append_expected_event(
  1434. struct ast_channel *chan,
  1435. enum ast_cel_event_type type,
  1436. const char *userdefevname,
  1437. struct ast_json *extra,
  1438. const char *peer)
  1439. {
  1440. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  1441. ast_channel_lock(chan);
  1442. snapshot = ast_channel_snapshot_create(chan);
  1443. ast_channel_unlock(chan);
  1444. if (!snapshot) {
  1445. return -1;
  1446. }
  1447. return append_expected_event_snapshot(snapshot, type, userdefevname, extra, peer);
  1448. }
  1449. static void test_sub(struct ast_event *event)
  1450. {
  1451. RAII_VAR(struct ast_event *, event_dup, ao2_dup_event(event), ao2_cleanup);
  1452. const char *chan_name;
  1453. SCOPED_MUTEX(mid_test_lock, &mid_test_sync_lock);
  1454. if (!event_dup) {
  1455. return;
  1456. }
  1457. chan_name = ast_event_get_ie_str(event_dup, AST_EVENT_IE_CEL_CHANNAME);
  1458. if (chan_name && strncmp(chan_name, CHANNEL_TECH_NAME, 14)) {
  1459. return;
  1460. }
  1461. /* save the event for later processing */
  1462. ao2_link(cel_received_events, event_dup);
  1463. if (do_mid_test_sync) {
  1464. int expected = ao2_container_count(cel_expected_events);
  1465. int received = ao2_container_count(cel_received_events);
  1466. if (expected <= received) {
  1467. {
  1468. SCOPED_MUTEX(lock, &sync_lock);
  1469. ast_cond_signal(&sync_out);
  1470. do_mid_test_sync = 0;
  1471. }
  1472. }
  1473. }
  1474. }
  1475. /*!
  1476. * \internal
  1477. * \brief Callback function called before each test executes
  1478. */
  1479. static int test_cel_init_cb(struct ast_test_info *info, struct ast_test *test)
  1480. {
  1481. ast_assert(cel_received_events == NULL);
  1482. ast_assert(cel_expected_events == NULL);
  1483. ast_mutex_init(&mid_test_sync_lock);
  1484. ast_mutex_init(&sync_lock);
  1485. ast_cond_init(&sync_out, NULL);
  1486. /* Back up the real CEL config and insert the test's config */
  1487. saved_config = ast_cel_get_config();
  1488. ast_cel_set_config(cel_test_config);
  1489. /* init CEL event storage (degenerate hash table becomes a linked list) */
  1490. cel_received_events = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
  1491. cel_expected_events = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
  1492. /* start the CEL event callback */
  1493. if (ast_cel_backend_register(TEST_BACKEND_NAME, test_sub)) {
  1494. return -1;
  1495. }
  1496. return 0;
  1497. }
  1498. /*!
  1499. * \brief Check two peer strings for equality
  1500. *
  1501. * \retval zero if the peer strings do not match
  1502. * \retval non-zero if the peer strings match
  1503. */
  1504. static int test_cel_peer_strings_match(const char *str1, const char *str2)
  1505. {
  1506. RAII_VAR(struct ao2_container *, intersection, ast_str_container_alloc(11), ao2_cleanup);
  1507. RAII_VAR(char *, str1_dup, ast_strdup(str1), ast_free);
  1508. RAII_VAR(char *, str2_dup, ast_strdup(str2), ast_free);
  1509. char *chan;
  1510. if (!intersection) {
  1511. return 1;
  1512. }
  1513. while ((chan = strsep(&str1_dup, ","))) {
  1514. ast_str_container_add(intersection, chan);
  1515. }
  1516. while ((chan = strsep(&str2_dup, ","))) {
  1517. RAII_VAR(char *, ao2_chan, ao2_find(intersection, chan, OBJ_SEARCH_KEY), ao2_cleanup);
  1518. /* item in str2 not in str1 */
  1519. if (!ao2_chan) {
  1520. return 0;
  1521. }
  1522. ast_str_container_remove(intersection, chan);
  1523. }
  1524. /* item in str1 not in str2 */
  1525. if (ao2_container_count(intersection)) {
  1526. return 0;
  1527. }
  1528. return 1;
  1529. }
  1530. /*!
  1531. * \brief Check an IE value from two events
  1532. *
  1533. * \retval zero if the IEs in the events of the specified type do not match
  1534. * \retval non-zero if the IEs in the events of the specified type match
  1535. */
  1536. static int match_ie_val(
  1537. const struct ast_event *event1,
  1538. const struct ast_event *event2,
  1539. enum ast_event_ie_type type)
  1540. {
  1541. enum ast_event_ie_pltype pltype = ast_event_get_ie_pltype(type);
  1542. /* XXX ignore sec/usec for now */
  1543. if (type == AST_EVENT_IE_CEL_EVENT_TIME_USEC) {
  1544. return 1;
  1545. }
  1546. if (type == AST_EVENT_IE_CEL_EVENT_TIME) {
  1547. return 1;
  1548. }
  1549. switch (pltype) {
  1550. case AST_EVENT_IE_PLTYPE_UINT:
  1551. {
  1552. uint32_t val = ast_event_get_ie_uint(event2, type);
  1553. return (val == ast_event_get_ie_uint(event1, type)) ? 1 : 0;
  1554. }
  1555. case AST_EVENT_IE_PLTYPE_STR:
  1556. {
  1557. const char *str1 = ast_event_get_ie_str(event1, type);
  1558. const char *str2 = ast_event_get_ie_str(event2, type);
  1559. if (!str1 && !str2) {
  1560. return 1;
  1561. } else if (!str1) {
  1562. return 0;
  1563. } else if (!str2) {
  1564. return 0;
  1565. }
  1566. /* use special matching for CEL PEER field */
  1567. if (type == AST_EVENT_IE_CEL_PEER) {
  1568. return test_cel_peer_strings_match(str1, str2);
  1569. }
  1570. return !strcmp(str1, str2);
  1571. }
  1572. case AST_EVENT_IE_PLTYPE_RAW:
  1573. case AST_EVENT_IE_PLTYPE_BITFLAGS:
  1574. /* Fall through: just pass on these types */
  1575. return 1;
  1576. default:
  1577. break;
  1578. }
  1579. return 0;
  1580. }
  1581. static int events_are_equal(struct ast_test *test, struct ast_event *received, struct ast_event *expected)
  1582. {
  1583. struct ast_event_iterator iterator;
  1584. int res;
  1585. if (ast_event_get_type(expected) == AST_EVENT_CUSTOM) {
  1586. /* this event is flagged as a wildcard match */
  1587. return 1;
  1588. }
  1589. for (res = ast_event_iterator_init(&iterator, received); !res; res = ast_event_iterator_next(&iterator)) {
  1590. int ie_type = ast_event_iterator_get_ie_type(&iterator);
  1591. if (!match_ie_val(received, expected, ie_type)) {
  1592. ast_test_status_update(test, "Failed matching on field %s\n", ast_event_get_ie_type_name(ie_type));
  1593. return 0;
  1594. }
  1595. }
  1596. return 1;
  1597. }
  1598. static int dump_event(struct ast_test *test, struct ast_event *event)
  1599. {
  1600. struct ast_event_iterator i;
  1601. if (ast_event_iterator_init(&i, event)) {
  1602. ast_test_status_update(test, "Failed to initialize event iterator. :-(\n");
  1603. return 0;
  1604. }
  1605. ast_test_status_update(test, "Event: %s\n",
  1606. ast_cel_get_type_name(ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TYPE)));
  1607. do {
  1608. enum ast_event_ie_type ie_type;
  1609. enum ast_event_ie_pltype ie_pltype;
  1610. const char *ie_type_name;
  1611. ie_type = ast_event_iterator_get_ie_type(&i);
  1612. ie_type_name = ast_event_get_ie_type_name(ie_type);
  1613. ie_pltype = ast_event_get_ie_pltype(ie_type);
  1614. switch (ie_pltype) {
  1615. case AST_EVENT_IE_PLTYPE_UNKNOWN:
  1616. case AST_EVENT_IE_PLTYPE_STR:
  1617. ast_test_status_update(test, "%.30s: %s\n", ie_type_name,
  1618. ast_event_iterator_get_ie_str(&i));
  1619. break;
  1620. case AST_EVENT_IE_PLTYPE_UINT:
  1621. ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
  1622. ast_event_iterator_get_ie_uint(&i));
  1623. break;
  1624. default:
  1625. break;
  1626. }
  1627. } while (!ast_event_iterator_next(&i));
  1628. ast_test_status_update(test, "\n");
  1629. return 0;
  1630. }
  1631. static int check_events(struct ast_test *test, struct ao2_container *local_expected, struct ao2_container *local_received)
  1632. {
  1633. struct ao2_iterator received_it;
  1634. struct ao2_iterator expected_it;
  1635. RAII_VAR(struct ast_event *, rx_event, NULL, ao2_cleanup);
  1636. RAII_VAR(struct ast_event *, ex_event, NULL, ao2_cleanup);
  1637. int debug = 0;
  1638. if (ao2_container_count(local_expected) != ao2_container_count(local_received)) {
  1639. ast_test_status_update(test, "Increasing verbosity since the number of expected events (%d)"
  1640. " did not match number of received events (%d).\n",
  1641. ao2_container_count(local_expected),
  1642. ao2_container_count(local_received));
  1643. debug = 1;
  1644. }
  1645. received_it = ao2_iterator_init(local_received, 0);
  1646. expected_it = ao2_iterator_init(local_expected, 0);
  1647. rx_event = ao2_iterator_next(&received_it);
  1648. ex_event = ao2_iterator_next(&expected_it);
  1649. while (rx_event && ex_event) {
  1650. if (!events_are_equal(test, rx_event, ex_event)) {
  1651. ao2_iterator_destroy(&received_it);
  1652. ao2_iterator_destroy(&expected_it);
  1653. ast_test_status_update(test, "Received event:\n");
  1654. dump_event(test, rx_event);
  1655. ast_test_status_update(test, "Expected event:\n");
  1656. dump_event(test, ex_event);
  1657. return -1;
  1658. }
  1659. if (debug) {
  1660. ast_test_status_update(test, "Compared events successfully%s\n",
  1661. ast_event_get_type(ex_event) == AST_EVENT_CUSTOM
  1662. ? " (wildcard match)" : "");
  1663. dump_event(test, rx_event);
  1664. }
  1665. ao2_cleanup(rx_event);
  1666. ao2_cleanup(ex_event);
  1667. rx_event = ao2_iterator_next(&received_it);
  1668. ex_event = ao2_iterator_next(&expected_it);
  1669. }
  1670. ao2_iterator_destroy(&received_it);
  1671. ao2_iterator_destroy(&expected_it);
  1672. if (rx_event) {
  1673. ast_test_status_update(test, "Received event:\n");
  1674. dump_event(test, rx_event);
  1675. return -1;
  1676. }
  1677. if (ex_event) {
  1678. ast_test_status_update(test, "Expected event:\n");
  1679. dump_event(test, ex_event);
  1680. return -1;
  1681. }
  1682. return 0;
  1683. }
  1684. /*!
  1685. * \internal
  1686. * \brief Callback function called after each test executes.
  1687. *
  1688. * \details
  1689. * In addition to cleanup, this function also performs verification
  1690. * that the events received during a test match the events that were
  1691. * expected to have been generated during the test.
  1692. */
  1693. static int cel_verify_and_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
  1694. {
  1695. RAII_VAR(struct ao2_container *, local_expected, cel_expected_events, ao2_cleanup);
  1696. RAII_VAR(struct ao2_container *, local_received, cel_received_events, ao2_cleanup);
  1697. ast_assert(cel_received_events != NULL);
  1698. ast_assert(cel_expected_events != NULL);
  1699. do_sleep();
  1700. /* stop the CEL event callback and clean up storage structures*/
  1701. ast_cel_backend_unregister(TEST_BACKEND_NAME);
  1702. /* cleaned up by RAII_VAR's */
  1703. cel_expected_events = NULL;
  1704. cel_received_events = NULL;
  1705. /* check events */
  1706. ast_test_validate(test, !check_events(test, local_expected, local_received));
  1707. /* Restore the real CEL config */
  1708. ast_cel_set_config(saved_config);
  1709. ao2_cleanup(saved_config);
  1710. saved_config = NULL;
  1711. /* clean up the locks */
  1712. ast_mutex_destroy(&sync_lock);
  1713. ast_mutex_destroy(&mid_test_sync_lock);
  1714. ast_cond_destroy(&sync_out);
  1715. return 0;
  1716. }
  1717. static int unload_module(void)
  1718. {
  1719. AST_TEST_UNREGISTER(test_cel_channel_creation);
  1720. AST_TEST_UNREGISTER(test_cel_unanswered_inbound_call);
  1721. AST_TEST_UNREGISTER(test_cel_unanswered_outbound_call);
  1722. AST_TEST_UNREGISTER(test_cel_single_party);
  1723. AST_TEST_UNREGISTER(test_cel_single_bridge);
  1724. AST_TEST_UNREGISTER(test_cel_single_bridge_continue);
  1725. AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_a);
  1726. AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_b);
  1727. #ifdef RACEY_TESTS
  1728. AST_TEST_UNREGISTER(test_cel_single_multiparty_bridge);
  1729. #endif
  1730. AST_TEST_UNREGISTER(test_cel_dial_unanswered);
  1731. AST_TEST_UNREGISTER(test_cel_dial_unanswered_filter);
  1732. AST_TEST_UNREGISTER(test_cel_dial_congestion);
  1733. AST_TEST_UNREGISTER(test_cel_dial_busy);
  1734. AST_TEST_UNREGISTER(test_cel_dial_unavailable);
  1735. AST_TEST_UNREGISTER(test_cel_dial_caller_cancel);
  1736. AST_TEST_UNREGISTER(test_cel_dial_parallel_failed);
  1737. AST_TEST_UNREGISTER(test_cel_dial_answer_no_bridge);
  1738. AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_a);
  1739. AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_b);
  1740. #ifdef RACEY_TESTS
  1741. AST_TEST_UNREGISTER(test_cel_dial_answer_multiparty);
  1742. AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_swap);
  1743. AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_link);
  1744. #endif
  1745. AST_TEST_UNREGISTER(test_cel_blind_transfer);
  1746. AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_merge);
  1747. AST_TEST_UNREGISTER(test_cel_dial_pickup);
  1748. AST_TEST_UNREGISTER(test_cel_local_optimize);
  1749. ast_channel_unregister(&test_cel_chan_tech);
  1750. ao2_cleanup(cel_expected_events);
  1751. cel_expected_events = NULL;
  1752. ao2_cleanup(cel_received_events);
  1753. cel_received_events = NULL;
  1754. ao2_cleanup(cel_test_config);
  1755. cel_test_config = NULL;
  1756. return 0;
  1757. }
  1758. static int load_module(void)
  1759. {
  1760. /* build the test config */
  1761. cel_test_config = ast_cel_general_config_alloc();
  1762. if (!cel_test_config) {
  1763. return -1;
  1764. }
  1765. cel_test_config->enable = 1;
  1766. if (ast_str_container_add(cel_test_config->apps, "dial")) {
  1767. return -1;
  1768. }
  1769. if (ast_str_container_add(cel_test_config->apps, "park")) {
  1770. return -1;
  1771. }
  1772. if (ast_str_container_add(cel_test_config->apps, "queue")) {
  1773. return -1;
  1774. }
  1775. cel_test_config->events |= 1<<AST_CEL_APP_START;
  1776. cel_test_config->events |= 1<<AST_CEL_CHANNEL_START;
  1777. cel_test_config->events |= 1<<AST_CEL_CHANNEL_END;
  1778. cel_test_config->events |= 1<<AST_CEL_ANSWER;
  1779. cel_test_config->events |= 1<<AST_CEL_HANGUP;
  1780. cel_test_config->events |= 1<<AST_CEL_BRIDGE_ENTER;
  1781. cel_test_config->events |= 1<<AST_CEL_BRIDGE_EXIT;
  1782. cel_test_config->events |= 1<<AST_CEL_BLINDTRANSFER;
  1783. cel_test_config->events |= 1<<AST_CEL_ATTENDEDTRANSFER;
  1784. cel_test_config->events |= 1<<AST_CEL_PICKUP;
  1785. cel_test_config->events |= 1<<AST_CEL_LOCAL_OPTIMIZE;
  1786. ast_channel_register(&test_cel_chan_tech);
  1787. AST_TEST_REGISTER(test_cel_channel_creation);
  1788. AST_TEST_REGISTER(test_cel_unanswered_inbound_call);
  1789. AST_TEST_REGISTER(test_cel_unanswered_outbound_call);
  1790. AST_TEST_REGISTER(test_cel_single_party);
  1791. AST_TEST_REGISTER(test_cel_single_bridge);
  1792. AST_TEST_REGISTER(test_cel_single_bridge_continue);
  1793. AST_TEST_REGISTER(test_cel_single_twoparty_bridge_a);
  1794. AST_TEST_REGISTER(test_cel_single_twoparty_bridge_b);
  1795. #ifdef RACEY_TESTS
  1796. AST_TEST_REGISTER(test_cel_single_multiparty_bridge);
  1797. #endif
  1798. AST_TEST_REGISTER(test_cel_dial_unanswered);
  1799. AST_TEST_REGISTER(test_cel_dial_unanswered_filter);
  1800. AST_TEST_REGISTER(test_cel_dial_congestion);
  1801. AST_TEST_REGISTER(test_cel_dial_busy);
  1802. AST_TEST_REGISTER(test_cel_dial_unavailable);
  1803. AST_TEST_REGISTER(test_cel_dial_caller_cancel);
  1804. AST_TEST_REGISTER(test_cel_dial_parallel_failed);
  1805. AST_TEST_REGISTER(test_cel_dial_answer_no_bridge);
  1806. AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_a);
  1807. AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_b);
  1808. #ifdef RACEY_TESTS
  1809. AST_TEST_REGISTER(test_cel_dial_answer_multiparty);
  1810. AST_TEST_REGISTER(test_cel_attended_transfer_bridges_swap);
  1811. AST_TEST_REGISTER(test_cel_attended_transfer_bridges_link);
  1812. #endif
  1813. AST_TEST_REGISTER(test_cel_blind_transfer);
  1814. AST_TEST_REGISTER(test_cel_attended_transfer_bridges_merge);
  1815. AST_TEST_REGISTER(test_cel_dial_pickup);
  1816. AST_TEST_REGISTER(test_cel_local_optimize);
  1817. /* ast_test_register_* has to happen after AST_TEST_REGISTER */
  1818. /* Verify received vs expected events and clean things up after every test */
  1819. ast_test_register_init(TEST_CATEGORY, test_cel_init_cb);
  1820. ast_test_register_cleanup(TEST_CATEGORY, cel_verify_and_cleanup_cb);
  1821. return AST_MODULE_LOAD_SUCCESS;
  1822. }
  1823. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CEL unit tests");