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