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