Asterisk - The Open Source Telephony Project  18.5.0
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"
43 #include "asterisk/stringfields.h"
44 #include "asterisk/threadstorage.h"
45 #include "asterisk/test.h"
46 #include "asterisk/vector.h"
47 
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 
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 
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 
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 
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 
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 
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;
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}"));
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}"));
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 
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);
348 }
349 
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
struct ast_party_caller caller
Channel Caller ID information.
String manipulation functions.
int presentation
Q.931 presentation-indicator and screening-indicator encoded fields.
Definition: channel.h:296
void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
#define var
Definition: ast_expr2f.c:614
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
Test Framework API.
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
static struct test_val c
#define ast_str_alloca(init_len)
Definition: strings.h:800
const char * str
Definition: app_jack.c:147
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
#define NULL
Definition: resample.c:96
#define TEST(t)
Definitions to aid in the use of thread local storage.
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)
Number structure.
Definition: app_followme.c:154
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:1065
struct ast_party_id id
Caller party ID.
Definition: channel.h:421
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:2731
General Asterisk PBX channel definitions.
AST_TEST_DEFINE(test_substitution)
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
static enum ast_test_result_state test_chan_integer(struct ast_test *test, struct ast_channel *c, int *ifield, const char *expression)
const char * ast_channel_exten(const struct ast_channel *chan)
Core PBX routines and definitions.
static enum ast_test_result_state test_chan_function(struct ast_test *test, struct ast_channel *c, const char *expression)
struct ast_custom_function * ast_custom_function_find(const char *name)
Definition: ael_main.c:173
ast_acf_read2_fn_t read2
Definition: pbx.h:137
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
def info(msg)
struct ast_party_dialed * ast_channel_dialed(struct ast_channel *chan)
#define ast_channel_unlock(chan)
Definition: channel.h:2946
#define ast_free(a)
Definition: astmm.h:182
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2548
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)
Vector container support.
#define AST_VECTOR_PTR_FREE(vec)
Deallocates this vector pointer.
Definition: vector.h:189
void ast_channel_exten_set(struct ast_channel *chan, const char *value)
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...
static int load_module(void)
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682
Definition: test.c:65
ast_acf_read_fn_t read
Definition: pbx.h:129
void ast_channel_context_set(struct ast_channel *chan, const char *value)
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)
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
static PGresult * result
Definition: cel_pgsql.c:88
static enum ast_test_result_state test_expected_result(struct ast_test *test, struct ast_channel *c, const char *expression, const char *result)
const char * ast_channel_context(const struct ast_channel *chan)
static int unload_module(void)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
void ast_channel_priority_set(struct ast_channel *chan, int value)
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
Definition: channel.h:1259
static enum ast_test_result_state test_chan_variable(struct ast_test *test, struct ast_channel *c, const char *varname)
Asterisk module definitions.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611
ast_test_result_state
Definition: test.h:200
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:865
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:343