123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2014, Digium, Inc.
- *
- * 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 Test module for out-of-call text message module
- *
- * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
- *
- * \ingroup tests
- */
- /*** MODULEINFO
- <depend>TEST_FRAMEWORK</depend>
- <support_level>core</support_level>
- ***/
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include <regex.h>
- #include "asterisk/module.h"
- #include "asterisk/test.h"
- #include "asterisk/message.h"
- #include "asterisk/pbx.h"
- #include "asterisk/manager.h"
- #include "asterisk/vector.h"
- #define TEST_CATEGORY "/main/message/"
- #define TEST_CONTEXT "__TEST_MESSAGE_CONTEXT__"
- #define TEST_EXTENSION "test_message_extension"
- /*! \brief The number of user events we should get in a dialplan test */
- #define DEFAULT_EXPECTED_EVENTS 4
- /*! \brief The current number of received user events */
- static int received_user_events;
- /*! \brief The number of user events we expect for this test */
- static int expected_user_events;
- /*! \brief Predicate for the \ref test_message_handler receiving a message */
- static int handler_received_message;
- /*! \brief Condition wait variable for all dialplan user events being received */
- static ast_cond_t user_event_cond;
- /*! \brief Mutex for \c user_event_cond */
- AST_MUTEX_DEFINE_STATIC(user_event_lock);
- /*! \brief Condition wait variable for \ref test_msg_handler receiving message */
- static ast_cond_t handler_cond;
- /*! \brief Mutex for \c handler_cond */
- AST_MUTEX_DEFINE_STATIC(handler_lock);
- /*! \brief The expected user event fields */
- AST_VECTOR(var_vector, struct ast_variable *) expected_user_event_fields;
- /*! \brief If a user event fails, the bad headers that didn't match */
- AST_VECTOR(, struct ast_variable *) bad_headers;
- static int test_msg_send(const struct ast_msg *msg, const char *to, const char *from);
- static struct ast_msg_tech test_msg_tech = {
- .name = "testmsg",
- .msg_send = test_msg_send,
- };
- static int test_msg_handle_msg_cb(struct ast_msg *msg);
- static int test_msg_has_destination_cb(const struct ast_msg *msg);
- /*! \brief Our test message handler */
- static struct ast_msg_handler test_msg_handler = {
- .name = "testmsg",
- .handle_msg = test_msg_handle_msg_cb,
- .has_destination = test_msg_has_destination_cb,
- };
- static int user_event_hook_cb(int category, const char *event, char *body);
- /*! \brief AMI event hook that verifies whether or not we've gotten our user events */
- static struct manager_custom_hook user_event_hook = {
- .file = AST_MODULE,
- .helper = user_event_hook_cb,
- };
- /*!
- * \brief Verifies a user event header/value pair
- *
- * \param user_event which user event to check
- * \param header The header to verify
- * \param value The value read from the event
- *
- * \retval -1 on error or evaluation failure
- * \retval 0 if match not needed or success
- */
- static int verify_user_event_fields(int user_event, const char *header, const char *value)
- {
- struct ast_variable *current;
- struct ast_variable *expected;
- regex_t regexbuf;
- int error;
- if (user_event >= AST_VECTOR_SIZE(&expected_user_event_fields)) {
- return -1;
- }
- expected = AST_VECTOR_GET(&expected_user_event_fields, user_event);
- if (!expected) {
- return -1;
- }
- for (current = expected; current; current = current->next) {
- struct ast_variable *bad_header;
- if (strcmp(current->name, header)) {
- continue;
- }
- error = regcomp(®exbuf, current->value, REG_EXTENDED | REG_NOSUB);
- if (error) {
- char error_buf[128];
- regerror(error, ®exbuf, error_buf, sizeof(error_buf));
- ast_log(LOG_ERROR, "Failed to compile regex '%s' for header check '%s': %s\n",
- current->value, current->name, error_buf);
- return -1;
- }
- if (!regexec(®exbuf, value, 0, NULL, 0)) {
- regfree(®exbuf);
- return 0;
- }
- bad_header = ast_variable_new(header, value, __FILE__);
- if (bad_header) {
- struct ast_variable *bad_headers_head = NULL;
- if (user_event < AST_VECTOR_SIZE(&bad_headers)) {
- bad_headers_head = AST_VECTOR_GET(&bad_headers, user_event);
- }
- ast_variable_list_append(&bad_headers_head, bad_header);
- AST_VECTOR_REPLACE(&bad_headers, user_event, bad_headers_head);
- }
- regfree(®exbuf);
- return -1;
- }
- return 0;
- }
- static int message_received;
- static int test_msg_send(const struct ast_msg *msg, const char *to, const char *from)
- {
- message_received = 1;
- return 0;
- }
- static int test_msg_handle_msg_cb(struct ast_msg *msg)
- {
- ast_mutex_lock(&handler_lock);
- handler_received_message = 1;
- ast_cond_signal(&handler_cond);
- ast_mutex_unlock(&handler_lock);
- return 0;
- }
- static int test_msg_has_destination_cb(const struct ast_msg *msg)
- {
- /* We only care about one destination: foo! */
- if (ast_strlen_zero(ast_msg_get_to(msg))) {
- return 0;
- }
- return (!strcmp(ast_msg_get_to(msg), "foo") ? 1 : 0);
- }
- static int user_event_hook_cb(int category, const char *event, char *body)
- {
- char *parse;
- char *kvp;
- if (strcmp(event, "UserEvent")) {
- return -1;
- }
- parse = ast_strdupa(body);
- while ((kvp = strsep(&parse, "\r\n"))) {
- char *key, *value;
- kvp = ast_trim_blanks(kvp);
- if (ast_strlen_zero(kvp)) {
- continue;
- }
- key = strsep(&kvp, ":");
- value = ast_skip_blanks(kvp);
- verify_user_event_fields(received_user_events, key, value);
- }
- received_user_events++;
- ast_mutex_lock(&user_event_lock);
- if (received_user_events == expected_user_events) {
- ast_cond_signal(&user_event_cond);
- }
- ast_mutex_unlock(&user_event_lock);
- return 0;
- }
- /*! \brief Wait for the \ref test_msg_handler to receive the message */
- static int handler_wait_for_message(struct ast_test *test)
- {
- int error = 0;
- struct timeval wait = ast_tvadd(ast_tvnow(), ast_tv(5 /* seconds */, 0));
- struct timespec wait_time = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000 };
- ast_mutex_lock(&handler_lock);
- while (!handler_received_message) {
- error = ast_cond_timedwait(&handler_cond, &handler_lock, &wait_time);
- if (error == ETIMEDOUT) {
- ast_test_status_update(test, "Test timed out while waiting for handler to get message\n");
- ast_test_set_result(test, AST_TEST_FAIL);
- break;
- }
- }
- ast_mutex_unlock(&handler_lock);
- return (error != ETIMEDOUT);
- }
- /*! \brief Wait for the expected number of user events to be received */
- static int user_event_wait_for_events(struct ast_test *test, int expected_events)
- {
- int error;
- struct timeval wait = ast_tvadd(ast_tvnow(), ast_tv(5 /* seconds */, 0));
- struct timespec wait_time = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000 };
- expected_user_events = expected_events;
- ast_mutex_lock(&user_event_lock);
- while (received_user_events != expected_user_events) {
- error = ast_cond_timedwait(&user_event_cond, &user_event_lock, &wait_time);
- if (error == ETIMEDOUT) {
- ast_test_status_update(test, "Test timed out while waiting for %d expected user events\n", expected_events);
- ast_test_set_result(test, AST_TEST_FAIL);
- break;
- }
- }
- ast_mutex_unlock(&user_event_lock);
- ast_test_status_update(test, "Received %d of %d user events\n", received_user_events, expected_events);
- return !(received_user_events == expected_events);
- }
- static int verify_bad_headers(struct ast_test *test)
- {
- int res = 0;
- int i;
- for (i = 0; i < AST_VECTOR_SIZE(&bad_headers); i++) {
- struct ast_variable *headers;
- struct ast_variable *current;
- headers = AST_VECTOR_GET(&bad_headers, i);
- if (!headers) {
- continue;
- }
- res = -1;
- for (current = headers; current; current = current->next) {
- ast_test_status_update(test, "Expected UserEvent %d: Failed to match %s: %s\n",
- i, current->name, current->value);
- ast_test_set_result(test, AST_TEST_FAIL);
- }
- }
- return res;
- }
- AST_TEST_DEFINE(test_message_msg_tech_registration)
- {
- int reg_result;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = TEST_CATEGORY;
- info->summary = "Test register/unregister of a message tech";
- info->description =
- "Test that:\n"
- "\tA message technology can be registered once only\n"
- "\tA registered message technology can be unregistered once only";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- reg_result = ast_msg_tech_register(&test_msg_tech);
- ast_test_validate(test, reg_result == 0);
- reg_result = ast_msg_tech_register(&test_msg_tech);
- ast_test_validate(test, reg_result == -1);
- reg_result = ast_msg_tech_unregister(&test_msg_tech);
- ast_test_validate(test, reg_result == 0);
- reg_result = ast_msg_tech_unregister(&test_msg_tech);
- ast_test_validate(test, reg_result == -1);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(test_message_msg_handler_registration)
- {
- int reg_result;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = TEST_CATEGORY;
- info->summary = "Test register/unregister of a message handler";
- info->description =
- "Test that:\n"
- "\tA message handler can be registered once only\n"
- "\tA registered message handler can be unregistered once only";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- reg_result = ast_msg_handler_register(&test_msg_handler);
- ast_test_validate(test, reg_result == 0);
- reg_result = ast_msg_handler_register(&test_msg_handler);
- ast_test_validate(test, reg_result == -1);
- reg_result = ast_msg_handler_unregister(&test_msg_handler);
- ast_test_validate(test, reg_result == 0);
- reg_result = ast_msg_handler_unregister(&test_msg_handler);
- ast_test_validate(test, reg_result == -1);
- return AST_TEST_PASS;
- }
- static void ast_msg_safe_destroy(void *obj)
- {
- struct ast_msg *msg = obj;
- if (msg) {
- ast_msg_destroy(msg);
- }
- }
- AST_TEST_DEFINE(test_message_manipulation)
- {
- RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
- RAII_VAR(struct ast_msg_var_iterator *, it_vars, NULL, ast_msg_var_iterator_destroy);
- int result;
- const char *actual;
- const char *out_name;
- const char *out_value;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = TEST_CATEGORY;
- info->summary = "Test manipulating properties of a message";
- info->description =
- "This test covers the following:\n"
- "\tSetting/getting the body\n"
- "\tSetting/getting inbound/outbound variables\n"
- "\tIterating over variables";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- msg = ast_msg_alloc();
- ast_test_validate(test, msg != NULL);
- /* Test setting/getting to */
- result = ast_msg_set_to(msg, "testmsg:%s", "foo");
- ast_test_validate(test, result == 0);
- actual = ast_msg_get_to(msg);
- ast_test_validate(test, !strcmp(actual, "testmsg:foo"));
- /* Test setting/getting from */
- result = ast_msg_set_from(msg, "testmsg:%s", "bar");
- ast_test_validate(test, result == 0);
- actual = ast_msg_get_from(msg);
- ast_test_validate(test, !strcmp(actual, "testmsg:bar"));
- /* Test setting/getting body */
- result = ast_msg_set_body(msg, "BodyTest: %s", "foo");
- ast_test_validate(test, result == 0);
- actual = ast_msg_get_body(msg);
- ast_test_validate(test, !strcmp(actual, "BodyTest: foo"));
- /* Test setting/getting technology */
- result = ast_msg_set_tech(msg, "%s", "my_tech");
- ast_test_validate(test, result == 0);
- actual = ast_msg_get_tech(msg);
- ast_test_validate(test, !strcmp(actual, "my_tech"));
- /* Test setting/getting endpoint */
- result = ast_msg_set_endpoint(msg, "%s", "terminus");
- ast_test_validate(test, result == 0);
- actual = ast_msg_get_endpoint(msg);
- ast_test_validate(test, !strcmp(actual, "terminus"));
- /* Test setting/getting non-outbound variable */
- result = ast_msg_set_var(msg, "foo", "bar");
- ast_test_validate(test, result == 0);
- actual = ast_msg_get_var(msg, "foo");
- ast_test_validate(test, !strcmp(actual, "bar"));
- /* Test updating existing variable */
- result = ast_msg_set_var(msg, "foo", "new_bar");
- ast_test_validate(test, result == 0);
- actual = ast_msg_get_var(msg, "foo");
- ast_test_validate(test, !strcmp(actual, "new_bar"));
- /* Verify a non-outbound variable is not iterable */
- it_vars = ast_msg_var_iterator_init(msg);
- ast_test_validate(test, it_vars != NULL);
- ast_test_validate(test, ast_msg_var_iterator_next(msg, it_vars, &out_name, &out_value) == 0);
- ast_msg_var_iterator_destroy(it_vars);
- /* Test updating an existing variable as an outbound variable */
- result = ast_msg_set_var_outbound(msg, "foo", "outbound_bar");
- ast_test_validate(test, result == 0);
- it_vars = ast_msg_var_iterator_init(msg);
- ast_test_validate(test, it_vars != NULL);
- result = ast_msg_var_iterator_next(msg, it_vars, &out_name, &out_value);
- ast_test_validate(test, result == 1);
- ast_test_validate(test, !strcmp(out_name, "foo"));
- ast_test_validate(test, !strcmp(out_value, "outbound_bar"));
- ast_msg_var_unref_current(it_vars);
- result = ast_msg_var_iterator_next(msg, it_vars, &out_name, &out_value);
- ast_test_validate(test, result == 0);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(test_message_queue_dialplan_nominal)
- {
- RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
- struct ast_variable *expected;
- struct ast_variable *expected_response = NULL;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = TEST_CATEGORY;
- info->summary = "Test enqueueing messages to the dialplan";
- info->description =
- "Test that a message enqueued for the dialplan is\n"
- "passed to that particular extension";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- msg = ast_msg_alloc();
- ast_test_validate(test, msg != NULL);
- expected = ast_variable_new("Verify","^To$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- expected = ast_variable_new("Value","^foo$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_REPLACE(&expected_user_event_fields, 0, expected_response);
- expected_response = NULL;
- expected = ast_variable_new("Verify", "^From$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- expected = ast_variable_new("Value","^bar$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_REPLACE(&expected_user_event_fields, 1, expected_response);
- expected_response = NULL;
- expected = ast_variable_new("Verify", "^Body$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- expected = ast_variable_new("Value", "^a body$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_REPLACE(&expected_user_event_fields, 2, expected_response);
- expected_response = NULL;
- expected = ast_variable_new("Verify", "^Custom$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- expected = ast_variable_new("Value", "^field$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_REPLACE(&expected_user_event_fields, 3, expected_response);
- ast_msg_set_to(msg, "foo");
- ast_msg_set_from(msg, "bar");
- ast_msg_set_body(msg, "a body");
- ast_msg_set_var_outbound(msg, "custom_data", "field");
- ast_msg_set_context(msg, TEST_CONTEXT);
- ast_msg_set_exten(msg, TEST_EXTENSION);
- ast_msg_queue(msg);
- msg = NULL;
- if (user_event_wait_for_events(test, DEFAULT_EXPECTED_EVENTS)) {
- ast_test_status_update(test, "Failed to received %d expected user events\n", DEFAULT_EXPECTED_EVENTS);
- return AST_TEST_FAIL;
- }
- if (verify_bad_headers(test)) {
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(test_message_queue_handler_nominal)
- {
- RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
- int result;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = TEST_CATEGORY;
- info->summary = "Test enqueueing messages to a handler";
- info->description =
- "Test that a message enqueued can be handled by a\n"
- "non-dialplan handler";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- msg = ast_msg_alloc();
- ast_test_validate(test, msg != NULL);
- result = ast_msg_handler_register(&test_msg_handler);
- ast_test_validate(test, result == 0);
- ast_msg_set_to(msg, "foo");
- ast_msg_set_from(msg, "bar");
- ast_msg_set_body(msg, "a body");
- ast_msg_queue(msg);
- msg = NULL;
- /* This will automatically fail the test if we don't get the message */
- handler_wait_for_message(test);
- result = ast_msg_handler_unregister(&test_msg_handler);
- ast_test_validate(test, result == 0);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(test_message_queue_both_nominal)
- {
- RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
- struct ast_variable *expected;
- struct ast_variable *expected_response = NULL;
- int result;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = TEST_CATEGORY;
- info->summary = "Test enqueueing messages to a dialplan and custom handler";
- info->description =
- "Test that a message enqueued is passed to all\n"
- "handlers that can process it, dialplan as well as\n"
- "a custom handler";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- msg = ast_msg_alloc();
- ast_test_validate(test, msg != NULL);
- result = ast_msg_handler_register(&test_msg_handler);
- ast_test_validate(test, result == 0);
- expected = ast_variable_new("Verify","^To$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- expected = ast_variable_new("Value","^foo$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_REPLACE(&expected_user_event_fields, 0, expected_response);
- expected_response = NULL;
- expected = ast_variable_new("Verify", "^From$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- expected = ast_variable_new("Value","^bar$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_REPLACE(&expected_user_event_fields, 1, expected_response);
- expected_response = NULL;
- expected = ast_variable_new("Verify", "^Body$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- expected = ast_variable_new("Value", "^a body$", __FILE__);
- ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_REPLACE(&expected_user_event_fields, 2, expected_response);
- ast_msg_set_to(msg, "foo");
- ast_msg_set_from(msg, "bar");
- ast_msg_set_body(msg, "a body");
- ast_msg_set_context(msg, TEST_CONTEXT);
- ast_msg_set_exten(msg, TEST_EXTENSION);
- ast_msg_queue(msg);
- msg = NULL;
- if (user_event_wait_for_events(test, DEFAULT_EXPECTED_EVENTS)) {
- ast_test_status_update(test, "Failed to received %d expected user events\n", DEFAULT_EXPECTED_EVENTS);
- ast_test_set_result(test, AST_TEST_FAIL);
- }
- /* This will automatically fail the test if we don't get the message */
- handler_wait_for_message(test);
- result = ast_msg_handler_unregister(&test_msg_handler);
- ast_test_validate(test, result == 0);
- if (verify_bad_headers(test)) {
- return AST_TEST_FAIL;
- }
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(test_message_has_destination_dialplan)
- {
- RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = TEST_CATEGORY;
- info->summary = "Test checking for a dialplan destination";
- info->description =
- "Test that a message's destination is verified via the\n"
- "dialplan";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- msg = ast_msg_alloc();
- ast_test_validate(test, msg != NULL);
- ast_msg_set_context(msg, TEST_CONTEXT);
- ast_msg_set_exten(msg, TEST_EXTENSION);
- ast_test_validate(test, ast_msg_has_destination(msg) == 1);
- ast_msg_set_context(msg, "__I_SHOULD_NOT_EXIST_PLZ__");
- ast_test_validate(test, ast_msg_has_destination(msg) == 0);
- ast_msg_set_context(msg, TEST_CONTEXT);
- ast_msg_set_exten(msg, "__I_SHOULD_NOT_EXIST_PLZ__");
- ast_test_validate(test, ast_msg_has_destination(msg) == 0);
- ast_msg_set_exten(msg, NULL);
- ast_test_validate(test, ast_msg_has_destination(msg) == 0);
- ast_msg_set_context(msg, NULL);
- ast_msg_set_exten(msg, TEST_EXTENSION);
- ast_test_validate(test, ast_msg_has_destination(msg) == 0);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(test_message_has_destination_handler)
- {
- RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
- int result;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = TEST_CATEGORY;
- info->summary = "Test checking for a handler destination";
- info->description =
- "Test that a message's destination is verified via a\n"
- "handler";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- result = ast_msg_handler_register(&test_msg_handler);
- ast_test_validate(test, result == 0);
- msg = ast_msg_alloc();
- ast_test_validate(test, msg != NULL);
- ast_msg_set_to(msg, "foo");
- ast_msg_set_context(msg, TEST_CONTEXT);
- ast_msg_set_exten(msg, NULL);
- ast_test_validate(test, ast_msg_has_destination(msg) == 1);
- ast_msg_set_context(msg, NULL);
- ast_test_validate(test, ast_msg_has_destination(msg) == 1);
- ast_msg_set_to(msg, "__I_SHOULD_NOT_EXIST_PLZ__");
- ast_test_validate(test, ast_msg_has_destination(msg) == 0);
- result = ast_msg_handler_unregister(&test_msg_handler);
- ast_test_validate(test, result == 0);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(test_message_msg_send)
- {
- RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = TEST_CATEGORY;
- info->summary = "Test message routing";
- info->description =
- "Test that a message can be routed if it has\n"
- "a valid handler";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- ast_test_validate(test, ast_msg_tech_register(&test_msg_tech) == 0);
- ast_test_validate(test, ast_msg_handler_register(&test_msg_handler) == 0);
- msg = ast_msg_alloc();
- ast_test_validate(test, msg != NULL);
- ast_msg_set_to(msg, "foo");
- ast_msg_set_context(msg, TEST_CONTEXT);
- ast_msg_set_exten(msg, NULL);
- ast_test_validate(test, ast_msg_has_destination(msg) == 1);
- if (!ast_msg_send(msg, "testmsg:foo", "blah")) {
- msg = NULL;
- } else {
- ast_test_status_update(test, "Failed to send message\n");
- ast_test_set_result(test, AST_TEST_FAIL);
- }
- ast_test_validate(test, ast_msg_handler_unregister(&test_msg_handler) == 0);
- ast_test_validate(test, ast_msg_tech_unregister(&test_msg_tech) == 0);
- return AST_TEST_PASS;
- }
- static int test_init_cb(struct ast_test_info *info, struct ast_test *test)
- {
- received_user_events = 0;
- handler_received_message = 0;
- message_received = 0;
- AST_VECTOR_INIT(&expected_user_event_fields, DEFAULT_EXPECTED_EVENTS);
- AST_VECTOR_INIT(&bad_headers, DEFAULT_EXPECTED_EVENTS);
- return 0;
- }
- #define FREE_VARIABLE_VECTOR(vector) do { \
- int i; \
- for (i = 0; i < AST_VECTOR_SIZE(&(vector)); i++) { \
- struct ast_variable *headers; \
- headers = AST_VECTOR_GET(&(vector), i); \
- if (!headers) { \
- continue; \
- } \
- ast_variables_destroy(headers); \
- } \
- AST_VECTOR_FREE(&(vector)); \
- } while (0)
- static int test_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
- {
- FREE_VARIABLE_VECTOR(expected_user_event_fields);
- FREE_VARIABLE_VECTOR(bad_headers);
- return 0;
- }
- static int unload_module(void)
- {
- AST_TEST_UNREGISTER(test_message_msg_tech_registration);
- AST_TEST_UNREGISTER(test_message_msg_handler_registration);
- AST_TEST_UNREGISTER(test_message_manipulation);
- AST_TEST_UNREGISTER(test_message_queue_dialplan_nominal);
- AST_TEST_UNREGISTER(test_message_queue_handler_nominal);
- AST_TEST_UNREGISTER(test_message_queue_both_nominal);
- AST_TEST_UNREGISTER(test_message_has_destination_dialplan);
- AST_TEST_UNREGISTER(test_message_has_destination_handler);
- AST_TEST_UNREGISTER(test_message_msg_send);
- ast_context_destroy(NULL, AST_MODULE);
- ast_manager_unregister_hook(&user_event_hook);
- return 0;
- }
- static int create_test_dialplan(void)
- {
- int res = 0;
- if (!ast_context_find_or_create(NULL, NULL, TEST_CONTEXT, AST_MODULE)) {
- return -1;
- }
- res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 1, NULL, NULL,
- "UserEvent", "TestMessageUnitTest,Verify:To,Value:${MESSAGE(to)}",
- NULL, AST_MODULE);
- res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 2, NULL, NULL,
- "UserEvent", "TestMessageUnitTest,Verify:From,Value:${MESSAGE(from)}",
- NULL, AST_MODULE);
- res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 3, NULL, NULL,
- "UserEvent", "TestMessageUnitTest,Verify:Body,Value:${MESSAGE(body)}",
- NULL, AST_MODULE);
- res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 4, NULL, NULL,
- "UserEvent", "TestMessageUnitTest,Verify:Custom,Value:${MESSAGE_DATA(custom_data)}",
- NULL, AST_MODULE);
- res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 5, NULL, NULL,
- "Set", "MESSAGE_DATA(custom_data)=${MESSAGE_DATA(custom_data)}",
- NULL, AST_MODULE);
- res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 6, NULL, NULL,
- "MessageSend", "testmsg:${MESSAGE(from)},testmsg:${MESSAGE(to)}",
- NULL, AST_MODULE);
- ast_manager_register_hook(&user_event_hook);
- return res;
- }
- static int load_module(void)
- {
- AST_TEST_REGISTER(test_message_msg_tech_registration);
- AST_TEST_REGISTER(test_message_msg_handler_registration);
- AST_TEST_REGISTER(test_message_manipulation);
- AST_TEST_REGISTER(test_message_queue_dialplan_nominal);
- AST_TEST_REGISTER(test_message_queue_handler_nominal);
- AST_TEST_REGISTER(test_message_queue_both_nominal);
- AST_TEST_REGISTER(test_message_has_destination_dialplan);
- AST_TEST_REGISTER(test_message_has_destination_handler);
- AST_TEST_REGISTER(test_message_msg_send);
- create_test_dialplan();
- ast_test_register_init(TEST_CATEGORY, test_init_cb);
- ast_test_register_cleanup(TEST_CATEGORY, test_cleanup_cb);
- return AST_MODULE_LOAD_SUCCESS;
- }
- AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Out-of-call text message support");
|