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
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",
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) {
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);
405 ast_test_validate(
test, !strcmp(actual,
"testmsg:foo"));
411 ast_test_validate(
test, !strcmp(actual,
"testmsg:bar"));
417 ast_test_validate(
test, !strcmp(actual,
"BodyTest: foo"));
423 ast_test_validate(
test, !strcmp(actual,
"my_tech"));
429 ast_test_validate(
test, !strcmp(actual,
"terminus"));
435 ast_test_validate(
test, !strcmp(actual,
"bar"));
441 ast_test_validate(
test, !strcmp(actual,
"new_bar"));
445 ast_test_validate(
test, it_vars !=
NULL);
453 ast_test_validate(
test, it_vars !=
NULL);
456 ast_test_validate(
test, !strcmp(out_name,
"foo"));
457 ast_test_validate(
test, !strcmp(out_value,
"outbound_bar"));
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);
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);
610 expected_response =
NULL;
617 expected_response =
NULL;
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"
715 ast_test_validate(
test, msg !=
NULL);
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)}",
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
void ast_manager_unregister_hook(struct manager_custom_hook *hook)
Delete a custom hook to be called when an event is fired.
void ast_manager_register_hook(struct manager_custom_hook *hook)
Add a custom hook to be called when an event is fired.
char * strsep(char **str, const char *delims)
#define ast_variable_new(name, value, filename)
#define ast_variable_list_append(head, new_var)
#define ast_cond_timedwait(cond, mutex, time)
#define ast_mutex_unlock(a)
pthread_cond_t ast_cond_t
#define ast_mutex_lock(a)
#define AST_MUTEX_DEFINE_STATIC(mutex)
#define ast_cond_signal(cond)
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
Out-of-call text message support.
int ast_msg_set_from(struct ast_msg *msg, const char *fmt,...)
Set the 'from' URI of a message.
int ast_msg_send(struct ast_msg *msg, const char *to, const char *from)
Send a msg directly to an endpoint.
const char * ast_msg_get_var(struct ast_msg *msg, const char *name)
Get the specified variable on the message.
int ast_msg_handler_unregister(const struct ast_msg_handler *handler)
Unregister a ast_msg_handler.
const char * ast_msg_get_from(const struct ast_msg *msg)
Retrieve the source of this message.
void ast_msg_var_iterator_destroy(struct ast_msg_var_iterator *iter)
Destroy a message variable iterator.
struct ast_msg * ast_msg_destroy(struct ast_msg *msg)
Destroy an ast_msg.
int ast_msg_set_endpoint(struct ast_msg *msg, const char *fmt,...)
Set the technology's endpoint associated with this message.
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.
const char * ast_msg_get_endpoint(const struct ast_msg *msg)
Retrieve the endpoint associated with this message.
int ast_msg_set_exten(struct ast_msg *msg, const char *fmt,...)
Set the dialplan extension for this message.
int ast_msg_set_tech(struct ast_msg *msg, const char *fmt,...)
Set the technology associated with this message.
int ast_msg_has_destination(const struct ast_msg *msg)
Determine if a particular message has a destination via some handler.
struct ast_msg * ast_msg_alloc(void)
Allocate a message.
int ast_msg_tech_unregister(const struct ast_msg_tech *tech)
Unregister a message technology.
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.
const char * ast_msg_get_to(const struct ast_msg *msg)
Retrieve the destination of this message.
const char * ast_msg_get_tech(const struct ast_msg *msg)
Retrieve the technology associated with this message.
int ast_msg_tech_register(const struct ast_msg_tech *tech)
Register a message technology.
const char * ast_msg_get_body(const struct ast_msg *msg)
Get the body of a message.
int ast_msg_handler_register(const struct ast_msg_handler *handler)
Register a ast_msg_handler.
int ast_msg_set_context(struct ast_msg *msg, const char *fmt,...)
Set the dialplan context for 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_msg_set_to(struct ast_msg *msg, const char *fmt,...)
Set the 'to' URI of a message.
void ast_msg_var_unref_current(struct ast_msg_var_iterator *iter)
Unref a message var from inside an iterator loop.
struct ast_msg_var_iterator * ast_msg_var_iterator_init(const struct ast_msg *msg)
Create a new message variable iterator.
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.
int ast_msg_queue(struct ast_msg *msg)
Queue a message for routing through the dialplan.
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
#define ASTERISK_GPL_KEY
The text the key() function should return.
@ AST_MODULE_LOAD_SUCCESS
Core PBX routines and definitions.
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.
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.
void ast_context_destroy(struct ast_context *con, const char *registrar)
Destroy a context (matches the specified context or ANY context if NULL)
static force_inline int attribute_pure ast_strlen_zero(const char *s)
char * ast_trim_blanks(char *str)
Trims trailing whitespace characters from a string.
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
An external processor of received messages.
const char * name
Name of the message handler.
const char *const name
Name of this message technology.
Contains all the initialization information required to store a new test definition.
Structure for variables, used for configurations and for channel variables.
The expected user event fields.
#define AST_TEST_REGISTER(cb)
#define ast_test_status_update(a, b, c...)
#define AST_TEST_UNREGISTER(cb)
struct @503 bad_headers
If a user event fails, the bad headers that didn't match.
static struct ast_msg_handler test_msg_handler
Our test message handler.
AST_TEST_DEFINE(test_message_msg_tech_registration)
static int verify_bad_headers(struct ast_test *test)
static ast_cond_t user_event_cond
Condition wait variable for all dialplan user events being received.
static int create_test_dialplan(void)
static int handler_received_message
Predicate for the test_msg_handler receiving a message.
static ast_mutex_t user_event_lock
Mutex for user_event_cond.
static int user_event_hook_cb(int category, const char *event, char *body)
#define FREE_VARIABLE_VECTOR(vector)
static struct manager_custom_hook user_event_hook
AMI event hook that verifies whether or not we've gotten our user events.
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.
static int test_init_cb(struct ast_test_info *info, struct ast_test *test)
static int handler_wait_for_message(struct ast_test *test)
Wait for the test_msg_handler to receive the message.
static ast_cond_t handler_cond
Condition wait variable for test_msg_handler receiving message.
static int expected_user_events
The number of user events we expect for this test.
static int test_msg_has_destination_cb(const struct ast_msg *msg)
static ast_mutex_t handler_lock
Mutex for handler_cond.
static int test_msg_send(const struct ast_msg *msg, const char *to, const char *from)
#define DEFAULT_EXPECTED_EVENTS
The number of user events we should get in a dialplan test.
static int test_msg_handle_msg_cb(struct ast_msg *msg)
static struct ast_msg_tech test_msg_tech
static int received_user_events
The current number of received user events.
static int load_module(void)
static int unload_module(void)
static int test_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
static void ast_msg_safe_destroy(void *obj)
static int message_received
static int verify_user_event_fields(int user_event, const char *header, const char *value)
Verifies a user event header/value pair.
struct var_vector expected_user_event_fields
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
int error(const char *format,...)
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Vector container support.
#define AST_VECTOR_REPLACE(vec, idx, elem)
Replace an element at a specific position in a vector, growing the vector if needed.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
#define AST_VECTOR(name, type)
Define a vector structure.
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.