Skip to content
Snippets Groups Projects
test_cdr.c 89.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 2013, Digium, Inc.
     *
     * Matt Jordan <mjordan@digium.com>
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    /*!
     * \file
     * \brief CDR unit tests
     *
     * \author Matt Jordan <mjordan@digium.com>
     *
     */
    
    /*** MODULEINFO
    	<depend>TEST_FRAMEWORK</depend>
    	<support_level>core</support_level>
     ***/
    
    #include "asterisk.h"
    
    #include <math.h>
    #include "asterisk/module.h"
    #include "asterisk/test.h"
    #include "asterisk/cdr.h"
    #include "asterisk/linkedlists.h"
    #include "asterisk/chanvars.h"
    #include "asterisk/utils.h"
    #include "asterisk/causes.h"
    #include "asterisk/time.h"
    
    #include "asterisk/bridge.h"
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    #include "asterisk/bridge_basic.h"
    
    #include "asterisk/stasis_channels.h"
    
    #include "asterisk/stasis_bridges.h"
    
    #include "asterisk/format_cache.h"
    
    
    #define EPSILON 0.001
    
    #define TEST_CATEGORY "/main/cdr/"
    
    #define MOCK_CDR_BACKEND "mock_cdr_backend"
    
    #define CHANNEL_TECH_NAME "CDRTestChannel"
    
    /*! \brief A placeholder for Asterisk's 'real' CDR configuration */
    static struct ast_cdr_config *saved_config;
    
    /*! \brief A configuration suitable for 'normal' CDRs */
    static struct ast_cdr_config debug_cdr_config = {
    	.settings.flags = CDR_ENABLED | CDR_DEBUG,
    };
    
    /*! \brief A configuration suitable for CDRs with unanswered records */
    static struct ast_cdr_config unanswered_cdr_config = {
    	.settings.flags = CDR_ENABLED | CDR_UNANSWERED | CDR_DEBUG,
    };
    
    /*! \brief A configuration suitable for CDRs with congestion enabled */
    static struct ast_cdr_config congestion_cdr_config = {
    	.settings.flags = CDR_ENABLED | CDR_UNANSWERED | CDR_DEBUG | CDR_CONGESTION,
    };
    
    /*! \brief Macro to swap a configuration out from the CDR engine. This should be
     * used at the beginning of each test to set the needed configuration for that
     * test.
     */
    #define SWAP_CONFIG(ao2_config, template) do { \
    	*(ao2_config) = (template); \
    	ast_cdr_set_config((ao2_config)); \
    	} while (0)
    
    /*! \brief A linked list of received CDR entries from the engine */
    static AST_LIST_HEAD(, test_cdr_entry) actual_cdr_entries = AST_LIST_HEAD_INIT_VALUE;
    
    /*! \brief The Mock CDR backend condition wait */
    static ast_cond_t mock_cdr_cond;
    
    /*! \brief A channel technology used for the unit tests */
    static struct ast_channel_tech test_cdr_chan_tech = {
    	.type = CHANNEL_TECH_NAME,
    	.description = "Mock channel technology for CDR tests",
    };
    
    struct test_cdr_entry {
    	struct ast_cdr *cdr;
    	AST_LIST_ENTRY(test_cdr_entry) list;
    };
    
    /*! \brief The number of CDRs the mock backend has received */
    static int global_mock_cdr_count;
    
    /*! \internal
     * \brief Callback function for the mock CDR backend
     *
     * This function 'processes' a dispatched CDR record by adding it to the
     * \ref actual_cdr_entries list. When a test completes, it can verify the
     * expected records against this list of actual CDRs created by the engine.
     *
     * \param cdr The public CDR object created by the engine
     *
     * \retval -1 on error
     * \retval 0 on success
     */
    static int mock_cdr_backend_cb(struct ast_cdr *cdr)
    {
    	struct ast_cdr *cdr_copy, *cdr_prev = NULL;
    	struct ast_cdr *mock_cdr = NULL;
    	struct test_cdr_entry *cdr_wrapper;
    
    	cdr_wrapper = ast_calloc(1, sizeof(*cdr_wrapper));
    	if (!cdr_wrapper) {
    		return -1;
    	}
    
    	for (; cdr; cdr = cdr->next) {
    		struct ast_var_t *var_entry, *var_copy;
    
    		cdr_copy = ast_calloc(1, sizeof(*cdr_copy));
    		if (!cdr_copy) {
    			return -1;
    		}
    		*cdr_copy = *cdr;
    		cdr_copy->varshead.first = NULL;
    		cdr_copy->varshead.last = NULL;
    		cdr_copy->next = NULL;
    
    		AST_LIST_TRAVERSE(&cdr->varshead, var_entry, entries) {
    			var_copy = ast_var_assign(var_entry->name, var_entry->value);
    			if (!var_copy) {
    				return -1;
    			}
    			AST_LIST_INSERT_TAIL(&cdr_copy->varshead, var_copy, entries);
    		}
    
    		if (!mock_cdr) {
    			mock_cdr = cdr_copy;
    		}
    		if (cdr_prev) {
    			cdr_prev->next = cdr_copy;
    		}
    		cdr_prev = cdr_copy;
    	}
    	cdr_wrapper->cdr = mock_cdr;
    
    	AST_LIST_LOCK(&actual_cdr_entries);
    	AST_LIST_INSERT_TAIL(&actual_cdr_entries, cdr_wrapper, list);
    	global_mock_cdr_count++;
    	ast_cond_signal(&mock_cdr_cond);
    	AST_LIST_UNLOCK(&actual_cdr_entries);
    
    	return 0;
    }
    
    /*! \internal
     * \brief Remove all entries from \ref actual_cdr_entries
     */
    static void clear_mock_cdr_backend(void)
    {
    	struct test_cdr_entry *cdr_wrapper;
    
    	AST_LIST_LOCK(&actual_cdr_entries);
    	while ((cdr_wrapper = AST_LIST_REMOVE_HEAD(&actual_cdr_entries, list))) {
    		ast_cdr_free(cdr_wrapper->cdr);
    		ast_free(cdr_wrapper);
    	}
    	global_mock_cdr_count = 0;
    	AST_LIST_UNLOCK(&actual_cdr_entries);
    }
    
    /*! \brief Verify a string field. This will set the test status result to fail;
     * as such, it assumes that (a) test is the test object variable, and (b) that
     * a return variable res exists.
     */
    #define VERIFY_STRING_FIELD(field, actual, expected) do { \
    	if (strcmp((actual)->field, (expected)->field)) { \
    		ast_test_status_update(test, "Field %s failed: actual %s, expected %s\n", #field, (actual)->field, (expected)->field); \
    		ast_test_set_result(test, AST_TEST_FAIL); \
    		res = AST_TEST_FAIL; \
    	} } while (0)
    
    /*! \brief Verify a numeric field. This will set the test status result to fail;
     * as such, it assumes that (a) test is the test object variable, and (b) that
     * a return variable res exists.
     */
    #define VERIFY_NUMERIC_FIELD(field, actual, expected) do { \
    	if ((actual)->field != (expected)->field) { \
    		ast_test_status_update(test, "Field %s failed: actual %ld, expected %ld\n", #field, (long)(actual)->field, (long)(expected)->field); \
    		ast_test_set_result(test, AST_TEST_FAIL); \
    		res = AST_TEST_FAIL; \
    	} } while (0)
    
    /*! \brief Verify a time field. This will set the test status result to fail;
     * as such, it assumes that (a) test is the test object variable, and (b) that
     * a return variable res exists.
     */
    #define VERIFY_TIME_VALUE(field, actual) do { \
    	if (ast_tvzero((actual)->field)) { \
    		ast_test_status_update(test, "Field %s failed: should not be 0\n", #field); \
    		ast_test_set_result(test, AST_TEST_FAIL); \
    		res = AST_TEST_FAIL; \
    	} } while (0)
    
    /*! \brief Alice's Caller ID */
    #define ALICE_CALLERID { .id.name.str = "Alice", .id.name.valid = 1, .id.number.str = "100", .id.number.valid = 1, }
    
    /*! \brief Bob's Caller ID */
    #define BOB_CALLERID { .id.name.str = "Bob", .id.name.valid = 1, .id.number.str = "200", .id.number.valid = 1, }
    
    /*! \brief Charlie's Caller ID */
    #define CHARLIE_CALLERID { .id.name.str = "Charlie", .id.name.valid = 1, .id.number.str = "300", .id.number.valid = 1, }
    
    /*! \brief David's Caller ID */
    #define DAVID_CALLERID { .id.name.str = "David", .id.name.valid = 1, .id.number.str = "400", .id.number.valid = 1, }
    
    /*! \brief Copy the linkedid and uniqueid from a channel to an expected CDR */
    #define COPY_IDS(channel_var, expected_record) do { \
    	ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
    	ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
    	} while (0)
    
    
    /*! \brief Set ulaw format on channel */
    #define SET_FORMATS(chan) do {\
    	struct ast_format_cap *caps;\
    	caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);\
    	ast_format_cap_append(caps, ast_format_ulaw, 0);\
    	ast_channel_nativeformats_set((chan), caps);\
    	ast_channel_set_writeformat((chan), ast_format_ulaw);\
    	ast_channel_set_rawwriteformat((chan), ast_format_ulaw);\
    	ast_channel_set_readformat((chan), ast_format_ulaw);\
    	ast_channel_set_rawreadformat((chan), ast_format_ulaw);\
    	ao2_ref(caps, -1);\
    } while (0)
    
    
    /*! \brief Create a \ref test_cdr_chan_tech for Alice, and set the expected
     * CDR records' linkedid and uniqueid. */
    #define CREATE_ALICE_CHANNEL(channel_var, caller_id, expected_record) do { \
    
    	(channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Alice"); \
    
    	SET_FORMATS((channel_var));\
    
    	ast_channel_set_caller((channel_var), (caller_id), NULL); \
    	ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
    	ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
    
    	ast_channel_unlock((channel_var)); \
    
    	} while (0)
    
    /*! \brief Create a \ref test_cdr_chan_tech for Bob, and set the expected
     * CDR records' linkedid and uniqueid. */
    #define CREATE_BOB_CHANNEL(channel_var, caller_id, expected_record) do { \
    
    	(channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "200", "Bob", "200", "200", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Bob"); \
    
    	SET_FORMATS((channel_var));\
    
    	ast_channel_set_caller((channel_var), (caller_id), NULL); \
    	ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
    	ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
    
    	ast_channel_unlock((channel_var)); \
    
    	} while (0)
    
    /*! \brief Create a \ref test_cdr_chan_tech for Charlie, and set the expected
     * CDR records' linkedid and uniqueid. */
    #define CREATE_CHARLIE_CHANNEL(channel_var, caller_id, expected_record) do { \
    
    	(channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "300", "Charlie", "300", "300", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Charlie"); \
    
    	SET_FORMATS((channel_var));\
    
    	ast_channel_set_caller((channel_var), (caller_id), NULL); \
    	ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
    	ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
    
    	ast_channel_unlock((channel_var)); \
    
    	} while (0)
    
    /*! \brief Create a \ref test_cdr_chan_tech for Charlie, and set the expected
     * CDR records' linkedid and uniqueid. */
    #define CREATE_DAVID_CHANNEL(channel_var, caller_id, expected_record) do { \
    
    	(channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "400", "David", "400", "400", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/David"); \
    
    	SET_FORMATS((channel_var));\
    
    	ast_channel_set_caller((channel_var), (caller_id), NULL); \
    	ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
    	ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
    
    	ast_channel_unlock((channel_var)); \
    
    	} while (0)
    
    /*! \brief Emulate a channel entering into an application */
    #define EMULATE_APP_DATA(channel, priority, application, data) do { \
    	if ((priority) > 0) { \
    		ast_channel_priority_set((channel), (priority)); \
    	} \
    
    	ast_channel_lock((channel)); \
    
    	ast_channel_appl_set((channel), (application)); \
    	ast_channel_data_set((channel), (data)); \
    	ast_channel_publish_snapshot((channel)); \
    
    	ast_channel_unlock((channel)); \
    
    	} while (0)
    
    /*! \brief Hang up a test channel safely */
    
    #define HANGUP_CHANNEL(channel, cause) \
    	do { \
    		ast_channel_hangupcause_set((channel), (cause)); \
    		ast_hangup(channel); \
    
    
    static enum ast_test_result_state verify_mock_cdr_record(struct ast_test *test, struct ast_cdr *expected, int record)
    {
    	struct ast_cdr *actual = NULL;
    	struct test_cdr_entry *cdr_wrapper;
    	int count = 0;
    	struct timeval wait_now = ast_tvnow();
    	struct timespec wait_time = { .tv_sec = wait_now.tv_sec + 5, .tv_nsec = wait_now.tv_usec * 1000 };
    	enum ast_test_result_state res = AST_TEST_PASS;
    
    	while (count < record) {
    		AST_LIST_LOCK(&actual_cdr_entries);
    		if (global_mock_cdr_count < record) {
    			ast_cond_timedwait(&mock_cdr_cond, &actual_cdr_entries.lock, &wait_time);
    		}
    		cdr_wrapper = AST_LIST_REMOVE_HEAD(&actual_cdr_entries, list);
    		AST_LIST_UNLOCK(&actual_cdr_entries);
    
    		if (!cdr_wrapper) {
    			ast_test_status_update(test, "Unable to find actual CDR record at %d\n", count);
    			return AST_TEST_FAIL;
    		}
    		actual = cdr_wrapper->cdr;
    
    		if (!expected && actual) {
    			ast_test_status_update(test, "CDRs recorded where no record expected\n");
    			return AST_TEST_FAIL;
    		}
    
    		ast_test_debug(test, "Verifying expected record %s, %s\n",
    			expected->channel, S_OR(expected->dstchannel, "<none>"));
    
    		VERIFY_STRING_FIELD(accountcode, actual, expected);
    		VERIFY_NUMERIC_FIELD(amaflags, actual, expected);
    		VERIFY_STRING_FIELD(channel, actual, expected);
    		VERIFY_STRING_FIELD(clid, actual, expected);
    		VERIFY_STRING_FIELD(dcontext, actual, expected);
    		VERIFY_NUMERIC_FIELD(disposition, actual, expected);
    		VERIFY_STRING_FIELD(dst, actual, expected);
    		VERIFY_STRING_FIELD(dstchannel, actual, expected);
    		VERIFY_STRING_FIELD(lastapp, actual, expected);
    		VERIFY_STRING_FIELD(lastdata, actual, expected);
    		VERIFY_STRING_FIELD(linkedid, actual, expected);
    		VERIFY_STRING_FIELD(peeraccount, actual, expected);
    		VERIFY_STRING_FIELD(src, actual, expected);
    		VERIFY_STRING_FIELD(uniqueid, actual, expected);
    		VERIFY_STRING_FIELD(userfield, actual, expected);
    		VERIFY_TIME_VALUE(start, actual);
    		VERIFY_TIME_VALUE(end, actual);
    		/* Note: there's no way we can really calculate a duration or
    		 * billsec - the unit tests are too short. However, if billsec is
    		 * non-zero in the expected, then make sure we have an answer time
    		 */
    		if (expected->billsec) {
    			VERIFY_TIME_VALUE(answer, actual);
    		}
    		ast_test_debug(test, "Finished expected record %s, %s\n",
    				expected->channel, S_OR(expected->dstchannel, "<none>"));
    		expected = expected->next;
    		++count;
    	}
    	return res;
    }
    
    static void safe_channel_release(struct ast_channel *chan)
    {
    	if (!chan) {
    		return;
    	}
    	ast_channel_release(chan);
    }
    
    
    static void safe_bridge_destroy(struct ast_bridge *bridge)
    {
    	if (!bridge) {
    		return;
    	}
    	ast_bridge_destroy(bridge, 0);
    }
    
    static void do_sleep(struct timespec *to_sleep)
    {
    	while ((nanosleep(to_sleep, to_sleep) == -1) && (errno == EINTR)) {
    	}
    }
    
    
    AST_TEST_DEFINE(test_cdr_channel_creation)
    {
    	RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
    	RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
    			ao2_cleanup);
    
    	struct ast_party_caller caller = ALICE_CALLERID;
    	struct ast_cdr expected = {
    		.clid = "\"Alice\" <100>",
    		.src = "100",
    		.dst = "100",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Alice",
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.disposition = AST_CDR_NOANSWER,
    		.accountcode = "100",
    	};
    	enum ast_test_result_state result = AST_TEST_NOT_RUN;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = __func__;
    		info->category = TEST_CATEGORY;
    		info->summary = "Test that a CDR is created when a channel is created";
    		info->description =
    			"Test that a CDR is created when a channel is created";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	SWAP_CONFIG(config, unanswered_cdr_config);
    
    	CREATE_ALICE_CHANNEL(chan, (&caller), &expected);
    
    	HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
    
    	result = verify_mock_cdr_record(test, &expected, 1);
    
    	return result;
    }
    
    AST_TEST_DEFINE(test_cdr_unanswered_inbound_call)
    {
    	RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
    	RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
    			ao2_cleanup);
    
    	struct ast_party_caller caller = ALICE_CALLERID;
    	struct ast_cdr expected = {
    		.clid = "\"Alice\" <100>",
    		.src = "100",
    		.dst = "100",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Alice",
    		.lastapp = "Wait",
    		.lastdata = "1",
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.disposition = AST_CDR_NOANSWER,
    		.accountcode = "100",
    	};
    	enum ast_test_result_state result = AST_TEST_NOT_RUN;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = __func__;
    		info->category = TEST_CATEGORY;
    		info->summary = "Test inbound unanswered calls";
    		info->description =
    			"Test the properties of a CDR for a call that is\n"
    			"inbound to Asterisk, executes some dialplan, but\n"
    
    			"is never answered.";
    
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	SWAP_CONFIG(config, unanswered_cdr_config);
    
    	CREATE_ALICE_CHANNEL(chan, &caller, &expected);
    
    	EMULATE_APP_DATA(chan, 1, "Wait", "1");
    
    	HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
    
    	result = verify_mock_cdr_record(test, &expected, 1);
    
    	return result;
    }
    
    AST_TEST_DEFINE(test_cdr_unanswered_outbound_call)
    {
    	RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
    	RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
    			ao2_cleanup);
    
    	struct ast_party_caller caller = {
    			.id.name.str = "",
    			.id.name.valid = 1,
    			.id.number.str = "",
    			.id.number.valid = 1, };
    	struct ast_cdr expected = {
    		.clid = "\"\" <>",
    		.dst = "s",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Alice",
    		.lastapp = "AppDial",
    		.lastdata = "(Outgoing Line)",
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.disposition = AST_CDR_NOANSWER,
    		.accountcode = "100",
    	};
    	enum ast_test_result_state result = AST_TEST_NOT_RUN;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = __func__;
    		info->category = TEST_CATEGORY;
    		info->summary = "Test outbound unanswered calls";
    		info->description =
    			"Test the properties of a CDR for a call that is\n"
    
    			"outbound to Asterisk but is never answered.";
    
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	SWAP_CONFIG(config, unanswered_cdr_config);
    
    	CREATE_ALICE_CHANNEL(chan, &caller, &expected);
    
    	ast_channel_exten_set(chan, "s");
    	ast_channel_context_set(chan, "default");
    	ast_set_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED);
    	EMULATE_APP_DATA(chan, 0, "AppDial", "(Outgoing Line)");
    	HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
    
    	result = verify_mock_cdr_record(test, &expected, 1);
    
    	return result;
    }
    
    
    AST_TEST_DEFINE(test_cdr_outbound_bridged_call)
    {
    	RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
    	RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
    
    	RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
    
    	RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
    			ao2_cleanup);
    	struct timespec to_sleep = {1, 0};
    	enum ast_test_result_state result = AST_TEST_NOT_RUN;
    
    	struct ast_party_caller caller = ALICE_CALLERID;
    	struct ast_cdr alice_expected = {
    		.clid = "\"Alice\" <100>",
    		.src = "100",
    		.dst = "100",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Alice",
    		.dstchannel = CHANNEL_TECH_NAME "/Bob",
    		.lastapp = "",
    		.lastdata = "",
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.billsec = 1,
    		.disposition = AST_CDR_ANSWERED,
    		.accountcode = "100",
    		.peeraccount = "200",
    	};
    	struct ast_cdr bob_expected = {
    		.clid = "\"\" <>",
    		.src = "",
    		.dst = "s",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Bob",
    		.dstchannel = "",
    		.lastapp = "AppDial",
    		.lastdata = "(Outgoing Line)",
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.billsec = 1,
    		.disposition = AST_CDR_ANSWERED,
    		.accountcode = "200",
    		.peeraccount = "",
    		.next = &alice_expected,
    	};
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = __func__;
    		info->category = TEST_CATEGORY;
    		info->summary = "Test dialing, answering, and going into a 2-party bridge";
    		info->description =
    
    			"The most 'basic' of scenarios";
    
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	SWAP_CONFIG(config, debug_cdr_config);
    
    	CREATE_ALICE_CHANNEL(chan_alice, &caller, &alice_expected);
    	ast_channel_state_set(chan_alice, AST_STATE_UP);
    
    	bridge = ast_bridge_basic_new();
    	ast_test_validate(test, bridge != NULL);
    
    	ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
    
    	chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_alice, 0, CHANNEL_TECH_NAME "/Bob");
    
    	SET_FORMATS(chan_bob);
    
    	ast_channel_unlock(chan_bob);
    
    	ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_bob), sizeof(bob_expected.linkedid));
    	ast_copy_string(bob_expected.uniqueid, ast_channel_uniqueid(chan_bob), sizeof(bob_expected.uniqueid));
    	ast_set_flag(ast_channel_flags(chan_bob), AST_FLAG_OUTGOING);
    	ast_set_flag(ast_channel_flags(chan_bob), AST_FLAG_ORIGINATED);
    	EMULATE_APP_DATA(chan_bob, 0, "AppDial", "(Outgoing Line)");
    
    	ast_channel_publish_dial(NULL, chan_bob, "Bob", NULL);
    	ast_channel_state_set(chan_bob, AST_STATE_RINGING);
    	ast_channel_publish_dial(NULL, chan_bob, NULL, "ANSWER");
    
    	ast_channel_state_set(chan_bob, AST_STATE_UP);
    
    
    	ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
    
    
    	ast_bridge_depart(chan_bob);
    	ast_bridge_depart(chan_alice);
    
    	HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
    	HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL);
    
    	result = verify_mock_cdr_record(test, &bob_expected, 2);
    	return result;
    }
    
    
    
    AST_TEST_DEFINE(test_cdr_single_party)
    {
    	RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
    	RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
    			ao2_cleanup);
    
    	struct ast_party_caller caller = ALICE_CALLERID;
    	struct ast_cdr expected = {
    		.clid = "\"Alice\" <100>",
    		.src = "100",
    		.dst = "100",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Alice",
    		.dstchannel = "",
    		.lastapp = "VoiceMailMain",
    		.lastdata = "1",
    		.billsec = 1,
    
    	.amaflags = AST_AMA_DOCUMENTATION,
    
    		.disposition = AST_CDR_ANSWERED,
    		.accountcode = "100",
    	};
    	enum ast_test_result_state result = AST_TEST_NOT_RUN;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = __func__;
    		info->category = TEST_CATEGORY;
    		info->summary = "Test cdrs for a single party";
    		info->description =
    			"Test the properties of a CDR for a call that is\n"
    
    			"answered, but only involves a single channel";
    
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    	SWAP_CONFIG(config, debug_cdr_config);
    	CREATE_ALICE_CHANNEL(chan, &caller, &expected);
    
    
    	EMULATE_APP_DATA(chan, 1, "Answer", "");
    	ast_setstate(chan, AST_STATE_UP);
    	EMULATE_APP_DATA(chan, 2, "VoiceMailMain", "1");
    
    	ast_channel_unlock(chan);
    
    
    	HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
    
    	result = verify_mock_cdr_record(test, &expected, 1);
    
    	return result;
    }
    
    AST_TEST_DEFINE(test_cdr_single_bridge)
    {
    	RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
    
    	RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
    
    	RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
    			ao2_cleanup);
    	struct timespec to_sleep = {1, 0};
    
    	struct ast_party_caller caller = ALICE_CALLERID;
    	struct ast_cdr expected = {
    		.clid = "\"Alice\" <100>",
    		.src = "100",
    		.dst = "100",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Alice",
    		.lastapp = "Bridge",
    		.billsec = 1,
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.disposition = AST_CDR_ANSWERED,
    		.accountcode = "100",
    	};
    	enum ast_test_result_state result = AST_TEST_NOT_RUN;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = __func__;
    		info->category = TEST_CATEGORY;
    		info->summary = "Test cdrs for a single party entering/leaving a bridge";
    		info->description =
    			"Test the properties of a CDR for a call that is\n"
    
    			"answered, enters a bridge, and leaves it.";
    
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    	SWAP_CONFIG(config, debug_cdr_config);
    	CREATE_ALICE_CHANNEL(chan, &caller, &expected);
    
    
    	EMULATE_APP_DATA(chan, 1, "Answer", "");
    	ast_setstate(chan, AST_STATE_UP);
    	EMULATE_APP_DATA(chan, 2, "Bridge", "");
    
    	ast_channel_unlock(chan);
    
    
    	bridge = ast_bridge_basic_new();
    	ast_test_validate(test, bridge != NULL);
    
    
    	ast_test_validate(test, !ast_bridge_impart(bridge, chan, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
    
    
    	ast_bridge_depart(chan);
    
    	HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
    
    	result = verify_mock_cdr_record(test, &expected, 1);
    
    	return result;
    }
    
    AST_TEST_DEFINE(test_cdr_single_bridge_continue)
    {
    	RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
    
    	RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
    
    	RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
    			ao2_cleanup);
    	struct timespec to_sleep = {1, 0};
    
    	struct ast_party_caller caller = ALICE_CALLERID;
    	struct ast_cdr expected_two = {
    		.clid = "\"Alice\" <100>",
    		.src = "100",
    		.dst = "100",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Alice",
    		.lastapp = "Wait",
    		.billsec = 1,
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.disposition = AST_CDR_ANSWERED,
    		.accountcode = "100",
    	};
    	struct ast_cdr expected_one = {
    		.clid = "\"Alice\" <100>",
    		.src = "100",
    		.dst = "100",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Alice",
    		.lastapp = "Bridge",
    		.billsec = 1,
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.disposition = AST_CDR_ANSWERED,
    		.accountcode = "100",
    		.next = &expected_two,
    	};
    
    	enum ast_test_result_state result = AST_TEST_NOT_RUN;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = __func__;
    		info->category = TEST_CATEGORY;
    		info->summary = "Test cdrs for a single party entering/leaving a bridge";
    		info->description =
    			"Test the properties of a CDR for a call that is\n"
    
    			"answered, enters a bridge, and leaves it.";
    
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    	SWAP_CONFIG(config, debug_cdr_config);
    	CREATE_ALICE_CHANNEL(chan, &caller, &expected_one);
    	COPY_IDS(chan, &expected_two);
    
    
    	EMULATE_APP_DATA(chan, 1, "Answer", "");
    	ast_setstate(chan, AST_STATE_UP);
    	EMULATE_APP_DATA(chan, 2, "Bridge", "");
    
    	ast_channel_unlock(chan);
    
    	bridge = ast_bridge_basic_new();
    	ast_test_validate(test, bridge != NULL);
    	do_sleep(&to_sleep);
    
    	ast_test_validate(test, !ast_bridge_impart(bridge, chan, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
    
    
    	ast_bridge_depart(chan);
    
    	EMULATE_APP_DATA(chan, 3, "Wait", "");
    
    	/* And then it hangs up */
    	HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
    
    	result = verify_mock_cdr_record(test, &expected_one, 2);
    
    	return result;
    }
    
    AST_TEST_DEFINE(test_cdr_single_twoparty_bridge_a)
    {
    	RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
    	RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
    
    	RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
    
    	RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
    			ao2_cleanup);
    	struct timespec to_sleep = {1, 0};
    
    	struct ast_party_caller caller_alice = ALICE_CALLERID;
    	struct ast_party_caller caller_bob = BOB_CALLERID;
    	struct ast_cdr bob_expected = {
    		.clid = "\"Bob\" <200>",
    		.src = "200",
    		.dst = "200",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Bob",
    		.lastapp = "Bridge",
    		.billsec = 1,
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.disposition = AST_CDR_ANSWERED,
    		.accountcode = "200",
    	};
    	struct ast_cdr alice_expected = {
    		.clid = "\"Alice\" <100>",
    		.src = "100",
    		.dst = "100",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Alice",
    		.dstchannel = CHANNEL_TECH_NAME "/Bob",
    		.lastapp = "Bridge",
    		.billsec = 1,
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.disposition = AST_CDR_ANSWERED,
    		.accountcode = "100",
    		.peeraccount = "200",
    		.next = &bob_expected,
    	};
    
    	enum ast_test_result_state result = AST_TEST_NOT_RUN;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = __func__;
    		info->category = TEST_CATEGORY;
    		info->summary = "Test cdrs for a single party entering/leaving a bridge";
    		info->description =
    			"Test the properties of a CDR for a call that is\n"
    			"answered, enters a bridge, and leaves it. In this scenario, the\n"
    
    			"Party A should answer the bridge first.";
    
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    	SWAP_CONFIG(config, debug_cdr_config);
    	CREATE_ALICE_CHANNEL(chan_alice, &caller_alice, &alice_expected);
    
    	CREATE_BOB_CHANNEL(chan_bob, &caller_bob, &bob_expected);
    	ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_alice), sizeof(bob_expected.linkedid));
    
    
    	ast_channel_lock(chan_alice);
    
    	EMULATE_APP_DATA(chan_alice, 1, "Answer", "");
    	ast_setstate(chan_alice, AST_STATE_UP);
    	EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
    
    	ast_channel_unlock(chan_alice);
    
    
    	bridge = ast_bridge_basic_new();
    	ast_test_validate(test, bridge != NULL);
    
    
    	ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
    
    	ast_channel_lock(chan_bob);
    
    	EMULATE_APP_DATA(chan_bob, 1, "Answer", "");
    	ast_setstate(chan_bob, AST_STATE_UP);
    	EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
    
    	ast_channel_unlock(chan_bob);
    
    	ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
    
    
    	ast_bridge_depart(chan_alice);
    	ast_bridge_depart(chan_bob);
    
    	HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL);
    	HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
    
    	result = verify_mock_cdr_record(test, &alice_expected, 2);
    
    	return result;
    }
    
    AST_TEST_DEFINE(test_cdr_single_twoparty_bridge_b)
    {
    	RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
    	RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
    
    	RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
    
    	RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
    			ao2_cleanup);
    	struct timespec to_sleep = {1, 0};
    
    	struct ast_party_caller caller_alice = ALICE_CALLERID;
    	struct ast_party_caller caller_bob = BOB_CALLERID;
    	struct ast_cdr bob_expected = {
    		.clid = "\"Bob\" <200>",
    		.src = "200",
    		.dst = "200",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Bob",
    		.lastapp = "Bridge",
    		.billsec = 1,
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.disposition = AST_CDR_ANSWERED,
    		.accountcode = "200",
    	};
    	struct ast_cdr alice_expected = {
    		.clid = "\"Alice\" <100>",
    		.src = "100",
    		.dst = "100",
    		.dcontext = "default",
    		.channel = CHANNEL_TECH_NAME "/Alice",
    		.dstchannel = CHANNEL_TECH_NAME "/Bob",
    		.lastapp = "Bridge",
    		.billsec = 1,
    		.amaflags = AST_AMA_DOCUMENTATION,
    		.disposition = AST_CDR_ANSWERED,
    		.accountcode = "100",
    		.peeraccount = "200",
    		.next = &bob_expected,
    	};
    
    	enum ast_test_result_state result = AST_TEST_NOT_RUN;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = __func__;
    		info->category = TEST_CATEGORY;
    		info->summary = "Test cdrs for a single party entering/leaving a bridge";
    		info->description =
    			"Test the properties of a CDR for a call that is\n"
    			"answered, enters a bridge, and leaves it. In this scenario, the\n"
    
    			"Party B should answer the bridge first.";
    
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    	SWAP_CONFIG(config, debug_cdr_config);
    	CREATE_ALICE_CHANNEL(chan_alice, &caller_alice, &alice_expected);
    
    	CREATE_BOB_CHANNEL(chan_bob, &caller_bob, &bob_expected);
    	ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_alice), sizeof(bob_expected.linkedid));
    
    
    	ast_channel_unlock(chan_alice);
    
    	EMULATE_APP_DATA(chan_alice, 1, "Answer", "");
    	ast_setstate(chan_alice, AST_STATE_UP);
    	EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
    
    	ast_channel_unlock(chan_alice);
    
    
    	bridge = ast_bridge_basic_new();
    	ast_test_validate(test, bridge != NULL);
    
    
    	ast_channel_lock(chan_bob);
    
    	EMULATE_APP_DATA(chan_bob, 1, "Answer", "");
    	ast_setstate(chan_bob, AST_STATE_UP);
    	EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
    
    	ast_channel_unlock(chan_bob);
    
    	ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
    
    	ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
    
    
    	ast_bridge_depart(chan_alice);
    	ast_bridge_depart(chan_bob);