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