summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Peeler <jpeeler@digium.com>2010-02-17 19:51:53 +0000
committerJeff Peeler <jpeeler@digium.com>2010-02-17 19:51:53 +0000
commit27a4cda821b3093bb41c29e23d1fb9d62890597a (patch)
tree850473272a4f4cf99ce0a3c90eecca8facc60bcc
parente8d201e870bbadea78d448e21cc2c19c3da8c13d (diff)
Add support for GROUP_MATCH_COUNT regex matching on category
Current support for regex matching was previously only available on the group. Also, error reporting for regex failures has been added. In addition to this feature enhancement a unit test has been written to check the regular expression logic to ensure the count operation is working as expected. (closes issue #16642) Reported by: kobaz Patches: groupmatch2.patch uploaded by kobaz (license 834) Review: https://reviewboard.asterisk.org/r/503/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@247295 65c4cc65-6c06-0410-ace0-fbb531ad65f3
-rw-r--r--CHANGES1
-rw-r--r--funcs/func_groupcount.c5
-rw-r--r--main/app.c19
-rw-r--r--tests/test_app.c159
4 files changed, 176 insertions, 8 deletions
diff --git a/CHANGES b/CHANGES
index a2b6c5658..f430a7bb1 100644
--- a/CHANGES
+++ b/CHANGES
@@ -191,6 +191,7 @@ Dialplan Functions
* HASH-associated variables now can be inherited across channel creation, by
prefixing the name of the hash at assignment with the appropriate number of
underscores, just like variables.
+ * GROUP_MATCH_COUNT has been improved to allow regex matching on category
Dialplan Variables
------------------
diff --git a/funcs/func_groupcount.c b/funcs/func_groupcount.c
index 4ee88a882..70c396436 100644
--- a/funcs/func_groupcount.c
+++ b/funcs/func_groupcount.c
@@ -58,12 +58,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
<para>A standard regular expression used to match a group name.</para>
</parameter>
<parameter name="category">
- <para>Category name.</para>
+ <para>A standard regular expression used to match a category name.</para>
</parameter>
</syntax>
<description>
<para>Calculates the group count for all groups that match the specified pattern.
- Uses standard regular expression matching (see regex(7)).</para>
+ Note: category matching is applied after matching based on group.
+ Uses standard regular expression matching on both (see regex(7)).</para>
</description>
</function>
<function name="GROUP" language="en_US">
diff --git a/main/app.c b/main/app.c
index 24676713f..7cb7e30b0 100644
--- a/main/app.c
+++ b/main/app.c
@@ -1091,27 +1091,34 @@ int ast_app_group_get_count(const char *group, const char *category)
int ast_app_group_match_get_count(const char *groupmatch, const char *category)
{
struct ast_group_info *gi = NULL;
- regex_t regexbuf;
+ regex_t regexbuf_group;
+ regex_t regexbuf_category;
int count = 0;
- if (ast_strlen_zero(groupmatch)) {
+ if (ast_strlen_zero(groupmatch))
return 0;
- }
/* if regex compilation fails, return zero matches */
- if (regcomp(&regexbuf, groupmatch, REG_EXTENDED | REG_NOSUB)) {
+ if (regcomp(&regexbuf_group, groupmatch, REG_EXTENDED | REG_NOSUB)) {
+ ast_log(LOG_ERROR, "Regex compile failed on: %s\n", groupmatch);
+ return 0;
+ }
+
+ if (regcomp(&regexbuf_category, category, REG_EXTENDED | REG_NOSUB)) {
+ ast_log(LOG_ERROR, "Regex compile failed on: %s\n", category);
return 0;
}
AST_RWLIST_RDLOCK(&groups);
AST_RWLIST_TRAVERSE(&groups, gi, group_list) {
- if (!regexec(&regexbuf, gi->group, 0, NULL, 0) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))) {
+ if (!regexec(&regexbuf_group, gi->group, 0, NULL, 0) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !regexec(&regexbuf_category, gi->category, 0, NULL, 0)))) {
count++;
}
}
AST_RWLIST_UNLOCK(&groups);
- regfree(&regexbuf);
+ regfree(&regexbuf_group);
+ regfree(&regexbuf_category);
return count;
}
diff --git a/tests/test_app.c b/tests/test_app.c
new file mode 100644
index 000000000..fa19b6e9c
--- /dev/null
+++ b/tests/test_app.c
@@ -0,0 +1,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");