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