Doxygen comment tweaks.
[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 /*! \brief A placeholder for Asterisk's 'real' CEL configuration */
60 static struct ast_cel_general_config *saved_config;
61
62 /*! \brief The CEL config used for CEL unit tests */
63 static struct ast_cel_general_config *cel_test_config;
64
65 /*! \brief Lock used for synchronizing test execution stages with received events */
66 ast_mutex_t mid_test_sync_lock;
67
68 /*! \brief Lock used with sync_out for checking the end of test execution */
69 ast_mutex_t sync_lock;
70
71 /*! \brief Condition used for checking the end of test execution */
72 ast_cond_t sync_out;
73
74 /*! \brief Flag used to trigger a mid-test synchronization, access controlled by mid_test_sync_lock */
75 int do_mid_test_sync = 0;
76
77 /*! \brief A channel technology used for the unit tests */
78 static struct ast_channel_tech test_cel_chan_tech = {
79         .type = CHANNEL_TECH_NAME,
80         .description = "Mock channel technology for CEL tests",
81 };
82
83 /*! \brief A 1 second sleep */
84 static struct timespec to_sleep = {1, 0};
85
86 static void do_sleep(void)
87 {
88         while ((nanosleep(&to_sleep, &to_sleep) == -1) && (errno == EINTR));
89 }
90
91 #define APPEND_EVENT(chan, ev_type, userevent, extra, peer) do { \
92         if (append_expected_event(chan, ev_type, userevent, extra, peer)) { \
93                 return AST_TEST_FAIL; \
94         } \
95         } while (0)
96
97 #define APPEND_EVENT_SNAPSHOT(snapshot, ev_type, userevent, extra, peer) do { \
98         if (append_expected_event_snapshot(snapshot, ev_type, userevent, extra, peer)) { \
99                 return AST_TEST_FAIL; \
100         } \
101         } while (0)
102
103 #define APPEND_DUMMY_EVENT() do { \
104         if (append_dummy_event()) { \
105                 return AST_TEST_FAIL; \
106         } \
107         } while (0)
108
109 #define CONF_EXIT(channel, bridge) do { \
110         ast_test_validate(test, 0 == ast_bridge_depart(channel)); \
111         CONF_EXIT_EVENT(channel, bridge); \
112         mid_test_sync(); \
113         } while (0)
114
115 #define CONF_EXIT_EVENT(channel, bridge) do { \
116         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
117         extra = ast_json_pack("{s: s}", "bridge_id", bridge->uniqueid); \
118         ast_test_validate(test, extra != NULL); \
119         APPEND_EVENT(channel, AST_CEL_CONF_EXIT, NULL, extra, NULL); \
120         } while (0)
121
122 #define CONF_EXIT_SNAPSHOT(channel, bridge) do { \
123         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
124         extra = ast_json_pack("{s: s}", "bridge_id", bridge->uniqueid); \
125         ast_test_validate(test, extra != NULL); \
126         APPEND_EVENT_SNAPSHOT(channel, AST_CEL_CONF_EXIT, NULL, extra, NULL); \
127         } while (0)
128
129 #define CONF_ENTER_EVENT(channel, bridge) do { \
130         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
131         extra = ast_json_pack("{s: s}", "bridge_id", bridge->uniqueid); \
132         ast_test_validate(test, extra != NULL); \
133         APPEND_EVENT(channel, AST_CEL_CONF_ENTER, NULL, extra, NULL); \
134         } while (0)
135
136 #define BRIDGE_TO_CONF(first, second, third, bridge) do { \
137         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
138         extra = ast_json_pack("{s: s, s: s}", \
139                 "channel_name", ast_channel_name(third), \
140                 "bridge_id", bridge->uniqueid); \
141         ast_test_validate(test, extra != NULL); \
142         APPEND_EVENT(first, AST_CEL_BRIDGE_TO_CONF, NULL, extra, ast_channel_name(second)); \
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, NULL); \
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, NULL); \
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, 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, 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, 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, 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, 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, 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, NULL); \
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, const char *peer);
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, const char *peer);
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         ast_bridge_impart(bridge, chan, NULL, NULL, 0);
410
411         do_sleep();
412
413         ast_bridge_depart(chan);
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         ast_bridge_impart(bridge, chan, NULL, NULL, 0);
448
449         do_sleep();
450
451         ast_bridge_depart(chan);
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         ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
493         do_sleep();
494
495         ANSWER_CHANNEL(chan_bob);
496         EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
497
498         ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
499         do_sleep();
500         APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_bob));
501
502         ast_bridge_depart(chan_alice);
503         ast_bridge_depart(chan_bob);
504         APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_bob));
505
506         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
507         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
508
509         return AST_TEST_PASS;
510 }
511
512 AST_TEST_DEFINE(test_cel_single_twoparty_bridge_b)
513 {
514         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
515         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
516         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
517         struct ast_party_caller caller_alice = ALICE_CALLERID;
518         struct ast_party_caller caller_bob = BOB_CALLERID;
519
520         switch (cmd) {
521         case TEST_INIT:
522                 info->name = __func__;
523                 info->category = TEST_CATEGORY;
524                 info->summary = "Test CEL for a single party entering/leaving a bridge";
525                 info->description =
526                         "Test CEL records for a call that is\n"
527                         "answered, enters a bridge, and leaves it. In this scenario, the\n"
528                         "Party B should answer the bridge first.\n";
529                 return AST_TEST_NOT_RUN;
530         case TEST_EXECUTE:
531                 break;
532         }
533         bridge = ast_bridge_basic_new();
534         ast_test_validate(test, bridge != NULL);
535
536         CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
537
538         CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
539
540         ANSWER_CHANNEL(chan_alice);
541         EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
542
543         ANSWER_CHANNEL(chan_bob);
544         EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
545         do_sleep();
546
547         ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
548         do_sleep();
549
550         ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
551         do_sleep();
552         APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
553
554         ast_bridge_depart(chan_alice);
555         ast_bridge_depart(chan_bob);
556         APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
557
558         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
559         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
560
561         return AST_TEST_PASS;
562 }
563
564 /* XXX Validation needs to be reworked on a per-channel basis before
565  * test_cel_single_multiparty_bridge and test_cel_dial_answer_multiparty
566  * can operate properly. */
567 #if 0
568 AST_TEST_DEFINE(test_cel_single_multiparty_bridge)
569 {
570         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
571         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
572         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
573         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
574         struct ast_party_caller caller_alice = ALICE_CALLERID;
575         struct ast_party_caller caller_bob = BOB_CALLERID;
576         struct ast_party_caller caller_charlie = CHARLIE_CALLERID;
577
578         switch (cmd) {
579         case TEST_INIT:
580                 info->name = __func__;
581                 info->category = TEST_CATEGORY;
582                 info->summary = "Test CEL for a single party entering/leaving a multi-party bridge";
583                 info->description =
584                         "Test CEL records for a call that is\n"
585                         "answered, enters a bridge, and leaves it. A total of three\n"
586                         "parties perform this action.\n";
587                 return AST_TEST_NOT_RUN;
588         case TEST_EXECUTE:
589                 break;
590         }
591         bridge = ast_bridge_basic_new();
592         ast_test_validate(test, bridge != NULL);
593
594         CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
595         CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
596         CREATE_CHARLIE_CHANNEL(chan_charlie, &caller_charlie);
597
598         ANSWER_CHANNEL(chan_alice);
599         EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
600
601         do_sleep();
602
603         ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
604
605         ANSWER_CHANNEL(chan_bob);
606         EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
607         do_sleep();
608
609         ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
610         do_sleep();
611         APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_bob));
612
613         ANSWER_CHANNEL(chan_charlie);
614         EMULATE_APP_DATA(chan_charlie, 2, "Bridge", "");
615         do_sleep();
616         ast_bridge_impart(bridge, chan_charlie, NULL, NULL, 0);
617         do_sleep();
618         BRIDGE_TO_CONF(chan_alice, chan_bob, chan_charlie, bridge);
619
620         CONF_EXIT(chan_alice, bridge);
621         CONF_EXIT(chan_bob, bridge);
622         CONF_EXIT(chan_charlie, bridge);
623
624         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
625         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
626         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
627
628         return AST_TEST_PASS;
629 }
630 #endif
631
632 #define EMULATE_DIAL(channel, dialstring) do { \
633         EMULATE_APP_DATA(channel, 1, "Dial", dialstring); \
634         if (append_expected_event(channel, AST_CEL_APP_START, NULL, NULL, NULL)) { \
635                 return AST_TEST_FAIL; \
636         } \
637         } while (0)
638
639 #define START_DIALED(caller, callee) \
640         START_DIALED_FULL(caller, callee, "200", "Bob")
641
642 #define START_DIALED_FULL(caller, callee, number, name) do { \
643         callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, number, NULL, NULL, ast_channel_linkedid(caller), 0, CHANNEL_TECH_NAME "/" name); \
644         if (append_expected_event(callee, AST_CEL_CHANNEL_START, NULL, NULL, NULL)) { \
645                 return AST_TEST_FAIL; \
646         } \
647         ast_set_flag(ast_channel_flags(callee), AST_FLAG_OUTGOING); \
648         EMULATE_APP_DATA(callee, 0, "AppDial", "(Outgoing Line)"); \
649         ast_channel_publish_dial(caller, callee, name, NULL); \
650         } while (0)
651
652 AST_TEST_DEFINE(test_cel_dial_unanswered)
653 {
654         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
655         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
656         struct ast_party_caller caller = ALICE_CALLERID;
657
658         switch (cmd) {
659         case TEST_INIT:
660                 info->name = __func__;
661                 info->category = TEST_CATEGORY;
662                 info->summary = "Test CEL for a dial that isn't answered";
663                 info->description =
664                         "Test CEL records for a channel that\n"
665                         "performs a dial operation that isn't answered\n";
666                 return AST_TEST_NOT_RUN;
667         case TEST_EXECUTE:
668                 break;
669         }
670
671         CREATE_ALICE_CHANNEL(chan_caller, &caller);
672
673         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
674
675         START_DIALED(chan_caller, chan_callee);
676
677         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
678         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOANSWER");
679
680         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ANSWER, "NOANSWER");
681         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ANSWER, "");
682
683         return AST_TEST_PASS;
684 }
685
686
687 AST_TEST_DEFINE(test_cel_dial_busy)
688 {
689         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
690         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
691         struct ast_party_caller caller = ALICE_CALLERID;
692
693         switch (cmd) {
694         case TEST_INIT:
695                 info->name = __func__;
696                 info->category = TEST_CATEGORY;
697                 info->summary = "Test CEL for a dial that results in a busy";
698                 info->description =
699                         "Test CEL records for a channel that\n"
700                         "performs a dial operation to an endpoint that's busy\n";
701                 return AST_TEST_NOT_RUN;
702         case TEST_EXECUTE:
703                 break;
704         }
705
706         CREATE_ALICE_CHANNEL(chan_caller, &caller);
707
708         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
709
710         START_DIALED(chan_caller, chan_callee);
711
712         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
713         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "BUSY");
714
715         HANGUP_CHANNEL(chan_caller, AST_CAUSE_BUSY, "BUSY");
716         HANGUP_CHANNEL(chan_callee, AST_CAUSE_BUSY, "");
717
718         return AST_TEST_PASS;
719 }
720
721 AST_TEST_DEFINE(test_cel_dial_congestion)
722 {
723         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
724         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
725         struct ast_party_caller caller = ALICE_CALLERID;
726
727         switch (cmd) {
728         case TEST_INIT:
729                 info->name = __func__;
730                 info->category = TEST_CATEGORY;
731                 info->summary = "Test CEL for a dial that results in congestion";
732                 info->description =
733                         "Test CEL records for a channel that\n"
734                         "performs a dial operation to an endpoint that's congested\n";
735                 return AST_TEST_NOT_RUN;
736         case TEST_EXECUTE:
737                 break;
738         }
739
740         CREATE_ALICE_CHANNEL(chan_caller, &caller);
741
742         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
743
744         START_DIALED(chan_caller, chan_callee);
745
746         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
747         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CONGESTION");
748
749         HANGUP_CHANNEL(chan_caller, AST_CAUSE_CONGESTION, "CONGESTION");
750         HANGUP_CHANNEL(chan_callee, AST_CAUSE_CONGESTION, "");
751
752         return AST_TEST_PASS;
753 }
754
755 AST_TEST_DEFINE(test_cel_dial_unavailable)
756 {
757         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
758         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
759         struct ast_party_caller caller = ALICE_CALLERID;
760
761         switch (cmd) {
762         case TEST_INIT:
763                 info->name = __func__;
764                 info->category = TEST_CATEGORY;
765                 info->summary = "Test CEL for a dial that results in unavailable";
766                 info->description =
767                         "Test CEL records for a channel that\n"
768                         "performs a dial operation to an endpoint that's unavailable\n";
769                 return AST_TEST_NOT_RUN;
770         case TEST_EXECUTE:
771                 break;
772         }
773
774         CREATE_ALICE_CHANNEL(chan_caller, &caller);
775
776         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
777
778         START_DIALED(chan_caller, chan_callee);
779
780         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
781         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CHANUNAVAIL");
782
783         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ROUTE_DESTINATION, "CHANUNAVAIL");
784         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ROUTE_DESTINATION, "");
785
786         return AST_TEST_PASS;
787 }
788
789 AST_TEST_DEFINE(test_cel_dial_caller_cancel)
790 {
791         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
792         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
793         struct ast_party_caller caller = ALICE_CALLERID;
794
795         switch (cmd) {
796         case TEST_INIT:
797                 info->name = __func__;
798                 info->category = TEST_CATEGORY;
799                 info->summary = "Test CEL for a dial where the caller cancels";
800                 info->description =
801                         "Test CEL records for a channel that\n"
802                         "performs a dial operation to an endpoint but then decides\n"
803                         "to hang up, cancelling the dial\n";
804                 return AST_TEST_NOT_RUN;
805         case TEST_EXECUTE:
806                 break;
807         }
808
809         CREATE_ALICE_CHANNEL(chan_caller, &caller);
810
811         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
812
813         START_DIALED(chan_caller, chan_callee);
814
815         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
816         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CANCEL");
817
818         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
819         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "CANCEL");
820
821         return AST_TEST_PASS;
822 }
823
824 AST_TEST_DEFINE(test_cel_dial_parallel_failed)
825 {
826         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
827         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
828         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
829         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
830         struct ast_party_caller caller = ALICE_CALLERID;
831
832         switch (cmd) {
833         case TEST_INIT:
834                 info->name = __func__;
835                 info->category = TEST_CATEGORY;
836                 info->summary = "Test a parallel dial where all channels fail to answer";
837                 info->description =
838                         "This tests dialing three parties: Bob, Charlie, David. Charlie\n"
839                         "returns BUSY; David returns CONGESTION; Bob fails to answer and\n"
840                         "Alice hangs up. Three records are created for Alice as a result.\n";
841                 return AST_TEST_NOT_RUN;
842         case TEST_EXECUTE:
843                 break;
844         }
845
846         CREATE_ALICE_CHANNEL(chan_caller, &caller);
847
848         /* Channel enters Dial app */
849         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob&" CHANNEL_TECH_NAME "/Charlie&" CHANNEL_TECH_NAME "/David");
850
851         /* Outbound channels are created */
852         START_DIALED_FULL(chan_caller, chan_bob, "200", "Bob");
853         START_DIALED_FULL(chan_caller, chan_charlie, "300", "Charlie");
854         START_DIALED_FULL(chan_caller, chan_david, "400", "David");
855
856         /* Dial starts */
857         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
858
859         /* Charlie is busy */
860         ast_channel_publish_dial(chan_caller, chan_charlie, NULL, "BUSY");
861         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_BUSY, "");
862
863         /* David is congested */
864         ast_channel_publish_dial(chan_caller, chan_david, NULL, "CONGESTION");
865         HANGUP_CHANNEL(chan_david, AST_CAUSE_CONGESTION, "");
866
867         /* Bob is canceled */
868         ast_channel_publish_dial(chan_caller, chan_bob, NULL, "CANCEL");
869         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
870
871         /* Alice hangs up */
872         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "BUSY");
873
874         return AST_TEST_PASS;
875 }
876
877 AST_TEST_DEFINE(test_cel_dial_answer_no_bridge)
878 {
879         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
880         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
881         struct ast_party_caller caller = ALICE_CALLERID;
882
883         switch (cmd) {
884         case TEST_INIT:
885                 info->name = __func__;
886                 info->category = TEST_CATEGORY;
887                 info->summary = "Test dialing, answering, and not going into a bridge.";
888                 info->description =
889                         "This is a weird one, but theoretically possible. You can perform\n"
890                         "a dial, then bounce both channels to different priorities and\n"
891                         "never have them enter a bridge together. Ew. This makes sure that\n"
892                         "when we answer, we get a CEL, it gets ended at that point, and\n"
893                         "that it gets finalized appropriately.\n";
894                 return AST_TEST_NOT_RUN;
895         case TEST_EXECUTE:
896                 break;
897         }
898
899         CREATE_ALICE_CHANNEL(chan_caller, &caller);
900
901         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
902
903         START_DIALED(chan_caller, chan_callee);
904
905         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
906         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
907
908         ANSWER_NO_APP(chan_caller);
909         ast_clear_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
910         ANSWER_NO_APP(chan_callee);
911
912         EMULATE_APP_DATA(chan_caller, 2, "Wait", "1");
913         EMULATE_APP_DATA(chan_callee, 1, "Wait", "1");
914
915         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
916         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
917
918         return AST_TEST_PASS;
919 }
920
921 AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_a)
922 {
923         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
924         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
925         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
926         struct ast_party_caller caller = ALICE_CALLERID;
927
928         switch (cmd) {
929         case TEST_INIT:
930                 info->name = __func__;
931                 info->category = TEST_CATEGORY;
932                 info->summary = "Test dialing, answering, and going into a 2-party bridge";
933                 info->description =
934                         "The most 'basic' of scenarios\n";
935                 return AST_TEST_NOT_RUN;
936         case TEST_EXECUTE:
937                 break;
938         }
939         bridge = ast_bridge_basic_new();
940         ast_test_validate(test, bridge != NULL);
941
942         CREATE_ALICE_CHANNEL(chan_caller, &caller);
943
944         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
945
946         START_DIALED(chan_caller, chan_callee);
947
948         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
949         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
950
951         ANSWER_NO_APP(chan_caller);
952         ANSWER_NO_APP(chan_callee);
953
954         do_sleep();
955
956         ast_bridge_impart(bridge, chan_caller, NULL, NULL, 0);
957         do_sleep();
958         ast_bridge_impart(bridge, chan_callee, NULL, NULL, 0);
959         do_sleep();
960         APPEND_EVENT(chan_caller, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_callee));
961
962         ast_bridge_depart(chan_caller);
963         ast_bridge_depart(chan_callee);
964         APPEND_EVENT(chan_caller, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_callee));
965
966         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
967         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
968
969         return AST_TEST_PASS;
970 }
971
972 AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_b)
973 {
974         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
975         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
976         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
977         struct ast_party_caller caller = ALICE_CALLERID;
978
979         switch (cmd) {
980         case TEST_INIT:
981                 info->name = __func__;
982                 info->category = TEST_CATEGORY;
983                 info->summary = "Test dialing, answering, and going into a 2-party bridge";
984                 info->description =
985                         "The most 'basic' of scenarios\n";
986                 return AST_TEST_NOT_RUN;
987         case TEST_EXECUTE:
988                 break;
989         }
990         bridge = ast_bridge_basic_new();
991         ast_test_validate(test, bridge != NULL);
992
993         CREATE_ALICE_CHANNEL(chan_caller, &caller);
994
995         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
996
997         START_DIALED(chan_caller, chan_callee);
998
999         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1000         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1001
1002         ANSWER_NO_APP(chan_caller);
1003         ANSWER_NO_APP(chan_callee);
1004
1005         do_sleep();
1006         ast_bridge_impart(bridge, chan_callee, NULL, NULL, 0);
1007         do_sleep();
1008         ast_bridge_impart(bridge, chan_caller, NULL, NULL, 0);
1009         do_sleep();
1010         APPEND_EVENT(chan_callee, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_caller));
1011
1012         ast_bridge_depart(chan_caller);
1013         ast_bridge_depart(chan_callee);
1014         APPEND_EVENT(chan_callee, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_caller));
1015
1016         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
1017         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1018
1019         return AST_TEST_PASS;
1020 }
1021
1022 #if 0
1023 AST_TEST_DEFINE(test_cel_dial_answer_multiparty)
1024 {
1025         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1026         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1027         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1028         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1029         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
1030         struct ast_party_caller alice_caller = ALICE_CALLERID;
1031         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1032
1033         switch (cmd) {
1034         case TEST_INIT:
1035                 info->name = __func__;
1036                 info->category = TEST_CATEGORY;
1037                 info->summary = "Test dialing, answering, and going into a multi-party bridge";
1038                 info->description =
1039                         "A little tricky to get to do, but possible with some redirects.\n";
1040                 return AST_TEST_NOT_RUN;
1041         case TEST_EXECUTE:
1042                 break;
1043         }
1044         bridge = ast_bridge_basic_new();
1045         ast_test_validate(test, bridge != NULL);
1046
1047         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1048
1049         EMULATE_DIAL(chan_alice, CHANNEL_TECH_NAME "/Bob");
1050
1051         START_DIALED(chan_alice, chan_bob);
1052         do_sleep();
1053
1054         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1055         do_sleep();
1056         EMULATE_DIAL(chan_charlie, CHANNEL_TECH_NAME "/Bob");
1057         do_sleep();
1058
1059         START_DIALED_FULL(chan_charlie, chan_david, "400", "David");
1060
1061         ast_channel_state_set(chan_alice, AST_STATE_RINGING);
1062         do_sleep();
1063         ast_channel_state_set(chan_charlie, AST_STATE_RINGING);
1064         do_sleep();
1065         ast_channel_publish_dial(chan_alice, chan_bob, NULL, "ANSWER");
1066         do_sleep();
1067         ast_channel_publish_dial(chan_charlie, chan_david, NULL, "ANSWER");
1068         do_sleep();
1069
1070         ANSWER_NO_APP(chan_alice);
1071         do_sleep();
1072         ANSWER_NO_APP(chan_bob);
1073         do_sleep();
1074         ANSWER_NO_APP(chan_charlie);
1075         do_sleep();
1076         ANSWER_NO_APP(chan_david);
1077         do_sleep();
1078
1079         do_sleep();
1080         ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_charlie, NULL, NULL, 0));
1081         do_sleep();
1082         ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_david, NULL, NULL, 0));
1083         do_sleep();
1084         APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1085
1086         ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0));
1087         do_sleep();
1088         BRIDGE_TO_CONF(chan_charlie, chan_david, chan_bob, bridge);
1089
1090         ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0));
1091         do_sleep();
1092         CONF_ENTER_EVENT(chan_alice, bridge);
1093
1094         CONF_EXIT(chan_alice, bridge);
1095         CONF_EXIT(chan_bob, bridge);
1096         CONF_EXIT(chan_charlie, bridge);
1097         CONF_EXIT(chan_david, bridge);
1098
1099         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "ANSWER");
1100         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1101         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "ANSWER");
1102         HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1103
1104         return AST_TEST_PASS;
1105 }
1106 #endif
1107
1108 AST_TEST_DEFINE(test_cel_blind_transfer)
1109 {
1110         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1111         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1112         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
1113         struct ast_party_caller alice_caller = ALICE_CALLERID;
1114         struct ast_party_caller bob_caller = BOB_CALLERID;
1115         struct ast_bridge_channel_pair pair;
1116
1117         switch (cmd) {
1118         case TEST_INIT:
1119                 info->name = __func__;
1120                 info->category = TEST_CATEGORY;
1121                 info->summary = "Test blind transfers to an extension";
1122                 info->description =
1123                         "This test creates two channels, bridges them, and then"
1124                         " blind transfers the bridge to an extension.\n";
1125                 return AST_TEST_NOT_RUN;
1126         case TEST_EXECUTE:
1127                 break;
1128         }
1129         bridge = ast_bridge_basic_new();
1130         ast_test_validate(test, bridge != NULL);
1131
1132         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1133         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1134
1135         ANSWER_NO_APP(chan_alice);
1136         ANSWER_NO_APP(chan_bob);
1137
1138         ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0));
1139         do_sleep();
1140
1141         ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0));
1142         do_sleep();
1143         APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1144
1145         pair.bridge = bridge;
1146         pair.channel = chan_alice;
1147         ast_bridge_publish_blind_transfer(1, AST_BRIDGE_TRANSFER_SUCCESS,
1148                 &pair, "transfer_context", "transfer_extension");
1149         BLINDTRANSFER_EVENT(chan_alice, bridge, "transfer_extension", "transfer_context");
1150
1151         APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
1152         ast_test_validate(test, 0 == ast_bridge_depart(chan_alice));
1153
1154         ast_test_validate(test, 0 == ast_bridge_depart(chan_bob));
1155
1156         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1157         do_sleep();
1158         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1159
1160         return AST_TEST_PASS;
1161 }
1162
1163 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_swap)
1164 {
1165         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1166         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1167         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1168         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1169         RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
1170         RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
1171         struct ast_party_caller alice_caller = ALICE_CALLERID;
1172         struct ast_party_caller bob_caller = BOB_CALLERID;
1173         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1174         struct ast_party_caller david_caller = ALICE_CALLERID;
1175
1176         switch (cmd) {
1177         case TEST_INIT:
1178                 info->name = __func__;
1179                 info->category = TEST_CATEGORY;
1180                 info->summary = "Test attended transfers between two pairs of bridged parties";
1181                 info->description =
1182                         "This test creates four channels, places each pair in"
1183                         " a bridge, and then attended transfers the bridges"
1184                         " together.\n";
1185                 return AST_TEST_NOT_RUN;
1186         case TEST_EXECUTE:
1187                 break;
1188         }
1189         /* Create first set of bridged parties */
1190         bridge1 = ast_bridge_basic_new();
1191         ast_test_validate(test, bridge1 != NULL);
1192
1193         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1194         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1195         ANSWER_NO_APP(chan_alice);
1196         ANSWER_NO_APP(chan_bob);
1197
1198         ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
1199         do_sleep();
1200
1201         ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
1202         do_sleep();
1203         APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1204
1205         /* Create second set of bridged parties */
1206         bridge2 = ast_bridge_basic_new();
1207         ast_test_validate(test, bridge2 != NULL);
1208
1209         CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1210         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1211         ANSWER_NO_APP(chan_david);
1212         ANSWER_NO_APP(chan_charlie);
1213
1214         ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
1215         do_sleep();
1216
1217         ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
1218         do_sleep();
1219         APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1220
1221         /* Perform attended transfer */
1222         APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
1223
1224         ast_bridge_transfer_attended(chan_alice, chan_david);
1225         do_sleep();
1226         BRIDGE_TO_CONF(chan_charlie, chan_david, chan_bob, bridge2);
1227         CONF_EXIT_EVENT(chan_david, bridge2);
1228
1229         ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
1230
1231         do_sleep();
1232         CONF_EXIT(chan_bob, bridge2);
1233         CONF_EXIT(chan_charlie, bridge2);
1234
1235         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1236         do_sleep();
1237         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1238         do_sleep();
1239         HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1240         do_sleep();
1241         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1242
1243         return AST_TEST_PASS;
1244 }
1245
1246 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
1247 {
1248         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1249         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1250         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1251         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1252         RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
1253         RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
1254         struct ast_party_caller alice_caller = ALICE_CALLERID;
1255         struct ast_party_caller bob_caller = BOB_CALLERID;
1256         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1257         struct ast_party_caller david_caller = ALICE_CALLERID;
1258
1259         switch (cmd) {
1260         case TEST_INIT:
1261                 info->name = __func__;
1262                 info->category = TEST_CATEGORY;
1263                 info->summary = "Test attended transfers between two pairs of"
1264                         " bridged parties that results in a bridge merge";
1265                 info->description =
1266                         "This test creates four channels, places each pair"
1267                         " in a bridge, and then attended transfers the bridges"
1268                         " together causing a bridge merge.\n";
1269                 return AST_TEST_NOT_RUN;
1270         case TEST_EXECUTE:
1271                 break;
1272         }
1273         /* Create first set of bridged parties */
1274         bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1275                 AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1276         ast_test_validate(test, bridge1 != NULL);
1277
1278         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1279         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1280         ANSWER_NO_APP(chan_alice);
1281         ANSWER_NO_APP(chan_bob);
1282
1283         ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
1284         do_sleep();
1285
1286         ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
1287         do_sleep();
1288         APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1289
1290         /* Create second set of bridged parties */
1291         bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1292                 AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1293         ast_test_validate(test, bridge2 != NULL);
1294
1295         CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1296         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1297         ANSWER_NO_APP(chan_david);
1298         ANSWER_NO_APP(chan_charlie);
1299
1300         ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
1301         do_sleep();
1302
1303         ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
1304         do_sleep();
1305         APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1306
1307         /* Perform attended transfer */
1308         APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_david));
1309
1310         ast_bridge_transfer_attended(chan_alice, chan_david);
1311         do_sleep();
1312         BRIDGE_TO_CONF(chan_bob, chan_alice, chan_charlie, bridge1);
1313         CONF_EXIT_EVENT(chan_alice, bridge1);
1314
1315         ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
1316
1317         do_sleep();
1318         CONF_EXIT(chan_bob, bridge1);
1319         CONF_EXIT(chan_charlie, bridge1);
1320
1321         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1322         do_sleep();
1323         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1324         do_sleep();
1325         HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1326         do_sleep();
1327         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1328
1329         return AST_TEST_PASS;
1330 }
1331
1332 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
1333 {
1334         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1335         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1336         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1337         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1338         RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
1339         RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
1340         struct ast_party_caller alice_caller = ALICE_CALLERID;
1341         struct ast_party_caller bob_caller = BOB_CALLERID;
1342         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1343         struct ast_party_caller david_caller = ALICE_CALLERID;
1344
1345         switch (cmd) {
1346         case TEST_INIT:
1347                 info->name = __func__;
1348                 info->category = TEST_CATEGORY;
1349                 info->summary = "Test attended transfers between two pairs of"
1350                         " bridged parties that results in a bridge merge";
1351                 info->description =
1352                         "This test creates four channels, places each pair"
1353                         " in a bridge, and then attended transfers the bridges"
1354                         " together causing a bridge link.\n";
1355                 return AST_TEST_NOT_RUN;
1356         case TEST_EXECUTE:
1357                 break;
1358         }
1359         /* Create first set of bridged parties */
1360         bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1361                 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
1362                 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
1363                 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1364         ast_test_validate(test, bridge1 != NULL);
1365
1366         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1367         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1368         ANSWER_NO_APP(chan_alice);
1369         ANSWER_NO_APP(chan_bob);
1370
1371         ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
1372         do_sleep();
1373
1374         ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
1375         do_sleep();
1376         APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1377
1378         /* Create second set of bridged parties */
1379         bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1380                 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
1381                 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
1382                 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1383         ast_test_validate(test, bridge2 != NULL);
1384
1385         CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1386         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1387         ANSWER_NO_APP(chan_david);
1388         ANSWER_NO_APP(chan_charlie);
1389
1390         ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
1391         do_sleep();
1392
1393         ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
1394         do_sleep();
1395         APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1396
1397         /* Perform attended transfer */
1398
1399         /* The following events can not be matched directly since nothing is known
1400          * about the linking local channel.
1401          * local channel ;1 and ;2 creation and ;2 answer */
1402         APPEND_DUMMY_EVENT();
1403         APPEND_DUMMY_EVENT();
1404         APPEND_DUMMY_EVENT();
1405
1406         ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
1407
1408         /* The two BRIDGE_TO_CONFs and CONF_EXITs are all racing to be first */
1409
1410         /* BRIDGE_TO_CONF with primary charlie, peer david, and trigger channel ;2 */
1411         APPEND_DUMMY_EVENT();
1412
1413         ast_bridge_transfer_attended(chan_alice, chan_david);
1414         do_sleep();
1415
1416         /* BRIDGE_TO_CONF with primary bob, peer alice, and trigger channel ;1 */
1417         APPEND_DUMMY_EVENT();
1418
1419         /* CONF_EXIT alice and david */
1420         APPEND_DUMMY_EVENT();
1421         APPEND_DUMMY_EVENT();
1422
1423         /* ANSWER ;1 */
1424         APPEND_DUMMY_EVENT();
1425
1426         do_sleep();
1427         CONF_EXIT(chan_bob, bridge1);
1428         CONF_EXIT(chan_charlie, bridge2);
1429
1430         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1431         do_sleep();
1432         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1433         do_sleep();
1434         HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1435         do_sleep();
1436         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1437
1438         return AST_TEST_PASS;
1439 }
1440
1441 AST_TEST_DEFINE(test_cel_dial_pickup)
1442 {
1443         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1444         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1445         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1446         struct ast_party_caller caller = ALICE_CALLERID;
1447         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1448
1449         switch (cmd) {
1450         case TEST_INIT:
1451                 info->name = __func__;
1452                 info->category = TEST_CATEGORY;
1453                 info->summary = "Test call pickup";
1454                 info->description =
1455                         "Test CEL records for a call that is\n"
1456                         "inbound to Asterisk, executes some dialplan, and\n"
1457                         "is picked up.\n";
1458                 return AST_TEST_NOT_RUN;
1459         case TEST_EXECUTE:
1460                 break;
1461         }
1462
1463         CREATE_ALICE_CHANNEL(chan_caller, &caller);
1464
1465         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
1466
1467         START_DIALED(chan_caller, chan_callee);
1468
1469         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1470
1471         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1472
1473         {
1474                 SCOPED_CHANNELLOCK(lock, chan_callee);
1475                 APPEND_EVENT(chan_callee, AST_CEL_PICKUP, NULL, NULL, ast_channel_name(chan_charlie));
1476                 ast_test_validate(test, 0 == ast_do_pickup(chan_charlie, chan_callee));
1477         }
1478
1479         /* Hang up the masqueraded zombie */
1480         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1481
1482         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1483
1484         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
1485         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1486
1487         return AST_TEST_PASS;
1488 }
1489
1490 AST_TEST_DEFINE(test_cel_local_optimize)
1491 {
1492         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1493         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1494         struct ast_party_caller alice_caller = ALICE_CALLERID;
1495         struct ast_party_caller bob_caller = BOB_CALLERID;
1496         RAII_VAR(struct ast_multi_channel_blob *, mc_blob, NULL, ao2_cleanup);
1497         RAII_VAR(struct ast_channel_snapshot *, alice_snapshot, NULL, ao2_cleanup);
1498         RAII_VAR(struct ast_channel_snapshot *, bob_snapshot, NULL, ao2_cleanup);
1499         RAII_VAR(struct stasis_message *, local_opt_begin, NULL, ao2_cleanup);
1500         RAII_VAR(struct stasis_message *, local_opt_end, NULL, ao2_cleanup);
1501
1502         switch (cmd) {
1503         case TEST_INIT:
1504                 info->name = __func__;
1505                 info->category = TEST_CATEGORY;
1506                 info->summary = "Test local channel optimization record generation";
1507                 info->description =
1508                         "Test CEL records for two local channels being optimized\n"
1509                         "out by sending a messages indicating local optimization\n"
1510                         "begin and end\n";
1511                 return AST_TEST_NOT_RUN;
1512         case TEST_EXECUTE:
1513                 break;
1514         }
1515
1516         mc_blob = ast_multi_channel_blob_create(ast_json_null());
1517         ast_test_validate(test, mc_blob != NULL);
1518
1519         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1520         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1521
1522         alice_snapshot = ast_channel_snapshot_create(chan_alice);
1523         ast_test_validate(test, alice_snapshot != NULL);
1524
1525         bob_snapshot = ast_channel_snapshot_create(chan_bob);
1526         ast_test_validate(test, bob_snapshot != NULL);
1527
1528         ast_multi_channel_blob_add_channel(mc_blob, "1", alice_snapshot);
1529         ast_multi_channel_blob_add_channel(mc_blob, "2", bob_snapshot);
1530
1531         local_opt_begin = stasis_message_create(ast_local_optimization_begin_type(), mc_blob);
1532         ast_test_validate(test, local_opt_begin != NULL);
1533
1534         local_opt_end = stasis_message_create(ast_local_optimization_end_type(), mc_blob);
1535         ast_test_validate(test, local_opt_end != NULL);
1536
1537         stasis_publish(ast_channel_topic(chan_alice), local_opt_begin);
1538         stasis_publish(ast_channel_topic(chan_alice), local_opt_end);
1539         APPEND_EVENT_SNAPSHOT(alice_snapshot, AST_CEL_LOCAL_OPTIMIZE, NULL, NULL, bob_snapshot->name);
1540
1541         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1542         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1543
1544         return AST_TEST_PASS;
1545 }
1546
1547 /*! Subscription for CEL events */
1548 static struct ast_event_sub *event_sub = NULL;
1549
1550 /*! Container for astobj2 duplicated ast_events */
1551 static struct ao2_container *cel_received_events = NULL;
1552
1553 /*! Container for expected CEL events */
1554 static struct ao2_container *cel_expected_events = NULL;
1555
1556 static struct ast_event *ao2_dup_event(const struct ast_event *event)
1557 {
1558         struct ast_event *event_dup;
1559         uint16_t event_len;
1560
1561         event_len = ast_event_get_size(event);
1562
1563         event_dup = ao2_alloc(event_len, NULL);
1564         if (!event_dup) {
1565                 return NULL;
1566         }
1567
1568         memcpy(event_dup, event, event_len);
1569
1570         return event_dup;
1571 }
1572
1573 static void mid_test_sync(void)
1574 {
1575         ast_mutex_lock(&mid_test_sync_lock);
1576         if (ao2_container_count(cel_expected_events) <= ao2_container_count(cel_received_events)) {
1577                 ast_mutex_unlock(&mid_test_sync_lock);
1578                 return;
1579         }
1580
1581         do_mid_test_sync = 1;
1582         ast_mutex_unlock(&mid_test_sync_lock);
1583
1584         {
1585                 struct timeval start = ast_tvnow();
1586                 struct timespec end = {
1587                         .tv_sec = start.tv_sec + 15,
1588                         .tv_nsec = start.tv_usec * 1000
1589                 };
1590
1591                 SCOPED_MUTEX(lock, &sync_lock);
1592                 ast_cond_timedwait(&sync_out, &sync_lock, &end);
1593         }
1594 }
1595
1596 static int append_event(struct ast_event *ev)
1597 {
1598         RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
1599         ao2_ev = ao2_dup_event(ev);
1600         if (!ao2_ev) {
1601                 return -1;
1602         }
1603
1604         ao2_link(cel_expected_events, ao2_ev);
1605         return 0;
1606 }
1607
1608 static int append_dummy_event(void)
1609 {
1610         RAII_VAR(struct ast_event *, ev, NULL, ast_free);
1611         RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
1612
1613         ev = ast_event_new(AST_EVENT_CUSTOM, AST_EVENT_IE_END);
1614         if (!ev) {
1615                 return -1;
1616         }
1617
1618         return append_event(ev);
1619 }
1620
1621 static int append_expected_event_snapshot(
1622         struct ast_channel_snapshot *snapshot,
1623         enum ast_cel_event_type type,
1624         const char *userdefevname,
1625         struct ast_json *extra, const char *peer)
1626 {
1627         RAII_VAR(struct ast_event *, ev, NULL, ast_free);
1628         ev = ast_cel_create_event(snapshot, type, userdefevname, extra, peer);
1629         if (!ev) {
1630                 return -1;
1631         }
1632
1633         return append_event(ev);
1634 }
1635
1636 static int append_expected_event(
1637         struct ast_channel *chan,
1638         enum ast_cel_event_type type,
1639         const char *userdefevname,
1640         struct ast_json *extra, const char *peer)
1641 {
1642         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1643         snapshot = ast_channel_snapshot_create(chan);
1644         if (!snapshot) {
1645                 return -1;
1646         }
1647
1648         return append_expected_event_snapshot(snapshot, type, userdefevname, extra, peer);
1649 }
1650
1651 static void test_sub(const struct ast_event *event, void *data)
1652 {
1653         struct ast_event *event_dup = ao2_dup_event(event);
1654         const char *sync_tag;
1655         SCOPED_MUTEX(mid_test_lock, &mid_test_sync_lock);
1656
1657         if (!event_dup) {
1658                 return;
1659         }
1660
1661         sync_tag = ast_event_get_ie_str(event, AST_EVENT_IE_SERVICE);
1662         if (sync_tag) {
1663                 if (!strcmp(sync_tag, "SYNC")) {
1664                         /* trigger things */
1665                         SCOPED_MUTEX(lock, &sync_lock);
1666                         ast_cond_signal(&sync_out);
1667                         return;
1668                 }
1669         }
1670
1671         /* save the event for later processing */
1672         ao2_link(cel_received_events, event_dup);
1673
1674         if (do_mid_test_sync) {
1675                 int expected = ao2_container_count(cel_expected_events);
1676                 int received = ao2_container_count(cel_received_events);
1677                 if (expected <= received) {
1678                         {
1679                         SCOPED_MUTEX(lock, &sync_lock);
1680                         ast_cond_signal(&sync_out);
1681                         do_mid_test_sync = 0;
1682                         }
1683                 }
1684         }
1685 }
1686
1687 /*!
1688  * \internal
1689  * \brief Callback function called before each test executes
1690  */
1691 static int test_cel_init_cb(struct ast_test_info *info, struct ast_test *test)
1692 {
1693         ast_assert(event_sub == NULL);
1694         ast_assert(cel_received_events == NULL);
1695         ast_assert(cel_expected_events == NULL);
1696
1697         ast_mutex_init(&mid_test_sync_lock);
1698         ast_mutex_init(&sync_lock);
1699         ast_cond_init(&sync_out, NULL);
1700
1701         /* Back up the real CEL config and insert the test's config */
1702         saved_config = ast_cel_get_config();
1703         ast_cel_set_config(cel_test_config);
1704
1705         /* init CEL event storage (degenerate hash table becomes a linked list) */
1706         cel_received_events = ao2_container_alloc(1, NULL, NULL);
1707         cel_expected_events = ao2_container_alloc(1, NULL, NULL);
1708
1709         /* start the CEL event callback */
1710         event_sub = ast_event_subscribe(AST_EVENT_CEL, test_sub, "CEL Test Logging",
1711                 NULL, AST_EVENT_IE_END);
1712         return 0;
1713 }
1714
1715 /*! \brief Check an IE value from two events,  */
1716 static int match_ie_val(
1717         const struct ast_event *event1,
1718         const struct ast_event *event2,
1719         enum ast_event_ie_type type)
1720 {
1721         enum ast_event_ie_pltype pltype = ast_event_get_ie_pltype(type);
1722
1723         switch (pltype) {
1724         case AST_EVENT_IE_PLTYPE_UINT:
1725         {
1726                 uint32_t val = ast_event_get_ie_uint(event2, type);
1727
1728                 return (val == ast_event_get_ie_uint(event1, type)) ? 1 : 0;
1729         }
1730         case AST_EVENT_IE_PLTYPE_STR:
1731         {
1732                 const char *str;
1733                 uint32_t hash;
1734
1735                 hash = ast_event_get_ie_str_hash(event2, type);
1736                 if (hash != ast_event_get_ie_str_hash(event1, type)) {
1737                         return 0;
1738                 }
1739
1740                 str = ast_event_get_ie_str(event2, type);
1741                 if (str) {
1742                         const char *e1str, *e2str;
1743                         e1str = ast_event_get_ie_str(event1, type);
1744                         e2str = str;
1745
1746                         if (type == AST_EVENT_IE_DEVICE) {
1747                                 e1str = ast_tech_to_upper(ast_strdupa(e1str));
1748                                 e2str = ast_tech_to_upper(ast_strdupa(e2str));
1749                         }
1750
1751                         if (!strcmp(e1str, e2str)) {
1752                                 return 1;
1753                         }
1754                 }
1755
1756                 return 0;
1757         }
1758         default:
1759                 break;
1760         }
1761         return 0;
1762 }
1763
1764 static int events_are_equal(struct ast_test *test, struct ast_event *received, struct ast_event *expected)
1765 {
1766         struct ast_event_iterator iterator;
1767         int res;
1768
1769         if (ast_event_get_type(expected) == AST_EVENT_CUSTOM) {
1770                 /* this event is flagged as a wildcard match */
1771                 return 1;
1772         }
1773
1774         for (res = ast_event_iterator_init(&iterator, received); !res; res = ast_event_iterator_next(&iterator)) {
1775                 /* XXX ignore sec/usec for now */
1776                 /* ignore EID */
1777                 int ie_type = ast_event_iterator_get_ie_type(&iterator);
1778                 if (ie_type != AST_EVENT_IE_CEL_EVENT_TIME_USEC
1779                         && ie_type != AST_EVENT_IE_EID
1780                         && ie_type != AST_EVENT_IE_CEL_EVENT_TIME
1781                         && !match_ie_val(received, expected, ie_type)) {
1782                         ast_test_status_update(test, "Failed matching on field %s\n", ast_event_get_ie_type_name(ie_type));
1783                         return 0;
1784                 }
1785         }
1786
1787         return 1;
1788 }
1789
1790 static int dump_event(struct ast_test *test, struct ast_event *event)
1791 {
1792         struct ast_event_iterator i;
1793
1794         if (ast_event_iterator_init(&i, event)) {
1795                 ast_test_status_update(test, "Failed to initialize event iterator.  :-(\n");
1796                 return 0;
1797         }
1798
1799         ast_test_status_update(test, "Event: %s %s\n", ast_event_get_type_name(event),
1800                 ast_cel_get_type_name(ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TYPE)));
1801
1802         do {
1803                 enum ast_event_ie_type ie_type;
1804                 enum ast_event_ie_pltype ie_pltype;
1805                 const char *ie_type_name;
1806
1807                 ie_type = ast_event_iterator_get_ie_type(&i);
1808                 ie_type_name = ast_event_get_ie_type_name(ie_type);
1809                 ie_pltype = ast_event_get_ie_pltype(ie_type);
1810
1811                 switch (ie_pltype) {
1812                 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1813                 case AST_EVENT_IE_PLTYPE_EXISTS:
1814                         ast_test_status_update(test, "%s\n", ie_type_name);
1815                         break;
1816                 case AST_EVENT_IE_PLTYPE_STR:
1817                         ast_test_status_update(test, "%.30s: %s\n", ie_type_name,
1818                                         ast_event_iterator_get_ie_str(&i));
1819                         break;
1820                 case AST_EVENT_IE_PLTYPE_UINT:
1821                         ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
1822                                         ast_event_iterator_get_ie_uint(&i));
1823                         break;
1824                 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1825                         ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
1826                                         ast_event_iterator_get_ie_bitflags(&i));
1827                 default:
1828                         break;
1829                 }
1830         } while (!ast_event_iterator_next(&i));
1831
1832         ast_test_status_update(test, "\n");
1833
1834         return 0;
1835 }
1836
1837 static int check_events(struct ast_test *test, struct ao2_container *local_expected, struct ao2_container *local_received)
1838 {
1839         struct ao2_iterator expected_it, received_it;
1840         struct ast_event *rx_event, *ex_event;
1841         int debug = 0;
1842
1843         if (ao2_container_count(local_expected) != ao2_container_count(local_received)) {
1844                 ast_test_status_update(test, "Increasing verbosity since the number of expected events (%d)"
1845                         " did not match number of received events (%d).\n",
1846                         ao2_container_count(local_expected),
1847                         ao2_container_count(local_received));
1848                 debug = 1;
1849         }
1850
1851         expected_it = ao2_iterator_init(local_expected, 0);
1852         received_it = ao2_iterator_init(local_received, 0);
1853         rx_event = ao2_iterator_next(&received_it);
1854         ex_event = ao2_iterator_next(&expected_it);
1855         while (rx_event && ex_event) {
1856                 if (!events_are_equal(test, rx_event, ex_event)) {
1857                         ast_test_status_update(test, "Received event:\n");
1858                         dump_event(test, rx_event);
1859                         ast_test_status_update(test, "Expected event:\n");
1860                         dump_event(test, ex_event);
1861                         return -1;
1862                 }
1863                 if (debug) {
1864                         ast_test_status_update(test, "Compared events successfully%s\n", ast_event_get_type(ex_event) == AST_EVENT_CUSTOM ? " (wildcard match)" : "");
1865                         dump_event(test, rx_event);
1866                 }
1867                 ao2_cleanup(rx_event);
1868                 ao2_cleanup(ex_event);
1869                 rx_event = ao2_iterator_next(&received_it);
1870                 ex_event = ao2_iterator_next(&expected_it);
1871         }
1872
1873         if (rx_event) {
1874                 ast_test_status_update(test, "Received event:\n");
1875                 dump_event(test, rx_event);
1876                 ao2_cleanup(rx_event);
1877                 return -1;
1878         }
1879         if (ex_event) {
1880                 ast_test_status_update(test, "Expected event:\n");
1881                 dump_event(test, ex_event);
1882                 ao2_cleanup(ex_event);
1883                 return -1;
1884         }
1885         return 0;
1886 }
1887
1888 static struct ast_event *create_sync_event(void)
1889 {
1890         struct ast_event *event_dup;
1891         RAII_VAR(struct ast_event *, event, ao2_callback(cel_expected_events, 0, NULL, NULL), ao2_cleanup);
1892         uint16_t event_len;
1893
1894         if (!event) {
1895                 return NULL;
1896         }
1897
1898         event_len = ast_event_get_size(event);
1899
1900         event_dup = ast_calloc(1, event_len);
1901         if (!event_dup) {
1902                 return NULL;
1903         }
1904
1905         memcpy(event_dup, event, event_len);
1906         ast_event_append_ie_str(&event_dup, AST_EVENT_IE_SERVICE, "SYNC");
1907
1908         return event_dup;
1909 }
1910
1911 /*!
1912  * \internal
1913  * \brief Callback function called after each test executes.
1914  *
1915  * \details
1916  * In addition to cleanup, this function also performs verification
1917  * that the events received during a test match the events that were
1918  * expected to have been generated during the test.
1919  */
1920 static int cel_verify_and_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
1921 {
1922         struct ast_event *sync;
1923         RAII_VAR(struct ao2_container *, local_expected, cel_expected_events, ao2_cleanup);
1924         RAII_VAR(struct ao2_container *, local_received, cel_received_events, ao2_cleanup);
1925         ast_assert(event_sub != NULL);
1926         ast_assert(cel_received_events != NULL);
1927         ast_assert(cel_expected_events != NULL);
1928
1929         do_sleep();
1930
1931         /* sync with the event system */
1932         sync = create_sync_event();
1933         ast_test_validate(test, sync != NULL);
1934         if (ast_event_queue(sync)) {
1935                 ast_event_destroy(sync);
1936                 ast_test_validate(test, NULL);
1937         } else {
1938                 struct timeval start = ast_tvnow();
1939                 struct timespec end = {
1940                         .tv_sec = start.tv_sec + 15,
1941                         .tv_nsec = start.tv_usec * 1000
1942                 };
1943
1944                 SCOPED_MUTEX(lock, &sync_lock);
1945                 ast_cond_timedwait(&sync_out, &sync_lock, &end);
1946         }
1947
1948         /* stop the CEL event callback and clean up storage structures*/
1949         ast_event_unsubscribe(event_sub);
1950         event_sub = NULL;
1951
1952         /* cleaned up by RAII_VAR's */
1953         cel_expected_events = NULL;
1954         cel_received_events = NULL;
1955
1956         /* check events */
1957         ast_test_validate(test, !check_events(test, local_expected, local_received));
1958
1959         /* Restore the real CEL config */
1960         ast_cel_set_config(saved_config);
1961         ao2_cleanup(saved_config);
1962         saved_config = NULL;
1963
1964         /* clean up the locks */
1965         ast_mutex_destroy(&sync_lock);
1966         ast_mutex_destroy(&mid_test_sync_lock);
1967         ast_cond_destroy(&sync_out);
1968         return 0;
1969 }
1970
1971 static int unload_module(void)
1972 {
1973         AST_TEST_UNREGISTER(test_cel_channel_creation);
1974         AST_TEST_UNREGISTER(test_cel_unanswered_inbound_call);
1975         AST_TEST_UNREGISTER(test_cel_unanswered_outbound_call);
1976         AST_TEST_UNREGISTER(test_cel_single_party);
1977         AST_TEST_UNREGISTER(test_cel_single_bridge);
1978         AST_TEST_UNREGISTER(test_cel_single_bridge_continue);
1979         AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_a);
1980         AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_b);
1981         /*AST_TEST_UNREGISTER(test_cel_single_multiparty_bridge);*/
1982
1983         AST_TEST_UNREGISTER(test_cel_dial_unanswered);
1984         AST_TEST_UNREGISTER(test_cel_dial_congestion);
1985         AST_TEST_UNREGISTER(test_cel_dial_busy);
1986         AST_TEST_UNREGISTER(test_cel_dial_unavailable);
1987         AST_TEST_UNREGISTER(test_cel_dial_caller_cancel);
1988         AST_TEST_UNREGISTER(test_cel_dial_parallel_failed);
1989         AST_TEST_UNREGISTER(test_cel_dial_answer_no_bridge);
1990         AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_a);
1991         AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_b);
1992         /*AST_TEST_UNREGISTER(test_cel_dial_answer_multiparty);*/
1993
1994         AST_TEST_UNREGISTER(test_cel_blind_transfer);
1995         AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_swap);
1996         AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_merge);
1997         AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_link);
1998
1999         AST_TEST_UNREGISTER(test_cel_dial_pickup);
2000
2001         AST_TEST_UNREGISTER(test_cel_local_optimize);
2002
2003         ast_channel_unregister(&test_cel_chan_tech);
2004
2005         ao2_cleanup(cel_test_config);
2006         cel_test_config = NULL;
2007
2008         return 0;
2009 }
2010
2011 static int load_module(void)
2012 {
2013         /* build the test config */
2014         cel_test_config = ast_cel_general_config_alloc();
2015         if (!cel_test_config) {
2016                 return -1;
2017         }
2018         cel_test_config->enable = 1;
2019         if (ast_str_container_add(cel_test_config->apps, "dial")) {
2020                 return -1;
2021         }
2022         if (ast_str_container_add(cel_test_config->apps, "park")) {
2023                 return -1;
2024         }
2025         if (ast_str_container_add(cel_test_config->apps, "queue")) {
2026                 return -1;
2027         }
2028         cel_test_config->events |= 1<<AST_CEL_APP_START;
2029         cel_test_config->events |= 1<<AST_CEL_CHANNEL_START;
2030         cel_test_config->events |= 1<<AST_CEL_CHANNEL_END;
2031         cel_test_config->events |= 1<<AST_CEL_ANSWER;
2032         cel_test_config->events |= 1<<AST_CEL_HANGUP;
2033         cel_test_config->events |= 1<<AST_CEL_BRIDGE_START;
2034         cel_test_config->events |= 1<<AST_CEL_BRIDGE_END;
2035         cel_test_config->events |= 1<<AST_CEL_BRIDGE_TO_CONF;
2036         cel_test_config->events |= 1<<AST_CEL_CONF_ENTER;
2037         cel_test_config->events |= 1<<AST_CEL_CONF_EXIT;
2038         cel_test_config->events |= 1<<AST_CEL_BLINDTRANSFER;
2039         cel_test_config->events |= 1<<AST_CEL_ATTENDEDTRANSFER;
2040         cel_test_config->events |= 1<<AST_CEL_PICKUP;
2041         cel_test_config->events |= 1<<AST_CEL_LOCAL_OPTIMIZE;
2042
2043         ast_channel_register(&test_cel_chan_tech);
2044
2045         AST_TEST_REGISTER(test_cel_channel_creation);
2046         AST_TEST_REGISTER(test_cel_unanswered_inbound_call);
2047         AST_TEST_REGISTER(test_cel_unanswered_outbound_call);
2048
2049         AST_TEST_REGISTER(test_cel_single_party);
2050         AST_TEST_REGISTER(test_cel_single_bridge);
2051         AST_TEST_REGISTER(test_cel_single_bridge_continue);
2052         AST_TEST_REGISTER(test_cel_single_twoparty_bridge_a);
2053         AST_TEST_REGISTER(test_cel_single_twoparty_bridge_b);
2054         /*AST_TEST_REGISTER(test_cel_single_multiparty_bridge);*/
2055
2056         AST_TEST_REGISTER(test_cel_dial_unanswered);
2057         AST_TEST_REGISTER(test_cel_dial_congestion);
2058         AST_TEST_REGISTER(test_cel_dial_busy);
2059         AST_TEST_REGISTER(test_cel_dial_unavailable);
2060         AST_TEST_REGISTER(test_cel_dial_caller_cancel);
2061         AST_TEST_REGISTER(test_cel_dial_parallel_failed);
2062         AST_TEST_REGISTER(test_cel_dial_answer_no_bridge);
2063         AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_a);
2064         AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_b);
2065         /*AST_TEST_REGISTER(test_cel_dial_answer_multiparty);*/
2066
2067         AST_TEST_REGISTER(test_cel_blind_transfer);
2068         AST_TEST_REGISTER(test_cel_attended_transfer_bridges_swap);
2069         AST_TEST_REGISTER(test_cel_attended_transfer_bridges_merge);
2070         AST_TEST_REGISTER(test_cel_attended_transfer_bridges_link);
2071
2072         AST_TEST_REGISTER(test_cel_dial_pickup);
2073
2074         AST_TEST_REGISTER(test_cel_local_optimize);
2075
2076         /* ast_test_register_* has to happen after AST_TEST_REGISTER */
2077         /* Verify received vs expected events and clean things up after every test */
2078         ast_test_register_init(TEST_CATEGORY, test_cel_init_cb);
2079         ast_test_register_cleanup(TEST_CATEGORY, cel_verify_and_cleanup_cb);
2080
2081         return AST_MODULE_LOAD_SUCCESS;
2082 }
2083
2084 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CEL unit tests");