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