Asterisk - The Open Source Telephony Project GIT-master-f36a736
test_substitution.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2009, Digium, Inc.
5 *
6 * Tilghman Lesher <tlesher AT digium DOT com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*! \file
20 *
21 * \brief Substitution Test
22 *
23 * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
24 *
25 * \ingroup tests
26 */
27
28/*** MODULEINFO
29 <depend>TEST_FRAMEWORK</depend>
30 <depend>func_curl</depend>
31 <support_level>core</support_level>
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"
45#include "asterisk/test.h"
46#include "asterisk/vector.h"
47
48static 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
68
69 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
70}
71
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
92
93 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
94}
95
96static 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
123static 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]);
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
153static 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
176static 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);
185 ast_str_set(&expression, 0, "%s%s%s", decode1, ast_str_buffer(str), decode2);
187
188 okay = !strcmp(ast_str_buffer(str), "foobarbaz");
189 if (!okay) {
190 ast_test_status_update(test, "'%s' != 'foobarbaz'\n",
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
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",
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
224AST_TEST_DEFINE(test_substitution)
225{
226 struct ast_channel *c;
227 int i;
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");
248
249#define TEST(t) if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; }
250#if 0
251 /*
252 * We can no longer test the CALLINGPRES value this way because it is now
253 * a calculated value from the name and number presentation information to
254 * get a combined presentation value.
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}"));
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 /* For testing dialplan functions */
307 funcs = ast_cli_completion_vector("core show function", "");
308
309 /* Skip "best match" element 0 */
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")) {
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) {
331 AST_VECTOR_PTR_FREE(funcs);
332 }
333 ast_hangup(c);
334
335 return res;
336}
337
338static int unload_module(void)
339{
340 AST_TEST_UNREGISTER(test_substitution);
341 return 0;
342}
343
344static int load_module(void)
345{
346 AST_TEST_REGISTER(test_substitution);
348}
349
const char * str
Definition: app_jack.c:147
#define var
Definition: ast_expr2f.c:605
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
static PGresult * result
Definition: cel_pgsql.c:84
General Asterisk PBX channel definitions.
void ast_channel_exten_set(struct ast_channel *chan, const char *value)
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2560
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
Definition: channel.h:1299
const char * ast_channel_context(const struct ast_channel *chan)
struct ast_party_dialed * ast_channel_dialed(struct ast_channel *chan)
void ast_channel_context_set(struct ast_channel *chan, const char *value)
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
void ast_channel_priority_set(struct ast_channel *chan, int value)
void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
const char * ast_channel_exten(const struct ast_channel *chan)
#define ast_channel_unlock(chan)
Definition: channel.h:2969
struct ast_vector_string * ast_cli_completion_vector(const char *text, const char *word)
Generates a vector of strings for CLI completion.
Definition: main/cli.c:2773
Generic File Format Support. Should be included by clients of the file handling routines....
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
Asterisk locking-related definitions:
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
def info(msg)
Core PBX routines and definitions.
struct ast_custom_function * ast_custom_function_find(const char *name)
Definition: ael_main.c:173
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
#define NULL
Definition: resample.c:96
String manipulation functions.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define ast_str_alloca(init_len)
Definition: strings.h:848
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
ast_acf_read_fn_t read
Definition: pbx.h:129
ast_acf_read2_fn_t read2
Definition: pbx.h:137
Support for dynamic strings.
Definition: strings.h:623
String vector definitions.
Definition: vector.h:55
Number structure.
Definition: app_followme.c:154
Test Framework API.
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
ast_test_result_state
Definition: test.h:193
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_FAIL
Definition: test.h:196
@ AST_TEST_NOT_RUN
Definition: test.h:194
static struct test_val c
static enum ast_test_result_state test_chan_integer(struct ast_test *test, struct ast_channel *c, int *ifield, const char *expression)
static enum ast_test_result_state test_chan_function(struct ast_test *test, struct ast_channel *c, const char *expression)
AST_TEST_DEFINE(test_substitution)
static enum ast_test_result_state test_2way_function(struct ast_test *test, struct ast_channel *c, const char *encode1, const char *encode2, const char *decode1, const char *decode2)
static enum ast_test_result_state test_chan_string(struct ast_test *test, struct ast_channel *c, void(*setter)(struct ast_channel *, const char *), const char *(*getter)(const struct ast_channel *), const char *expression)
static enum ast_test_result_state test_chan_variable(struct ast_test *test, struct ast_channel *c, const char *varname)
static int load_module(void)
static int unload_module(void)
#define TEST(t)
static enum ast_test_result_state test_chan_integer_accessor(struct ast_test *test, struct ast_channel *c, void(*setter)(struct ast_channel *, int), const char *expression)
static enum ast_test_result_state test_expected_result(struct ast_test *test, struct ast_channel *c, const char *expression, const char *result)
Definitions to aid in the use of thread local storage.
#define ARRAY_LEN(a)
Definition: utils.h:666
Vector container support.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
#define AST_VECTOR_PTR_FREE(vec)
Deallocates this vector pointer.
Definition: vector.h:189
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:862
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680