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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2010, Digium, Inc.
*
* Mark Michelson <mmichelson@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 PBX Tests
*
* \author Mark Michelson <mmichelson@digium.com>
*
* This module will run some PBX tests.
* \ingroup tests
*/
/*** MODULEINFO
<depend>TEST_FRAMEWORK</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 337061 $")
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/test.h"
/*!
* If we determine that we really need
* to be able to register more than 10
* priorities for a single extension, then
* fine, we can do that later.
*/
#define MAX_PRIORITIES 10
/*!
* \brief an extension to add to our context
*/
struct exten_info {
/*!
* \brief Context
*
* \details
* The extension specified will be added to
* this context when it is created.
*/
const char *context;
/*!
* \brief Extension pattern
*
* \details
* The extension pattern to use. This can be
* anything you would normally find in a dialplan,
* such as "1000" or "NXXNXXX" or whatever you
* wish it to be. If, however, you want a CID match
* to be part of the extension, do not include that
* here.
*/
const char *exten;
/*!
* \brief CID match
*
* \details
* If your extension requires a specific caller ID in
* order to match, place that in this field. Note that
* a NULL and an empty CID match are two very different
* things. If you want no CID match, leave this NULL. If
* you want to explicitly match a blank CID, then put
* an empty string here.
*/
const char *cid;
/*!
* \brief Number of priorities
*
* \details
* Tell the number of priorities to register for this
* extension. All priorities registered will just have a
* Noop application with the extension pattern as its
* data.
*/
const int num_priorities;
/*!
* \brief The priorities to register
*
* \details
* In most cases, when registering multiple priorities for
* an extension, we'll be starting at priority 1 and going
* sequentially until we've read num_priorities. However,
* for some tests, it may be beneficial to start at a higher
* priority or skip certain priorities. This is why you have
* the freedom here to specify which priorities to register
* for the extension.
*/
const int priorities[MAX_PRIORITIES];
};
struct pbx_test_pattern {
/*!
* \brief Test context
*
* \details
* This is the context to look in for a specific extension.
*/
const char *context;
/*!
* \brief Test extension number
*
* \details
* This should be in the form of a specific number or string.
* For instance, if you were trying to match an extension defined
* with the pattern "_2." you might have as the test_exten one of
* "2000" , "2legit2quit" or some other specific match for the pattern.
*/
const char *test_exten;
/*!
* \brief Test CID match
*
* \details
* If a specific CID match is required for pattern matching, then specify
* it in this parameter. Remember that a NULL CID and an empty CID are
* interpreted differently. For no CID match, leave this NULL. If you wish
* to explicitly match an empty CID, then use an empty string here.
*/
const char *test_cid;
/*!
* \brief The priority to find
*/
const int priority;
/*!
* \brief Expected extension match.
*
* \details
* This struct corresponds to an extension that was previously
* added to our test context. Once we have used all the above data
* to find an extension in the dialplan. We compare the data from that
* extension to the data that we have stored in this structure to be
* sure that what was matched was what we expected to match.
*/
const struct exten_info *exten;
};
static int test_exten(const struct pbx_test_pattern *test_pattern, struct ast_test *test, int new_engine)
{
struct pbx_find_info pfi = { { 0 }, };
struct ast_exten *exten;
if (!(exten = pbx_find_extension(NULL, NULL, &pfi, test_pattern->context,
test_pattern->test_exten, test_pattern->priority, NULL,
test_pattern->test_cid, E_MATCH))) {
ast_test_status_update(test, "Cannot find extension %s in context %s with the %s pattern match engine. "
"Test failed.\n", test_pattern->test_exten, test_pattern->context, (new_engine ? "new" : "old"));
return -1;
}
if (strcmp(ast_get_extension_name(exten), test_pattern->exten->exten)) {
ast_test_status_update(test, "Expected extension %s but got extension %s instead with the %s pattern match engine. "
"Test failed.\n", test_pattern->exten->exten, ast_get_extension_name(exten), (new_engine ? "new" : "old"));
return -1;
}
if (test_pattern->test_cid && strcmp(ast_get_extension_cidmatch(exten), test_pattern->test_cid)) {
ast_test_status_update(test, "Expected CID match %s but got CID match %s instead with the %s pattern match engine. "
"Test failed.\n", test_pattern->exten->cid, ast_get_extension_cidmatch(exten), (new_engine ? "new" : "old"));
return -1;
}
if (!ast_canmatch_extension(NULL, test_pattern->context, test_pattern->test_exten,
test_pattern->priority, test_pattern->test_cid)) {
ast_test_status_update(test, "Partial match failed for extension %s in context %s with the %s pattern match engine. "
"Test failed.\n", test_pattern->test_exten, test_pattern->context, (new_engine ? "new" : "old"));
return -1;
}
ast_test_status_update(test, "Successfully matched %s to exten %s in context %s with the %s pattern match engine\n",
test_pattern->test_exten, test_pattern->exten->exten, test_pattern->context, (new_engine ? "new" : "old"));
return 0;
}
AST_TEST_DEFINE(pattern_match_test)
{
static const char registrar[] = "test_pbx";
enum ast_test_result_state res = AST_TEST_PASS;
static const char TEST_PATTERN[] = "test_pattern";
static const char TEST_PATTERN_INCLUDE[] = "test_pattern_include";
int i, j;
/* The array of contexts to register for our test.
* To add more contexts, just add more rows to this array.
*/
struct {
const char * context_string;
struct ast_context *context;
} contexts[] = {
{ TEST_PATTERN, },
{ TEST_PATTERN_INCLUDE, },
};
/*
* Map to indicate which contexts should be included inside
* other contexts. The first context listed will include
* the second context listed.
*
* To add more inclusions, add new rows to this array.
*/
const struct {
const char *outer_context;
const char *inner_context;
} context_includes[] = {
{ TEST_PATTERN, TEST_PATTERN_INCLUDE },
};
/* The array of extensions to add to our test context.
* For more information about the individual fields, see
* the doxygen for struct exten_info.
*
* To add new extensions to the test, simply add new rows
* to this array. All extensions will automatically be
* added when the test is run.
*/
const struct exten_info extens[] = {
[0] = { TEST_PATTERN, "_2.", NULL, 1, { 1 } },
[1] = { TEST_PATTERN, "2000", NULL, 1, { 1 } },
[2] = { TEST_PATTERN_INCLUDE, "2000", NULL, 1, { 2 } },
};
/* This array contains our test material. See the doxygen
* for struct pbx_test_pattern for more information on each
* component.
*
* To add more test cases, add more lines to this array. Each
* case will be tested automatically when the test is run.
*/
const struct pbx_test_pattern tests[] = {
{ TEST_PATTERN, "200", NULL, 1, &extens[0] },
{ TEST_PATTERN, "2000", NULL, 1, &extens[1] },
{ TEST_PATTERN, "2000", NULL, 2, &extens[2] },
{ TEST_PATTERN_INCLUDE, "2000", NULL, 2, &extens[2] },
};
switch (cmd) {
case TEST_INIT:
info->name = "pattern_match_test";
info->category = "/main/pbx/";
info->summary = "Test pattern matching";
info->description = "Create a context with a bunch of extensions within. Then attempt\n"
"to match some strings to the extensions.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* Step one is to build the dialplan.
*
* We iterate first through the contexts array to build
* all the contexts we'll need. Then, we iterate over the
* extens array to add all the extensions to the appropriate
* contexts.
*/
for (i = 0; i < ARRAY_LEN(contexts); ++i) {
if (!(contexts[i].context = ast_context_find_or_create(NULL, NULL, contexts[i].context_string, registrar))) {
ast_test_status_update(test, "Failed to create context %s\n", contexts[i].context_string);
res = AST_TEST_FAIL;
goto cleanup;
}
}
for (i = 0; i < ARRAY_LEN(context_includes); ++i) {
if (ast_context_add_include(context_includes[i].outer_context,
context_includes[i].inner_context, registrar)) {
ast_test_status_update(test, "Failed to include context %s inside context %s\n",
context_includes[i].inner_context, context_includes[i].outer_context);
res = AST_TEST_FAIL;
goto cleanup;
}
}
for (i = 0; i < ARRAY_LEN(extens); ++i) {
int priority;
if (extens[i].num_priorities > MAX_PRIORITIES) {
ast_test_status_update(test, "Invalid number of priorities specified for extension %s."
"Max is %d, but we requested %d. Test failed\n",
extens[i].exten, MAX_PRIORITIES, extens[i].num_priorities);
res = AST_TEST_FAIL;
goto cleanup;
}
for (priority = 0; priority < extens[i].num_priorities; ++priority) {
if (ast_add_extension(extens[i].context, 0, extens[i].exten, extens[i].priorities[priority],
NULL, extens[i].cid, "Noop", (void *) extens[i].exten, NULL, registrar)) {
ast_test_status_update(test, "Failed to add extension %s, priority %d, to context %s."
"Test failed\n", extens[i].exten, extens[i].priorities[priority], extens[i].context);
res = AST_TEST_FAIL;
goto cleanup;
}
}
}
/* At this stage, the dialplan is built. Now we iterate over
* the tests array to attempt to find each of the specified
* extensions with the old and new pattern matching engines.
*/
for (j = 0; j < 2; j++) {
pbx_set_extenpatternmatchnew(j);
for (i = 0; i < ARRAY_LEN(tests); ++i) {
if (test_exten(&tests[i], test, j)) {
res = AST_TEST_FAIL;
break;
}
}
}
cleanup:
for (i = 0; i < ARRAY_LEN(contexts); ++i) {
if (contexts[i].context) {
ast_context_destroy(contexts[i].context, registrar);
}
}
return res;
}
static int unload_module(void)
{
AST_TEST_UNREGISTER(pattern_match_test);
return 0;
}
static int load_module(void)
{
AST_TEST_REGISTER(pattern_match_test);
return AST_MODULE_LOAD_SUCCESS;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "PBX test module");
|