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

Function FILE tests. More...

#include "asterisk.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/pbx.h"
Include dependency graph for test_func_file.c:

Go to the source code of this file.

Macros

#define C1024   "1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (test_func_file)
 
static char * file2display (struct ast_str **buf, ssize_t len, const char *input)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "FILE() 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
 
struct {
   const char *   args
 
   const char *   contents
 
   const char *   value
 
read_tests []
 
struct {
   const char *   args
 
   const char *   contents
 
   const char *   contents2
 
   const char *   value
 
write_tests []
 

Detailed Description

Function FILE tests.

Author
Tilghman Lesher <tlesher AT digium DOT com> 

Definition in file test_func_file.c.

Macro Definition Documentation

◆ C1024

#define C1024   "1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF"

Definition at line 41 of file test_func_file.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 399 of file test_func_file.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 399 of file test_func_file.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 399 of file test_func_file.c.

◆ AST_TEST_DEFINE()

AST_TEST_DEFINE ( test_func_file  )

Definition at line 248 of file test_func_file.c.

References args, ARRAY_LEN, ast_free, ast_func_write(), ast_str_buffer(), ast_str_create, ast_str_substitute_variables(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, buf, contents, contents2, errno, make_ari_stubs::file, file2display(), sip_to_pjsip::info(), mkdtemp(), NULL, read_tests, TEST_EXECUTE, TEST_INIT, value, and write_tests.

249 {
250  int res = AST_TEST_PASS;
251  int i;
252  char dir[] = "/tmp/test_func_file.XXXXXX";
253  char file[80], expression[256];
254  struct ast_str *buf, *disp[2] = { NULL, NULL };
255  char fbuf[8192];
256  FILE *fh;
257 
258  switch (cmd) {
259  case TEST_INIT:
260  info->name = "func_file";
261  info->category = "/funcs/func_env/";
262  info->summary = "Verify behavior of the FILE() dialplan function";
263  info->description =
264  "Verifies that the examples of the FILE() dialplan function documentation work as described.";
265  return AST_TEST_NOT_RUN;
266  case TEST_EXECUTE:
267  break;
268  }
269 
270  if (!mkdtemp(dir)) {
271  ast_test_status_update(test, "Cannot create temporary directory: %s\n", strerror(errno));
272  return AST_TEST_FAIL;
273  }
274 
275  disp[0] = ast_str_create(16);
276  disp[1] = ast_str_create(16);
277  if (!(buf = ast_str_create(16)) || !disp[0] || !disp[1]) {
278  ast_free(buf);
279  ast_free(disp[0]);
280  ast_free(disp[1]);
281  rmdir(dir);
282  return AST_TEST_FAIL;
283  }
284 
285  snprintf(file, sizeof(file), "%s/test.txt", dir);
286 
287  for (i = 0; i < ARRAY_LEN(read_tests); i++) {
288  if (!(fh = fopen(file, "w"))) {
289  ast_test_status_update(test, "Cannot open test file: %s\n", strerror(errno));
290  ast_free(buf);
291  ast_free(disp[0]);
292  ast_free(disp[1]);
293  unlink(file);
294  rmdir(dir);
295  return AST_TEST_FAIL;
296  }
297 
298  if (fwrite(read_tests[i].contents, 1, strlen(read_tests[i].contents), fh) < strlen(read_tests[i].contents)) {
299  ast_test_status_update(test, "Cannot write initial values into test file: %s\n", strerror(errno));
300  ast_free(buf);
301  ast_free(disp[0]);
302  ast_free(disp[1]);
303  fclose(fh);
304  unlink(file);
305  rmdir(dir);
306  return AST_TEST_FAIL;
307  }
308 
309  fclose(fh);
310 
311  snprintf(expression, sizeof(expression), "${FILE(%s,%s)}", file, read_tests[i].args);
312  ast_str_substitute_variables(&buf, 0, NULL, expression);
313 
314  if (strcmp(ast_str_buffer(buf), read_tests[i].value)) {
315  ast_test_status_update(test, "Expression '${FILE(...,%s)}' did not produce ('%s') the expected value ('%s')\n",
316  read_tests[i].args, file2display(&disp[0], 0, ast_str_buffer(buf)), file2display(&disp[1], 0, read_tests[i].value));
317  res = AST_TEST_FAIL;
318  }
319  }
320 
321  ast_free(buf);
322 
323  for (i = 0; i < ARRAY_LEN(write_tests); i++) {
324  if (!(fh = fopen(file, "w"))) {
325  ast_test_status_update(test, "Cannot open test file: %s\n", strerror(errno));
326  ast_free(disp[0]);
327  ast_free(disp[1]);
328  unlink(file);
329  rmdir(dir);
330  return AST_TEST_FAIL;
331  }
332 
333  if (fwrite(write_tests[i].contents, 1, strlen(write_tests[i].contents), fh) < strlen(write_tests[i].contents)) {
334  ast_test_status_update(test, "Cannot write initial values into test file: %s\n", strerror(errno));
335  ast_free(disp[0]);
336  ast_free(disp[1]);
337  fclose(fh);
338  unlink(file);
339  rmdir(dir);
340  return AST_TEST_FAIL;
341  }
342 
343  fclose(fh);
344 
345  snprintf(expression, sizeof(expression), "FILE(%s,%s)", file, write_tests[i].args);
346  ast_func_write(NULL, expression, write_tests[i].value);
347 
348  if (!(fh = fopen(file, "r"))) {
349  ast_test_status_update(test, "Cannot open test file: %s\n", strerror(errno));
350  ast_free(disp[0]);
351  ast_free(disp[1]);
352  unlink(file);
353  rmdir(dir);
354  return AST_TEST_FAIL;
355  }
356 
357  memset(fbuf, 0, sizeof(fbuf));
358  if (!fread(fbuf, 1, sizeof(fbuf), fh)) {
359  ast_test_status_update(test, "Cannot read write results from test file: %s\n", strerror(errno));
360  ast_free(disp[0]);
361  ast_free(disp[1]);
362  fclose(fh);
363  unlink(file);
364  rmdir(dir);
365  return AST_TEST_FAIL;
366  }
367 
368  fclose(fh);
369 
370  if (strcmp(fbuf, write_tests[i].contents2)) {
371  ast_test_status_update(test, "Expression 'FILE(...,%s)=%s' did not produce ('%s') the expected result ('%s')\n",
372  write_tests[i].args, write_tests[i].value, file2display(&disp[0], 0, fbuf), file2display(&disp[1], 0, write_tests[i].contents2));
373  res = AST_TEST_FAIL;
374  } else {
375  ast_test_status_update(test, "Expression 'FILE(...,%s)=%s'... OK!\n", write_tests[i].args, write_tests[i].value);
376  }
377  }
378 
379  ast_free(disp[0]);
380  ast_free(disp[1]);
381  unlink(file);
382  rmdir(dir);
383 
384  return res;
385 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
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 * args
#define NULL
Definition: resample.c:96
static char * file2display(struct ast_str **buf, ssize_t len, const char *input)
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
const char * contents
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
char * mkdtemp(char *template_s)
const char * value
def info(msg)
int errno
#define ast_free(a)
Definition: astmm.h:182
static struct @510 write_tests[]
const char * contents2
int ast_func_write(struct ast_channel *chan, const char *function, const char *value)
executes a write operation on a function
static struct @509 read_tests[]
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ file2display()

static char* file2display ( struct ast_str **  buf,
ssize_t  len,
const char *  input 
)
static

Definition at line 228 of file test_func_file.c.

References ast_str_append(), ast_str_buffer(), and ast_str_reset().

Referenced by AST_TEST_DEFINE().

229 {
230  const char *ptr;
231  ast_str_reset(*buf);
232  for (ptr = input; *ptr; ptr++) {
233  if (*ptr == '\n') {
234  ast_str_append(buf, len, "\\n");
235  } else if (*ptr == '\r') {
236  ast_str_append(buf, len, "\\r");
237  } else if (*ptr == '\t') {
238  ast_str_append(buf, len, "\\t");
239  } else if (*ptr < ' ' || *ptr > 125) {
240  ast_str_append(buf, len, "\\x%hhX", *ptr);
241  } else {
242  ast_str_append(buf, len, "%c", *ptr);
243  }
244  }
245  return ast_str_buffer(*buf);
246 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
static int input(yyscan_t yyscanner)
Definition: ast_expr2f.c:1584
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:653

◆ load_module()

static int load_module ( void  )
static

Definition at line 393 of file test_func_file.c.

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

394 {
395  AST_TEST_REGISTER(test_func_file);
397 }
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 387 of file test_func_file.c.

References AST_TEST_UNREGISTER.

388 {
389  AST_TEST_UNREGISTER(test_func_file);
390  return 0;
391 }
#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 = "FILE() 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 399 of file test_func_file.c.

◆ args

const char* args
Examples:
/usr/src/asterisk-18.5.0/main/app.c.

Definition at line 46 of file test_func_file.c.

Referenced by __ast_sorcery_object_field_register(), __find_call(), acf_abs_exec(), acf_curl_exec(), acf_curl_write(), acf_isexten_exec(), acf_jabberreceive_read(), acf_jabberstatus_read(), acf_max_exec(), acf_min_exec(), acf_odbc_read(), acf_odbc_write(), acf_rand_exec(), acf_strftime(), acf_strptime(), acf_transaction_read(), acf_transaction_write(), acf_version_exec(), acf_vmcount_exec(), add_action_to_menu_entry(), add_cfg_entry(), add_format_information_cb(), add_rt_cfg_entry(), add_rt_multi_cfg_entry(), adsi_process(), aelsub_exec(), aes_helper(), agent_function_read(), agent_login_exec(), agent_request_exec(), agi_exec_full(), aMYSQL_connect(), aMYSQL_fetch(), aMYSQL_nextresult(), aMYSQL_query(), aMYSQL_set(), answer_exec_enable(), app_control_dial(), app_exec(), applicationmap_handler(), aqm_exec(), ast_ari_sounds_list(), ast_beep_start(), ast_cc_agent_callback(), ast_cc_agent_status_response(), ast_cli_command_full(), ast_eivr_senddtmf(), ast_func_read(), ast_func_read2(), ast_func_write(), ast_json_pack(), ast_json_stringf(), ast_queue_log(), AST_TEST_DEFINE(), asyncgoto_exec(), attended_transfer_exec(), audiosocket_exec(), audiosocket_request(), background_detect_exec(), blind_transfer_exec(), bridge_exec(), bridgewait_exec(), build_profile(), calendar_query_exec(), calendar_query_result_exec(), callerid_read(), cb_events(), cc_agent_callback_helper(), cc_do_state_change(), cc_request_state_change(), cc_status_response(), cdr_prop_write_callback(), cdr_read_callback(), cdr_write(), cdr_write_callback(), celgenuserevent_exec(), chanavail_exec(), channel_set_debug(), channelvars_handler(), chanspy_exec(), cli_odbc_read(), cli_odbc_write(), confbridge_exec(), config_function_read(), confkick_exec(), control_dial_args_alloc(), control_dial_args_destroy(), controlplayback_exec(), cut_internal(), dahdi_call(), dahdiras_exec(), destroy_all_channels(), detect_write(), determine_starting_point(), dial_exec_full(), dialgroup_write(), dictate_exec(), directory_exec(), disa_exec(), dtmfstore_exec(), dundi_query_read(), dundi_result_read(), dundifunc_read(), enable_jack_hook(), enum_query_read(), enum_result_read(), exec_exec(), extenspy_exec(), festival_exec(), file_count_line(), file_read(), file_write(), filter(), find_by_name(), find_by_uniqueid(), find_realtime_gw(), forkcdr_exec(), func_args(), func_confbridge_helper(), func_confbridge_info(), func_get_parkingslot_channel(), func_header_read(), func_headers_read2(), func_mixmonitor_read(), func_read_header(), func_write_header(), function_amiclient(), function_db_delete(), function_db_exists(), function_db_read(), function_db_write(), function_enum(), function_fieldnum_helper(), function_fieldqty_helper(), function_realtime_read(), function_realtime_readdestroy(), function_realtime_write(), function_txtcidname(), gosubif_exec(), handle_cli_presencestate_change(), hangupcause_read(), hint_read(), hook_on(), iconv_read(), import_helper(), isAnsweringMachine(), isexten_function_read(), jb_debug_output(), jb_error_output(), jb_helper(), jb_warning_output(), jingle_request(), listfilter(), load_channelvars(), log_exec(), main(), man_do_variable_value(), manager_mixmonitor(), math(), mbl_sendsms_exec(), mbl_status_exec(), meetme_stasis_generate_msg(), misdn_call(), misdn_check_l2l1(), misdn_facility_exec(), misdn_request(), mixmonitor_exec(), msg_send_exec(), multicast_rtp_request(), my_on_hook(), originate_exec(), ospauth_exec(), ospfinished_exec(), osplookup_exec(), ospnext_exec(), oss_call(), oss_request(), page_exec(), park_and_announce_app_exec(), park_app_parse_data(), parked_call_app_exec(), parse_uri_cb(), pbx_builtin_answer(), pbx_builtin_background(), pbx_builtin_saycharacters_case(), pbx_builtin_setvar_multiple(), pbx_builtin_waitdigit(), pbx_builtin_waitexten(), peek_read(), pickupchan_exec(), pjsip_acf_channel_read(), pjsip_acf_dial_contacts_read(), pjsip_acf_parse_uri_read(), pjsip_aor_function_read(), pjsip_contact_function_read(), pjsip_endpoint_function_read(), play_moh_exec(), playback_exec(), pp_each_extension_helper(), pqm_exec(), presence_read(), presence_write(), privacy_exec(), pvalAppCallWalkArgs(), pvalMacroCallWalkArgs(), ql_exec(), queue_exec(), queue_function_mem_read(), queue_function_mem_write(), queue_function_memberpenalty_read(), queue_function_memberpenalty_write(), queue_function_queuegetchannel(), qupd_exec(), rcvfax_exec(), realtimefield_read(), receivefax_exec(), record_exec(), recordthread(), reg_source_db(), regex(), reload_single_member(), replace(), request(), resetcdr_exec(), retrydial_exec(), rqm_exec(), saycountedadj_exec(), saycountednoun_exec(), sayfile_exec(), sayunixtime_exec(), scramble_write(), senddtmf_exec(), sendfax_exec(), sendmf_exec(), sendurl_exec(), shared_read(), shared_write(), shift_pop(), sip_acf_channel_read(), sip_request_call(), sip_tcptls_client_args_destructor(), smdi_msg_read(), smdi_msg_retrieve_read(), sndfax_exec(), softhangup_exec(), sorcery_function_read(), spawn_ras(), speech_background(), speech_load(), srv_result_read(), stackpeek_read(), start_mixmonitor_callback(), start_moh_exec(), start_monitor_exec(), stasis_app_control_dial(), static_realtime_cb(), statsd_exec(), stir_shaken_read(), stop_mixmonitor_full(), stream_echo_exec(), strreplace(), talk_detect_fn_write(), test_jb_debug_output(), test_jb_error_output(), test_jb_warn_output(), tls_method_to_str(), transfer_exec(), tryexec_exec(), unicast_rtp_request(), unshift_push(), upqm_exec(), user_chan_cb(), userevent_exec(), verbose_exec(), vm_exec(), vm_execmain(), vm_playmsgexec(), volume_read(), volume_write(), wait_exec(), waitfor_exec(), websocket_client_args_create(), websocket_client_args_destroy(), xmpp_join_exec(), xmpp_leave_exec(), xmpp_send_exec(), xmpp_sendgroup_exec(), and zapateller_exec().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 399 of file test_func_file.c.

◆ contents

const char* contents

Definition at line 45 of file test_func_file.c.

Referenced by AST_TEST_DEFINE(), and call_pickup_to_ami().

◆ contents2

const char* contents2

Definition at line 91 of file test_func_file.c.

Referenced by AST_TEST_DEFINE().

◆ read_tests

struct { ... } read_tests[]

Referenced by AST_TEST_DEFINE().

◆ value

const char* value

Definition at line 47 of file test_func_file.c.

Referenced by AST_TEST_DEFINE().

◆ write_tests

struct { ... } write_tests[]

Referenced by AST_TEST_DEFINE().