| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "asterisk.h" |
| 26 |
|
| 27 |
#include "asterisk/test.h" |
| 28 |
#include "asterisk/module.h" |
| 29 |
#include "asterisk/file.h" |
| 30 |
#include "asterisk/http_websocket.h" |
| 31 |
#include "asterisk/json.h" |
| 32 |
|
| 33 |
#include "asterisk/res_aeap.h" |
| 34 |
#include "asterisk/res_aeap_message.h" |
| 35 |
|
| 36 |
#define CATEGORY "/res/aeap/" |
| 37 |
|
| 38 |
#define ADDR "127.0.0.1:8088" |
| 39 |
#define AEAP_TRANSPORT_TYPE "ws" |
| 40 |
#define AEAP_REMOTE_URL "ws://" ADDR "/ws" |
| 41 |
#define AEAP_REMOTE_PROTOCOL "echo" |
| 42 |
#define AEAP_MESSAGE_ID "foo" |
| 43 |
#define AEAP_CONNECTION_TIMEOUT 2000 |
| 44 |
|
| 45 |
AST_TEST_DEFINE(create_and_connect) |
| 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, |
| 62 |
NULL, AEAP_REMOTE_URL, AEAP_REMOTE_PROTOCOL, AEAP_CONNECTION_TIMEOUT))); |
| 63 |
|
| 64 |
return AST_TEST_PASS; |
| 65 |
} |
| 66 |
|
| 67 |
static void handle_string(struct ast_aeap *aeap, const char *buf, intmax_t size) |
| 68 |
{ |
| 69 |
int *passed = ast_aeap_user_data_object_by_id(aeap, AEAP_MESSAGE_ID); |
| 70 |
|
| 71 |
if (strstr(buf, AEAP_MESSAGE_ID)) { |
| 72 |
++*passed; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
static void handle_timeout(struct ast_aeap *aeap, struct ast_aeap_message *message, void *data) |
| 77 |
{ |
| 78 |
int *passed = ast_aeap_user_data_object_by_id(aeap, AEAP_MESSAGE_ID); |
| 79 |
|
| 80 |
++*passed; |
| 81 |
} |
| 82 |
|
| 83 |
AST_TEST_DEFINE(send_msg_handle_string) |
| 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 = { |
| 89 |
.on_string = handle_string, |
| 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; |
| 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, |
| 109 |
&aeap_params, AEAP_REMOTE_URL, AEAP_REMOTE_PROTOCOL, AEAP_CONNECTION_TIMEOUT))); |
| 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( |
| 113 |
ast_aeap_message_type_json, "foo", AEAP_MESSAGE_ID, NULL))); |
| 114 |
ast_test_validate(test, ast_aeap_send_msg_tsx(aeap, &tsx_params)); |
| 115 |
ast_aeap_user_data_unregister(aeap, AEAP_MESSAGE_ID); |
| 116 |
|
| 117 |
return passed == 2 ? AST_TEST_PASS : AST_TEST_FAIL; |
| 118 |
} |
| 119 |
|
| 120 |
static int handle_msg(struct ast_aeap *aeap, struct ast_aeap_message *message, void *data) |
| 121 |
{ |
| 122 |
int *passed = ast_aeap_user_data_object_by_id(aeap, AEAP_MESSAGE_ID); |
| 123 |
|
| 124 |
*passed = !strcmp(ast_aeap_message_id(message), AEAP_MESSAGE_ID) && |
| 125 |
ast_aeap_message_is_named(message, data); |
| 126 |
|
| 127 |
if (!*passed) { |
| 128 |
ast_log(LOG_ERROR, "Name '%s' did not equal '%s' for message '%s'", |
| 129 |
ast_aeap_message_name(message), (char *)data, ast_aeap_message_id(message)); |
| 130 |
} |
| 131 |
|
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
static const struct ast_aeap_message_handler handlers[] = { |
| 136 |
{ "foo", handle_msg }, |
| 137 |
}; |
| 138 |
|
| 139 |
AST_TEST_DEFINE(send_msg_handle_response) |
| 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 = { |
| 145 |
.response_handlers = handlers, |
| 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 |
|
| 162 |
aeap_params.msg_type = ast_aeap_message_type_json; |
| 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, |
| 169 |
&aeap_params, AEAP_REMOTE_URL, AEAP_REMOTE_PROTOCOL, AEAP_CONNECTION_TIMEOUT))); |
| 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( |
| 172 |
ast_aeap_message_type_json, name, AEAP_MESSAGE_ID, NULL))); |
| 173 |
ast_test_validate(test, !ast_aeap_send_msg_tsx(aeap, &tsx_params)); |
| 174 |
ast_aeap_user_data_unregister(aeap, AEAP_MESSAGE_ID); |
| 175 |
|
| 176 |
return passed ? AST_TEST_PASS : AST_TEST_FAIL; |
| 177 |
} |
| 178 |
|
| 179 |
AST_TEST_DEFINE(send_msg_handle_request) |
| 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 = { |
| 185 |
.request_handlers = handlers, |
| 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 |
|
| 202 |
aeap_params.msg_type = ast_aeap_message_type_json; |
| 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, |
| 209 |
&aeap_params, AEAP_REMOTE_URL, AEAP_REMOTE_PROTOCOL, AEAP_CONNECTION_TIMEOUT))); |
| 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( |
| 212 |
ast_aeap_message_type_json, name, AEAP_MESSAGE_ID, NULL))); |
| 213 |
ast_test_validate(test, !ast_aeap_send_msg_tsx(aeap, &tsx_params)); |
| 214 |
ast_aeap_user_data_unregister(aeap, AEAP_MESSAGE_ID); |
| 215 |
|
| 216 |
return passed ? AST_TEST_PASS : AST_TEST_FAIL; |
| 217 |
} |
| 218 |
|
| 219 |
static struct ast_http_server *http_server; |
| 220 |
|
| 221 |
static int load_module(void) |
| 222 |
{ |
| 223 |
if (!(http_server = ast_http_test_server_get("aeap transport http server", NULL))) { |
| 224 |
return AST_MODULE_LOAD_DECLINE; |
| 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 |
|
| 232 |
return AST_MODULE_LOAD_SUCCESS; |
| 233 |
} |
| 234 |
|
| 235 |
static int unload_module(void) |
| 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 |
} |
| 246 |
|
| 247 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk External Application Protocol Object Tests", |
| 248 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 249 |
.load = load_module, |
| 250 |
.unload = unload_module, |
| 251 |
.requires = "res_aeap", |
| 252 |
); |