| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include "asterisk/file.h" |
| 37 |
#include "asterisk/channel.h" |
| 38 |
#include "asterisk/pbx.h" |
| 39 |
#include "asterisk/module.h" |
| 40 |
#include "asterisk/lock.h" |
| 41 |
#include "asterisk/app.h" |
| 42 |
#include "asterisk/strings.h" |
| 43 |
#include "asterisk/stringfields.h" |
| 44 |
#include "asterisk/threadstorage.h" |
| 45 |
#include "asterisk/test.h" |
| 46 |
#include "asterisk/vector.h" |
| 47 |
|
| 48 |
static enum ast_test_result_state test_chan_integer(struct ast_test *test, |
| 49 |
struct ast_channel *c, int *ifield, const char *expression) |
| 50 |
{ |
| 51 |
int i, okay = 1, value1 = -1, value2 = -1; |
| 52 |
char workspace[4096]; |
| 53 |
struct ast_str *str = ast_str_create(16); |
| 54 |
|
| 55 |
for (i = 0; i < 256; i++) { |
| 56 |
*ifield = i; |
| 57 |
ast_str_substitute_variables(&str, 0, c, expression); |
| 58 |
pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); |
| 59 |
|
| 60 |
if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) { |
| 61 |
ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i); |
| 62 |
okay = 0; |
| 63 |
} |
| 64 |
} |
| 65 |
ast_test_status_update(test, "Tested '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); |
| 66 |
|
| 67 |
ast_free(str); |
| 68 |
|
| 69 |
return okay ? AST_TEST_PASS : AST_TEST_FAIL; |
| 70 |
} |
| 71 |
|
| 72 |
static enum ast_test_result_state test_chan_integer_accessor(struct ast_test *test, |
| 73 |
struct ast_channel *c, void (*setter)(struct ast_channel *, int),const char *expression) |
| 74 |
{ |
| 75 |
int i, okay = 1, value1 = -1, value2 = -1; |
| 76 |
char workspace[4096]; |
| 77 |
struct ast_str *str = ast_str_create(16); |
| 78 |
|
| 79 |
for (i = 0; i < 256; i++) { |
| 80 |
setter(c, i); |
| 81 |
ast_str_substitute_variables(&str, 0, c, expression); |
| 82 |
pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); |
| 83 |
|
| 84 |
if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) { |
| 85 |
ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i); |
| 86 |
okay = 0; |
| 87 |
} |
| 88 |
} |
| 89 |
ast_test_status_update(test, "Tested '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); |
| 90 |
|
| 91 |
ast_free(str); |
| 92 |
|
| 93 |
return okay ? AST_TEST_PASS : AST_TEST_FAIL; |
| 94 |
} |
| 95 |
|
| 96 |
static enum ast_test_result_state test_chan_string(struct ast_test *test, |
| 97 |
struct ast_channel *c, void (*setter)(struct ast_channel *, const char *), |
| 98 |
const char *(*getter)(const struct ast_channel *), const char *expression) |
| 99 |
{ |
| 100 |
const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" }; |
| 101 |
int i, okay = 1; |
| 102 |
char workspace[4096]; |
| 103 |
struct ast_str *str = ast_str_create(16); |
| 104 |
|
| 105 |
for (i = 0; i < ARRAY_LEN(values); i++) { |
| 106 |
setter(c, values[i]); |
| 107 |
ast_str_substitute_variables(&str, 0, c, expression); |
| 108 |
pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); |
| 109 |
|
| 110 |
if (strcmp(getter(c), ast_str_buffer(str)) != 0 || strcmp(getter(c), workspace) != 0) { |
| 111 |
ast_test_status_update(test, "%s != %s != %s\n", getter(c), ast_str_buffer(str), workspace); |
| 112 |
okay = 0; |
| 113 |
} |
| 114 |
} |
| 115 |
ast_test_status_update(test, "Tested '%s' . . . . . %s\n", |
| 116 |
expression, okay ? "passed" : "FAILED"); |
| 117 |
|
| 118 |
ast_free(str); |
| 119 |
|
| 120 |
return okay ? AST_TEST_PASS : AST_TEST_FAIL; |
| 121 |
} |
| 122 |
|
| 123 |
static enum ast_test_result_state test_chan_variable(struct ast_test *test, |
| 124 |
struct ast_channel *c, const char *varname) |
| 125 |
{ |
| 126 |
const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" }; |
| 127 |
int i, okay = 1; |
| 128 |
char workspace[4096]; |
| 129 |
struct ast_str *str = ast_str_create(16); |
| 130 |
struct ast_str *var = ast_str_create(16); |
| 131 |
|
| 132 |
ast_str_set(&var, 0, "${%s}", varname); |
| 133 |
for (i = 0; i < ARRAY_LEN(values); i++) { |
| 134 |
pbx_builtin_setvar_helper(c, varname, values[i]); |
| 135 |
ast_str_substitute_variables(&str, 0, c, ast_str_buffer(var)); |
| 136 |
pbx_substitute_variables_helper(c, ast_str_buffer(var), workspace, sizeof(workspace)); |
| 137 |
|
| 138 |
if (strcmp(values[i], ast_str_buffer(str)) != 0 || strcmp(values[i], workspace) != 0) { |
| 139 |
ast_test_status_update(test, "%s != %s != %s\n", |
| 140 |
values[i], ast_str_buffer(str), workspace); |
| 141 |
okay = 0; |
| 142 |
} |
| 143 |
} |
| 144 |
ast_test_status_update(test, "Tested '%s' . . . . . %s\n", |
| 145 |
ast_str_buffer(var), okay ? "passed" : "FAILED"); |
| 146 |
|
| 147 |
ast_free(str); |
| 148 |
ast_free(var); |
| 149 |
|
| 150 |
return okay ? AST_TEST_PASS : AST_TEST_FAIL; |
| 151 |
} |
| 152 |
|
| 153 |
static enum ast_test_result_state test_chan_function(struct ast_test *test, |
| 154 |
struct ast_channel *c, const char *expression) |
| 155 |
{ |
| 156 |
int okay = 1; |
| 157 |
char workspace[4096]; |
| 158 |
struct ast_str *str = ast_str_create(16); |
| 159 |
|
| 160 |
ast_str_substitute_variables(&str, 0, c, expression); |
| 161 |
pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); |
| 162 |
|
| 163 |
if (strcmp(workspace, ast_str_buffer(str)) != 0) { |
| 164 |
ast_test_status_update(test, "expr: '%s' ... %s != %s\n", |
| 165 |
expression, ast_str_buffer(str), workspace); |
| 166 |
okay = 0; |
| 167 |
} |
| 168 |
ast_test_status_update(test, "Tested '%s' . . . . . %s\n", |
| 169 |
expression, okay ? "passed" : "FAILED"); |
| 170 |
|
| 171 |
ast_free(str); |
| 172 |
|
| 173 |
return okay ? AST_TEST_PASS : AST_TEST_FAIL; |
| 174 |
} |
| 175 |
|
| 176 |
static enum ast_test_result_state test_2way_function(struct ast_test *test, |
| 177 |
struct ast_channel *c, const char *encode1, const char *encode2, |
| 178 |
const char *decode1, const char *decode2) |
| 179 |
{ |
| 180 |
struct ast_str *str = ast_str_create(16), *expression = ast_str_alloca(120); |
| 181 |
int okay; |
| 182 |
|
| 183 |
ast_str_set(&expression, 0, "%s%s%s", encode1, "foobarbaz", encode2); |
| 184 |
ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression)); |
| 185 |
ast_str_set(&expression, 0, "%s%s%s", decode1, ast_str_buffer(str), decode2); |
| 186 |
ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression)); |
| 187 |
|
| 188 |
okay = !strcmp(ast_str_buffer(str), "foobarbaz"); |
| 189 |
if (!okay) { |
| 190 |
ast_test_status_update(test, "'%s' != 'foobarbaz'\n", |
| 191 |
ast_str_buffer(str)); |
| 192 |
} |
| 193 |
ast_test_status_update(test, "Tested '%s%s' and '%s%s' . . . . . %s\n", |
| 194 |
encode1, encode2, decode1, decode2, |
| 195 |
okay ? "passed" : "FAILED"); |
| 196 |
|
| 197 |
ast_free(str); |
| 198 |
|
| 199 |
return okay ? AST_TEST_PASS : AST_TEST_FAIL; |
| 200 |
} |
| 201 |
|
| 202 |
static enum ast_test_result_state test_expected_result(struct ast_test *test, |
| 203 |
struct ast_channel *c, const char *expression, const char *result) |
| 204 |
{ |
| 205 |
struct ast_str *str = ast_str_create(16); |
| 206 |
int okay; |
| 207 |
|
| 208 |
ast_str_substitute_variables(&str, 0, c, expression); |
| 209 |
|
| 210 |
okay = !strcmp(ast_str_buffer(str), result); |
| 211 |
if (!okay) { |
| 212 |
ast_test_status_update(test, "'%s' != '%s'\n", |
| 213 |
ast_str_buffer(str), result); |
| 214 |
} |
| 215 |
ast_test_status_update(test, "Tested '%s' ('%s') == '%s' . . . . . %s\n", |
| 216 |
ast_str_buffer(str), expression, result, |
| 217 |
okay ? "passed" : "FAILED"); |
| 218 |
|
| 219 |
ast_free(str); |
| 220 |
|
| 221 |
return okay ? AST_TEST_PASS : AST_TEST_FAIL; |
| 222 |
} |
| 223 |
|
| 224 |
AST_TEST_DEFINE(test_substitution) |
| 225 |
{ |
| 226 |
struct ast_channel *c; |
| 227 |
int i; |
| 228 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 229 |
struct ast_vector_string *funcs; |
| 230 |
|
| 231 |
switch (cmd) { |
| 232 |
case TEST_INIT: |
| 233 |
info->name = "test_substitution"; |
| 234 |
info->category = "/main/pbx/"; |
| 235 |
info->summary = "Test variable and function substitution"; |
| 236 |
info->description = |
| 237 |
"This test executes a variety of variable and function substitutions " |
| 238 |
"and ensures that the expected results are received."; |
| 239 |
return AST_TEST_NOT_RUN; |
| 240 |
case TEST_EXECUTE: |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
ast_test_status_update(test, "Testing variable substitution ...\n"); |
| 245 |
|
| 246 |
c = ast_channel_alloc(0, 0, "", "", "", "", "", NULL, NULL, 0, "Test/substitution"); |
| 247 |
ast_channel_unlock(c); |
| 248 |
|
| 249 |
#define TEST(t) if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; } |
| 250 |
#if 0 |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
TEST(test_chan_integer(test, c, &c->caller.id.number.presentation, "${CALLINGPRES}")); |
| 257 |
#endif |
| 258 |
TEST(test_chan_integer(test, c, &ast_channel_caller(c)->ani2, "${CALLINGANI2}")); |
| 259 |
TEST(test_chan_integer(test, c, &ast_channel_caller(c)->id.number.plan, "${CALLINGTON}")); |
| 260 |
TEST(test_chan_integer(test, c, &ast_channel_dialed(c)->transit_network_select, "${CALLINGTNS}")); |
| 261 |
TEST(test_chan_integer_accessor(test, c, ast_channel_hangupcause_set, "${HANGUPCAUSE}")); |
| 262 |
TEST(test_chan_integer_accessor(test, c, ast_channel_priority_set, "${PRIORITY}")); |
| 263 |
TEST(test_chan_string(test, c, ast_channel_context_set, ast_channel_context, "${CONTEXT}")); |
| 264 |
TEST(test_chan_string(test, c, ast_channel_exten_set, ast_channel_exten, "${EXTEN}")); |
| 265 |
TEST(test_chan_variable(test, c, "CHANNEL(language)")); |
| 266 |
TEST(test_chan_variable(test, c, "CHANNEL(musicclass)")); |
| 267 |
TEST(test_chan_variable(test, c, "CHANNEL(parkinglot)")); |
| 268 |
TEST(test_chan_variable(test, c, "CALLERID(name)")); |
| 269 |
TEST(test_chan_variable(test, c, "CURLOPT(proxyuserpwd)")); |
| 270 |
TEST(test_chan_variable(test, c, "CDR(foo)")); |
| 271 |
TEST(test_chan_variable(test, c, "ENV(foo)")); |
| 272 |
TEST(test_chan_variable(test, c, "GLOBAL(foo)")); |
| 273 |
TEST(test_chan_variable(test, c, "GROUP()")); |
| 274 |
TEST(test_2way_function(test, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}")); |
| 275 |
TEST(test_2way_function(test, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}")); |
| 276 |
pbx_builtin_setvar_helper(c, "foo", "123"); |
| 277 |
pbx_builtin_setvar_helper(c, "bar", "foo"); |
| 278 |
pbx_builtin_setvar_helper(c, "baz", "fo"); |
| 279 |
TEST(test_expected_result(test, c, "${foo}${foo}", "123123")); |
| 280 |
TEST(test_expected_result(test, c, "A${foo}A${foo}A", "A123A123A")); |
| 281 |
TEST(test_expected_result(test, c, "A${${bar}}A", "A123A")); |
| 282 |
TEST(test_expected_result(test, c, "A${${baz}o}A", "A123A")); |
| 283 |
TEST(test_expected_result(test, c, "A${${baz}o:1}A", "A23A")); |
| 284 |
TEST(test_expected_result(test, c, "A${${baz}o:1:1}A", "A2A")); |
| 285 |
TEST(test_expected_result(test, c, "A${${baz}o:1:-1}A", "A2A")); |
| 286 |
TEST(test_expected_result(test, c, "A${${baz}o:-1:1}A", "A3A")); |
| 287 |
TEST(test_expected_result(test, c, "A${${baz}o:-2:1}A", "A2A")); |
| 288 |
TEST(test_expected_result(test, c, "A${${baz}o:-2:-1}A", "A2A")); |
| 289 |
pbx_builtin_setvar_helper(c, "list1", "ab&cd&ef"); |
| 290 |
TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,ab)}", "cd&ef")); |
| 291 |
TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,cd)}", "ab&ef")); |
| 292 |
TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,ef)}", "ab&cd")); |
| 293 |
TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,gh)}", "ab&cd&ef")); |
| 294 |
TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,c)}", "ab&cd&ef")); |
| 295 |
TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,d)}", "ab&cd&ef")); |
| 296 |
pbx_builtin_setvar_helper(c, "list2", "ab"); |
| 297 |
TEST(test_expected_result(test, c, "${LISTFILTER(list2,&,ab)}", "")); |
| 298 |
pbx_builtin_setvar_helper(c, "list_empty", ""); |
| 299 |
TEST(test_expected_result(test, c, "${LISTFILTER(list_empty,&,ab)}", "")); |
| 300 |
TEST(test_expected_result(test, c, "${SHELL(printf '%d' 123)},${SHELL(printf '%d' 456)}", "123,456")); |
| 301 |
TEST(test_expected_result(test, c, "${foo},${CDR(answer)},${SHELL(printf '%d' 456)}", "123,,456")); |
| 302 |
TEST(test_expected_result(test, c, "${foo},${CDR(answer,u)},${SHELL(printf '%d' 456)}", "123,0.000000,456")); |
| 303 |
TEST(test_expected_result(test, c, "${foo},${this_does_not_exist},${THIS_DOES_NOT_EXIST(either)}", "123,,")); |
| 304 |
#undef TEST |
| 305 |
|
| 306 |
|
| 307 |
funcs = ast_cli_completion_vector("core show function", ""); |
| 308 |
|
| 309 |
|
| 310 |
for (i = 1; funcs && i < AST_VECTOR_SIZE(funcs); i++) { |
| 311 |
char *cmd = AST_VECTOR_GET(funcs, i); |
| 312 |
|
| 313 |
if (strcmp(cmd, "CHANNEL") && strcmp(cmd, "CALLERID") && strncmp(cmd, "CURL", 4) && |
| 314 |
strncmp(cmd, "AES", 3) && strncmp(cmd, "BASE64", 6) && |
| 315 |
strcmp(cmd, "CDR") && strcmp(cmd, "ENV") && strcmp(cmd, "GLOBAL") && |
| 316 |
strcmp(cmd, "GROUP") && strcmp(cmd, "CUT") && strcmp(cmd, "LISTFILTER") && |
| 317 |
strcmp(cmd, "PP_EACH_EXTENSION") && strcmp(cmd, "SET")) { |
| 318 |
struct ast_custom_function *acf = ast_custom_function_find(cmd); |
| 319 |
if (acf->read && acf->read2) { |
| 320 |
char expression[80]; |
| 321 |
snprintf(expression, sizeof(expression), "${%s(foo)}", cmd); |
| 322 |
if (AST_TEST_FAIL == test_chan_function(test, c, expression)) { |
| 323 |
res = AST_TEST_FAIL; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
if (funcs) { |
| 330 |
AST_VECTOR_CALLBACK_VOID(funcs, ast_free); |
| 331 |
AST_VECTOR_PTR_FREE(funcs); |
| 332 |
} |
| 333 |
ast_hangup(c); |
| 334 |
|
| 335 |
return res; |
| 336 |
} |
| 337 |
|
| 338 |
static int unload_module(void) |
| 339 |
{ |
| 340 |
AST_TEST_UNREGISTER(test_substitution); |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
static int load_module(void) |
| 345 |
{ |
| 346 |
AST_TEST_REGISTER(test_substitution); |
| 347 |
return AST_MODULE_LOAD_SUCCESS; |
| 348 |
} |
| 349 |
|
| 350 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Substitution tests"); |