44 #define TEST_CATEGORY "/main/message/" 46 #define TEST_CONTEXT "__TEST_MESSAGE_CONTEXT__" 47 #define TEST_EXTENSION "test_message_extension" 50 #define DEFAULT_EXPECTED_EVENTS 4 125 expected =
AST_VECTOR_GET(&expected_user_event_fields, user_event);
130 for (current = expected; current; current = current->
next) {
133 if (strcmp(current->
name, header)) {
137 error = regcomp(®exbuf, current->
value, REG_EXTENDED | REG_NOSUB);
140 regerror(error, ®exbuf, error_buf,
sizeof(error_buf));
141 ast_log(
LOG_ERROR,
"Failed to compile regex '%s' for header check '%s': %s\n",
142 current->
value, current->
name, error_buf);
146 if (!regexec(®exbuf, value, 0,
NULL, 0)) {
201 if (strcmp(event,
"UserEvent")) {
206 while ((kvp =
strsep(&parse,
"\r\n"))) {
234 struct timespec wait_time = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000 };
239 if (error == ETIMEDOUT) {
247 return (error != ETIMEDOUT);
255 struct timespec wait_time = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000 };
262 if (error == ETIMEDOUT) {
263 ast_test_status_update(test,
"Test timed out while waiting for %d expected user events\n", expected_events);
289 for (current = headers; current; current = current->
next) {
305 info->name = __func__;
307 info->summary =
"Test register/unregister of a message tech";
310 "\tA message technology can be registered once only\n" 311 "\tA registered message technology can be unregistered once only";
318 ast_test_validate(
test, reg_result == 0);
321 ast_test_validate(
test, reg_result == -1);
324 ast_test_validate(
test, reg_result == 0);
327 ast_test_validate(
test, reg_result == -1);
338 info->name = __func__;
340 info->summary =
"Test register/unregister of a message handler";
343 "\tA message handler can be registered once only\n" 344 "\tA registered message handler can be unregistered once only";
351 ast_test_validate(
test, reg_result == 0);
354 ast_test_validate(
test, reg_result == -1);
357 ast_test_validate(
test, reg_result == 0);
360 ast_test_validate(
test, reg_result == -1);
380 const char *out_name;
381 const char *out_value;
385 info->name = __func__;
387 info->summary =
"Test manipulating properties of a message";
389 "This test covers the following:\n" 390 "\tSetting/getting the body\n" 391 "\tSetting/getting inbound/outbound variables\n" 392 "\tIterating over variables";
399 ast_test_validate(
test, msg !=
NULL);
403 ast_test_validate(
test, result == 0);
405 ast_test_validate(
test, !strcmp(actual,
"testmsg:foo"));
409 ast_test_validate(
test, result == 0);
411 ast_test_validate(
test, !strcmp(actual,
"testmsg:bar"));
415 ast_test_validate(
test, result == 0);
417 ast_test_validate(
test, !strcmp(actual,
"BodyTest: foo"));
421 ast_test_validate(
test, result == 0);
423 ast_test_validate(
test, !strcmp(actual,
"my_tech"));
427 ast_test_validate(
test, result == 0);
429 ast_test_validate(
test, !strcmp(actual,
"terminus"));
433 ast_test_validate(
test, result == 0);
435 ast_test_validate(
test, !strcmp(actual,
"bar"));
439 ast_test_validate(
test, result == 0);
441 ast_test_validate(
test, !strcmp(actual,
"new_bar"));
445 ast_test_validate(
test, it_vars !=
NULL);
451 ast_test_validate(
test, result == 0);
453 ast_test_validate(
test, it_vars !=
NULL);
455 ast_test_validate(
test, result == 1);
456 ast_test_validate(
test, !strcmp(out_name,
"foo"));
457 ast_test_validate(
test, !strcmp(out_value,
"outbound_bar"));
460 ast_test_validate(
test, result == 0);
473 info->name = __func__;
475 info->summary =
"Test enqueueing messages to the dialplan";
477 "Test that a message enqueued for the dialplan is\n" 478 "passed to that particular extension";
485 ast_test_validate(
test, msg !=
NULL);
493 expected_response =
NULL;
500 expected_response =
NULL;
507 expected_response =
NULL;
544 info->name = __func__;
546 info->summary =
"Test enqueueing messages to a handler";
548 "Test that a message enqueued can be handled by a\n" 549 "non-dialplan handler";
556 ast_test_validate(
test, msg !=
NULL);
559 ast_test_validate(
test, result == 0);
572 ast_test_validate(
test, result == 0);
586 info->name = __func__;
588 info->summary =
"Test enqueueing messages to a dialplan and custom handler";
590 "Test that a message enqueued is passed to all\n" 591 "handlers that can process it, dialplan as well as\n" 599 ast_test_validate(
test, msg !=
NULL);
602 ast_test_validate(
test, result == 0);
610 expected_response =
NULL;
617 expected_response =
NULL;
643 ast_test_validate(
test, result == 0);
658 info->name = __func__;
660 info->summary =
"Test checking for a dialplan destination";
662 "Test that a message's destination is verified via the\n" 670 ast_test_validate(
test, msg !=
NULL);
700 info->name = __func__;
702 info->summary =
"Test checking for a handler destination";
704 "Test that a message's destination is verified via a\n" 712 ast_test_validate(
test, result == 0);
715 ast_test_validate(
test, msg !=
NULL);
729 ast_test_validate(
test, result == 0);
740 info->name = __func__;
742 info->summary =
"Test message routing";
744 "Test that a message can be routed if it has\n" 755 ast_test_validate(
test, msg !=
NULL);
787 #define FREE_VARIABLE_VECTOR(vector) do { \ 789 for (i = 0; i < AST_VECTOR_SIZE(&(vector)); i++) { \ 790 struct ast_variable *headers; \ 791 headers = AST_VECTOR_GET(&(vector), i); \ 795 ast_variables_destroy(headers); \ 797 AST_VECTOR_FREE(&(vector)); \ 837 "UserEvent",
"TestMessageUnitTest,Verify:To,Value:${MESSAGE(to)}",
840 "UserEvent",
"TestMessageUnitTest,Verify:From,Value:${MESSAGE(from)}",
843 "UserEvent",
"TestMessageUnitTest,Verify:Body,Value:${MESSAGE(body)}",
846 "UserEvent",
"TestMessageUnitTest,Verify:Custom,Value:${MESSAGE_DATA(custom_data)}",
849 "Set",
"MESSAGE_DATA(custom_data)=${MESSAGE_DATA(custom_data)}",
852 "MessageSend",
"testmsg:${MESSAGE(from)},testmsg:${MESSAGE(to)}",
static ast_cond_t user_event_cond
Condition wait variable for all dialplan user events being received.
int ast_msg_handler_unregister(const struct ast_msg_handler *handler)
Unregister a ast_msg_handler.
struct ast_variable * next
Contains all the initialization information required to store a new test definition.
int ast_msg_set_tech(struct ast_msg *msg, const char *fmt,...)
Set the technology associated with this message.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Asterisk main include file. File version handling, generic pbx functions.
const char * ast_msg_get_var(struct ast_msg *msg, const char *name)
Get the specified variable on the message.
const char * ast_msg_get_tech(const struct ast_msg *msg)
Retrieve the technology associated with this message.
static int test_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
static int handler_received_message
Predicate for the test_message_handler receiving a message.
void ast_manager_unregister_hook(struct manager_custom_hook *hook)
Delete a custom hook to be called when an event is fired.
int ast_msg_set_context(struct ast_msg *msg, const char *fmt,...)
Set the dialplan context for this message.
const char * ast_msg_get_endpoint(const struct ast_msg *msg)
Retrieve the endpoint associated with this message.
int ast_msg_set_body(struct ast_msg *msg, const char *fmt,...)
Set the 'body' text of a message (in UTF-8)
int ast_test_register_init(const char *category, ast_test_init_cb_t *cb)
void ast_manager_register_hook(struct manager_custom_hook *hook)
Add a custom hook to be called when an event is fired.
An external processor of received messages.
static int verify_bad_headers(struct ast_test *test)
static int test_msg_handle_msg_cb(struct ast_msg *msg)
static int user_event_hook_cb(int category, const char *event, char *body)
static int verify_user_event_fields(int user_event, const char *header, const char *value)
Verifies a user event header/value pair.
int ast_msg_handler_register(const struct ast_msg_handler *handler)
Register a ast_msg_handler.
static ast_mutex_t handler_lock
Mutex for handler_cond.
struct ast_msg * ast_msg_alloc(void)
Allocate a message.
Structure for variables, used for configurations and for channel variables.
int ast_msg_var_iterator_next(const struct ast_msg *msg, struct ast_msg_var_iterator *iter, const char **name, const char **value)
Get the next variable name and value that is set for sending outbound.
AST_TEST_DEFINE(test_message_msg_tech_registration)
const char *const name
Name of this message technology.
#define AST_TEST_REGISTER(cb)
static int test_msg_send(const struct ast_msg *msg, const char *to, const char *from)
int ast_msg_tech_register(const struct ast_msg_tech *tech)
Register a message technology.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
#define ast_mutex_lock(a)
Out-of-call text message support.
#define ast_cond_signal(cond)
const char * ast_msg_get_body(const struct ast_msg *msg)
Get the body of a message.
void ast_msg_var_iterator_destroy(struct ast_msg_var_iterator *iter)
Destroy a message variable iterator.
int ast_msg_set_to(struct ast_msg *msg, const char *fmt,...)
Set the 'to' URI of a message.
pthread_cond_t ast_cond_t
#define ast_strlen_zero(foo)
const char * ast_msg_get_to(const struct ast_msg *msg)
Retrieve the destination of this message.
static int message_received
int ast_msg_send(struct ast_msg *msg, const char *to, const char *from)
Send a msg directly to an endpoint.
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
#define ast_test_status_update(a, b, c...)
void ast_msg_var_unref_current(struct ast_msg_var_iterator *iter)
Unref a message var from inside an iterator loop.
static int expected_user_events
The number of user events we expect for this test.
#define FREE_VARIABLE_VECTOR(vector)
#define ast_strdupa(s)
duplicate a string in memory from the stack
#define ast_variable_new(name, value, filename)
int ast_msg_set_var(struct ast_msg *msg, const char *name, const char *value)
Set a variable on the message going to the dialplan.
Core PBX routines and definitions.
int ast_test_register_cleanup(const char *category, ast_test_cleanup_cb_t *cb)
int ast_msg_queue(struct ast_msg *msg)
Queue a message for routing through the dialplan.
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
int ast_msg_tech_unregister(const struct ast_msg_tech *tech)
Unregister a message technology.
#define AST_TEST_UNREGISTER(cb)
const char * ast_msg_get_from(const struct ast_msg *msg)
Retrieve the source of this message.
static int received_user_events
The current number of received user events.
static struct ast_msg_handler test_msg_handler
Our test message handler.
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
char * ast_trim_blanks(char *str)
Trims trailing whitespace characters from a string.
static ast_cond_t handler_cond
Condition wait variable for test_msg_handler receiving message.
static void parse(struct mgcp_request *req)
void ast_test_set_result(struct ast_test *test, enum ast_test_result_state state)
static int handler_wait_for_message(struct ast_test *test)
Wait for the test_msg_handler to receive the message.
Vector container support.
int ast_msg_set_exten(struct ast_msg *msg, const char *fmt,...)
Set the dialplan extension for this message.
#define DEFAULT_EXPECTED_EVENTS
The number of user events we should get in a dialplan test.
static int load_module(void)
int ast_msg_set_from(struct ast_msg *msg, const char *fmt,...)
Set the 'from' URI of a message.
const char * name
Name of the message handler.
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
static ast_mutex_t user_event_lock
Mutex for user_event_cond.
char * strsep(char **str, const char *delims)
int ast_msg_set_endpoint(struct ast_msg *msg, const char *fmt,...)
Set the technology's endpoint associated with this message.
int ast_add_extension(const char *context, int replace, const char *extension, int priority, const char *label, const char *callerid, const char *application, void *data, void(*datad)(void *), const char *registrar)
Add and extension to an extension context.
int ast_msg_has_destination(const struct ast_msg *msg)
Determine if a particular message has a destination via some handler.
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
int ast_msg_set_var_outbound(struct ast_msg *msg, const char *name, const char *value)
Set a variable on the message being sent to a message tech directly.
#define AST_VECTOR_REPLACE(vec, idx, elem)
Replace an element at a specific position in a vector, growing the vector if needed.
void ast_context_destroy(struct ast_context *con, const char *registrar)
Destroy a context (matches the specified context or ANY context if NULL)
int error(const char *format,...)
static int test_init_cb(struct ast_test_info *info, struct ast_test *test)
static int user_event_wait_for_events(struct ast_test *test, int expected_events)
Wait for the expected number of user events to be received.
struct ast_context * ast_context_find_or_create(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *name, const char *registrar)
Register a new context or find an existing one.
struct ast_msg * ast_msg_destroy(struct ast_msg *msg)
Destroy an ast_msg.
static void ast_msg_safe_destroy(void *obj)
static struct manager_custom_hook user_event_hook
AMI event hook that verifies whether or not we've gotten our user events.
#define ASTERISK_GPL_KEY
The text the key() function should return.
AST_VECTOR(var_vector, struct ast_variable *)
The expected user event fields.
Asterisk module definitions.
#define ast_cond_timedwait(cond, mutex, time)
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
#define AST_MUTEX_DEFINE_STATIC(mutex)
#define ast_variable_list_append(head, new_var)
static int create_test_dialplan(void)
#define ast_mutex_unlock(a)
static int unload_module(void)
static int test_msg_has_destination_cb(const struct ast_msg *msg)
struct ast_msg_var_iterator * ast_msg_var_iterator_init(const struct ast_msg *msg)
Create a new message variable iterator.