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