Skip to content
Snippets Groups Projects
test_voicemail_api.c 60.3 KiB
Newer Older
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012, Matt Jordan
 *
 * 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 Skeleton Test
 *
 * \author\verbatim Matt Jordan <mjordan@digium.com> \endverbatim
 *
 * Tests for the publicly exposed Voicemail API
 * \ingroup tests
 */

/*** MODULEINFO
	<depend>TEST_FRAMEWORK</depend>
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include <sys/stat.h>

#include "asterisk/utils.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/paths.h"
#include "asterisk/channel.h"
#include "asterisk/app.h"
Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief Permissions to set on the voicemail directories we create
 *
 * \note taken from app_voicemail
 */
#define VOICEMAIL_DIR_MODE 0777

Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief Permissions to set on the voicemail files we create
 *
 * \note taken from app_voicemail
 */
#define VOICEMAIL_FILE_MODE 0666

Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief The number of mock snapshot objects we use for tests
 */
#define TOTAL_SNAPSHOTS 4

Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief Create and populate the mock message objects and create the
 * envelope files on the file system
 */
#define VM_API_TEST_SETUP do { \
	if (!ast_vm_is_registered()) { \
		ast_test_status_update(test, "No voicemail provider registered.\n"); \
		return AST_TEST_FAIL; \
	} else if (test_vm_api_test_setup()) { \
		VM_API_TEST_CLEANUP; \
		ast_test_status_update(test, "Failed to set up necessary mock objects for voicemail API test\n"); \
		return AST_TEST_FAIL; \
	} else { \
		int i = 0; \
		for (; i < TOTAL_SNAPSHOTS; i++) { \
			ast_test_status_update(test, "Created message in %s/%s with ID %s\n", \
				test_snapshots[i]->exten, test_snapshots[i]->folder_name, test_snapshots[i]->msg_id); \
		} \
} } while (0)

Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief Safely cleanup after a test run.
 *
 * \note This should be called both when a test fails and when it passes
 */
#define VM_API_TEST_CLEANUP test_vm_api_test_teardown()

Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief Safely cleanup a snapshot and a test run.
 *
 * \note It assumes that the mailbox snapshot object is test_mbox_snapshot
 */
#define VM_API_SNAPSHOT_TEST_CLEANUP \
		if (test_mbox_snapshot) { \
			test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot); \
		} \
		VM_API_TEST_CLEANUP; \

Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief Verify the expected result from two string values obtained
 * from a mailbox snapshot.
 *
 * \note It assumes the mailbox snapshot object is test_mbox_snapshot
 */
#define VM_API_STRING_FIELD_VERIFY(expected, actual) do { \
	if (strcmp((expected), (actual))) { \
		ast_test_status_update(test, "Test failed for parameter %s: Expected [%s], Actual [%s]\n", #actual, expected, actual); \
		VM_API_SNAPSHOT_TEST_CLEANUP; \
		return AST_TEST_FAIL; \
	} } while (0)

Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief Verify the expected result from two integer values.
 *
 * \note It assumes the mailbox snapshot object is test_mbox_snapshot
 */
#define VM_API_INT_VERIFY(expected, actual) do { \
	if ((expected) != (actual)) { \
		ast_test_status_update(test, "Test failed for parameter %s: Expected [%d], Actual [%d]\n", #actual, (int)expected, (int)actual); \
		VM_API_SNAPSHOT_TEST_CLEANUP; \
		return AST_TEST_FAIL; \
	} } while (0)

Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief Verify that a mailbox snapshot contains the expected message
 * snapshot, in the correct position, with the expected values.
 *
 * \note It assumes the mailbox snapshot object is test_mbox_snapshot
 */
#define VM_API_SNAPSHOT_MSG_VERIFY(expected, actual, expected_folder, expected_index) do { \
	struct ast_vm_msg_snapshot *msg; \
	int found = 0; \
	int counter = 0; \
	AST_LIST_TRAVERSE(&((actual)->snapshots[get_folder_by_name(expected_folder)]), msg, msg) { \
		if (!(strcmp(msg->msg_id, (expected)->msg_id))) { \
			ast_test_status_update(test, "Found message %s in snapshot\n", msg->msg_id); \
			found = 1; \
			if ((expected_index) != counter) { \
				ast_test_status_update(test, "Expected message %s at index %d; Actual [%d]\n", \
					(expected)->msg_id, (expected_index), counter); \
				VM_API_SNAPSHOT_TEST_CLEANUP; \
				return AST_TEST_FAIL; \
			} \
			VM_API_STRING_FIELD_VERIFY((expected)->callerid, msg->callerid); \
			VM_API_STRING_FIELD_VERIFY((expected)->callerchan, msg->callerchan); \
			VM_API_STRING_FIELD_VERIFY((expected)->exten, msg->exten); \
			VM_API_STRING_FIELD_VERIFY((expected)->origdate, msg->origdate); \
			VM_API_STRING_FIELD_VERIFY((expected)->origtime, msg->origtime); \
			VM_API_STRING_FIELD_VERIFY((expected)->duration, msg->duration); \
			VM_API_STRING_FIELD_VERIFY((expected)->folder_name, msg->folder_name); \
			VM_API_STRING_FIELD_VERIFY((expected)->flag, msg->flag); \
			VM_API_INT_VERIFY((expected)->msg_number, msg->msg_number); \
			break; \
		} \
		++counter; \
	} \
	if (!found) { \
		ast_test_status_update(test, "Test failed for message snapshot %s: not found in mailbox snapshot\n", (expected)->msg_id); \
		VM_API_SNAPSHOT_TEST_CLEANUP; \
		return AST_TEST_FAIL; \
} } while (0)


Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief Create a message snapshot, failing the test if the snapshot could not be created.
 *
 * \note This requires having a snapshot named test_mbox_snapshot.
 */
#define VM_API_SNAPSHOT_CREATE(mailbox, context, folder, desc, sort, old_and_inbox) do { \
	if (!(test_mbox_snapshot = ast_vm_mailbox_snapshot_create( \
		(mailbox), (context), (folder), (desc), (sort), (old_and_inbox)))) { \
		ast_test_status_update(test, "Failed to create voicemail mailbox snapshot\n"); \
		VM_API_TEST_CLEANUP; \
		return AST_TEST_FAIL; \
	} } while (0)

Richard Mudgett's avatar
Richard Mudgett committed
/*!
 * \internal
 * \brief Create a message snapshot, failing the test if the snapshot could be created.
 *
 * \note This is used to test off nominal conditions.
 * \note This requires having a snapshot named test_mbox_snapshot.
 */
#define VM_API_SNAPSHOT_OFF_NOMINAL_TEST(mailbox, context, folder, desc, sort, old_and_inbox) do { \
	if ((test_mbox_snapshot = ast_vm_mailbox_snapshot_create( \
		(mailbox), (context), (folder), (desc), (sort), (old_and_inbox)))) { \
Loading
Loading full blame...