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