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