Asterisk - The Open Source Telephony Project GIT-master-a358458
Macros | Functions | Variables
test_aeap.c File Reference
#include "asterisk.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/file.h"
#include "asterisk/http_websocket.h"
#include "asterisk/json.h"
#include "asterisk/res_aeap.h"
#include "asterisk/res_aeap_message.h"
Include dependency graph for test_aeap.c:

Go to the source code of this file.

Macros

#define ADDR   "127.0.0.1:8088"
 
#define AEAP_CONNECTION_TIMEOUT   2000
 
#define AEAP_MESSAGE_ID   "foo"
 
#define AEAP_REMOTE_PROTOCOL   "echo"
 
#define AEAP_REMOTE_URL   "ws://" ADDR "/ws"
 
#define AEAP_TRANSPORT_TYPE   "ws"
 
#define CATEGORY   "/res/aeap/"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (create_and_connect)
 
 AST_TEST_DEFINE (send_msg_handle_request)
 
 AST_TEST_DEFINE (send_msg_handle_response)
 
 AST_TEST_DEFINE (send_msg_handle_string)
 
static int handle_msg (struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)
 
static void handle_string (struct ast_aeap *aeap, const char *buf, intmax_t size)
 
static void handle_timeout (struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Asterisk External Application Protocol Object Tests" , .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, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_aeap", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const struct ast_aeap_message_handler handlers []
 
static struct ast_http_serverhttp_server
 

Macro Definition Documentation

◆ ADDR

#define ADDR   "127.0.0.1:8088"

Definition at line 38 of file test_aeap.c.

◆ AEAP_CONNECTION_TIMEOUT

#define AEAP_CONNECTION_TIMEOUT   2000

Definition at line 43 of file test_aeap.c.

◆ AEAP_MESSAGE_ID

#define AEAP_MESSAGE_ID   "foo"

Definition at line 42 of file test_aeap.c.

◆ AEAP_REMOTE_PROTOCOL

#define AEAP_REMOTE_PROTOCOL   "echo"

Definition at line 41 of file test_aeap.c.

◆ AEAP_REMOTE_URL

#define AEAP_REMOTE_URL   "ws://" ADDR "/ws"

Definition at line 40 of file test_aeap.c.

◆ AEAP_TRANSPORT_TYPE

#define AEAP_TRANSPORT_TYPE   "ws"

Definition at line 39 of file test_aeap.c.

◆ CATEGORY

#define CATEGORY   "/res/aeap/"

Definition at line 36 of file test_aeap.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 252 of file test_aeap.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 252 of file test_aeap.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 252 of file test_aeap.c.

◆ AST_TEST_DEFINE() [1/4]

AST_TEST_DEFINE ( create_and_connect  )

Definition at line 45 of file test_aeap.c.

46{
47 RAII_VAR(struct ast_aeap *, aeap, NULL, ao2_cleanup);
48
49 switch (cmd) {
50 case TEST_INIT:
51 info->name = __func__;
52 info->explicit_only = 0;
53 info->category = CATEGORY;
54 info->summary = "test creating and connecting to an AEAP application";
55 info->description = info->summary;
56 return AST_TEST_NOT_RUN;
57 case TEST_EXECUTE:
58 break;
59 }
60
61 ast_test_validate(test, (aeap = ast_aeap_create_and_connect(AEAP_TRANSPORT_TYPE,
63
64 return AST_TEST_PASS;
65}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
def info(msg)
struct ast_aeap * ast_aeap_create_and_connect(const char *type, const struct ast_aeap_params *params, const char *url, const char *protocol, int timeout)
Create and connect to an Asterisk external application.
Definition: aeap.c:363
#define NULL
Definition: resample.c:96
Definition: aeap.c:47
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_NOT_RUN
Definition: test.h:194
#define AEAP_TRANSPORT_TYPE
Definition: test_aeap.c:39
#define CATEGORY
Definition: test_aeap.c:36
#define AEAP_REMOTE_PROTOCOL
Definition: test_aeap.c:41
#define AEAP_CONNECTION_TIMEOUT
Definition: test_aeap.c:43
#define AEAP_REMOTE_URL
Definition: test_aeap.c:40
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

References AEAP_CONNECTION_TIMEOUT, AEAP_REMOTE_PROTOCOL, AEAP_REMOTE_URL, AEAP_TRANSPORT_TYPE, ao2_cleanup, ast_aeap_create_and_connect(), AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, sip_to_pjsip::info(), NULL, RAII_VAR, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [2/4]

AST_TEST_DEFINE ( send_msg_handle_request  )

Definition at line 179 of file test_aeap.c.

180{
181 int passed = 0;
182 RAII_VAR(struct ast_aeap *, aeap, NULL, ao2_cleanup);
183 char *name = "foo";
184 struct ast_aeap_params aeap_params = {
186 .request_handlers_size = ARRAY_LEN(handlers),
187 };
188 struct ast_aeap_tsx_params tsx_params = {0};
189
190 switch (cmd) {
191 case TEST_INIT:
192 info->name = __func__;
193 info->explicit_only = 0;
194 info->category = CATEGORY;
195 info->summary = "test an AEAP application request handler";
196 info->description = info->summary;
197 return AST_TEST_NOT_RUN;
198 case TEST_EXECUTE:
199 break;
200 }
201
203
204 tsx_params.timeout = 2000;
205 tsx_params.wait = 1;
206 tsx_params.obj = name;
207
208 ast_test_validate(test, (aeap = ast_aeap_create_and_connect(AEAP_TRANSPORT_TYPE,
210 ast_test_validate(test, (!ast_aeap_user_data_register(aeap, AEAP_MESSAGE_ID, &passed, NULL)));
211 ast_test_validate(test, (tsx_params.msg = ast_aeap_message_create_request(
213 ast_test_validate(test, !ast_aeap_send_msg_tsx(aeap, &tsx_params));
215
216 return passed ? AST_TEST_PASS : AST_TEST_FAIL;
217}
static const char name[]
Definition: format_mp3.c:68
int ast_aeap_user_data_register(struct ast_aeap *aeap, const char *id, void *obj, ast_aeap_user_obj_cleanup cleanup)
Register a user data object.
Definition: aeap.c:150
int ast_aeap_send_msg_tsx(struct ast_aeap *aeap, struct ast_aeap_tsx_params *params)
Send a transaction based message to an external application using the given parameters.
Definition: aeap.c:464
void ast_aeap_user_data_unregister(struct ast_aeap *aeap, const char *id)
Un-register a user data object.
Definition: aeap.c:169
struct ast_aeap_message * ast_aeap_message_create_request(const struct ast_aeap_message_type *type, const char *name, const char *id, const void *params)
Create an Asterisk external application request object.
const struct ast_aeap_message_type * ast_aeap_message_type_json
Asterisk external application JSON message type.
Definition: message_json.c:191
Callbacks and other parameters used by an Asterisk external application object.
Definition: res_aeap.h:144
const struct ast_aeap_message_type * msg_type
Definition: res_aeap.h:152
const struct ast_aeap_message_handler * request_handlers
Definition: res_aeap.h:160
Parameters to be used when sending a transaction based message.
Definition: res_aeap.h:331
struct ast_aeap_message * msg
Definition: res_aeap.h:333
@ AST_TEST_FAIL
Definition: test.h:196
#define AEAP_MESSAGE_ID
Definition: test_aeap.c:42
static const struct ast_aeap_message_handler handlers[]
Definition: test_aeap.c:135
#define ARRAY_LEN(a)
Definition: utils.h:666

References AEAP_CONNECTION_TIMEOUT, AEAP_MESSAGE_ID, AEAP_REMOTE_PROTOCOL, AEAP_REMOTE_URL, AEAP_TRANSPORT_TYPE, ao2_cleanup, ARRAY_LEN, ast_aeap_create_and_connect(), ast_aeap_message_create_request(), ast_aeap_message_type_json, ast_aeap_send_msg_tsx(), ast_aeap_user_data_register(), ast_aeap_user_data_unregister(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, handlers, sip_to_pjsip::info(), ast_aeap_tsx_params::msg, ast_aeap_params::msg_type, name, NULL, ast_aeap_tsx_params::obj, RAII_VAR, ast_aeap_params::request_handlers, TEST_EXECUTE, TEST_INIT, ast_aeap_tsx_params::timeout, and ast_aeap_tsx_params::wait.

◆ AST_TEST_DEFINE() [3/4]

AST_TEST_DEFINE ( send_msg_handle_response  )

Definition at line 139 of file test_aeap.c.

140{
141 int passed = 0;
142 RAII_VAR(struct ast_aeap *, aeap, NULL, ao2_cleanup);
143 char *name = "foo";
144 struct ast_aeap_params aeap_params = {
146 .response_handlers_size = ARRAY_LEN(handlers),
147 };
148 struct ast_aeap_tsx_params tsx_params = {0};
149
150 switch (cmd) {
151 case TEST_INIT:
152 info->name = __func__;
153 info->explicit_only = 0;
154 info->category = CATEGORY;
155 info->summary = "test an AEAP application response handler";
156 info->description = info->summary;
157 return AST_TEST_NOT_RUN;
158 case TEST_EXECUTE:
159 break;
160 }
161
163
164 tsx_params.timeout = 2000;
165 tsx_params.wait = 1;
166 tsx_params.obj = name;
167
168 ast_test_validate(test, (aeap = ast_aeap_create_and_connect(AEAP_TRANSPORT_TYPE,
170 ast_test_validate(test, (!ast_aeap_user_data_register(aeap, AEAP_MESSAGE_ID, &passed, NULL)));
171 ast_test_validate(test, (tsx_params.msg = ast_aeap_message_create_response(
173 ast_test_validate(test, !ast_aeap_send_msg_tsx(aeap, &tsx_params));
175
176 return passed ? AST_TEST_PASS : AST_TEST_FAIL;
177}
struct ast_aeap_message * ast_aeap_message_create_response(const struct ast_aeap_message_type *type, const char *name, const char *id, const void *params)
Create an Asterisk external application response object.
const struct ast_aeap_message_handler * response_handlers
Definition: res_aeap.h:155

References AEAP_CONNECTION_TIMEOUT, AEAP_MESSAGE_ID, AEAP_REMOTE_PROTOCOL, AEAP_REMOTE_URL, AEAP_TRANSPORT_TYPE, ao2_cleanup, ARRAY_LEN, ast_aeap_create_and_connect(), ast_aeap_message_create_response(), ast_aeap_message_type_json, ast_aeap_send_msg_tsx(), ast_aeap_user_data_register(), ast_aeap_user_data_unregister(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, handlers, sip_to_pjsip::info(), ast_aeap_tsx_params::msg, ast_aeap_params::msg_type, name, NULL, ast_aeap_tsx_params::obj, RAII_VAR, ast_aeap_params::response_handlers, TEST_EXECUTE, TEST_INIT, ast_aeap_tsx_params::timeout, and ast_aeap_tsx_params::wait.

◆ AST_TEST_DEFINE() [4/4]

AST_TEST_DEFINE ( send_msg_handle_string  )

Definition at line 83 of file test_aeap.c.

84{
85 int passed = 0;
86 RAII_VAR(struct ast_aeap *, aeap, NULL, ao2_cleanup);
87 struct ast_aeap_tsx_params tsx_params = {0};
88 struct ast_aeap_params aeap_params = {
90 };
91
92 switch (cmd) {
93 case TEST_INIT:
94 info->name = __func__;
95 info->explicit_only = 0;
96 info->category = CATEGORY;
97 info->summary = "test an AEAP application string handler";
98 info->description = info->summary;
99 return AST_TEST_NOT_RUN;
100 case TEST_EXECUTE:
101 break;
102 }
103
104 tsx_params.timeout = 2000; /* Test will end by timing out */
105 tsx_params.on_timeout = handle_timeout;
106 tsx_params.wait = 1;
107
108 ast_test_validate(test, (aeap = ast_aeap_create_and_connect(AEAP_TRANSPORT_TYPE,
110
111 ast_test_validate(test, (!ast_aeap_user_data_register(aeap, AEAP_MESSAGE_ID, &passed, NULL)));
112 ast_test_validate(test, (tsx_params.msg = ast_aeap_message_create_request(
114 ast_test_validate(test, ast_aeap_send_msg_tsx(aeap, &tsx_params)); /* Returns fail on timeout */
116
117 return passed == 2 ? AST_TEST_PASS : AST_TEST_FAIL;
118}
void(* on_string)(struct ast_aeap *aeap, const char *buf, intmax_t size)
Raised when string data is received.
Definition: res_aeap.h:180
ast_aeap_on_timeout on_timeout
Definition: res_aeap.h:337
static void handle_timeout(struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)
Definition: test_aeap.c:76
static void handle_string(struct ast_aeap *aeap, const char *buf, intmax_t size)
Definition: test_aeap.c:67

References AEAP_CONNECTION_TIMEOUT, AEAP_MESSAGE_ID, AEAP_REMOTE_PROTOCOL, AEAP_REMOTE_URL, AEAP_TRANSPORT_TYPE, ao2_cleanup, ast_aeap_create_and_connect(), ast_aeap_message_create_request(), ast_aeap_message_type_json, ast_aeap_send_msg_tsx(), ast_aeap_user_data_register(), ast_aeap_user_data_unregister(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, handle_string(), handle_timeout(), sip_to_pjsip::info(), ast_aeap_tsx_params::msg, NULL, ast_aeap_params::on_string, ast_aeap_tsx_params::on_timeout, RAII_VAR, TEST_EXECUTE, TEST_INIT, ast_aeap_tsx_params::timeout, and ast_aeap_tsx_params::wait.

◆ handle_msg()

static int handle_msg ( struct ast_aeap aeap,
struct ast_aeap_message message,
void *  data 
)
static

Definition at line 120 of file test_aeap.c.

121{
123
124 *passed = !strcmp(ast_aeap_message_id(message), AEAP_MESSAGE_ID) &&
126
127 if (!*passed) {
128 ast_log(LOG_ERROR, "Name '%s' did not equal '%s' for message '%s'",
130 }
131
132 return 0;
133}
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
void * ast_aeap_user_data_object_by_id(struct ast_aeap *aeap, const char *id)
Retrieve a registered user data object by its id.
Definition: aeap.c:174
const char * ast_aeap_message_id(const struct ast_aeap_message *message)
Retrieve a message id.
const char * ast_aeap_message_name(const struct ast_aeap_message *message)
Retrieve a message name.
int ast_aeap_message_is_named(const struct ast_aeap_message *message, const char *name)
Check whether or not a message's name matches the given one.

References AEAP_MESSAGE_ID, ast_aeap_message_id(), ast_aeap_message_is_named(), ast_aeap_message_name(), ast_aeap_user_data_object_by_id(), ast_log, and LOG_ERROR.

◆ handle_string()

static void handle_string ( struct ast_aeap aeap,
const char *  buf,
intmax_t  size 
)
static

Definition at line 67 of file test_aeap.c.

68{
70
71 if (strstr(buf, AEAP_MESSAGE_ID)) {
72 ++*passed;
73 }
74}
char buf[BUFSIZE]
Definition: eagi_proxy.c:66

References AEAP_MESSAGE_ID, ast_aeap_user_data_object_by_id(), and buf.

Referenced by AST_TEST_DEFINE().

◆ handle_timeout()

static void handle_timeout ( struct ast_aeap aeap,
struct ast_aeap_message message,
void *  data 
)
static

Definition at line 76 of file test_aeap.c.

77{
79
80 ++*passed;
81}

References AEAP_MESSAGE_ID, and ast_aeap_user_data_object_by_id().

Referenced by AST_TEST_DEFINE().

◆ load_module()

static int load_module ( void  )
static

Definition at line 221 of file test_aeap.c.

222{
223 if (!(http_server = ast_http_test_server_get("aeap transport http server", NULL))) {
225 }
226
227 AST_TEST_REGISTER(create_and_connect);
228 AST_TEST_REGISTER(send_msg_handle_string);
229 AST_TEST_REGISTER(send_msg_handle_response);
230 AST_TEST_REGISTER(send_msg_handle_request);
231
233}
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
static struct ast_http_server * http_server
Definition: test_aeap.c:219

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_TEST_REGISTER, http_server, and NULL.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 235 of file test_aeap.c.

236{
237 AST_TEST_UNREGISTER(send_msg_handle_request);
238 AST_TEST_UNREGISTER(send_msg_handle_response);
239 AST_TEST_UNREGISTER(send_msg_handle_string);
240 AST_TEST_UNREGISTER(create_and_connect);
241
242 ast_http_test_server_discard(http_server);
243
244 return 0;
245}
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

References AST_TEST_UNREGISTER, and http_server.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Asterisk External Application Protocol Object Tests" , .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, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_aeap", }
static

Definition at line 252 of file test_aeap.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 252 of file test_aeap.c.

◆ handlers

const struct ast_aeap_message_handler handlers[]
static
Initial value:
= {
{ "foo", handle_msg },
}
static int handle_msg(struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)
Definition: test_aeap.c:120

Definition at line 135 of file test_aeap.c.

Referenced by ast_pbx_hangup_handler_destroy(), ast_pbx_hangup_handler_init(), ast_pbx_hangup_handler_pop(), ast_pbx_hangup_handler_push(), ast_pbx_hangup_handler_run(), ast_pbx_hangup_handler_show(), AST_TEST_DEFINE(), channel_do_masquerade(), and raise_msg_handler().

◆ http_server

struct ast_http_server* http_server
static

Definition at line 219 of file test_aeap.c.

Referenced by load_module(), and unload_module().