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