Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Functions | Variables
test_message.c File Reference

Test module for out-of-call text message module. More...

#include "asterisk.h"
#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"

Go to the source code of this file.

Data Structures

struct  var_vector
 The expected user event fields. More...
 

Macros

#define DEFAULT_EXPECTED_EVENTS   4
 The number of user events we should get in a dialplan test.
 
#define FREE_VARIABLE_VECTOR(vector)
 
#define TEST_CATEGORY   "/main/message/"
 
#define TEST_CONTEXT   "__TEST_MESSAGE_CONTEXT__"
 
#define TEST_EXTENSION   "test_message_extension"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static void ast_msg_safe_destroy (void *obj)
 
 AST_TEST_DEFINE (test_message_msg_tech_registration)
 
 AST_TEST_DEFINE (test_message_msg_handler_registration)
 
 AST_TEST_DEFINE (test_message_manipulation)
 
 AST_TEST_DEFINE (test_message_queue_dialplan_nominal)
 
 AST_TEST_DEFINE (test_message_queue_handler_nominal)
 
 AST_TEST_DEFINE (test_message_queue_both_nominal)
 
 AST_TEST_DEFINE (test_message_has_destination_dialplan)
 
 AST_TEST_DEFINE (test_message_has_destination_handler)
 
 AST_TEST_DEFINE (test_message_msg_send)
 
static int create_test_dialplan (void)
 
static int handler_wait_for_message (struct ast_test *test)
 Wait for the test_msg_handler to receive the message.
 
static int load_module (void)
 
static int test_cleanup_cb (struct ast_test_info *info, struct ast_test *test)
 
static int test_init_cb (struct ast_test_info *info, struct ast_test *test)
 
static int test_msg_handle_msg_cb (struct ast_msg *msg)
 
static int test_msg_has_destination_cb (const struct ast_msg *msg)
 
static int test_msg_send (const struct ast_msg *msg, const char *to, const char *from)
 
static int unload_module (void)
 
static int user_event_hook_cb (int category, const char *event, char *body)
 
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 verify_bad_headers (struct ast_test *test)
 
static int verify_user_event_fields (int user_event, const char *header, const char *value)
 Verifies a user event header/value pair. More...
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Out-of-call text message support" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
struct {
   size_t   current
 
   struct ast_variable **   elems
 
   size_t   max
 
bad_headers
 If a user event fails, the bad headers that didn't match.
 
struct var_vector expected_user_event_fields
 
static int expected_user_events
 The number of user events we expect for this test.
 
static ast_cond_t handler_cond
 Condition wait variable for test_msg_handler receiving message.
 
static ast_mutex_t handler_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
 Mutex for handler_cond.
 
static int handler_received_message
 Predicate for the test_msg_handler receiving a message.
 
static int message_received
 
static int received_user_events
 The current number of received user events.
 
static struct ast_msg_handler test_msg_handler
 Our test message handler. More...
 
static struct ast_msg_tech test_msg_tech
 
static ast_cond_t user_event_cond
 Condition wait variable for all dialplan user events being received.
 
static struct manager_custom_hook user_event_hook
 AMI event hook that verifies whether or not we've gotten our user events. More...
 
static ast_mutex_t user_event_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
 Mutex for user_event_cond.
 

Detailed Description

Test module for out-of-call text message module.

Author
Matt Jordan <mjordan@digium.com> 

Definition in file test_message.c.

Function Documentation

static int verify_user_event_fields ( int  user_event,
const char *  header,
const char *  value 
)
static

Verifies a user event header/value pair.

Parameters
user_eventwhich user event to check
headerThe header to verify
valueThe value read from the event
Return values
-1on error or evaluation failure
0if match not needed or success

Definition at line 114 of file test_message.c.

References AST_VECTOR_GET, AST_VECTOR_REPLACE, AST_VECTOR_SIZE, bad_headers, ast_variable::name, ast_variable::next, and ast_variable::value.

115 {
116  struct ast_variable *current;
117  struct ast_variable *expected;
118  regex_t regexbuf;
119  int error;
120 
121  if (user_event >= AST_VECTOR_SIZE(&expected_user_event_fields)) {
122  return -1;
123  }
124 
125  expected = AST_VECTOR_GET(&expected_user_event_fields, user_event);
126  if (!expected) {
127  return -1;
128  }
129 
130  for (current = expected; current; current = current->next) {
131  struct ast_variable *bad_header;
132 
133  if (strcmp(current->name, header)) {
134  continue;
135  }
136 
137  error = regcomp(&regexbuf, current->value, REG_EXTENDED | REG_NOSUB);
138  if (error) {
139  char error_buf[128];
140  regerror(error, &regexbuf, 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);
143  return -1;
144  }
145 
146  if (!regexec(&regexbuf, value, 0, NULL, 0)) {
147  regfree(&regexbuf);
148  return 0;
149  }
150 
151  bad_header = ast_variable_new(header, value, __FILE__);
152  if (bad_header) {
153  struct ast_variable *bad_headers_head = NULL;
154 
155  if (user_event < AST_VECTOR_SIZE(&bad_headers)) {
156  bad_headers_head = AST_VECTOR_GET(&bad_headers, user_event);
157  }
158  ast_variable_list_append(&bad_headers_head, bad_header);
159  AST_VECTOR_REPLACE(&bad_headers, user_event, bad_headers_head);
160  }
161  regfree(&regexbuf);
162  return -1;
163  }
164 
165  return 0;
166 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680
#define AST_VECTOR_REPLACE(vec, idx, elem)
Replace an element at a specific position in a vector, growing the vector if needed.
Definition: vector.h:284
struct @511 bad_headers
If a user event fails, the bad headers that didn't match.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609

Variable Documentation

struct ast_msg_handler test_msg_handler
static
Initial value:
= {
.name = "testmsg",
.handle_msg = test_msg_handle_msg_cb,
.has_destination = test_msg_has_destination_cb,
}

Our test message handler.

Definition at line 90 of file test_message.c.

struct ast_msg_tech test_msg_tech
static
Initial value:
= {
.name = "testmsg",
.msg_send = test_msg_send,
}

Definition at line 81 of file test_message.c.

struct manager_custom_hook user_event_hook
static
Initial value:
= {
.file = AST_MODULE,
.helper = user_event_hook_cb,
}

AMI event hook that verifies whether or not we've gotten our user events.

Definition at line 99 of file test_message.c.