bridges/bridge_t38: Add a bridging module for managing T.38 state
[asterisk/asterisk.git] / tests / test_cdr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Matt Jordan <mjordan@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 CDR unit tests
22  *
23  * \author Matt Jordan <mjordan@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_REGISTER_FILE()
35
36 #include <math.h>
37 #include "asterisk/module.h"
38 #include "asterisk/test.h"
39 #include "asterisk/cdr.h"
40 #include "asterisk/linkedlists.h"
41 #include "asterisk/chanvars.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/causes.h"
44 #include "asterisk/time.h"
45 #include "asterisk/bridge.h"
46 #include "asterisk/bridge_basic.h"
47 #include "asterisk/stasis_channels.h"
48 #include "asterisk/stasis_bridges.h"
49 #include "asterisk/format_cache.h"
50
51 #define EPSILON 0.001
52
53 #define TEST_CATEGORY "/main/cdr/"
54
55 #define MOCK_CDR_BACKEND "mock_cdr_backend"
56
57 #define CHANNEL_TECH_NAME "CDRTestChannel"
58
59 /*! \brief A placeholder for Asterisk's 'real' CDR configuration */
60 static struct ast_cdr_config *saved_config;
61
62 /*! \brief A configuration suitable for 'normal' CDRs */
63 static struct ast_cdr_config debug_cdr_config = {
64         .settings.flags = CDR_ENABLED | CDR_DEBUG,
65 };
66
67 /*! \brief A configuration suitable for CDRs with unanswered records */
68 static struct ast_cdr_config unanswered_cdr_config = {
69         .settings.flags = CDR_ENABLED | CDR_UNANSWERED | CDR_DEBUG,
70 };
71
72 /*! \brief A configuration suitable for CDRs with congestion enabled */
73 static struct ast_cdr_config congestion_cdr_config = {
74         .settings.flags = CDR_ENABLED | CDR_UNANSWERED | CDR_DEBUG | CDR_CONGESTION,
75 };
76
77 /*! \brief Macro to swap a configuration out from the CDR engine. This should be
78  * used at the beginning of each test to set the needed configuration for that
79  * test.
80  */
81 #define SWAP_CONFIG(ao2_config, template) do { \
82         *(ao2_config) = (template); \
83         ast_cdr_set_config((ao2_config)); \
84         } while (0)
85
86 /*! \brief A linked list of received CDR entries from the engine */
87 static AST_LIST_HEAD(, test_cdr_entry) actual_cdr_entries = AST_LIST_HEAD_INIT_VALUE;
88
89 /*! \brief The Mock CDR backend condition wait */
90 static ast_cond_t mock_cdr_cond;
91
92 /*! \brief A channel technology used for the unit tests */
93 static struct ast_channel_tech test_cdr_chan_tech = {
94         .type = CHANNEL_TECH_NAME,
95         .description = "Mock channel technology for CDR tests",
96 };
97
98 struct test_cdr_entry {
99         struct ast_cdr *cdr;
100         AST_LIST_ENTRY(test_cdr_entry) list;
101 };
102
103 /*! \brief The number of CDRs the mock backend has received */
104 static int global_mock_cdr_count;
105
106 /*! \internal
107  * \brief Callback function for the mock CDR backend
108  *
109  * This function 'processes' a dispatched CDR record by adding it to the
110  * \ref actual_cdr_entries list. When a test completes, it can verify the
111  * expected records against this list of actual CDRs created by the engine.
112  *
113  * \param cdr The public CDR object created by the engine
114  *
115  * \retval -1 on error
116  * \retval 0 on success
117  */
118 static int mock_cdr_backend_cb(struct ast_cdr *cdr)
119 {
120         struct ast_cdr *cdr_copy, *cdr_prev = NULL;
121         struct ast_cdr *mock_cdr = NULL;
122         struct test_cdr_entry *cdr_wrapper;
123
124         cdr_wrapper = ast_calloc(1, sizeof(*cdr_wrapper));
125         if (!cdr_wrapper) {
126                 return -1;
127         }
128
129         for (; cdr; cdr = cdr->next) {
130                 struct ast_var_t *var_entry, *var_copy;
131
132                 cdr_copy = ast_calloc(1, sizeof(*cdr_copy));
133                 if (!cdr_copy) {
134                         return -1;
135                 }
136                 *cdr_copy = *cdr;
137                 cdr_copy->varshead.first = NULL;
138                 cdr_copy->varshead.last = NULL;
139                 cdr_copy->next = NULL;
140
141                 AST_LIST_TRAVERSE(&cdr->varshead, var_entry, entries) {
142                         var_copy = ast_var_assign(var_entry->name, var_entry->value);
143                         if (!var_copy) {
144                                 return -1;
145                         }
146                         AST_LIST_INSERT_TAIL(&cdr_copy->varshead, var_copy, entries);
147                 }
148
149                 if (!mock_cdr) {
150                         mock_cdr = cdr_copy;
151                 }
152                 if (cdr_prev) {
153                         cdr_prev->next = cdr_copy;
154                 }
155                 cdr_prev = cdr_copy;
156         }
157         cdr_wrapper->cdr = mock_cdr;
158
159         AST_LIST_LOCK(&actual_cdr_entries);
160         AST_LIST_INSERT_TAIL(&actual_cdr_entries, cdr_wrapper, list);
161         global_mock_cdr_count++;
162         ast_cond_signal(&mock_cdr_cond);
163         AST_LIST_UNLOCK(&actual_cdr_entries);
164
165         return 0;
166 }
167
168 /*! \internal
169  * \brief Remove all entries from \ref actual_cdr_entries
170  */
171 static void clear_mock_cdr_backend(void)
172 {
173         struct test_cdr_entry *cdr_wrapper;
174
175         AST_LIST_LOCK(&actual_cdr_entries);
176         while ((cdr_wrapper = AST_LIST_REMOVE_HEAD(&actual_cdr_entries, list))) {
177                 ast_cdr_free(cdr_wrapper->cdr);
178                 ast_free(cdr_wrapper);
179         }
180         global_mock_cdr_count = 0;
181         AST_LIST_UNLOCK(&actual_cdr_entries);
182 }
183
184 /*! \brief Verify a string field. This will set the test status result to fail;
185  * as such, it assumes that (a) test is the test object variable, and (b) that
186  * a return variable res exists.
187  */
188 #define VERIFY_STRING_FIELD(field, actual, expected) do { \
189         if (strcmp((actual)->field, (expected)->field)) { \
190                 ast_test_status_update(test, "Field %s failed: actual %s, expected %s\n", #field, (actual)->field, (expected)->field); \
191                 ast_test_set_result(test, AST_TEST_FAIL); \
192                 res = AST_TEST_FAIL; \
193         } } while (0)
194
195 /*! \brief Verify a numeric field. This will set the test status result to fail;
196  * as such, it assumes that (a) test is the test object variable, and (b) that
197  * a return variable res exists.
198  */
199 #define VERIFY_NUMERIC_FIELD(field, actual, expected) do { \
200         if ((actual)->field != (expected)->field) { \
201                 ast_test_status_update(test, "Field %s failed: actual %ld, expected %ld\n", #field, (long)(actual)->field, (long)(expected)->field); \
202                 ast_test_set_result(test, AST_TEST_FAIL); \
203                 res = AST_TEST_FAIL; \
204         } } while (0)
205
206 /*! \brief Verify a time field. This will set the test status result to fail;
207  * as such, it assumes that (a) test is the test object variable, and (b) that
208  * a return variable res exists.
209  */
210 #define VERIFY_TIME_VALUE(field, actual) do { \
211         if (ast_tvzero((actual)->field)) { \
212                 ast_test_status_update(test, "Field %s failed: should not be 0\n", #field); \
213                 ast_test_set_result(test, AST_TEST_FAIL); \
214                 res = AST_TEST_FAIL; \
215         } } while (0)
216
217 /*! \brief Alice's Caller ID */
218 #define ALICE_CALLERID { .id.name.str = "Alice", .id.name.valid = 1, .id.number.str = "100", .id.number.valid = 1, }
219
220 /*! \brief Bob's Caller ID */
221 #define BOB_CALLERID { .id.name.str = "Bob", .id.name.valid = 1, .id.number.str = "200", .id.number.valid = 1, }
222
223 /*! \brief Charlie's Caller ID */
224 #define CHARLIE_CALLERID { .id.name.str = "Charlie", .id.name.valid = 1, .id.number.str = "300", .id.number.valid = 1, }
225
226 /*! \brief David's Caller ID */
227 #define DAVID_CALLERID { .id.name.str = "David", .id.name.valid = 1, .id.number.str = "400", .id.number.valid = 1, }
228
229 /*! \brief Copy the linkedid and uniqueid from a channel to an expected CDR */
230 #define COPY_IDS(channel_var, expected_record) do { \
231         ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
232         ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
233         } while (0)
234
235 /*! \brief Set ulaw format on channel */
236 #define SET_FORMATS(chan) do {\
237         struct ast_format_cap *caps;\
238         caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);\
239         ast_format_cap_append(caps, ast_format_ulaw, 0);\
240         ast_channel_nativeformats_set((chan), caps);\
241         ast_channel_set_writeformat((chan), ast_format_ulaw);\
242         ast_channel_set_rawwriteformat((chan), ast_format_ulaw);\
243         ast_channel_set_readformat((chan), ast_format_ulaw);\
244         ast_channel_set_rawreadformat((chan), ast_format_ulaw);\
245         ao2_ref(caps, -1);\
246 } while (0)
247
248 /*! \brief Create a \ref test_cdr_chan_tech for Alice, and set the expected
249  * CDR records' linkedid and uniqueid. */
250 #define CREATE_ALICE_CHANNEL(channel_var, caller_id, expected_record) do { \
251         (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Alice"); \
252         SET_FORMATS((channel_var));\
253         ast_channel_set_caller((channel_var), (caller_id), NULL); \
254         ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
255         ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
256         ast_channel_unlock((channel_var)); \
257         } while (0)
258
259 /*! \brief Create a \ref test_cdr_chan_tech for Bob, and set the expected
260  * CDR records' linkedid and uniqueid. */
261 #define CREATE_BOB_CHANNEL(channel_var, caller_id, expected_record) do { \
262         (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "200", "Bob", "200", "200", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Bob"); \
263         SET_FORMATS((channel_var));\
264         ast_channel_set_caller((channel_var), (caller_id), NULL); \
265         ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
266         ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
267         ast_channel_unlock((channel_var)); \
268         } while (0)
269
270 /*! \brief Create a \ref test_cdr_chan_tech for Charlie, and set the expected
271  * CDR records' linkedid and uniqueid. */
272 #define CREATE_CHARLIE_CHANNEL(channel_var, caller_id, expected_record) do { \
273         (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "300", "Charlie", "300", "300", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Charlie"); \
274         SET_FORMATS((channel_var));\
275         ast_channel_set_caller((channel_var), (caller_id), NULL); \
276         ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
277         ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
278         ast_channel_unlock((channel_var)); \
279         } while (0)
280
281 /*! \brief Create a \ref test_cdr_chan_tech for Charlie, and set the expected
282  * CDR records' linkedid and uniqueid. */
283 #define CREATE_DAVID_CHANNEL(channel_var, caller_id, expected_record) do { \
284         (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "400", "David", "400", "400", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/David"); \
285         SET_FORMATS((channel_var));\
286         ast_channel_set_caller((channel_var), (caller_id), NULL); \
287         ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
288         ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
289         ast_channel_unlock((channel_var)); \
290         } while (0)
291
292 /*! \brief Emulate a channel entering into an application */
293 #define EMULATE_APP_DATA(channel, priority, application, data) do { \
294         if ((priority) > 0) { \
295                 ast_channel_priority_set((channel), (priority)); \
296         } \
297         ast_channel_lock((channel)); \
298         ast_channel_appl_set((channel), (application)); \
299         ast_channel_data_set((channel), (data)); \
300         ast_channel_publish_snapshot((channel)); \
301         ast_channel_unlock((channel)); \
302         } while (0)
303
304 /*! \brief Hang up a test channel safely */
305 #define HANGUP_CHANNEL(channel, cause) \
306         do { \
307                 ast_channel_hangupcause_set((channel), (cause)); \
308                 ast_hangup(channel); \
309                 channel = NULL; \
310         } while (0)
311
312 static enum ast_test_result_state verify_mock_cdr_record(struct ast_test *test, struct ast_cdr *expected, int record)
313 {
314         struct ast_cdr *actual = NULL;
315         struct test_cdr_entry *cdr_wrapper;
316         int count = 0;
317         struct timeval wait_now = ast_tvnow();
318         struct timespec wait_time = { .tv_sec = wait_now.tv_sec + 5, .tv_nsec = wait_now.tv_usec * 1000 };
319         enum ast_test_result_state res = AST_TEST_PASS;
320
321         while (count < record) {
322                 AST_LIST_LOCK(&actual_cdr_entries);
323                 if (global_mock_cdr_count < record) {
324                         ast_cond_timedwait(&mock_cdr_cond, &actual_cdr_entries.lock, &wait_time);
325                 }
326                 cdr_wrapper = AST_LIST_REMOVE_HEAD(&actual_cdr_entries, list);
327                 AST_LIST_UNLOCK(&actual_cdr_entries);
328
329                 if (!cdr_wrapper) {
330                         ast_test_status_update(test, "Unable to find actual CDR record at %d\n", count);
331                         return AST_TEST_FAIL;
332                 }
333                 actual = cdr_wrapper->cdr;
334
335                 if (!expected && actual) {
336                         ast_test_status_update(test, "CDRs recorded where no record expected\n");
337                         return AST_TEST_FAIL;
338                 }
339                 ast_test_debug(test, "Verifying expected record %s, %s\n",
340                         expected->channel, S_OR(expected->dstchannel, "<none>"));
341                 VERIFY_STRING_FIELD(accountcode, actual, expected);
342                 VERIFY_NUMERIC_FIELD(amaflags, actual, expected);
343                 VERIFY_STRING_FIELD(channel, actual, expected);
344                 VERIFY_STRING_FIELD(clid, actual, expected);
345                 VERIFY_STRING_FIELD(dcontext, actual, expected);
346                 VERIFY_NUMERIC_FIELD(disposition, actual, expected);
347                 VERIFY_STRING_FIELD(dst, actual, expected);
348                 VERIFY_STRING_FIELD(dstchannel, actual, expected);
349                 VERIFY_STRING_FIELD(lastapp, actual, expected);
350                 VERIFY_STRING_FIELD(lastdata, actual, expected);
351                 VERIFY_STRING_FIELD(linkedid, actual, expected);
352                 VERIFY_STRING_FIELD(peeraccount, actual, expected);
353                 VERIFY_STRING_FIELD(src, actual, expected);
354                 VERIFY_STRING_FIELD(uniqueid, actual, expected);
355                 VERIFY_STRING_FIELD(userfield, actual, expected);
356                 VERIFY_TIME_VALUE(start, actual);
357                 VERIFY_TIME_VALUE(end, actual);
358                 /* Note: there's no way we can really calculate a duration or
359                  * billsec - the unit tests are too short. However, if billsec is
360                  * non-zero in the expected, then make sure we have an answer time
361                  */
362                 if (expected->billsec) {
363                         VERIFY_TIME_VALUE(answer, actual);
364                 }
365                 ast_test_debug(test, "Finished expected record %s, %s\n",
366                                 expected->channel, S_OR(expected->dstchannel, "<none>"));
367                 expected = expected->next;
368                 ++count;
369         }
370         return res;
371 }
372
373 static void safe_channel_release(struct ast_channel *chan)
374 {
375         if (!chan) {
376                 return;
377         }
378         ast_channel_release(chan);
379 }
380
381 static void safe_bridge_destroy(struct ast_bridge *bridge)
382 {
383         if (!bridge) {
384                 return;
385         }
386         ast_bridge_destroy(bridge, 0);
387 }
388
389 static void do_sleep(struct timespec *to_sleep)
390 {
391         while ((nanosleep(to_sleep, to_sleep) == -1) && (errno == EINTR)) {
392         }
393 }
394
395 AST_TEST_DEFINE(test_cdr_channel_creation)
396 {
397         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
398         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
399                         ao2_cleanup);
400
401         struct ast_party_caller caller = ALICE_CALLERID;
402         struct ast_cdr expected = {
403                 .clid = "\"Alice\" <100>",
404                 .src = "100",
405                 .dst = "100",
406                 .dcontext = "default",
407                 .channel = CHANNEL_TECH_NAME "/Alice",
408                 .amaflags = AST_AMA_DOCUMENTATION,
409                 .disposition = AST_CDR_NOANSWER,
410                 .accountcode = "100",
411         };
412         enum ast_test_result_state result = AST_TEST_NOT_RUN;
413
414         switch (cmd) {
415         case TEST_INIT:
416                 info->name = __func__;
417                 info->category = TEST_CATEGORY;
418                 info->summary = "Test that a CDR is created when a channel is created";
419                 info->description =
420                         "Test that a CDR is created when a channel is created";
421                 return AST_TEST_NOT_RUN;
422         case TEST_EXECUTE:
423                 break;
424         }
425
426         SWAP_CONFIG(config, unanswered_cdr_config);
427
428         CREATE_ALICE_CHANNEL(chan, (&caller), &expected);
429
430         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
431
432         result = verify_mock_cdr_record(test, &expected, 1);
433
434         return result;
435 }
436
437 AST_TEST_DEFINE(test_cdr_unanswered_inbound_call)
438 {
439         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
440         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
441                         ao2_cleanup);
442
443         struct ast_party_caller caller = ALICE_CALLERID;
444         struct ast_cdr expected = {
445                 .clid = "\"Alice\" <100>",
446                 .src = "100",
447                 .dst = "100",
448                 .dcontext = "default",
449                 .channel = CHANNEL_TECH_NAME "/Alice",
450                 .lastapp = "Wait",
451                 .lastdata = "1",
452                 .amaflags = AST_AMA_DOCUMENTATION,
453                 .disposition = AST_CDR_NOANSWER,
454                 .accountcode = "100",
455         };
456         enum ast_test_result_state result = AST_TEST_NOT_RUN;
457
458         switch (cmd) {
459         case TEST_INIT:
460                 info->name = __func__;
461                 info->category = TEST_CATEGORY;
462                 info->summary = "Test inbound unanswered calls";
463                 info->description =
464                         "Test the properties of a CDR for a call that is\n"
465                         "inbound to Asterisk, executes some dialplan, but\n"
466                         "is never answered.";
467                 return AST_TEST_NOT_RUN;
468         case TEST_EXECUTE:
469                 break;
470         }
471
472         SWAP_CONFIG(config, unanswered_cdr_config);
473
474         CREATE_ALICE_CHANNEL(chan, &caller, &expected);
475
476         EMULATE_APP_DATA(chan, 1, "Wait", "1");
477
478         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
479
480         result = verify_mock_cdr_record(test, &expected, 1);
481
482         return result;
483 }
484
485 AST_TEST_DEFINE(test_cdr_unanswered_outbound_call)
486 {
487         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
488         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
489                         ao2_cleanup);
490
491         struct ast_party_caller caller = {
492                         .id.name.str = "",
493                         .id.name.valid = 1,
494                         .id.number.str = "",
495                         .id.number.valid = 1, };
496         struct ast_cdr expected = {
497                 .clid = "\"\" <>",
498                 .dst = "s",
499                 .dcontext = "default",
500                 .channel = CHANNEL_TECH_NAME "/Alice",
501                 .lastapp = "AppDial",
502                 .lastdata = "(Outgoing Line)",
503                 .amaflags = AST_AMA_DOCUMENTATION,
504                 .disposition = AST_CDR_NOANSWER,
505                 .accountcode = "100",
506         };
507         enum ast_test_result_state result = AST_TEST_NOT_RUN;
508
509         switch (cmd) {
510         case TEST_INIT:
511                 info->name = __func__;
512                 info->category = TEST_CATEGORY;
513                 info->summary = "Test outbound unanswered calls";
514                 info->description =
515                         "Test the properties of a CDR for a call that is\n"
516                         "outbound to Asterisk but is never answered.";
517                 return AST_TEST_NOT_RUN;
518         case TEST_EXECUTE:
519                 break;
520         }
521
522         SWAP_CONFIG(config, unanswered_cdr_config);
523
524         CREATE_ALICE_CHANNEL(chan, &caller, &expected);
525
526         ast_channel_exten_set(chan, "s");
527         ast_channel_context_set(chan, "default");
528         ast_set_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED);
529         EMULATE_APP_DATA(chan, 0, "AppDial", "(Outgoing Line)");
530         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
531
532         result = verify_mock_cdr_record(test, &expected, 1);
533
534         return result;
535 }
536
537 AST_TEST_DEFINE(test_cdr_outbound_bridged_call)
538 {
539         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
540         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
541         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
542         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
543                         ao2_cleanup);
544         struct timespec to_sleep = {1, 0};
545         enum ast_test_result_state result = AST_TEST_NOT_RUN;
546
547         struct ast_party_caller caller = ALICE_CALLERID;
548         struct ast_cdr alice_expected = {
549                 .clid = "\"Alice\" <100>",
550                 .src = "100",
551                 .dst = "100",
552                 .dcontext = "default",
553                 .channel = CHANNEL_TECH_NAME "/Alice",
554                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
555                 .lastapp = "",
556                 .lastdata = "",
557                 .amaflags = AST_AMA_DOCUMENTATION,
558                 .billsec = 1,
559                 .disposition = AST_CDR_ANSWERED,
560                 .accountcode = "100",
561                 .peeraccount = "200",
562         };
563         struct ast_cdr bob_expected = {
564                 .clid = "\"\" <>",
565                 .src = "",
566                 .dst = "s",
567                 .dcontext = "default",
568                 .channel = CHANNEL_TECH_NAME "/Bob",
569                 .dstchannel = "",
570                 .lastapp = "AppDial",
571                 .lastdata = "(Outgoing Line)",
572                 .amaflags = AST_AMA_DOCUMENTATION,
573                 .billsec = 1,
574                 .disposition = AST_CDR_ANSWERED,
575                 .accountcode = "200",
576                 .peeraccount = "",
577                 .next = &alice_expected,
578         };
579
580         switch (cmd) {
581         case TEST_INIT:
582                 info->name = __func__;
583                 info->category = TEST_CATEGORY;
584                 info->summary = "Test dialing, answering, and going into a 2-party bridge";
585                 info->description =
586                         "The most 'basic' of scenarios";
587                 return AST_TEST_NOT_RUN;
588         case TEST_EXECUTE:
589                 break;
590         }
591
592         SWAP_CONFIG(config, debug_cdr_config);
593
594         CREATE_ALICE_CHANNEL(chan_alice, &caller, &alice_expected);
595         ast_channel_state_set(chan_alice, AST_STATE_UP);
596
597         bridge = ast_bridge_basic_new();
598         ast_test_validate(test, bridge != NULL);
599         do_sleep(&to_sleep);
600
601         ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
602
603         chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_alice, 0, CHANNEL_TECH_NAME "/Bob");
604         SET_FORMATS(chan_bob);
605         ast_channel_unlock(chan_bob);
606         ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_bob), sizeof(bob_expected.linkedid));
607         ast_copy_string(bob_expected.uniqueid, ast_channel_uniqueid(chan_bob), sizeof(bob_expected.uniqueid));
608         ast_set_flag(ast_channel_flags(chan_bob), AST_FLAG_OUTGOING);
609         ast_set_flag(ast_channel_flags(chan_bob), AST_FLAG_ORIGINATED);
610         EMULATE_APP_DATA(chan_bob, 0, "AppDial", "(Outgoing Line)");
611
612         ast_channel_publish_dial(NULL, chan_bob, "Bob", NULL);
613         ast_channel_state_set(chan_bob, AST_STATE_RINGING);
614         ast_channel_publish_dial(NULL, chan_bob, NULL, "ANSWER");
615
616         ast_channel_state_set(chan_bob, AST_STATE_UP);
617
618         do_sleep(&to_sleep);
619
620         ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
621
622         do_sleep(&to_sleep);
623
624         ast_bridge_depart(chan_bob);
625         ast_bridge_depart(chan_alice);
626
627         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
628         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL);
629
630         result = verify_mock_cdr_record(test, &bob_expected, 2);
631         return result;
632 }
633
634
635 AST_TEST_DEFINE(test_cdr_single_party)
636 {
637         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
638         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
639                         ao2_cleanup);
640
641         struct ast_party_caller caller = ALICE_CALLERID;
642         struct ast_cdr expected = {
643                 .clid = "\"Alice\" <100>",
644                 .src = "100",
645                 .dst = "100",
646                 .dcontext = "default",
647                 .channel = CHANNEL_TECH_NAME "/Alice",
648                 .dstchannel = "",
649                 .lastapp = "VoiceMailMain",
650                 .lastdata = "1",
651                 .billsec = 1,
652         .amaflags = AST_AMA_DOCUMENTATION,
653                 .disposition = AST_CDR_ANSWERED,
654                 .accountcode = "100",
655         };
656         enum ast_test_result_state result = AST_TEST_NOT_RUN;
657
658         switch (cmd) {
659         case TEST_INIT:
660                 info->name = __func__;
661                 info->category = TEST_CATEGORY;
662                 info->summary = "Test cdrs for a single party";
663                 info->description =
664                         "Test the properties of a CDR for a call that is\n"
665                         "answered, but only involves a single channel";
666                 return AST_TEST_NOT_RUN;
667         case TEST_EXECUTE:
668                 break;
669         }
670         SWAP_CONFIG(config, debug_cdr_config);
671         CREATE_ALICE_CHANNEL(chan, &caller, &expected);
672
673         ast_channel_lock(chan);
674         EMULATE_APP_DATA(chan, 1, "Answer", "");
675         ast_setstate(chan, AST_STATE_UP);
676         EMULATE_APP_DATA(chan, 2, "VoiceMailMain", "1");
677         ast_channel_unlock(chan);
678
679         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
680
681         result = verify_mock_cdr_record(test, &expected, 1);
682
683         return result;
684 }
685
686 AST_TEST_DEFINE(test_cdr_single_bridge)
687 {
688         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
689         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
690         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
691                         ao2_cleanup);
692         struct timespec to_sleep = {1, 0};
693
694         struct ast_party_caller caller = ALICE_CALLERID;
695         struct ast_cdr expected = {
696                 .clid = "\"Alice\" <100>",
697                 .src = "100",
698                 .dst = "100",
699                 .dcontext = "default",
700                 .channel = CHANNEL_TECH_NAME "/Alice",
701                 .lastapp = "Bridge",
702                 .billsec = 1,
703                 .amaflags = AST_AMA_DOCUMENTATION,
704                 .disposition = AST_CDR_ANSWERED,
705                 .accountcode = "100",
706         };
707         enum ast_test_result_state result = AST_TEST_NOT_RUN;
708
709         switch (cmd) {
710         case TEST_INIT:
711                 info->name = __func__;
712                 info->category = TEST_CATEGORY;
713                 info->summary = "Test cdrs for a single party entering/leaving a bridge";
714                 info->description =
715                         "Test the properties of a CDR for a call that is\n"
716                         "answered, enters a bridge, and leaves it.";
717                 return AST_TEST_NOT_RUN;
718         case TEST_EXECUTE:
719                 break;
720         }
721         SWAP_CONFIG(config, debug_cdr_config);
722         CREATE_ALICE_CHANNEL(chan, &caller, &expected);
723
724         ast_channel_lock(chan);
725         EMULATE_APP_DATA(chan, 1, "Answer", "");
726         ast_setstate(chan, AST_STATE_UP);
727         EMULATE_APP_DATA(chan, 2, "Bridge", "");
728         ast_channel_unlock(chan);
729
730         bridge = ast_bridge_basic_new();
731         ast_test_validate(test, bridge != NULL);
732
733         do_sleep(&to_sleep);
734         ast_test_validate(test, !ast_bridge_impart(bridge, chan, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
735
736         do_sleep(&to_sleep);
737
738         ast_bridge_depart(chan);
739
740         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
741
742         result = verify_mock_cdr_record(test, &expected, 1);
743
744         return result;
745 }
746
747 AST_TEST_DEFINE(test_cdr_single_bridge_continue)
748 {
749         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
750         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
751         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
752                         ao2_cleanup);
753         struct timespec to_sleep = {1, 0};
754
755         struct ast_party_caller caller = ALICE_CALLERID;
756         struct ast_cdr expected_two = {
757                 .clid = "\"Alice\" <100>",
758                 .src = "100",
759                 .dst = "100",
760                 .dcontext = "default",
761                 .channel = CHANNEL_TECH_NAME "/Alice",
762                 .lastapp = "Wait",
763                 .billsec = 1,
764                 .amaflags = AST_AMA_DOCUMENTATION,
765                 .disposition = AST_CDR_ANSWERED,
766                 .accountcode = "100",
767         };
768         struct ast_cdr expected_one = {
769                 .clid = "\"Alice\" <100>",
770                 .src = "100",
771                 .dst = "100",
772                 .dcontext = "default",
773                 .channel = CHANNEL_TECH_NAME "/Alice",
774                 .lastapp = "Bridge",
775                 .billsec = 1,
776                 .amaflags = AST_AMA_DOCUMENTATION,
777                 .disposition = AST_CDR_ANSWERED,
778                 .accountcode = "100",
779                 .next = &expected_two,
780         };
781
782         enum ast_test_result_state result = AST_TEST_NOT_RUN;
783
784         switch (cmd) {
785         case TEST_INIT:
786                 info->name = __func__;
787                 info->category = TEST_CATEGORY;
788                 info->summary = "Test cdrs for a single party entering/leaving a bridge";
789                 info->description =
790                         "Test the properties of a CDR for a call that is\n"
791                         "answered, enters a bridge, and leaves it.";
792                 return AST_TEST_NOT_RUN;
793         case TEST_EXECUTE:
794                 break;
795         }
796         SWAP_CONFIG(config, debug_cdr_config);
797         CREATE_ALICE_CHANNEL(chan, &caller, &expected_one);
798         COPY_IDS(chan, &expected_two);
799
800         ast_channel_lock(chan);
801         EMULATE_APP_DATA(chan, 1, "Answer", "");
802         ast_setstate(chan, AST_STATE_UP);
803         EMULATE_APP_DATA(chan, 2, "Bridge", "");
804         ast_channel_unlock(chan);
805
806         bridge = ast_bridge_basic_new();
807         ast_test_validate(test, bridge != NULL);
808         do_sleep(&to_sleep);
809
810         ast_test_validate(test, !ast_bridge_impart(bridge, chan, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
811
812         do_sleep(&to_sleep);
813
814         ast_bridge_depart(chan);
815
816         EMULATE_APP_DATA(chan, 3, "Wait", "");
817
818         /* And then it hangs up */
819         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
820
821         result = verify_mock_cdr_record(test, &expected_one, 2);
822
823         return result;
824 }
825
826 AST_TEST_DEFINE(test_cdr_single_twoparty_bridge_a)
827 {
828         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
829         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
830         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
831         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
832                         ao2_cleanup);
833         struct timespec to_sleep = {1, 0};
834
835         struct ast_party_caller caller_alice = ALICE_CALLERID;
836         struct ast_party_caller caller_bob = BOB_CALLERID;
837         struct ast_cdr bob_expected = {
838                 .clid = "\"Bob\" <200>",
839                 .src = "200",
840                 .dst = "200",
841                 .dcontext = "default",
842                 .channel = CHANNEL_TECH_NAME "/Bob",
843                 .lastapp = "Bridge",
844                 .billsec = 1,
845                 .amaflags = AST_AMA_DOCUMENTATION,
846                 .disposition = AST_CDR_ANSWERED,
847                 .accountcode = "200",
848         };
849         struct ast_cdr alice_expected = {
850                 .clid = "\"Alice\" <100>",
851                 .src = "100",
852                 .dst = "100",
853                 .dcontext = "default",
854                 .channel = CHANNEL_TECH_NAME "/Alice",
855                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
856                 .lastapp = "Bridge",
857                 .billsec = 1,
858                 .amaflags = AST_AMA_DOCUMENTATION,
859                 .disposition = AST_CDR_ANSWERED,
860                 .accountcode = "100",
861                 .peeraccount = "200",
862                 .next = &bob_expected,
863         };
864
865         enum ast_test_result_state result = AST_TEST_NOT_RUN;
866
867         switch (cmd) {
868         case TEST_INIT:
869                 info->name = __func__;
870                 info->category = TEST_CATEGORY;
871                 info->summary = "Test cdrs for a single party entering/leaving a bridge";
872                 info->description =
873                         "Test the properties of a CDR for a call that is\n"
874                         "answered, enters a bridge, and leaves it. In this scenario, the\n"
875                         "Party A should answer the bridge first.";
876                 return AST_TEST_NOT_RUN;
877         case TEST_EXECUTE:
878                 break;
879         }
880         SWAP_CONFIG(config, debug_cdr_config);
881         CREATE_ALICE_CHANNEL(chan_alice, &caller_alice, &alice_expected);
882
883         CREATE_BOB_CHANNEL(chan_bob, &caller_bob, &bob_expected);
884         ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_alice), sizeof(bob_expected.linkedid));
885
886         ast_channel_lock(chan_alice);
887         EMULATE_APP_DATA(chan_alice, 1, "Answer", "");
888         ast_setstate(chan_alice, AST_STATE_UP);
889         EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
890         ast_channel_unlock(chan_alice);
891
892         bridge = ast_bridge_basic_new();
893         ast_test_validate(test, bridge != NULL);
894
895         ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
896         do_sleep(&to_sleep);
897
898         ast_channel_lock(chan_bob);
899         EMULATE_APP_DATA(chan_bob, 1, "Answer", "");
900         ast_setstate(chan_bob, AST_STATE_UP);
901         EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
902         ast_channel_unlock(chan_bob);
903
904         ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
905         do_sleep(&to_sleep);
906
907         ast_bridge_depart(chan_alice);
908         ast_bridge_depart(chan_bob);
909
910         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL);
911         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
912
913         result = verify_mock_cdr_record(test, &alice_expected, 2);
914
915         return result;
916 }
917
918 AST_TEST_DEFINE(test_cdr_single_twoparty_bridge_b)
919 {
920         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
921         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
922         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
923         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
924                         ao2_cleanup);
925         struct timespec to_sleep = {1, 0};
926
927         struct ast_party_caller caller_alice = ALICE_CALLERID;
928         struct ast_party_caller caller_bob = BOB_CALLERID;
929         struct ast_cdr bob_expected = {
930                 .clid = "\"Bob\" <200>",
931                 .src = "200",
932                 .dst = "200",
933                 .dcontext = "default",
934                 .channel = CHANNEL_TECH_NAME "/Bob",
935                 .lastapp = "Bridge",
936                 .billsec = 1,
937                 .amaflags = AST_AMA_DOCUMENTATION,
938                 .disposition = AST_CDR_ANSWERED,
939                 .accountcode = "200",
940         };
941         struct ast_cdr alice_expected = {
942                 .clid = "\"Alice\" <100>",
943                 .src = "100",
944                 .dst = "100",
945                 .dcontext = "default",
946                 .channel = CHANNEL_TECH_NAME "/Alice",
947                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
948                 .lastapp = "Bridge",
949                 .billsec = 1,
950                 .amaflags = AST_AMA_DOCUMENTATION,
951                 .disposition = AST_CDR_ANSWERED,
952                 .accountcode = "100",
953                 .peeraccount = "200",
954                 .next = &bob_expected,
955         };
956
957         enum ast_test_result_state result = AST_TEST_NOT_RUN;
958
959         switch (cmd) {
960         case TEST_INIT:
961                 info->name = __func__;
962                 info->category = TEST_CATEGORY;
963                 info->summary = "Test cdrs for a single party entering/leaving a bridge";
964                 info->description =
965                         "Test the properties of a CDR for a call that is\n"
966                         "answered, enters a bridge, and leaves it. In this scenario, the\n"
967                         "Party B should answer the bridge first.";
968                 return AST_TEST_NOT_RUN;
969         case TEST_EXECUTE:
970                 break;
971         }
972         SWAP_CONFIG(config, debug_cdr_config);
973         CREATE_ALICE_CHANNEL(chan_alice, &caller_alice, &alice_expected);
974
975         CREATE_BOB_CHANNEL(chan_bob, &caller_bob, &bob_expected);
976         ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_alice), sizeof(bob_expected.linkedid));
977
978         ast_channel_unlock(chan_alice);
979         EMULATE_APP_DATA(chan_alice, 1, "Answer", "");
980         ast_setstate(chan_alice, AST_STATE_UP);
981         EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
982         ast_channel_unlock(chan_alice);
983
984         bridge = ast_bridge_basic_new();
985         ast_test_validate(test, bridge != NULL);
986
987         ast_channel_lock(chan_bob);
988         EMULATE_APP_DATA(chan_bob, 1, "Answer", "");
989         ast_setstate(chan_bob, AST_STATE_UP);
990         EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
991         ast_channel_unlock(chan_bob);
992         do_sleep(&to_sleep);
993
994         ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
995         do_sleep(&to_sleep);
996
997         ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
998         do_sleep(&to_sleep);
999
1000         ast_bridge_depart(chan_alice);
1001         ast_bridge_depart(chan_bob);
1002
1003         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL);
1004         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
1005
1006         result = verify_mock_cdr_record(test, &alice_expected, 2);
1007
1008         return result;
1009 }
1010
1011 AST_TEST_DEFINE(test_cdr_single_multiparty_bridge)
1012 {
1013         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1014         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1015         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1016         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
1017         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1018                         ao2_cleanup);
1019         struct timespec to_sleep = {1, 0};
1020
1021         struct ast_party_caller caller_alice = ALICE_CALLERID;
1022         struct ast_party_caller caller_bob = BOB_CALLERID;
1023         struct ast_party_caller caller_charlie = CHARLIE_CALLERID;
1024         struct ast_cdr charlie_expected = {
1025                 .clid = "\"Charlie\" <300>",
1026                 .src = "300",
1027                 .dst = "300",
1028                 .dcontext = "default",
1029                 .channel = CHANNEL_TECH_NAME "/Charlie",
1030                 .lastapp = "Bridge",
1031                 .billsec = 1,
1032                 .amaflags = AST_AMA_DOCUMENTATION,
1033                 .disposition = AST_CDR_ANSWERED,
1034                 .accountcode = "300",
1035         };
1036         struct ast_cdr bob_expected = {
1037                 .clid = "\"Bob\" <200>",
1038                 .src = "200",
1039                 .dst = "200",
1040                 .dcontext = "default",
1041                 .channel = CHANNEL_TECH_NAME "/Bob",
1042                 .dstchannel = CHANNEL_TECH_NAME "/Charlie",
1043                 .lastapp = "Bridge",
1044                 .billsec = 1,
1045                 .amaflags = AST_AMA_DOCUMENTATION,
1046                 .disposition = AST_CDR_ANSWERED,
1047                 .accountcode = "200",
1048                 .peeraccount = "300",
1049                 .next = &charlie_expected,
1050         };
1051         struct ast_cdr alice_expected_two = {
1052                 .clid = "\"Alice\" <100>",
1053                 .src = "100",
1054                 .dst = "100",
1055                 .dcontext = "default",
1056                 .channel = CHANNEL_TECH_NAME "/Alice",
1057                 .dstchannel = CHANNEL_TECH_NAME "/Charlie",
1058                 .lastapp = "Bridge",
1059                 .billsec = 1,
1060                 .amaflags = AST_AMA_DOCUMENTATION,
1061                 .disposition = AST_CDR_ANSWERED,
1062                 .accountcode = "100",
1063                 .peeraccount = "300",
1064                 .next = &bob_expected,
1065         };
1066         struct ast_cdr alice_expected_one = {
1067                 .clid = "\"Alice\" <100>",
1068                 .src = "100",
1069                 .dst = "100",
1070                 .dcontext = "default",
1071                 .channel = CHANNEL_TECH_NAME "/Alice",
1072                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1073                 .lastapp = "Bridge",
1074                 .billsec = 1,
1075                 .amaflags = AST_AMA_DOCUMENTATION,
1076                 .disposition = AST_CDR_ANSWERED,
1077                 .accountcode = "100",
1078                 .peeraccount = "200",
1079                 .next = &alice_expected_two,
1080         };
1081
1082         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1083
1084         switch (cmd) {
1085         case TEST_INIT:
1086                 info->name = __func__;
1087                 info->category = TEST_CATEGORY;
1088                 info->summary = "Test cdrs for a single party entering/leaving a multi-party bridge";
1089                 info->description =
1090                         "Test the properties of a CDR for a call that is\n"
1091                         "answered, enters a bridge, and leaves it. A total of three\n"
1092                         "parties perform this action.";
1093                 return AST_TEST_NOT_RUN;
1094         case TEST_EXECUTE:
1095                 break;
1096         }
1097         SWAP_CONFIG(config, debug_cdr_config);
1098         CREATE_ALICE_CHANNEL(chan_alice, &caller_alice, &alice_expected_one);
1099         COPY_IDS(chan_alice, &alice_expected_two);
1100         CREATE_BOB_CHANNEL(chan_bob, &caller_bob, &bob_expected);
1101         ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_alice), sizeof(bob_expected.linkedid));
1102         CREATE_CHARLIE_CHANNEL(chan_charlie, &caller_charlie, &charlie_expected);
1103         ast_copy_string(charlie_expected.linkedid, ast_channel_linkedid(chan_alice), sizeof(charlie_expected.linkedid));
1104
1105         ast_channel_lock(chan_alice);
1106         EMULATE_APP_DATA(chan_alice, 1, "Answer", "");
1107         ast_setstate(chan_alice, AST_STATE_UP);
1108         EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
1109         ast_channel_unlock(chan_alice);
1110
1111         bridge = ast_bridge_basic_new();
1112         ast_test_validate(test, bridge != NULL);
1113         do_sleep(&to_sleep);
1114
1115         ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
1116
1117         ast_channel_lock(chan_bob);
1118         EMULATE_APP_DATA(chan_bob, 1, "Answer", "");
1119         ast_setstate(chan_bob, AST_STATE_UP);
1120         EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
1121         ast_channel_unlock(chan_bob);
1122         do_sleep(&to_sleep);
1123
1124         ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
1125
1126         do_sleep(&to_sleep);
1127
1128         ast_channel_lock(chan_charlie);
1129         EMULATE_APP_DATA(chan_charlie, 1, "Answer", "");
1130         ast_setstate(chan_charlie, AST_STATE_UP);
1131         EMULATE_APP_DATA(chan_charlie, 2, "Bridge", "");
1132         ast_channel_unlock(chan_charlie);
1133         ast_test_validate(test, !ast_bridge_impart(bridge, chan_charlie, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
1134
1135         do_sleep(&to_sleep);
1136
1137         ast_bridge_depart(chan_alice);
1138         ast_bridge_depart(chan_bob);
1139         ast_bridge_depart(chan_charlie);
1140
1141         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL);
1142         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
1143         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL);
1144
1145         result = verify_mock_cdr_record(test, &alice_expected_one, 4);
1146
1147         return result;
1148 }
1149
1150 AST_TEST_DEFINE(test_cdr_dial_unanswered)
1151 {
1152         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1153         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1154         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1155                         ao2_cleanup);
1156
1157         struct ast_party_caller caller = ALICE_CALLERID;
1158         struct ast_cdr expected = {
1159                 .clid = "\"Alice\" <100>",
1160                 .src = "100",
1161                 .dst = "100",
1162                 .dcontext = "default",
1163                 .channel = CHANNEL_TECH_NAME "/Alice",
1164                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1165                 .lastapp = "Dial",
1166                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1167                 .billsec = 0,
1168                 .amaflags = AST_AMA_DOCUMENTATION,
1169                 .disposition = AST_CDR_NOANSWER,
1170                 .accountcode = "100",
1171                 .peeraccount = "200",
1172         };
1173         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1174
1175         switch (cmd) {
1176         case TEST_INIT:
1177                 info->name = __func__;
1178                 info->category = TEST_CATEGORY;
1179                 info->summary = "Test CDRs for a dial that isn't answered";
1180                 info->description =
1181                         "Test the properties of a CDR for a channel that\n"
1182                         "performs a dial operation that isn't answered";
1183                 return AST_TEST_NOT_RUN;
1184         case TEST_EXECUTE:
1185                 break;
1186         }
1187
1188         SWAP_CONFIG(config, unanswered_cdr_config);
1189
1190         CREATE_ALICE_CHANNEL(chan_caller, &caller, &expected);
1191
1192         EMULATE_APP_DATA(chan_caller, 1, "Dial", "CDRTestChannel/Bob");
1193
1194         chan_callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/Bob");
1195         SET_FORMATS(chan_callee);
1196         ast_channel_unlock(chan_callee);
1197         ast_set_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
1198         EMULATE_APP_DATA(chan_callee, 0, "AppDial", "(Outgoing Line)");
1199
1200         ast_channel_publish_dial(chan_caller, chan_callee, "Bob", NULL);
1201         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1202         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOANSWER");
1203
1204         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ANSWER);
1205         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ANSWER);
1206
1207         result = verify_mock_cdr_record(test, &expected, 1);
1208
1209         return result;
1210 }
1211
1212
1213 AST_TEST_DEFINE(test_cdr_dial_busy)
1214 {
1215         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1216         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1217         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1218                         ao2_cleanup);
1219
1220         struct ast_party_caller caller = ALICE_CALLERID;
1221         struct ast_cdr expected = {
1222                 .clid = "\"Alice\" <100>",
1223                 .src = "100",
1224                 .dst = "100",
1225                 .dcontext = "default",
1226                 .channel = CHANNEL_TECH_NAME "/Alice",
1227                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1228                 .lastapp = "Dial",
1229                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1230                 .billsec = 0,
1231                 .amaflags = AST_AMA_DOCUMENTATION,
1232                 .disposition = AST_CDR_BUSY,
1233                 .accountcode = "100",
1234                 .peeraccount = "200",
1235         };
1236         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1237
1238         switch (cmd) {
1239         case TEST_INIT:
1240                 info->name = __func__;
1241                 info->category = TEST_CATEGORY;
1242                 info->summary = "Test CDRs for a dial that results in a busy";
1243                 info->description =
1244                         "Test the properties of a CDR for a channel that\n"
1245                         "performs a dial operation to an endpoint that's busy";
1246                 return AST_TEST_NOT_RUN;
1247         case TEST_EXECUTE:
1248                 break;
1249         }
1250
1251         SWAP_CONFIG(config, unanswered_cdr_config);
1252
1253         CREATE_ALICE_CHANNEL(chan_caller, &caller, &expected);
1254
1255         EMULATE_APP_DATA(chan_caller, 1, "Dial", CHANNEL_TECH_NAME "/Bob");
1256
1257         chan_callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/Bob");
1258         SET_FORMATS(chan_callee);
1259         ast_channel_unlock(chan_callee);
1260         ast_set_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
1261         EMULATE_APP_DATA(chan_callee, 0, "AppDial", "(Outgoing Line)");
1262
1263         ast_channel_publish_dial(chan_caller, chan_callee, "Bob", NULL);
1264         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1265         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "BUSY");
1266
1267         HANGUP_CHANNEL(chan_caller, AST_CAUSE_BUSY);
1268         HANGUP_CHANNEL(chan_callee, AST_CAUSE_BUSY);
1269
1270         result = verify_mock_cdr_record(test, &expected, 1);
1271
1272         return result;
1273 }
1274
1275 AST_TEST_DEFINE(test_cdr_dial_congestion)
1276 {
1277         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1278         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1279         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1280                         ao2_cleanup);
1281
1282         struct ast_party_caller caller = ALICE_CALLERID;
1283         struct ast_cdr expected = {
1284                 .clid = "\"Alice\" <100>",
1285                 .src = "100",
1286                 .dst = "100",
1287                 .dcontext = "default",
1288                 .channel = CHANNEL_TECH_NAME "/Alice",
1289                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1290                 .lastapp = "Dial",
1291                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1292                 .billsec = 0,
1293                 .amaflags = AST_AMA_DOCUMENTATION,
1294                 .disposition = AST_CDR_CONGESTION,
1295                 .accountcode = "100",
1296                 .peeraccount = "200",
1297         };
1298         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1299
1300         switch (cmd) {
1301         case TEST_INIT:
1302                 info->name = __func__;
1303                 info->category = TEST_CATEGORY;
1304                 info->summary = "Test CDRs for a dial that results in congestion";
1305                 info->description =
1306                         "Test the properties of a CDR for a channel that\n"
1307                         "performs a dial operation to an endpoint that's congested";
1308                 return AST_TEST_NOT_RUN;
1309         case TEST_EXECUTE:
1310                 break;
1311         }
1312
1313         SWAP_CONFIG(config, congestion_cdr_config);
1314
1315         CREATE_ALICE_CHANNEL(chan_caller, &caller, &expected);
1316
1317         EMULATE_APP_DATA(chan_caller, 1, "Dial", CHANNEL_TECH_NAME "/Bob");
1318
1319         chan_callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/Bob");
1320         SET_FORMATS(chan_callee);
1321         ast_channel_unlock(chan_callee);
1322         ast_set_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
1323         EMULATE_APP_DATA(chan_callee, 0, "AppDial", "(Outgoing Line)");
1324
1325         ast_channel_publish_dial(chan_caller, chan_callee, "Bob", NULL);
1326         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1327         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CONGESTION");
1328
1329         HANGUP_CHANNEL(chan_caller, AST_CAUSE_CONGESTION);
1330         HANGUP_CHANNEL(chan_callee, AST_CAUSE_CONGESTION);
1331
1332         result = verify_mock_cdr_record(test, &expected, 1);
1333
1334         return result;
1335 }
1336
1337 AST_TEST_DEFINE(test_cdr_dial_unavailable)
1338 {
1339         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1340         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1341         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1342                         ao2_cleanup);
1343
1344         struct ast_party_caller caller = ALICE_CALLERID;
1345         struct ast_cdr expected = {
1346                 .clid = "\"Alice\" <100>",
1347                 .src = "100",
1348                 .dst = "100",
1349                 .dcontext = "default",
1350                 .channel = CHANNEL_TECH_NAME "/Alice",
1351                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1352                 .lastapp = "Dial",
1353                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1354                 .billsec = 0,
1355                 .amaflags = AST_AMA_DOCUMENTATION,
1356                 .disposition = AST_CDR_FAILED,
1357                 .accountcode = "100",
1358                 .peeraccount = "200",
1359         };
1360         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1361
1362         switch (cmd) {
1363         case TEST_INIT:
1364                 info->name = __func__;
1365                 info->category = TEST_CATEGORY;
1366                 info->summary = "Test CDRs for a dial that results in unavailable";
1367                 info->description =
1368                         "Test the properties of a CDR for a channel that\n"
1369                         "performs a dial operation to an endpoint that's unavailable";
1370                 return AST_TEST_NOT_RUN;
1371         case TEST_EXECUTE:
1372                 break;
1373         }
1374
1375         SWAP_CONFIG(config, unanswered_cdr_config);
1376
1377         CREATE_ALICE_CHANNEL(chan_caller, &caller, &expected);
1378
1379         EMULATE_APP_DATA(chan_caller, 1, "Dial", CHANNEL_TECH_NAME "/Bob");
1380
1381         chan_callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/Bob");
1382         SET_FORMATS(chan_callee);
1383         ast_channel_unlock(chan_callee);
1384         ast_set_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
1385         EMULATE_APP_DATA(chan_callee, 0, "AppDial", "(Outgoing Line)");
1386
1387         ast_channel_publish_dial(chan_caller, chan_callee, "Bob", NULL);
1388         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1389         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CHANUNAVAIL");
1390
1391         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ROUTE_DESTINATION);
1392         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ROUTE_DESTINATION);
1393
1394         result = verify_mock_cdr_record(test, &expected, 1);
1395
1396         return result;
1397 }
1398
1399 AST_TEST_DEFINE(test_cdr_dial_caller_cancel)
1400 {
1401         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1402         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1403         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1404                         ao2_cleanup);
1405
1406         struct ast_party_caller caller = ALICE_CALLERID;
1407         struct ast_cdr expected = {
1408                 .clid = "\"Alice\" <100>",
1409                 .src = "100",
1410                 .dst = "100",
1411                 .dcontext = "default",
1412                 .channel = CHANNEL_TECH_NAME "/Alice",
1413                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1414                 .lastapp = "Dial",
1415                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1416                 .billsec = 0,
1417                 .amaflags = AST_AMA_DOCUMENTATION,
1418                 .disposition = AST_CDR_NOANSWER,
1419                 .accountcode = "100",
1420                 .peeraccount = "200",
1421         };
1422         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1423
1424         switch (cmd) {
1425         case TEST_INIT:
1426                 info->name = __func__;
1427                 info->category = TEST_CATEGORY;
1428                 info->summary = "Test CDRs for a dial where the caller cancels";
1429                 info->description =
1430                         "Test the properties of a CDR for a channel that\n"
1431                         "performs a dial operation to an endpoint but then decides\n"
1432                         "to hang up, cancelling the dial";
1433                 return AST_TEST_NOT_RUN;
1434         case TEST_EXECUTE:
1435                 break;
1436         }
1437
1438         SWAP_CONFIG(config, unanswered_cdr_config);
1439
1440         CREATE_ALICE_CHANNEL(chan_caller, &caller, &expected);
1441
1442         EMULATE_APP_DATA(chan_caller, 1, "Dial", CHANNEL_TECH_NAME "/Bob");
1443
1444         chan_callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/Bob");
1445         SET_FORMATS(chan_callee);
1446         ast_channel_unlock(chan_callee);
1447         ast_set_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
1448         EMULATE_APP_DATA(chan_callee, 0, "AppDial", "(Outgoing Line)");
1449
1450         ast_channel_publish_dial(chan_caller, chan_callee, "Bob", NULL);
1451         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1452         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CANCEL");
1453
1454         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL);
1455         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL);
1456
1457         result = verify_mock_cdr_record(test, &expected, 1);
1458
1459         return result;
1460 }
1461
1462 AST_TEST_DEFINE(test_cdr_dial_parallel_failed)
1463 {
1464         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1465         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1466         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1467         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1468         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1469                         ao2_cleanup);
1470
1471         struct ast_party_caller caller = ALICE_CALLERID;
1472         struct ast_cdr bob_expected = {
1473                 .clid = "\"Alice\" <100>",
1474                 .src = "100",
1475                 .dst = "100",
1476                 .dcontext = "default",
1477                 .channel = CHANNEL_TECH_NAME "/Alice",
1478                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1479                 .lastapp = "Dial",
1480                 .lastdata = CHANNEL_TECH_NAME "/Bob&" CHANNEL_TECH_NAME "/Charlie&" CHANNEL_TECH_NAME "/David",
1481                 .billsec = 0,
1482                 .amaflags = AST_AMA_DOCUMENTATION,
1483                 .disposition = AST_CDR_NOANSWER,
1484                 .accountcode = "100",
1485                 .peeraccount = "200",
1486         };
1487         struct ast_cdr charlie_expected = {
1488                 .clid = "\"Alice\" <100>",
1489                 .src = "100",
1490                 .dst = "100",
1491                 .dcontext = "default",
1492                 .channel = CHANNEL_TECH_NAME "/Alice",
1493                 .dstchannel = CHANNEL_TECH_NAME "/Charlie",
1494                 .lastapp = "Dial",
1495                 .lastdata = CHANNEL_TECH_NAME "/Bob&" CHANNEL_TECH_NAME "/Charlie&" CHANNEL_TECH_NAME "/David",
1496                 .billsec = 0,
1497                 .amaflags = AST_AMA_DOCUMENTATION,
1498                 .disposition = AST_CDR_BUSY,
1499                 .accountcode = "100",
1500                 .peeraccount = "300",
1501         };
1502         struct ast_cdr david_expected = {
1503                 .clid = "\"Alice\" <100>",
1504                 .src = "100",
1505                 .dst = "100",
1506                 .dcontext = "default",
1507                 .channel = CHANNEL_TECH_NAME "/Alice",
1508                 .dstchannel = CHANNEL_TECH_NAME "/David",
1509                 .lastapp = "Dial",
1510                 .lastdata = CHANNEL_TECH_NAME "/Bob&" CHANNEL_TECH_NAME "/Charlie&" CHANNEL_TECH_NAME "/David",
1511                 .billsec = 0,
1512                 .amaflags = AST_AMA_DOCUMENTATION,
1513                 .disposition = AST_CDR_CONGESTION,
1514                 .accountcode = "100",
1515                 .peeraccount = "400",
1516         };
1517         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1518
1519         struct ast_cdr *expected = &bob_expected;
1520         bob_expected.next = &charlie_expected;
1521         charlie_expected.next = &david_expected;
1522
1523         switch (cmd) {
1524         case TEST_INIT:
1525                 info->name = __func__;
1526                 info->category = TEST_CATEGORY;
1527                 info->summary = "Test a parallel dial where all channels fail to answer";
1528                 info->description =
1529                         "This tests dialing three parties: Bob, Charlie, David. Charlie\n"
1530                         "returns BUSY; David returns CONGESTION; Bob fails to answer and\n"
1531                         "Alice hangs up. Three records are created for Alice as a result.";
1532                 return AST_TEST_NOT_RUN;
1533         case TEST_EXECUTE:
1534                 break;
1535         }
1536
1537         SWAP_CONFIG(config, congestion_cdr_config);
1538
1539         CREATE_ALICE_CHANNEL(chan_caller, &caller, &bob_expected);
1540         COPY_IDS(chan_caller, &charlie_expected);
1541         COPY_IDS(chan_caller, &david_expected);
1542
1543         /* Channel enters Dial app */
1544         EMULATE_APP_DATA(chan_caller, 1, "Dial", CHANNEL_TECH_NAME "/Bob&" CHANNEL_TECH_NAME "/Charlie&" CHANNEL_TECH_NAME "/David");
1545
1546         /* Outbound channels are created */
1547         chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/Bob");
1548         SET_FORMATS(chan_bob);
1549         ast_channel_unlock(chan_bob);
1550         ast_set_flag(ast_channel_flags(chan_bob), AST_FLAG_OUTGOING);
1551         EMULATE_APP_DATA(chan_bob, 0, "AppDial", "(Outgoing Line)");
1552
1553         chan_charlie = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "300", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/Charlie");
1554         SET_FORMATS(chan_charlie);
1555         ast_channel_unlock(chan_charlie);
1556         ast_set_flag(ast_channel_flags(chan_charlie), AST_FLAG_OUTGOING);
1557         EMULATE_APP_DATA(chan_charlie, 0, "AppDial", "(Outgoing Line)");
1558
1559         chan_david = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "400", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/David");
1560         SET_FORMATS(chan_charlie);
1561         ast_channel_unlock(chan_david);
1562         ast_set_flag(ast_channel_flags(chan_david), AST_FLAG_OUTGOING);
1563         EMULATE_APP_DATA(chan_david, 0, "AppDial", "(Outgoing Line)");
1564
1565         /* Dial starts */
1566         ast_channel_publish_dial(chan_caller, chan_bob, "Bob", NULL);
1567         ast_channel_publish_dial(chan_caller, chan_charlie, "Charlie", NULL);
1568         ast_channel_publish_dial(chan_caller, chan_david, "David", NULL);
1569         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1570
1571         /* Charlie is busy */
1572         ast_channel_publish_dial(chan_caller, chan_charlie, NULL, "BUSY");
1573         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_BUSY);
1574
1575         /* David is congested */
1576         ast_channel_publish_dial(chan_caller, chan_david, NULL, "CONGESTION");
1577         HANGUP_CHANNEL(chan_david, AST_CAUSE_CONGESTION);
1578
1579         /* Bob is canceled */
1580         ast_channel_publish_dial(chan_caller, chan_bob, NULL, "CANCEL");
1581         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
1582
1583         /* Alice hangs up */
1584         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL);
1585
1586         result = verify_mock_cdr_record(test, expected, 3);
1587
1588         return result;
1589 }
1590
1591 AST_TEST_DEFINE(test_cdr_dial_answer_no_bridge)
1592 {
1593         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1594         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1595         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1596                         ao2_cleanup);
1597
1598         struct ast_party_caller caller = ALICE_CALLERID;
1599         struct ast_cdr bob_expected_one = {
1600                 .clid = "\"\" <>",
1601                 .src = "",
1602                 .dst = "s",
1603                 .dcontext = "default",
1604                 .channel = CHANNEL_TECH_NAME "/Bob",
1605                 .lastapp = "Wait",
1606                 .lastdata = "1",
1607                 .amaflags = AST_AMA_DOCUMENTATION,
1608                 .disposition = AST_CDR_ANSWERED,
1609                 .accountcode = "200",
1610         };
1611         struct ast_cdr alice_expected_two = {
1612                 .clid = "\"Alice\" <100>",
1613                 .src = "100",
1614                 .dst = "100",
1615                 .dcontext = "default",
1616                 .channel = CHANNEL_TECH_NAME "/Alice",
1617                 .lastapp = "Wait",
1618                 .lastdata = "1",
1619                 .amaflags = AST_AMA_DOCUMENTATION,
1620                 .disposition = AST_CDR_ANSWERED,
1621                 .accountcode = "100",
1622                 .next = &bob_expected_one,
1623         };
1624         struct ast_cdr alice_expected_one = {
1625                 .clid = "\"Alice\" <100>",
1626                 .src = "100",
1627                 .dst = "100",
1628                 .dcontext = "default",
1629                 .channel = CHANNEL_TECH_NAME "/Alice",
1630                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1631                 .lastapp = "Dial",
1632                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1633                 .amaflags = AST_AMA_DOCUMENTATION,
1634                 .disposition = AST_CDR_ANSWERED,
1635                 .accountcode = "100",
1636                 .peeraccount = "200",
1637                 .next = &alice_expected_two,
1638         };
1639         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1640
1641         switch (cmd) {
1642         case TEST_INIT:
1643                 info->name = __func__;
1644                 info->category = TEST_CATEGORY;
1645                 info->summary = "Test dialing, answering, and not going into a bridge.";
1646                 info->description =
1647                         "This is a weird one, but theoretically possible. You can perform\n"
1648                         "a dial, then bounce both channels to different priorities and\n"
1649                         "never have them enter a bridge together. Ew. This makes sure that\n"
1650                         "when we answer, we get a CDR, it gets ended at that point, and\n"
1651                         "that it gets finalized appropriately. We should get three CDRs in\n"
1652                         "the end - one for the dial, and one for each CDR as they continued\n"
1653                         "on.";
1654                 return AST_TEST_NOT_RUN;
1655         case TEST_EXECUTE:
1656                 break;
1657         }
1658
1659         SWAP_CONFIG(config, debug_cdr_config);
1660
1661         CREATE_ALICE_CHANNEL(chan_caller, &caller, &alice_expected_one);
1662         COPY_IDS(chan_caller, &alice_expected_two);
1663
1664         EMULATE_APP_DATA(chan_caller, 1, "Dial", CHANNEL_TECH_NAME "/Bob");
1665
1666         chan_callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/Bob");
1667         SET_FORMATS(chan_callee);
1668         ast_channel_unlock(chan_callee);
1669         ast_set_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
1670         COPY_IDS(chan_callee, &bob_expected_one);
1671
1672         ast_channel_publish_dial(chan_caller, chan_callee, "Bob", NULL);
1673         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1674         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1675
1676         ast_channel_state_set(chan_caller, AST_STATE_UP);
1677         ast_clear_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
1678         ast_channel_state_set(chan_callee, AST_STATE_UP);
1679
1680         EMULATE_APP_DATA(chan_caller, 2, "Wait", "1");
1681         EMULATE_APP_DATA(chan_callee, 1, "Wait", "1");
1682
1683         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL);
1684         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL);
1685
1686         result = verify_mock_cdr_record(test, &alice_expected_one, 3);
1687         return result;
1688 }
1689
1690 AST_TEST_DEFINE(test_cdr_dial_answer_twoparty_bridge_a)
1691 {
1692         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1693         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1694         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
1695         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1696                         ao2_cleanup);
1697         struct timespec to_sleep = {1, 0};
1698         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1699
1700         struct ast_party_caller caller = ALICE_CALLERID;
1701         struct ast_cdr expected = {
1702                 .clid = "\"Alice\" <100>",
1703                 .src = "100",
1704                 .dst = "100",
1705                 .dcontext = "default",
1706                 .channel = CHANNEL_TECH_NAME "/Alice",
1707                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1708                 .lastapp = "Dial",
1709                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1710                 .amaflags = AST_AMA_DOCUMENTATION,
1711                 .billsec = 1,
1712                 .disposition = AST_CDR_ANSWERED,
1713                 .accountcode = "100",
1714                 .peeraccount = "200",
1715         };
1716
1717         switch (cmd) {
1718         case TEST_INIT:
1719                 info->name = __func__;
1720                 info->category = TEST_CATEGORY;
1721                 info->summary = "Test dialing, answering, and going into a 2-party bridge";
1722                 info->description =
1723                         "The most 'basic' of scenarios";
1724                 return AST_TEST_NOT_RUN;
1725         case TEST_EXECUTE:
1726                 break;
1727         }
1728
1729         SWAP_CONFIG(config, debug_cdr_config);
1730
1731         CREATE_ALICE_CHANNEL(chan_caller, &caller, &expected);
1732
1733         EMULATE_APP_DATA(chan_caller, 1, "Dial", CHANNEL_TECH_NAME "/Bob");
1734
1735         chan_callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/Bob");
1736         SET_FORMATS(chan_callee);
1737         ast_channel_unlock(chan_callee);
1738         ast_set_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
1739         EMULATE_APP_DATA(chan_callee, 0, "AppDial", "(Outgoing Line)");
1740
1741         ast_channel_publish_dial(chan_caller, chan_callee, "Bob", NULL);
1742         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1743         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1744
1745         ast_channel_state_set(chan_caller, AST_STATE_UP);
1746         ast_channel_state_set(chan_callee, AST_STATE_UP);
1747
1748         bridge = ast_bridge_basic_new();
1749         ast_test_validate(test, bridge != NULL);
1750         do_sleep(&to_sleep);
1751
1752         ast_test_validate(test, !ast_bridge_impart(bridge, chan_caller, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
1753         ast_test_validate(test, !ast_bridge_impart(bridge, chan_callee, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
1754
1755         do_sleep(&to_sleep);
1756
1757         ast_bridge_depart(chan_caller);
1758         ast_bridge_depart(chan_callee);
1759
1760         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL);
1761         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL);
1762
1763         result = verify_mock_cdr_record(test, &expected, 1);
1764         return result;
1765 }
1766
1767 AST_TEST_DEFINE(test_cdr_dial_answer_twoparty_bridge_b)
1768 {
1769         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1770         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1771         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
1772         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1773                         ao2_cleanup);
1774         struct timespec to_sleep = {1, 0};
1775         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1776
1777         struct ast_party_caller caller = ALICE_CALLERID;
1778         struct ast_cdr expected = {
1779                 .clid = "\"Alice\" <100>",
1780                 .src = "100",
1781                 .dst = "100",
1782                 .dcontext = "default",
1783                 .channel = CHANNEL_TECH_NAME "/Alice",
1784                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1785                 .lastapp = "Dial",
1786                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1787                 .amaflags = AST_AMA_DOCUMENTATION,
1788                 .billsec = 1,
1789                 .disposition = AST_CDR_ANSWERED,
1790                 .accountcode = "100",
1791                 .peeraccount = "200",
1792         };
1793
1794         switch (cmd) {
1795         case TEST_INIT:
1796                 info->name = __func__;
1797                 info->category = TEST_CATEGORY;
1798                 info->summary = "Test dialing, answering, and going into a 2-party bridge";
1799                 info->description =
1800                         "The most 'basic' of scenarios";
1801                 return AST_TEST_NOT_RUN;
1802         case TEST_EXECUTE:
1803                 break;
1804         }
1805
1806         SWAP_CONFIG(config, debug_cdr_config);
1807
1808         CREATE_ALICE_CHANNEL(chan_caller, &caller, &expected);
1809
1810         EMULATE_APP_DATA(chan_caller, 1, "Dial", CHANNEL_TECH_NAME "/Bob");
1811
1812         chan_callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_caller, 0, CHANNEL_TECH_NAME "/Bob");
1813         SET_FORMATS(chan_callee);
1814         ast_channel_unlock(chan_callee);
1815         ast_set_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
1816         EMULATE_APP_DATA(chan_callee, 0, "AppDial", "(Outgoing Line)");
1817
1818         ast_channel_publish_dial(chan_caller, chan_callee, "Bob", NULL);
1819         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1820         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1821
1822         ast_channel_state_set(chan_caller, AST_STATE_UP);
1823         ast_channel_state_set(chan_callee, AST_STATE_UP);
1824
1825         bridge = ast_bridge_basic_new();
1826         ast_test_validate(test, bridge != NULL);
1827         do_sleep(&to_sleep);
1828         ast_test_validate(test, !ast_bridge_impart(bridge, chan_callee, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
1829         do_sleep(&to_sleep);
1830         ast_test_validate(test, !ast_bridge_impart(bridge, chan_caller, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
1831         do_sleep(&to_sleep);
1832         ast_bridge_depart(chan_caller);
1833         ast_bridge_depart(chan_callee);
1834
1835         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL);
1836         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL);
1837
1838         result = verify_mock_cdr_record(test, &expected, 1);
1839         return result;
1840 }
1841
1842 AST_TEST_DEFINE(test_cdr_dial_answer_multiparty)
1843 {
1844         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1845         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1846         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1847         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1848         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
1849         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
1850                         ao2_cleanup);
1851         struct timespec to_sleep = {1, 0};
1852         enum ast_test_result_state result = AST_TEST_NOT_RUN;
1853
1854         struct ast_party_caller alice_caller = ALICE_CALLERID;
1855         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1856         struct ast_cdr charlie_expected_two = {
1857                 .clid = "\"Charlie\" <300>",
1858                 .src = "300",
1859                 .dst = "300",
1860                 .dcontext = "default",
1861                 .channel = CHANNEL_TECH_NAME "/Charlie",
1862                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1863                 .lastapp = "Dial",
1864                 .lastdata = CHANNEL_TECH_NAME "/David",
1865                 .amaflags = AST_AMA_DOCUMENTATION,
1866                 .billsec = 1,
1867                 .disposition = AST_CDR_ANSWERED,
1868                 .accountcode = "300",
1869                 .peeraccount = "200",
1870         };
1871         struct ast_cdr charlie_expected_one = {
1872                 .clid = "\"Charlie\" <300>",
1873                 .src = "300",
1874                 .dst = "300",
1875                 .dcontext = "default",
1876                 .channel = CHANNEL_TECH_NAME "/Charlie",
1877                 .dstchannel = CHANNEL_TECH_NAME "/David",
1878                 .lastapp = "Dial",
1879                 .lastdata = CHANNEL_TECH_NAME "/David",
1880                 .amaflags = AST_AMA_DOCUMENTATION,
1881                 .billsec = 1,
1882                 .disposition = AST_CDR_ANSWERED,
1883                 .accountcode = "300",
1884                 .peeraccount = "400",
1885                 .next = &charlie_expected_two,
1886         };
1887         struct ast_cdr bob_expected_one = {
1888                 .clid = "\"Bob\" <200>",
1889                 .src = "200",
1890                 .dst = "200",
1891                 .dcontext = "default",
1892                 .channel = CHANNEL_TECH_NAME "/Bob",
1893                 .dstchannel = CHANNEL_TECH_NAME "/David",
1894                 .lastapp = "AppDial",
1895                 .lastdata = "(Outgoing Line)",
1896                 .amaflags = AST_AMA_DOCUMENTATION,
1897                 .billsec = 1,
1898                 .disposition = AST_CDR_ANSWERED,
1899                 .accountcode = "200",
1900                 .peeraccount = "400",
1901                 .next = &charlie_expected_one,
1902         };
1903         struct ast_cdr alice_expected_three = {
1904                 .clid = "\"Alice\" <100>",
1905                 .src = "100",
1906                 .dst = "100",
1907                 .dcontext = "default",
1908                 .channel = CHANNEL_TECH_NAME "/Alice",
1909                 .dstchannel = CHANNEL_TECH_NAME "/David",
1910                 .lastapp = "Dial",
1911                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1912                 .amaflags = AST_AMA_DOCUMENTATION,
1913                 .billsec = 1,
1914                 .disposition = AST_CDR_ANSWERED,
1915                 .accountcode = "100",
1916                 .peeraccount = "400",
1917                 .next = &bob_expected_one,
1918         };
1919         struct ast_cdr alice_expected_two = {
1920                 .clid = "\"Alice\" <100>",
1921                 .src = "100",
1922                 .dst = "100",
1923                 .dcontext = "default",
1924                 .channel = CHANNEL_TECH_NAME "/Alice",
1925                 .dstchannel = CHANNEL_TECH_NAME "/Charlie",
1926                 .lastapp = "Dial",
1927                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1928                 .amaflags = AST_AMA_DOCUMENTATION,
1929                 .billsec = 1,
1930                 .disposition = AST_CDR_ANSWERED,
1931                 .accountcode = "100",
1932                 .peeraccount = "300",
1933                 .next = &alice_expected_three,
1934         };
1935         struct ast_cdr alice_expected_one = {
1936                 .clid = "\"Alice\" <100>",
1937                 .src = "100",
1938                 .dst = "100",
1939                 .dcontext = "default",
1940                 .channel = CHANNEL_TECH_NAME "/Alice",
1941                 .dstchannel = CHANNEL_TECH_NAME "/Bob",
1942                 .lastapp = "Dial",
1943                 .lastdata = CHANNEL_TECH_NAME "/Bob",
1944                 .amaflags = AST_AMA_DOCUMENTATION,
1945                 .billsec = 1,
1946                 .disposition = AST_CDR_ANSWERED,
1947                 .accountcode = "100",
1948                 .peeraccount = "200",
1949                 .next = &alice_expected_two,
1950         };
1951
1952         switch (cmd) {
1953         case TEST_INIT:
1954                 info->name = __func__;
1955                 info->category = TEST_CATEGORY;
1956                 info->summary = "Test dialing, answering, and going into a multi-party bridge";
1957                 info->description =
1958                         "A little tricky to get to do, but possible with some redirects.";
1959                 return AST_TEST_NOT_RUN;
1960         case TEST_EXECUTE:
1961                 break;
1962         }
1963
1964         SWAP_CONFIG(config, debug_cdr_config);
1965
1966         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller, &alice_expected_one);
1967         COPY_IDS(chan_alice, &alice_expected_two);
1968         COPY_IDS(chan_alice, &alice_expected_three);
1969
1970         EMULATE_APP_DATA(chan_alice, 1, "Dial", CHANNEL_TECH_NAME "/Bob");
1971
1972         chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, "200", "Bob", "200", "200", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Bob");
1973         SET_FORMATS(chan_bob);
1974         ast_channel_unlock(chan_bob);
1975         ast_set_flag(ast_channel_flags(chan_bob), AST_FLAG_OUTGOING);
1976         EMULATE_APP_DATA(chan_bob, 0, "AppDial", "(Outgoing Line)");
1977         ast_copy_string(bob_expected_one.uniqueid, ast_channel_uniqueid(chan_bob), sizeof(bob_expected_one.uniqueid));
1978         ast_copy_string(bob_expected_one.linkedid, ast_channel_linkedid(chan_alice), sizeof(bob_expected_one.linkedid));
1979
1980         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller, &charlie_expected_one);
1981         EMULATE_APP_DATA(chan_charlie, 1, "Dial", CHANNEL_TECH_NAME "/David");
1982         ast_copy_string(charlie_expected_one.uniqueid, ast_channel_uniqueid(chan_charlie), sizeof(charlie_expected_one.uniqueid));
1983         ast_copy_string(charlie_expected_one.linkedid, ast_channel_linkedid(chan_alice), sizeof(charlie_expected_one.linkedid));
1984         ast_copy_string(charlie_expected_two.uniqueid, ast_channel_uniqueid(chan_charlie), sizeof(charlie_expected_two.uniqueid));
1985         ast_copy_string(charlie_expected_two.linkedid, ast_channel_linkedid(chan_alice), sizeof(charlie_expected_two.linkedid));
1986
1987         chan_david = ast_channel_alloc(0, AST_STATE_DOWN, "400", "David", "400", "400", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/David");
1988         SET_FORMATS(chan_david);
1989         ast_channel_unlock(chan_david);
1990         ast_set_flag(ast_channel_flags(chan_david), AST_FLAG_OUTGOING);
1991         EMULATE_APP_DATA(chan_david, 0, "AppDial", "(Outgoing Line)");
1992
1993         ast_channel_publish_dial(chan_alice, chan_bob, "Bob", NULL);
1994         ast_channel_state_set(chan_alice, AST_STATE_RINGING);
1995         ast_channel_publish_dial(chan_charlie, chan_david, "David", NULL);
1996         ast_channel_state_set(chan_charlie, AST_STATE_RINGING);
1997         ast_channel_publish_dial(chan_alice, chan_bob, NULL, "ANSWER");
1998         ast_channel_publish_dial(chan_charlie, chan_david, NULL, "ANSWER");
1999
2000         ast_channel_state_set(chan_alice, AST_STATE_UP);
2001         ast_channel_state_set(chan_bob, AST_STATE_UP);
2002         ast_channel_state_set(chan_charlie, AST_STATE_UP);
2003         ast_channel_state_set(chan_david, AST_STATE_UP);
2004
2005         bridge = ast_bridge_basic_new();
2006         ast_test_validate(test, bridge != NULL);
2007
2008         do_sleep(&to_sleep);
2009         ast_test_validate(test, !ast_bridge_impart(bridge, chan_charlie, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
2010         do_sleep(&to_sleep);
2011         ast_test_validate(test, !ast_bridge_impart(bridge, chan_david, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
2012         do_sleep(&to_sleep);
2013         ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
2014         do_sleep(&to_sleep);
2015         ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
2016         do_sleep(&to_sleep);
2017         ast_test_validate(test, !ast_bridge_depart(chan_alice));
2018         ast_test_validate(test, !ast_bridge_depart(chan_bob));
2019         ast_test_validate(test, !ast_bridge_depart(chan_charlie));
2020         ast_test_validate(test, !ast_bridge_depart(chan_david));
2021
2022         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL);
2023         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
2024         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL);
2025         HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL);
2026
2027         result = verify_mock_cdr_record(test, &alice_expected_one, 6);
2028
2029         return result;
2030 }
2031
2032 AST_TEST_DEFINE(test_cdr_park)
2033 {
2034         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
2035         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
2036         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
2037         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
2038                         ao2_cleanup);
2039         struct timespec to_sleep = {1, 0};
2040
2041         struct ast_party_caller bob_caller = BOB_CALLERID;
2042         struct ast_party_caller alice_caller = ALICE_CALLERID;
2043         struct ast_cdr bob_expected = {
2044                 .clid = "\"Bob\" <200>",
2045                 .src = "200",
2046                 .dst = "200",
2047                 .dcontext = "default",
2048                 .channel = CHANNEL_TECH_NAME "/Bob",
2049                 .lastapp = "Park",
2050                 .lastdata = "701",
2051                 .billsec = 1,
2052                 .amaflags = AST_AMA_DOCUMENTATION,
2053                 .disposition = AST_CDR_ANSWERED,
2054                 .accountcode = "200",
2055         };
2056         struct ast_cdr alice_expected = {
2057                 .clid = "\"Alice\" <100>",
2058                 .src = "100",
2059                 .dst = "100",
2060                 .dcontext = "default",
2061                 .channel = CHANNEL_TECH_NAME "/Alice",
2062                 .lastapp = "Park",
2063                 .lastdata = "700",
2064                 .billsec = 1,
2065                 .amaflags = AST_AMA_DOCUMENTATION,
2066                 .disposition = AST_CDR_ANSWERED,
2067                 .accountcode = "100",
2068                 .next = &bob_expected,
2069         };
2070         enum ast_test_result_state result = AST_TEST_NOT_RUN;
2071
2072         switch (cmd) {
2073         case TEST_INIT:
2074                 info->name = __func__;
2075                 info->category = TEST_CATEGORY;
2076                 info->summary = "Test cdrs for a single party entering Park";
2077                 info->description =
2078                         "Test the properties of a CDR for calls that are\n"
2079                         "answered, enters Park, and leaves it.";
2080                 return AST_TEST_NOT_RUN;
2081         case TEST_EXECUTE:
2082                 break;
2083         }
2084         SWAP_CONFIG(config, debug_cdr_config);
2085         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller, &alice_expected);
2086         CREATE_BOB_CHANNEL(chan_bob, &bob_caller, &bob_expected);
2087
2088         ast_channel_lock(chan_alice);
2089         EMULATE_APP_DATA(chan_alice, 1, "Park", "700");
2090         ast_setstate(chan_alice, AST_STATE_UP);
2091         ast_channel_unlock(chan_alice);
2092
2093         ast_channel_lock(chan_bob);
2094         EMULATE_APP_DATA(chan_bob, 1, "Park", "701");
2095         ast_setstate(chan_bob, AST_STATE_UP);
2096         ast_channel_unlock(chan_bob);
2097
2098         bridge = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_HOLDING,
2099                 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
2100                         | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED,
2101                 "test_cdr", "test_cdr_park", NULL);
2102         ast_test_validate(test, bridge != NULL);
2103
2104         do_sleep(&to_sleep);
2105         ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
2106         do_sleep(&to_sleep);
2107         ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
2108         do_sleep(&to_sleep);
2109         ast_bridge_depart(chan_alice);
2110         ast_bridge_depart(chan_bob);
2111
2112         /* And then it hangs up */
2113         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL);
2114         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
2115
2116         result = verify_mock_cdr_record(test, &alice_expected, 2);
2117
2118         return result;
2119 }
2120
2121
2122 AST_TEST_DEFINE(test_cdr_fields)
2123 {
2124         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
2125         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
2126                         ao2_cleanup);
2127         char varbuffer[128];
2128         int int_buffer;
2129         double db_buffer;
2130         struct timespec to_sleep = {2, 0};
2131         struct ast_flags fork_options = { 0, };
2132
2133         struct ast_party_caller caller = ALICE_CALLERID;
2134         struct ast_cdr original = {
2135                 .clid = "\"Alice\" <100>",
2136                 .src = "100",
2137                 .dst = "100",
2138                 .dcontext = "default",
2139                 .channel = CHANNEL_TECH_NAME "/Alice",
2140                 .lastapp = "Wait",
2141                 .lastdata = "10",
2142                 .billsec = 0,
2143                 .amaflags = AST_AMA_OMIT,
2144                 .disposition = AST_CDR_FAILED,
2145                 .accountcode = "XXX",
2146                 .userfield = "yackity",
2147         };
2148         struct ast_cdr fork_expected_one = {
2149                 .clid = "\"Alice\" <100>",
2150                 .src = "100",
2151                 .dst = "100",
2152                 .dcontext = "default",
2153                 .channel = CHANNEL_TECH_NAME "/Alice",
2154                 .lastapp = "Wait",
2155                 .lastdata = "10",
2156                 .billsec = 0,
2157                 .amaflags = AST_AMA_OMIT,
2158                 .disposition = AST_CDR_FAILED,
2159                 .accountcode = "XXX",
2160                 .userfield = "yackity",
2161         };
2162         struct ast_cdr fork_expected_two = {
2163                 .clid = "\"Alice\" <100>",
2164                 .src = "100",
2165                 .dst = "100",
2166                 .dcontext = "default",
2167                 .channel = CHANNEL_TECH_NAME "/Alice",
2168                 .lastapp = "Answer",
2169                 .billsec = 0,
2170                 .amaflags = AST_AMA_OMIT,
2171                 .disposition = AST_CDR_ANSWERED,
2172                 .accountcode = "ZZZ",
2173                 .userfield = "schmackity",
2174         };
2175         enum ast_test_result_state result = AST_TEST_NOT_RUN;
2176
2177         struct ast_cdr *expected = &original;
2178         original.next = &fork_expected_one;
2179         fork_expected_one.next = &fork_expected_two;
2180
2181         switch (cmd) {
2182         case TEST_INIT:
2183                 info->name = __func__;
2184                 info->category = TEST_CATEGORY;
2185                 info->summary = "Test field access CDRs";
2186                 info->description =
2187                         "This tests setting/retrieving data on CDR records.";
2188                 return AST_TEST_NOT_RUN;
2189         case TEST_EXECUTE:
2190                 break;
2191         }
2192
2193         SWAP_CONFIG(config, unanswered_cdr_config);
2194
2195         CREATE_ALICE_CHANNEL(chan, &caller, &original);
2196         ast_copy_string(fork_expected_one.uniqueid, ast_channel_uniqueid(chan), sizeof(fork_expected_one.uniqueid));
2197         ast_copy_string(fork_expected_one.linkedid, ast_channel_linkedid(chan), sizeof(fork_expected_one.linkedid));
2198         ast_copy_string(fork_expected_two.uniqueid, ast_channel_uniqueid(chan), sizeof(fork_expected_two.uniqueid));
2199         ast_copy_string(fork_expected_two.linkedid, ast_channel_linkedid(chan), sizeof(fork_expected_two.linkedid));
2200
2201         /* Channel enters Wait app */
2202         ast_channel_lock(chan);
2203         ast_channel_appl_set(chan, "Wait");
2204         ast_channel_data_set(chan, "10");
2205         ast_channel_priority_set(chan, 1);
2206         ast_channel_publish_snapshot(chan);
2207
2208         /* Set properties on the channel that propagate to the CDR */
2209         ast_channel_amaflags_set(chan, AST_AMA_OMIT);
2210         ast_channel_accountcode_set(chan, "XXX");
2211         ast_channel_unlock(chan);
2212
2213         /* Wait one second so we get a duration. */
2214         do_sleep(&to_sleep);
2215
2216         ast_cdr_setuserfield(ast_channel_name(chan), "foobar");
2217         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "test_variable", "record_1") == 0);
2218
2219         /* Verify that we can't set read-only fields or other fields directly */
2220         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "clid", "junk") != 0);
2221         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "src", "junk") != 0);
2222         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "dst", "junk") != 0);
2223         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "dcontext", "junk") != 0);
2224         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "channel", "junk") != 0);
2225         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "dstchannel", "junk") != 0);
2226         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "lastapp", "junk") != 0);
2227         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "lastdata", "junk") != 0);
2228         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "start", "junk") != 0);
2229         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "answer", "junk") != 0);
2230         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "end", "junk") != 0);
2231         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "duration", "junk") != 0);
2232         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "billsec", "junk") != 0);
2233         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "disposition", "junk") != 0);
2234         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "amaflags", "junk") != 0);
2235         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "accountcode", "junk") != 0);
2236         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "uniqueid", "junk") != 0);
2237         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "linkedid", "junk") != 0);
2238         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "userfield", "junk") != 0);
2239         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "sequence", "junk") != 0);
2240
2241         /* Verify the values */
2242         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "userfield", varbuffer, sizeof(varbuffer)) == 0);
2243         ast_test_validate(test, strcmp(varbuffer, "foobar") == 0);
2244         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "test_variable", varbuffer, sizeof(varbuffer)) == 0);
2245         ast_test_validate(test, strcmp(varbuffer, "record_1") == 0);
2246         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "amaflags", varbuffer, sizeof(varbuffer)) == 0);
2247         sscanf(varbuffer, "%d", &int_buffer);
2248         ast_test_validate(test, int_buffer == AST_AMA_OMIT);
2249         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "accountcode", varbuffer, sizeof(varbuffer)) == 0);
2250         ast_test_validate(test, strcmp(varbuffer, "XXX") == 0);
2251         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "clid", varbuffer, sizeof(varbuffer)) == 0);
2252         ast_test_validate(test, strcmp(varbuffer, "\"Alice\" <100>") == 0);
2253         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "src", varbuffer, sizeof(varbuffer)) == 0);
2254         ast_test_validate(test, strcmp(varbuffer, "100") == 0);
2255         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "dst", varbuffer, sizeof(varbuffer)) == 0);
2256         ast_test_validate(test, strcmp(varbuffer, "100") == 0);
2257         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "dcontext", varbuffer, sizeof(varbuffer)) == 0);
2258         ast_test_validate(test, strcmp(varbuffer, "default") == 0);
2259         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "channel", varbuffer, sizeof(varbuffer)) == 0);
2260         ast_test_validate(test, strcmp(varbuffer, CHANNEL_TECH_NAME "/Alice") == 0);
2261         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "dstchannel", varbuffer, sizeof(varbuffer)) == 0);
2262         ast_test_validate(test, strcmp(varbuffer, "") == 0);
2263         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "lastapp", varbuffer, sizeof(varbuffer)) == 0);
2264         ast_test_validate(test, strcmp(varbuffer, "Wait") == 0);
2265         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "lastdata", varbuffer, sizeof(varbuffer)) == 0);
2266         ast_test_validate(test, strcmp(varbuffer, "10") == 0);
2267         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "start", varbuffer, sizeof(varbuffer)) == 0);
2268         sscanf(varbuffer, "%lf", &db_buffer);
2269         ast_test_validate(test, fabs(db_buffer) > 0);
2270         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "answer", varbuffer, sizeof(varbuffer)) == 0);
2271         sscanf(varbuffer, "%lf", &db_buffer);
2272         ast_test_validate(test, fabs(db_buffer) < EPSILON);
2273         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "end", varbuffer, sizeof(varbuffer)) == 0);
2274         sscanf(varbuffer, "%lf", &db_buffer);
2275         ast_test_validate(test, fabs(db_buffer) < EPSILON);
2276         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "duration", varbuffer, sizeof(varbuffer)) == 0);
2277         sscanf(varbuffer, "%lf", &db_buffer);
2278         ast_test_validate(test, fabs(db_buffer) > 0);
2279         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "billsec", varbuffer, sizeof(varbuffer)) == 0);
2280         sscanf(varbuffer, "%lf", &db_buffer);
2281         ast_test_validate(test, fabs(db_buffer) < EPSILON);
2282         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "disposition", varbuffer, sizeof(varbuffer)) == 0);
2283         sscanf(varbuffer, "%d", &int_buffer);
2284         ast_test_validate(test, int_buffer == AST_CDR_NULL);
2285         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "uniqueid", varbuffer, sizeof(varbuffer)) == 0);
2286         ast_test_validate(test, strcmp(varbuffer, ast_channel_uniqueid(chan)) == 0);
2287         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "linkedid", varbuffer, sizeof(varbuffer)) == 0);
2288         ast_test_validate(test, strcmp(varbuffer, ast_channel_linkedid(chan)) == 0);
2289         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "sequence", varbuffer, sizeof(varbuffer)) == 0);
2290
2291         /* Fork the CDR, and check that we change the properties on both CDRs. */
2292         ast_set_flag(&fork_options, AST_CDR_FLAG_KEEP_VARS);
2293         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2294
2295         /* Change some properties */
2296         ast_cdr_setuserfield(ast_channel_name(chan), "yackity");
2297         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "test_variable", "record_1b") == 0);
2298
2299         /* Fork the CDR again, finalizing all current CDRs */
2300         ast_set_flag(&fork_options, AST_CDR_FLAG_KEEP_VARS | AST_CDR_FLAG_FINALIZE);
2301         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2302
2303         /* Channel enters Answer app */
2304         ast_channel_lock(chan);
2305         ast_channel_appl_set(chan, "Answer");
2306         ast_channel_data_set(chan, "");
2307         ast_channel_priority_set(chan, 1);
2308         ast_channel_publish_snapshot(chan);
2309         ast_setstate(chan, AST_STATE_UP);
2310
2311         /* Set properties on the last record */
2312         ast_channel_accountcode_set(chan, "ZZZ");
2313         ast_channel_unlock(chan);
2314         ast_cdr_setuserfield(ast_channel_name(chan), "schmackity");
2315         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "test_variable", "record_2") == 0);
2316
2317         /* Hang up and verify */
2318         ast_channel_hangupcause_set(chan, AST_CAUSE_NORMAL);
2319         ast_hangup(chan);
2320         chan = NULL;
2321         result = verify_mock_cdr_record(test, expected, 3);
2322
2323         return result;
2324 }
2325
2326 AST_TEST_DEFINE(test_cdr_no_reset_cdr)
2327 {
2328         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
2329         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
2330                         ao2_cleanup);
2331         struct ast_flags fork_options = { 0, };
2332         struct timespec to_sleep = {1, 0};
2333
2334         struct ast_party_caller caller = ALICE_CALLERID;
2335         struct ast_cdr expected = {
2336                 .clid = "\"Alice\" <100>",
2337                 .src = "100",
2338                 .dst = "100",
2339                 .dcontext = "default",
2340                 .channel = CHANNEL_TECH_NAME "/Alice",
2341                 .billsec = 0,
2342                 .amaflags = AST_AMA_DOCUMENTATION,
2343                 .disposition = AST_CDR_FAILED,
2344                 .accountcode = "100",
2345         };
2346         enum ast_test_result_state result = AST_TEST_NOT_RUN;
2347
2348         switch (cmd) {
2349         case TEST_INIT:
2350                 info->name = __func__;
2351                 info->category = TEST_CATEGORY;
2352                 info->summary = "Test field access CDRs";
2353                 info->description =
2354                         "This tests setting/retrieving data on CDR records.";
2355                 return AST_TEST_NOT_RUN;
2356         case TEST_EXECUTE:
2357                 break;
2358         }
2359
2360         SWAP_CONFIG(config, unanswered_cdr_config);
2361
2362         CREATE_ALICE_CHANNEL(chan, &caller, &expected);
2363
2364         do_sleep(&to_sleep);
2365
2366         /* Disable the CDR */
2367         ast_test_validate(test, ast_cdr_set_property(ast_channel_name(chan), AST_CDR_FLAG_DISABLE) == 0);
2368
2369         /* Fork the CDR. This should be enabled */
2370         ast_set_flag(&fork_options, AST_CDR_FLAG_FINALIZE);
2371         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2372
2373         /* Disable and enable the forked CDR */
2374         ast_test_validate(test, ast_cdr_set_property(ast_channel_name(chan), AST_CDR_FLAG_DISABLE) == 0);
2375         ast_test_validate(test, ast_cdr_clear_property(ast_channel_name(chan), AST_CDR_FLAG_DISABLE) == 0);
2376
2377         /* Fork and finalize again. This CDR should be propagated */
2378         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2379
2380         /* Disable all future CDRs */
2381         ast_test_validate(test, ast_cdr_set_property(ast_channel_name(chan), AST_CDR_FLAG_DISABLE_ALL) == 0);
2382
2383         /* Fork a few more */
2384         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2385         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2386         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2387
2388         ast_channel_hangupcause_set(chan, AST_CAUSE_NORMAL);
2389         ast_hangup(chan);
2390         chan = NULL;
2391         result = verify_mock_cdr_record(test, &expected, 1);
2392
2393         return result;
2394 }
2395
2396 AST_TEST_DEFINE(test_cdr_fork_cdr)
2397 {
2398         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
2399         RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
2400                         ao2_cleanup);
2401         char varbuffer[128];
2402         char fork_varbuffer[128];
2403         char answer_time[128];
2404         char fork_answer_time[128];
2405         char start_time[128];
2406         char fork_start_time[128];
2407         struct ast_flags fork_options = { 0, };
2408         struct timespec to_sleep = {1, 10000};
2409
2410         struct ast_party_caller caller = ALICE_CALLERID;
2411         struct ast_cdr original = {
2412                 .clid = "\"Alice\" <100>",
2413                 .src = "100",
2414                 .dst = "100",
2415                 .dcontext = "default",
2416                 .channel = CHANNEL_TECH_NAME "/Alice",
2417                 .amaflags = AST_AMA_DOCUMENTATION,
2418                 .disposition = AST_CDR_ANSWERED,
2419                 .accountcode = "100",
2420         };
2421         struct ast_cdr fork_expected_one = {
2422                 .clid = "\"Alice\" <100>",
2423                 .src = "100",
2424                 .dst = "100",
2425                 .dcontext = "default",
2426                 .channel = CHANNEL_TECH_NAME "/Alice",
2427                 .amaflags = AST_AMA_DOCUMENTATION,
2428                 .disposition = AST_CDR_ANSWERED,
2429                 .accountcode = "100",
2430         };
2431         struct ast_cdr fork_expected_two = {
2432                 .clid = "\"Alice\" <100>",
2433                 .src = "100",
2434                 .dst = "100",
2435                 .dcontext = "default",
2436                 .channel = CHANNEL_TECH_NAME "/Alice",
2437                 .amaflags = AST_AMA_DOCUMENTATION,
2438                 .disposition = AST_CDR_ANSWERED,
2439                 .accountcode = "100",
2440         };
2441         enum ast_test_result_state result = AST_TEST_NOT_RUN;
2442         struct ast_cdr *expected = &original;
2443         original.next = &fork_expected_one;
2444         fork_expected_one.next = &fork_expected_two;
2445
2446         switch (cmd) {
2447         case TEST_INIT:
2448                 info->name = __func__;
2449                 info->category = TEST_CATEGORY;
2450                 info->summary = "Test field access CDRs";
2451                 info->description =
2452                         "This tests setting/retrieving data on CDR records.";
2453                 return AST_TEST_NOT_RUN;
2454         case TEST_EXECUTE:
2455                 break;
2456         }
2457
2458         SWAP_CONFIG(config, debug_cdr_config);
2459
2460         CREATE_ALICE_CHANNEL(chan, &caller, &original);
2461         ast_copy_string(fork_expected_one.uniqueid, ast_channel_uniqueid(chan), sizeof(fork_expected_one.uniqueid));
2462         ast_copy_string(fork_expected_one.linkedid, ast_channel_linkedid(chan), sizeof(fork_expected_one.linkedid));
2463         ast_copy_string(fork_expected_two.uniqueid, ast_channel_uniqueid(chan), sizeof(fork_expected_two.uniqueid));
2464         ast_copy_string(fork_expected_two.linkedid, ast_channel_linkedid(chan), sizeof(fork_expected_two.linkedid));
2465
2466         do_sleep(&to_sleep);
2467
2468         /* Test blowing away variables */
2469         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "test_variable", "record_1") == 0);
2470         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "test_variable", varbuffer, sizeof(varbuffer)) == 0);
2471         ast_test_validate(test, strcmp(varbuffer, "record_1") == 0);
2472         ast_copy_string(varbuffer, "", sizeof(varbuffer));
2473
2474         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2475         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "test_variable", fork_varbuffer, sizeof(fork_varbuffer)) == 0);
2476         ast_test_validate(test, strcmp(varbuffer, "record_1") != 0);
2477
2478         /* Test finalizing previous CDRs */
2479         ast_set_flag(&fork_options, AST_CDR_FLAG_FINALIZE);
2480         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2481
2482         /* Test keep variables; setting a new answer time */
2483         ast_channel_lock(chan);
2484         ast_setstate(chan, AST_STATE_UP);
2485         ast_channel_unlock(chan);
2486         do_sleep(&to_sleep);
2487         ast_test_validate(test, ast_cdr_setvar(ast_channel_name(chan), "test_variable", "record_2") == 0);
2488         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "test_variable", varbuffer, sizeof(varbuffer)) == 0);
2489         ast_test_validate(test, strcmp(varbuffer, "record_2") == 0);
2490         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "answer", answer_time, sizeof(answer_time)) == 0);
2491         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "start", start_time, sizeof(start_time)) == 0);
2492
2493         ast_set_flag(&fork_options, AST_CDR_FLAG_FINALIZE);
2494         ast_set_flag(&fork_options, AST_CDR_FLAG_KEEP_VARS);
2495         ast_set_flag(&fork_options, AST_CDR_FLAG_SET_ANSWER);
2496         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2497         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "answer", fork_answer_time, sizeof(fork_answer_time)) == 0);
2498         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "start", fork_start_time, sizeof(fork_start_time)) == 0);
2499         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "test_variable", fork_varbuffer, sizeof(fork_varbuffer)) == 0);
2500         ast_test_validate(test, strcmp(fork_varbuffer, varbuffer) == 0);
2501         ast_test_validate(test, strcmp(fork_start_time, start_time) == 0);
2502         ast_test_validate(test, strcmp(fork_answer_time, answer_time) != 0);
2503
2504         ast_clear_flag(&fork_options, AST_CDR_FLAG_SET_ANSWER);
2505         ast_set_flag(&fork_options, AST_CDR_FLAG_RESET);
2506         ast_test_validate(test, ast_cdr_fork(ast_channel_name(chan), &fork_options) == 0);
2507         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "answer", fork_answer_time, sizeof(fork_answer_time)) == 0);
2508         ast_test_validate(test, ast_cdr_getvar(ast_channel_name(chan), "start", fork_start_time, sizeof(fork_start_time)) == 0);
2509         ast_test_validate(test, strcmp(fork_start_time, start_time) != 0);
2510         ast_test_validate(test, strcmp(fork_answer_time, answer_time) != 0);
2511
2512         ast_channel_hangupcause_set(chan, AST_CAUSE_NORMAL);
2513         ast_hangup(chan);
2514         chan = NULL;
2515         result = verify_mock_cdr_record(test, expected, 3);
2516
2517         return result;
2518 }
2519
2520 /*!
2521  * \internal
2522  * \brief Callback function called before each test executes
2523  */
2524 static int test_cdr_init_cb(struct ast_test_info *info, struct ast_test *test)
2525 {
2526         /* Back up the real config */
2527         saved_config = ast_cdr_get_config();
2528         clear_mock_cdr_backend();
2529         return 0;
2530 }
2531
2532 /*!
2533  * \internal
2534  * \brief Callback function called after each test executes
2535  */
2536 static int test_cdr_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
2537 {
2538         /* Restore the real config */
2539         ast_cdr_set_config(saved_config);
2540         ao2_cleanup(saved_config);
2541         saved_config = NULL;
2542         clear_mock_cdr_backend();
2543
2544         return 0;
2545 }
2546
2547
2548 static int unload_module(void)
2549 {
2550         AST_TEST_UNREGISTER(test_cdr_channel_creation);
2551         AST_TEST_UNREGISTER(test_cdr_unanswered_inbound_call);
2552         AST_TEST_UNREGISTER(test_cdr_unanswered_outbound_call);
2553
2554         AST_TEST_UNREGISTER(test_cdr_single_party);
2555         AST_TEST_UNREGISTER(test_cdr_single_bridge);
2556         AST_TEST_UNREGISTER(test_cdr_single_bridge_continue);
2557         AST_TEST_UNREGISTER(test_cdr_single_twoparty_bridge_a);
2558         AST_TEST_UNREGISTER(test_cdr_single_twoparty_bridge_b);
2559         AST_TEST_UNREGISTER(test_cdr_single_multiparty_bridge);
2560
2561         AST_TEST_UNREGISTER(test_cdr_outbound_bridged_call);
2562
2563         AST_TEST_UNREGISTER(test_cdr_dial_unanswered);
2564         AST_TEST_UNREGISTER(test_cdr_dial_congestion);
2565         AST_TEST_UNREGISTER(test_cdr_dial_busy);
2566         AST_TEST_UNREGISTER(test_cdr_dial_unavailable);
2567         AST_TEST_UNREGISTER(test_cdr_dial_caller_cancel);
2568         AST_TEST_UNREGISTER(test_cdr_dial_parallel_failed);
2569         AST_TEST_UNREGISTER(test_cdr_dial_answer_no_bridge);
2570         AST_TEST_UNREGISTER(test_cdr_dial_answer_twoparty_bridge_a);
2571         AST_TEST_UNREGISTER(test_cdr_dial_answer_twoparty_bridge_b);
2572         AST_TEST_UNREGISTER(test_cdr_dial_answer_multiparty);
2573
2574         AST_TEST_UNREGISTER(test_cdr_park);
2575
2576         AST_TEST_UNREGISTER(test_cdr_fields);
2577         AST_TEST_UNREGISTER(test_cdr_no_reset_cdr);
2578         AST_TEST_UNREGISTER(test_cdr_fork_cdr);
2579
2580         ast_cdr_unregister(MOCK_CDR_BACKEND);
2581         ast_channel_unregister(&test_cdr_chan_tech);
2582         clear_mock_cdr_backend();
2583
2584         return 0;
2585 }
2586
2587 static int load_module(void)
2588 {
2589         ast_cond_init(&mock_cdr_cond, NULL);
2590
2591         AST_TEST_REGISTER(test_cdr_channel_creation);
2592         AST_TEST_REGISTER(test_cdr_unanswered_inbound_call);
2593         AST_TEST_REGISTER(test_cdr_unanswered_outbound_call);
2594
2595         AST_TEST_REGISTER(test_cdr_single_party);
2596         AST_TEST_REGISTER(test_cdr_single_bridge);
2597         AST_TEST_REGISTER(test_cdr_single_bridge_continue);
2598         AST_TEST_REGISTER(test_cdr_single_twoparty_bridge_a);
2599         AST_TEST_REGISTER(test_cdr_single_twoparty_bridge_b);
2600         AST_TEST_REGISTER(test_cdr_single_multiparty_bridge);
2601
2602         AST_TEST_REGISTER(test_cdr_outbound_bridged_call);
2603
2604         AST_TEST_REGISTER(test_cdr_dial_unanswered);
2605         AST_TEST_REGISTER(test_cdr_dial_congestion);
2606         AST_TEST_REGISTER(test_cdr_dial_busy);
2607         AST_TEST_REGISTER(test_cdr_dial_unavailable);
2608         AST_TEST_REGISTER(test_cdr_dial_caller_cancel);
2609         AST_TEST_REGISTER(test_cdr_dial_parallel_failed);
2610         AST_TEST_REGISTER(test_cdr_dial_answer_no_bridge);
2611         AST_TEST_REGISTER(test_cdr_dial_answer_twoparty_bridge_a);
2612         AST_TEST_REGISTER(test_cdr_dial_answer_twoparty_bridge_b);
2613         AST_TEST_REGISTER(test_cdr_dial_answer_multiparty);
2614
2615         AST_TEST_REGISTER(test_cdr_park);
2616
2617         AST_TEST_REGISTER(test_cdr_fields);
2618         AST_TEST_REGISTER(test_cdr_no_reset_cdr);
2619         AST_TEST_REGISTER(test_cdr_fork_cdr);
2620
2621         ast_test_register_init(TEST_CATEGORY, test_cdr_init_cb);
2622         ast_test_register_cleanup(TEST_CATEGORY, test_cdr_cleanup_cb);
2623
2624         ast_channel_register(&test_cdr_chan_tech);
2625         ast_cdr_register(MOCK_CDR_BACKEND, "Mock CDR backend", mock_cdr_backend_cb);
2626
2627         return AST_MODULE_LOAD_SUCCESS;
2628 }
2629
2630 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CDR unit tests");