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