Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
test_substitution.c File Reference

Substitution Test. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/strings.h"
#include "asterisk/stringfields.h"
#include "asterisk/threadstorage.h"
#include "asterisk/test.h"
#include "asterisk/vector.h"
Include dependency graph for test_substitution.c:

Go to the source code of this file.

Macros

#define TEST(t)   if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; }
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (test_substitution)
 
static int load_module (void)
 
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_function (struct ast_test *test, struct ast_channel *c, const char *expression)
 
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_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_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 enum ast_test_result_state test_expected_result (struct ast_test *test, struct ast_channel *c, const char *expression, const char *result)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Substitution 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, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Substitution Test.

Author
Tilghman Lesher <tlesher AT digium DOT com> 

Definition in file test_substitution.c.

Macro Definition Documentation

◆ TEST

#define TEST (   t)    if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; }

Referenced by AST_TEST_DEFINE().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 350 of file test_substitution.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 350 of file test_substitution.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 350 of file test_substitution.c.

◆ AST_TEST_DEFINE()

AST_TEST_DEFINE ( test_substitution  )

Definition at line 224 of file test_substitution.c.

References ast_channel_alloc, ast_channel_caller(), ast_channel_context(), ast_channel_context_set(), ast_channel_dialed(), ast_channel_exten(), ast_channel_exten_set(), ast_channel_hangupcause_set(), ast_channel_priority_set(), ast_channel_unlock, ast_cli_completion_vector(), ast_custom_function_find(), ast_free, ast_hangup(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, AST_VECTOR_CALLBACK_VOID, AST_VECTOR_GET, AST_VECTOR_PTR_FREE, AST_VECTOR_SIZE, c, ast_channel::caller, ast_party_caller::id, sip_to_pjsip::info(), NULL, ast_party_id::number, pbx_builtin_setvar_helper(), ast_party_number::presentation, ast_custom_function::read, ast_custom_function::read2, TEST, test_2way_function(), test_chan_function(), test_chan_integer(), test_chan_integer_accessor(), test_chan_string(), test_chan_variable(), TEST_EXECUTE, test_expected_result(), and TEST_INIT.

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 }
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
Main Channel structure associated with a channel.
struct ast_party_caller caller
Channel Caller ID information.
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)
static struct test_val c
#define NULL
Definition: resample.c:96
#define TEST(t)
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
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
#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)
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
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)
#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...
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682
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)
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)
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)
#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
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:343

◆ load_module()

static int load_module ( void  )
static

Definition at line 344 of file test_substitution.c.

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

345 {
346  AST_TEST_REGISTER(test_substitution);
348 }
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

◆ test_2way_function()

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

Definition at line 176 of file test_substitution.c.

References ast_free, ast_str_alloca, ast_str_buffer(), ast_str_create, ast_str_set(), ast_str_substitute_variables(), AST_TEST_FAIL, AST_TEST_PASS, ast_test_status_update, and str.

Referenced by AST_TEST_DEFINE().

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 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
#define ast_str_alloca(init_len)
Definition: strings.h:800
const char * str
Definition: app_jack.c:147
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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
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_free(a)
Definition: astmm.h:182
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ test_chan_function()

static enum ast_test_result_state test_chan_function ( struct ast_test test,
struct ast_channel c,
const char *  expression 
)
static

Definition at line 153 of file test_substitution.c.

References ast_free, ast_str_buffer(), ast_str_create, ast_str_substitute_variables(), AST_TEST_FAIL, AST_TEST_PASS, ast_test_status_update, pbx_substitute_variables_helper(), and str.

Referenced by AST_TEST_DEFINE().

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 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
const char * str
Definition: app_jack.c:147
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
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_free(a)
Definition: astmm.h:182
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ test_chan_integer()

static enum ast_test_result_state test_chan_integer ( struct ast_test test,
struct ast_channel c,
int *  ifield,
const char *  expression 
)
static

Definition at line 48 of file test_substitution.c.

References ast_free, ast_str_buffer(), ast_str_create, ast_str_substitute_variables(), AST_TEST_FAIL, AST_TEST_PASS, ast_test_status_update, pbx_substitute_variables_helper(), and str.

Referenced by AST_TEST_DEFINE().

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 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
const char * str
Definition: app_jack.c:147
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
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_free(a)
Definition: astmm.h:182
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ test_chan_integer_accessor()

static enum ast_test_result_state test_chan_integer_accessor ( struct ast_test test,
struct ast_channel c,
void(*)(struct ast_channel *, int)  setter,
const char *  expression 
)
static

Definition at line 72 of file test_substitution.c.

References ast_free, ast_str_buffer(), ast_str_create, ast_str_substitute_variables(), AST_TEST_FAIL, AST_TEST_PASS, ast_test_status_update, pbx_substitute_variables_helper(), and str.

Referenced by AST_TEST_DEFINE().

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 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
const char * str
Definition: app_jack.c:147
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
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_free(a)
Definition: astmm.h:182
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ test_chan_string()

static enum ast_test_result_state test_chan_string ( struct ast_test test,
struct ast_channel c,
void(*)(struct ast_channel *, const char *)  setter,
const char *(*)(const struct ast_channel *)  getter,
const char *  expression 
)
static

Definition at line 96 of file test_substitution.c.

References ARRAY_LEN, ast_free, ast_str_buffer(), ast_str_create, ast_str_substitute_variables(), AST_TEST_FAIL, AST_TEST_PASS, ast_test_status_update, pbx_substitute_variables_helper(), and str.

Referenced by AST_TEST_DEFINE().

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 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
const char * str
Definition: app_jack.c:147
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
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_free(a)
Definition: astmm.h:182
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ test_chan_variable()

static enum ast_test_result_state test_chan_variable ( struct ast_test test,
struct ast_channel c,
const char *  varname 
)
static

Definition at line 123 of file test_substitution.c.

References ARRAY_LEN, ast_free, ast_str_buffer(), ast_str_create, ast_str_set(), ast_str_substitute_variables(), AST_TEST_FAIL, AST_TEST_PASS, ast_test_status_update, pbx_builtin_setvar_helper(), pbx_substitute_variables_helper(), str, and var.

Referenced by AST_TEST_DEFINE().

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 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
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)
const char * str
Definition: app_jack.c:147
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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
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_free(a)
Definition: astmm.h:182
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 ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ test_expected_result()

static enum ast_test_result_state test_expected_result ( struct ast_test test,
struct ast_channel c,
const char *  expression,
const char *  result 
)
static

Definition at line 202 of file test_substitution.c.

References ast_free, ast_str_buffer(), ast_str_create, ast_str_substitute_variables(), AST_TEST_FAIL, AST_TEST_PASS, ast_test_status_update, and str.

Referenced by AST_TEST_DEFINE().

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 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
const char * str
Definition: app_jack.c:147
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
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_free(a)
Definition: astmm.h:182
static PGresult * result
Definition: cel_pgsql.c:88
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 338 of file test_substitution.c.

References AST_TEST_UNREGISTER.

339 {
340  AST_TEST_UNREGISTER(test_substitution);
341  return 0;
342 }
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Substitution 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, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 350 of file test_substitution.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 350 of file test_substitution.c.