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