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