123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/module.h"
- #include "asterisk/pbx.h"
- #include "asterisk/test.h"
- #define MAX_PRIORITIES 10
- struct exten_info {
-
- const char *context;
-
- const char *exten;
-
- const char *cid;
-
- const int num_priorities;
-
- const int priorities[MAX_PRIORITIES];
- };
- struct pbx_test_pattern {
-
- const char *context;
-
- const char *test_exten;
-
- const char *test_cid;
-
- const int priority;
-
- 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;
-
- struct {
- const char * context_string;
- struct ast_context *context;
- } contexts[] = {
- { TEST_PATTERN, },
- { TEST_PATTERN_INCLUDE, },
- };
-
- const struct {
- const char *outer_context;
- const char *inner_context;
- } context_includes[] = {
- { TEST_PATTERN, TEST_PATTERN_INCLUDE },
- };
-
- 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 } },
- };
-
- 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;
- }
-
- 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;
- }
- }
- }
-
- 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");
|