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