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