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