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