summaryrefslogtreecommitdiff
path: root/tests/test_app.c
blob: fa19b6e9cb5fc269163f6e207d9297150ca1111d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2010, Digium, Inc.
 *
 * Jeff Peeler <jpeeler@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 App unit test
 *
 * \author Jeff Peeler <jpeeler@digium.com>
 *
 */

/*** MODULEINFO
	<depend>TEST_FRAMEWORK</depend>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/utils.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/app.h"
#include "asterisk/channel.h"

#define BASE_GROUP "a group"

AST_TEST_DEFINE(app_group)
{
	struct ast_channel *test_channel1 = NULL;
	struct ast_channel *test_channel2 = NULL;
	struct ast_channel *test_channel3 = NULL;
	struct ast_channel *test_channel4 = NULL;

	static const char group1_full[] = BASE_GROUP "groupgroup";
	static const char group2_full[] = BASE_GROUP "Groupgroup";
	static const char regex1[] = "gr"; /* matches everything */
	static const char regex2[] = "(group){2}$"; /* matches only group1_full */
	static const char regex3[] = "[:ascii:]"; /* matches everything */
	static const char regex4[] = "^(NOMATCH)"; /* matches nothing */
	static const char category1_full[] = BASE_GROUP "@a_category"; /* categories shouldn't have spaces */
	static const char category2_full[] = BASE_GROUP "@another!Category";
	static const char regex5[] = "(gory)$"; /* matches both categories */
	static const char regex6[] = "[A-Z]+"; /* matches only category2_full */
	static const char regex7[] = "[["; /* not valid syntax, yes an expected warning will be displayed */
	static enum ast_test_result_state res = AST_TEST_PASS;
	static const struct group_test_params {
		const char *groupmatch;
		const char *category;
		int expected;
	} subtests[] = {
		{ regex1, "", 4 },
		{ regex2, "", 1 },
		{ regex3, "", 4 },
		{ regex4, "", 0 },
		{ BASE_GROUP, regex5, 2 },
		{ BASE_GROUP, regex6, 1 },
		/* this test is expected to generate a warning message from the invalid regex */
		{ BASE_GROUP, regex7, 0 }
	};
	int i;
	int returned_count;

	switch (cmd) {
	case TEST_INIT:
		info->name = "app_group";
		info->category = "main/app/";
		info->summary = "App group unit test";
		info->description =
			"This tests various app group functionality";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	ast_test_status_update(test, "Creating test channels with the following groups:\n"
		"'%s', '%s', '%s', '%s'\n", group1_full, group2_full, category1_full, category2_full);

	if (!(test_channel1 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
        NULL, NULL, 0, 0, "TestChannel1"))) {
		goto exit_group_test;
	}
	if (!(test_channel2 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
        NULL, NULL, 0, 0, "TestChannel2"))) {
		goto exit_group_test;
	}
	if (!(test_channel3 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
        NULL, NULL, 0, 0, "TestChannel3"))) {
		goto exit_group_test;
	}
	if (!(test_channel4 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
        NULL, NULL, 0, 0, "TestChannel4"))) {
		goto exit_group_test;
	}

	ast_app_group_set_channel(test_channel1, group1_full);
	ast_app_group_set_channel(test_channel2, group2_full);
	ast_app_group_set_channel(test_channel3, category1_full);
	ast_app_group_set_channel(test_channel4, category2_full);

	for (i = 0; i < ARRAY_LEN(subtests); i++) {
		ast_assert(subtests[i].groupmatch != NULL || subtests[i].category != NULL);
		returned_count = ast_app_group_match_get_count(subtests[i].groupmatch, subtests[i].category);

		if (subtests[i].expected != returned_count) {
			ast_test_status_update(test, "(Subtest %d) Expected %d matches but found %d when examining group:'%s' category:'%s'\n",
				i + 1, subtests[i].expected, returned_count, subtests[i].groupmatch, subtests[i].category);
			res = AST_TEST_FAIL;
			goto exit_group_test;
		} else {
			ast_test_status_update(test, "(Subtest %d) Found %d matches as expected when examining group:'%s' category:'%s'\n",
				i + 1, subtests[i].expected, subtests[i].groupmatch, subtests[i].category);
		}
	}

exit_group_test:
	if (test_channel1) {
		ast_hangup(test_channel1);
	}
	if (test_channel2) {
		ast_hangup(test_channel2);
	}
	if (test_channel3) {
		ast_hangup(test_channel3);
	}
	if (test_channel4) {
		ast_hangup(test_channel4);
	}
	return res;
}

static int unload_module(void)
{
	AST_TEST_UNREGISTER(app_group);
	return 0;
}

static int load_module(void)
{
	AST_TEST_REGISTER(app_group);
	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "App unit test");