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