diff --git a/.github/workflows/script-tests.yml b/.github/workflows/script-tests.yml new file mode 100644 index 00000000000..14c6a214ddb --- /dev/null +++ b/.github/workflows/script-tests.yml @@ -0,0 +1,47 @@ +name: Script Tests + +# Builds a regular (non unit-test) opensips binary and runs script-level tests +# against it: config parsing and runtime behaviour. See test/script/. +on: + push: + pull_request: + workflow_dispatch: + +jobs: + script_tests: + runs-on: ubuntu-latest + container: + image: ubuntu:24.04 + env: + COMPILER: gcc + BUILD_OS: ubuntu:24.04 + MAKE_TGT: opensips + + steps: + - name: Install git + run: | + apt-get update + apt-get install -y git lsb-release gnupg2 wget + + - uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Install dependencies + run: | + sh -x scripts/build/reset_sources.sh + sh -x scripts/build/install_depends.sh + + - name: Build + run: sh -x scripts/build/do_build.sh + + - name: Run script tests + run: sh test/script/run_all.sh ./opensips + + all_done: + needs: script_tests + uses: ./.github/workflows/.notify.yml + with: + job_name: "Script Tests" + job_result: ${{ needs.script_tests.result }} + if: ${{ always() }} diff --git a/action.c b/action.c index 6730b73f4bc..c9f2cb25585 100644 --- a/action.c +++ b/action.c @@ -934,7 +934,35 @@ int do_action(struct action* a, struct sip_msg* msg) break; case XLOG_T: script_trace("core", "xlog", msg, a->file, a->line) ; - if (a->elem[1].u.data != NULL) + if (a->elem[2].u.data != NULL) + { + if (a->elem[1].type != SCRIPTVAR_ELEM_ST) + { + LM_ALERT("BUG in xlog() type %d\n", a->elem[1].type); + ret=E_BUG; + break; + } + if (a->elem[0].type != STR_ST) + { + LM_ALERT("BUG in xlog() type %d\n", a->elem[0].type); + ret=E_BUG; + break; + } + if (a->elem[2].type != NUMBER_ST) + { + LM_ALERT("BUG in xlog() type %d\n", a->elem[2].type); + ret=E_BUG; + break; + } + ret = xlog_3(msg, a->elem[0].u.data, a->elem[1].u.data, + (enum log_fmt_override)a->elem[2].u.number); + if (ret < 0) + { + LM_ERR("error while printing xlog message\n"); + break; + } + } + else if (a->elem[1].u.data != NULL) { if (a->elem[1].type != SCRIPTVAR_ELEM_ST) { diff --git a/cfg.y b/cfg.y index 766120d9759..f0c2017d565 100644 --- a/cfg.y +++ b/cfg.y @@ -130,6 +130,7 @@ extern int yylex(); static void yyerror(char* s); static void yyerrorf(char* fmt, ...); +static enum log_fmt_override parse_xlog_format(char* fmt); static char* tmp; static int i_tmp, rc; static struct socket_id* lst_tmp; @@ -1049,20 +1050,9 @@ assign_stm: LOGLEVEL EQUAL snumber { IFOR(); if ((i_tmp = parse_log_format(&s_tmp)) < 0) { yyerror("unknown log format"); } else { - if (i_tmp != LOG_FORMAT_PLAIN) { - if (init_log_json_buf(0) < 0) { - yyerror("failed to allocate json log buffer"); - YYABORT; - } - if (init_log_msg_buf(0) < 0) { - yyerror("failed to allocate msg log buffer"); - YYABORT; - } - - if (i_tmp == LOG_FORMAT_JSON_CEE && init_log_cee_hostname() < 0) { - yyerror("failed to allocate hostname buffer"); - YYABORT; - } + if (init_log_format_bufs(i_tmp) < 0) { + yyerror("failed to allocate log format buffers"); + YYABORT; } stderr_log_format = i_tmp; @@ -1074,20 +1064,9 @@ assign_stm: LOGLEVEL EQUAL snumber { IFOR(); if ((i_tmp = parse_log_format(&s_tmp)) < 0) { yyerror("unknown log format"); } else { - if (i_tmp != LOG_FORMAT_PLAIN) { - if (init_log_json_buf(0) < 0) { - yyerror("failed to allocate json log buffer"); - YYABORT; - } - if (init_log_msg_buf(0) < 0) { - yyerror("failed to allocate msg log buffer"); - YYABORT; - } - - if (i_tmp == LOG_FORMAT_JSON_CEE && init_log_cee_hostname() < 0) { - yyerror("failed to allocate hostname buffer"); - YYABORT; - } + if (init_log_format_bufs(i_tmp) < 0) { + yyerror("failed to allocate log format buffers"); + YYABORT; } syslog_log_format = i_tmp; @@ -2674,6 +2653,10 @@ cmd: ASSERT LPAREN exp COMMA STRING RPAREN { mk_action2($$, XLOG_T, STR_ST, STR_ST, $3, $5); } | XLOG LPAREN STRING COMMA folded_string RPAREN { mk_action2($$, XLOG_T, STR_ST, STR_ST, $3, $5); } + | XLOG LPAREN STRING COMMA STRING COMMA STRING RPAREN { + mk_action3($$, XLOG_T, STR_ST, STR_ST, NUMBER_ST, $3, $5, (void*)(long)parse_xlog_format($7)); } + | XLOG LPAREN STRING COMMA folded_string COMMA STRING RPAREN { + mk_action3($$, XLOG_T, STR_ST, STR_ST, NUMBER_ST, $3, $5, (void*)(long)parse_xlog_format($7)); } | ASYNC_TOKEN LPAREN async_func COMMA route_name RPAREN { rt_ref = ref_script_route_by_name( $5, sroutes->request, RT_NO, REQUEST_ROUTE, 0); @@ -2741,6 +2724,39 @@ static void yyerrorf(char *fmt, ...) } +/* resolve the xlog() per-call format override at parse time, so a typo fails + * config load instead of silently using the configured log format */ +static enum log_fmt_override parse_xlog_format(char* fmt) +{ + str s; + int fmt_id; + + s.s = fmt; + s.len = strlen(fmt); + fmt_id = parse_log_format(&s); + + /* the configured stderr/syslog formats may both be plain, in which case + * the json buffers were never allocated - the override needs them too */ + if (fmt_id >= 0 && init_log_format_bufs(fmt_id) < 0) { + yyerror("failed to allocate log format buffers"); + return LOG_FMT_OVR_NONE; + } + + switch (fmt_id) { + case LOG_FORMAT_PLAIN: + return LOG_FMT_OVR_PLAIN; + case LOG_FORMAT_JSON: + return LOG_FMT_OVR_JSON; + case LOG_FORMAT_JSON_CEE: + return LOG_FMT_OVR_JSON_CEE; + default: + yyerrorf("unknown xlog() log format '%s' (expected '%s', '%s' or " + "'%s')", fmt, LOG_PLAIN_NAME, LOG_JSON_NAME, LOG_JSON_CEE_NAME); + return LOG_FMT_OVR_NONE; + } +} + + static struct socket_id* mk_listen_id(char* host, enum sip_protos proto, int port) { diff --git a/dprint.c b/dprint.c index f3885cb0fd4..c85be56b143 100644 --- a/dprint.c +++ b/dprint.c @@ -39,7 +39,8 @@ #endif /* used internally by the log interface */ -typedef void (*log_print_pre_fmt_f)(log_print_f gen_print_func, int log_level, +typedef void (*log_print_pre_fmt_f)(log_print_f gen_print_func, + enum log_fmt_override fmt_ovr, int log_level, int facility, const char *module, const char *func, char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap); @@ -72,15 +73,17 @@ int log_msg_buf_size = 4096; str log_cee_hostname; -static void stderr_dprint(int log_level, int facility, const char *module, const char *func, - char *format, va_list ap); -static void syslog_dprint(int log_level, int facility, const char *module, const char *func, - char *format, va_list ap); +static void stderr_dprint(enum log_format eff_fmt, int log_level, int facility, + const char *module, const char *func, char *format, va_list ap); +static void syslog_dprint(enum log_format eff_fmt, int log_level, int facility, + const char *module, const char *func, char *format, va_list ap); -static void stderr_pre_fmt_func(log_print_f gen_print_func, int log_level, +static void stderr_pre_fmt_func(log_print_f gen_print_func, + enum log_fmt_override fmt_ovr, int log_level, int facility, const char *module, const char *func, char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap); -static void syslog_pre_fmt_func(log_print_f gen_print_func, int log_level, +static void syslog_pre_fmt_func(log_print_f gen_print_func, + enum log_fmt_override fmt_ovr, int log_level, int facility, const char *module, const char *func, char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap); @@ -137,7 +140,7 @@ int dp_my_pid(void) return my_pid(); } -int parse_log_format(str *format) +int parse_log_format(const str *format) { if (str_match(format, (&str_init(LOG_PLAIN_NAME)))) return LOG_FORMAT_PLAIN; @@ -455,8 +458,8 @@ static int log_print_json(str *buf, enum log_json_format json_fmt, char *time, return len; } -static void stderr_dprint(int log_level, int facility, const char *module, const char *func, - char *format, va_list ap) +static void stderr_dprint(enum log_format eff_fmt, int log_level, int facility, + const char *module, const char *func, char *format, va_list ap) { char *time; int pid; @@ -464,14 +467,14 @@ static void stderr_dprint(int log_level, int facility, const char *module, const int len; str buf = {log_json_buf, log_json_buf_size}; - if (stderr_log_format != LOG_FORMAT_PLAIN) { + if (eff_fmt != LOG_FORMAT_PLAIN) { time = va_arg(ap, char *); pid = va_arg(ap, int); prefix = va_arg(ap, char *); if (module && func) va_arg(ap, char *); - if ((len = log_print_json(&buf, stderr_log_format==LOG_FORMAT_JSON_CEE ? + if ((len = log_print_json(&buf, eff_fmt==LOG_FORMAT_JSON_CEE ? LOG_JSON_SCHEMA_CEE : LOG_JSON_SCHEMA_BASIC, time, pid, prefix, dp_log_level_str(log_level), module, func, format, ap)) < 0) { @@ -487,8 +490,8 @@ static void stderr_dprint(int log_level, int facility, const char *module, const } } -static void syslog_dprint(int log_level, int facility, const char *module, const char *func, - char *format, va_list ap) +static void syslog_dprint(enum log_format eff_fmt, int log_level, int facility, + const char *module, const char *func, char *format, va_list ap) { int level; char *time; @@ -521,14 +524,14 @@ static void syslog_dprint(int log_level, int facility, const char *module, const level = LOG_DEBUG; } - if (syslog_log_format != LOG_FORMAT_PLAIN) { + if (eff_fmt != LOG_FORMAT_PLAIN) { time = va_arg(ap, char *); pid = va_arg(ap, int); prefix = va_arg(ap, char *); if (module && func) va_arg(ap, char *); - if ((len = log_print_json(&buf, syslog_log_format==LOG_FORMAT_JSON_CEE ? + if ((len = log_print_json(&buf, eff_fmt==LOG_FORMAT_JSON_CEE ? LOG_JSON_SCHEMA_CEE_PREFIX : LOG_JSON_SCHEMA_BASIC, time, pid, prefix, dp_log_level_str(log_level), module, func, format, ap)) < 0) { @@ -554,14 +557,16 @@ static str evi_func_str = str_init("function"); static str evi_prefix_str = str_init("prefix"); static str evi_msg_str = str_init("message"); -static void event_dprint(int level, int facility, const char *module, const char *func, - char *format, va_list ap) +static void event_dprint(enum log_format eff_fmt, int level, int facility, + const char *module, const char *func, char *format, va_list ap) { evi_params_p list = NULL; str s; int n; int suppressed; + (void)eff_fmt; /* event consumer does not vary output by format */ + suppressed = pt[process_no].suppress_log_event; if (suppressed) @@ -652,7 +657,8 @@ static void event_dprint(int level, int facility, const char *module, const char } /* generic consumer that registers to the log interface */ -static void gen_consumer_pre_fmt_func(log_print_f gen_print_func, int log_level, +static void gen_consumer_pre_fmt_func(log_print_f gen_print_func, + enum log_fmt_override fmt_ovr, int log_level, int facility, const char *module, const char *func, char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap) { @@ -663,45 +669,72 @@ static void gen_consumer_pre_fmt_func(log_print_f gen_print_func, int log_level, if (module && func) va_arg(ap, char *); - gen_print_func(log_level, facility, module, func, format, ap); + /* generic consumers have no configured format of their own; honor an + * explicit override, else default to plain */ + gen_print_func(resolve_log_format(LOG_FORMAT_PLAIN, fmt_ovr), + log_level, facility, module, func, format, ap); } -static void stderr_pre_fmt_func(log_print_f gen_print_func, int log_level, +static void stderr_pre_fmt_func(log_print_f gen_print_func, + enum log_fmt_override fmt_ovr, int log_level, int facility, const char *module, const char *func, char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap) { - char *fmt = stderr_log_format == LOG_FORMAT_PLAIN ? stderr_plain_fmt : format; + enum log_format eff_fmt = resolve_log_format(stderr_log_format, fmt_ovr); + char *fmt = eff_fmt == LOG_FORMAT_PLAIN ? stderr_plain_fmt : format; - gen_print_func(log_level, facility, module, func, fmt, ap); + gen_print_func(eff_fmt, log_level, facility, module, func, fmt, ap); } -static void syslog_pre_fmt_func(log_print_f gen_print_func, int log_level, +static void syslog_pre_fmt_func(log_print_f gen_print_func, + enum log_fmt_override fmt_ovr, int log_level, int facility, const char *module, const char *func, char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap) { - char *fmt = syslog_log_format == LOG_FORMAT_PLAIN ? syslog_plain_fmt : format; + enum log_format eff_fmt = resolve_log_format(syslog_log_format, fmt_ovr); + char *fmt = eff_fmt == LOG_FORMAT_PLAIN ? syslog_plain_fmt : format; - gen_print_func(log_level, facility, module, func, fmt, ap); + gen_print_func(eff_fmt, log_level, facility, module, func, fmt, ap); } -void dprint(int log_level, int facility, const char *module, const char *func, - char *stderr_fmt, char *syslog_fmt, char *format, ...) +static void vdprint_fmt_ovr(enum log_fmt_override fmt_ovr, int log_level, int facility, + const char *module, const char *func, + char *stderr_fmt, char *syslog_fmt, char *format, va_list ap) { - va_list ap, ap_copy; + va_list ap_copy; int i; - va_start(ap, format); - for (i=0; i= log_level)) { va_copy(ap_copy, ap); log_consumers[i].pre_fmt_print_func(log_consumers[i].gen_print_func, - log_level, facility, module, func, stderr_fmt, syslog_fmt, + fmt_ovr, log_level, facility, module, func, stderr_fmt, syslog_fmt, format, ap_copy); va_end(ap_copy); } +} + +void dprint_fmt_ovr(enum log_fmt_override fmt_ovr, int log_level, int facility, + const char *module, const char *func, + char *stderr_fmt, char *syslog_fmt, char *format, ...) +{ + va_list ap; + + va_start(ap, format); + vdprint_fmt_ovr(fmt_ovr, log_level, facility, module, func, + stderr_fmt, syslog_fmt, format, ap); + va_end(ap); +} + +void dprint(int log_level, int facility, const char *module, const char *func, + char *stderr_fmt, char *syslog_fmt, char *format, ...) +{ + va_list ap; + va_start(ap, format); + vdprint_fmt_ovr(LOG_FMT_OVR_NONE, log_level, facility, module, func, + stderr_fmt, syslog_fmt, format, ap); va_end(ap); } @@ -724,6 +757,30 @@ int register_log_consumer(char *name, log_print_f print_func, return 0; } +/* allocate what a non-plain log format needs (json/msg buffers, cee hostname); + * idempotent, so it may be called for the configured formats as well as for + * every per-call format override */ +int init_log_format_bufs(enum log_format format) +{ + if (format == LOG_FORMAT_PLAIN) + return 0; + + if (init_log_json_buf(0) < 0) { + LM_ERR("failed to allocate json log buffer\n"); + return -1; + } + if (init_log_msg_buf(0) < 0) { + LM_ERR("failed to allocate msg log buffer\n"); + return -1; + } + if (format == LOG_FORMAT_JSON_CEE && init_log_cee_hostname() < 0) { + LM_ERR("failed to allocate hostname buffer\n"); + return -1; + } + + return 0; +} + int init_log_json_buf(int realloc_buf) { if (realloc_buf && log_json_buf) { diff --git a/dprint.h b/dprint.h index 266481efa29..8644be7d876 100644 --- a/dprint.h +++ b/dprint.h @@ -121,6 +121,16 @@ enum log_format { LOG_FORMAT_JSON_CEE }; +/* Per-call override of the configured output format. LOG_FMT_OVR_NONE means "use + * the consumer's configured format"; other values force a specific representation for + * that single log line regardless of the configured stderr_log_format/syslog_log_format. */ +enum log_fmt_override { + LOG_FMT_OVR_NONE = 0, + LOG_FMT_OVR_PLAIN, + LOG_FMT_OVR_JSON, + LOG_FMT_OVR_JSON_CEE +}; + /* vars:*/ extern int *log_level; @@ -154,6 +164,7 @@ int init_log_json_buf(int realloc); int init_log_msg_buf(int realloc); int init_log_cee_hostname(void); +int init_log_format_bufs(enum log_format format); int init_log_event_cons(); int set_log_event_cons_cfg_state(void); @@ -165,7 +176,7 @@ int get_log_consumer_mute_state(str *name, int *state); int set_log_consumer_level_filter(str *name, int level); int get_log_consumer_level_filter(str *name, int *level_filter); -int parse_log_format(str *format); +int parse_log_format(const str *format); int dp_my_pid(void); void stderr_dprint_tmp(char *format, ...); @@ -174,6 +185,27 @@ void dprint(int log_level, int facility, const char *module, const char *func, char *stderr_fmt, char *syslog_fmt, char *format, ...) __attribute__ ((__format__ (__printf__, 5, 8))); +/* Resolve a per-call format override against the channel's configured format. + * LOG_FMT_OVR_NONE keeps the configured format; any other override forces a + * specific format for this one call. */ +static inline enum log_format resolve_log_format(enum log_format configured, + enum log_fmt_override fmt_ovr) +{ + switch (fmt_ovr) { + case LOG_FMT_OVR_PLAIN: return LOG_FORMAT_PLAIN; + case LOG_FMT_OVR_JSON: return LOG_FORMAT_JSON; + case LOG_FMT_OVR_JSON_CEE: return LOG_FORMAT_JSON_CEE; + case LOG_FMT_OVR_NONE: + default: return configured; + } +} + +/* like dprint(), but with an explicit per-call format override */ +void dprint_fmt_ovr(enum log_fmt_override fmt_ovr, int log_level, int facility, + const char *module, const char *func, + char *stderr_fmt, char *syslog_fmt, char *format, ...) + __attribute__ ((__format__ (__printf__, 6, 9))); + int str2facility(char *s); void __set_proc_log_level(int proc_idx, int level); @@ -266,6 +298,7 @@ static inline const char *dp_log_level_str(int log_level) #ifdef __SUNPRO_C #define LM_GEN2(facility, lev, ...) #define LM_GEN1(lev, ...) + #define LM_GEN1_FMT_OVR(fmt_ovr, lev, ...) #define LM_GEN(lev, ...) #define LM_ALERT( ...) #define LM_CRIT( ...) @@ -277,6 +310,7 @@ static inline const char *dp_log_level_str(int log_level) #else #define LM_GEN2(facility, lev, fmt, args...) #define LM_GEN1(lev, fmt, args...) + #define LM_GEN1_FMT_OVR(fmt_ovr, lev, fmt, args...) #define LM_GEN(lev, fmt, args...) #define LM_ALERT(fmt, args...) #define LM_CRIT(fmt, args...) @@ -377,6 +411,18 @@ static inline const char *dp_log_level_str(int log_level) } \ }while(0) + /* like LM_GEN1, but with an explicit per-call format override */ + #define LM_GEN1_FMT_OVR(_fmt_ovr, _lev, fmt, ...) \ + do { \ + if (is_printable(_lev)){ \ + DISABLE_FORMAT_WARNING; \ + dprint_fmt_ovr(_fmt_ovr, _lev, log_facility, NULL, NULL, \ + "%.0s%.0s%.0s" fmt, "%.0s" fmt, fmt, \ + dp_time(), dp_my_pid(), log_prefix __VA_ARGS__); \ + ENABLE_FORMAT_WARNING; \ + } \ + }while(0) + #define LM_ALERT( fmt, ...) \ do { \ if (is_printable(L_ALERT)) \ @@ -520,6 +566,18 @@ static inline const char *dp_log_level_str(int log_level) } \ }while(0) + /* like LM_GEN1, but with an explicit per-call format override */ + #define LM_GEN1_FMT_OVR(_fmt_ovr, _lev, fmt, args...) \ + do { \ + if (is_printable(_lev)){ \ + DISABLE_FORMAT_WARNING; \ + dprint_fmt_ovr(_fmt_ovr, _lev, log_facility, NULL, NULL, \ + "%.0s%.0s%.0s" fmt, "%.0s" fmt, fmt, \ + dp_time(), dp_my_pid(), log_prefix, ## args); \ + ENABLE_FORMAT_WARNING; \ + } \ + }while(0) + #define LM_ALERT( fmt, args...) \ do { \ if (is_printable(L_ALERT)) \ diff --git a/log_interface.h b/log_interface.h index 89161d98c78..af665e59ea8 100644 --- a/log_interface.h +++ b/log_interface.h @@ -24,8 +24,13 @@ #include -/* printing function to be registered by a generic logging consumer */ -typedef void (*log_print_f)(int log_level, int facility, const char *module, const char *func, +enum log_format; + +/* printing function to be registered by a generic logging consumer. + * @eff_fmt is the effective output format for this call (the configured + * format, or a per-call override already resolved by the log interface). */ +typedef void (*log_print_f)(enum log_format eff_fmt, int log_level, + int facility, const char *module, const char *func, char *format, va_list ap); int register_log_consumer(char *name, log_print_f print_func, diff --git a/modules/topology_hiding/th_common_logic.h b/modules/topology_hiding/th_common_logic.h index fd5da4e9a17..ddd67fccaff 100644 --- a/modules/topology_hiding/th_common_logic.h +++ b/modules/topology_hiding/th_common_logic.h @@ -76,7 +76,7 @@ static inline char *topo_ct_param_copy(char *buf, str *name, str *val, int shoul return buf; } -static void shm_free_wrap(void *param) { +static inline void shm_free_wrap(void *param) { if (param) shm_free(param); } diff --git a/test/script/README.md b/test/script/README.md new file mode 100644 index 00000000000..263588dfcce --- /dev/null +++ b/test/script/README.md @@ -0,0 +1,34 @@ +# Script tests + +Black-box tests that run a real `opensips` binary (a normal build, not the +`-DUNIT_TESTS` one used by `make test`). CI runs them via +`.github/workflows/script-tests.yml`. + +Run locally after building (`make opensips`): + + sh test/script/run_all.sh ./opensips # everything + sh test/script/xlog-format/run.sh ./opensips # one feature + +## Adding a test + +1. Create `test/script//run.sh` (it is picked up automatically by + `run_all.sh` and CI - nothing to register). +2. Start it with: + + . "$(dirname "$0")/../lib.sh" + script_init "$@" + + and end it with `finish`. See `lib.sh` for the helpers: + + - `check_parse accept|reject "" ''` - config + parse check (`opensips -c`); the body is prefixed with `$CFG_PRELUDE`. + - `check_parse_file accept|reject "" ` - same, for a + config file. + - `run_for ` - run opensips briefly (use + `startup_route` to produce output) and capture stdout/stderr to check + with `grep`. + - `result "" $?` - record any other check. + +3. Put any config files your runtime checks need next to `run.sh`. + +`xlog-format/` is a worked example of both a parse and a runtime test. diff --git a/test/script/lib.sh b/test/script/lib.sh new file mode 100644 index 00000000000..ddc927ceea6 --- /dev/null +++ b/test/script/lib.sh @@ -0,0 +1,64 @@ +# Shared helpers for the script tests under test/script//run.sh. +# Source it from a run.sh: +# +# . "$(dirname "$0")/../lib.sh" +# script_init "$@" +# check_parse accept "my case" 'route { xlog("L_ERR", "hi\n"); }' +# ... +# finish +# +# Each check prints "ok" or "FAIL" and records failures; finish exits non-zero +# if any check failed. + +# minimal config needed for 'opensips -c' and for short test runs; proto_udp +# is built into the core, but must still be loaded for the socket to be valid +CFG_PRELUDE='loadmodule "proto_udp.so" +socket = udp:127.0.0.1:15060' + +# script_init [opensips-binary] (default ./opensips) +script_init() { + OPENSIPS="${1:-./opensips}" + TMP="$(mktemp -d)" + trap 'rm -rf "$TMP"' EXIT + fail=0 +} + +# result +result() { + if [ "$2" -eq 0 ]; then + echo "ok $1" + else + echo "FAIL $1" + fail=1 + fi +} + +# check_parse_file +# 'opensips -c' must exit 0 for accept, non-zero for reject. +check_parse_file() { + "$OPENSIPS" -c -f "$3" >"$TMP/parse.out" 2>&1 + rc=$? + if [ "$1" = accept ]; then [ $rc -eq 0 ]; else [ $rc -ne 0 ]; fi + ok=$? + result "parse: $2 -> $1" $ok + # show the parser output when the result was unexpected + [ $ok -eq 0 ] || cat "$TMP/parse.out" +} + +# check_parse +# Same as check_parse_file, with CFG_PRELUDE prepended to the given body. +check_parse() { + printf '%s\n\n%s\n' "$CFG_PRELUDE" "$3" > "$TMP/parse.cfg" + check_parse_file "$1" "$2" "$TMP/parse.cfg" +} + +# run_for +# Run opensips in the foreground for a few seconds, capturing stdout+stderr. +# Use with startup_route to exercise runtime behaviour without traffic. +run_for() { + timeout "$1" "$OPENSIPS" -F -f "$2" >"$3" 2>&1 +} + +finish() { + exit $fail +} diff --git a/test/script/run_all.sh b/test/script/run_all.sh new file mode 100755 index 00000000000..58a81f17614 --- /dev/null +++ b/test/script/run_all.sh @@ -0,0 +1,13 @@ +#!/bin/sh +# Run every test/script//run.sh against an opensips binary. +# Usage: run_all.sh [path-to-opensips-binary] +DIR="$(dirname "$0")" +BIN="${1:-./opensips}" +fail=0 + +for t in "$DIR"/*/run.sh; do + echo "=== $t" + sh "$t" "$BIN" || fail=1 +done + +exit $fail diff --git a/test/script/xlog-format/run.sh b/test/script/xlog-format/run.sh new file mode 100755 index 00000000000..db28bf12b5c --- /dev/null +++ b/test/script/xlog-format/run.sh @@ -0,0 +1,63 @@ +#!/bin/sh +# Tests for the xlog() per-call format override. +# 1. parse: the config parser accepts valid format names and rejects others +# 2. runtime: with each configured stderr format, xlog() without a format +# follows the configured one and xlog() with a format overrides it +# Usage: run.sh [path-to-opensips-binary] +DIR="$(dirname "$0")" +. "$DIR/../lib.sh" +script_init "$@" + +# ---- 1. parse --------------------------------------------------------------- + +xlog_fmt() { # + check_parse "$1" "xlog format '$2'" "route { xlog(\"L_ERR\", \"msg\\n\", \"$2\"); }" +} + +xlog_fmt accept plain_text +xlog_fmt accept json +xlog_fmt accept json_cee +xlog_fmt reject bogus +xlog_fmt reject JSON +xlog_fmt reject plain +xlog_fmt reject json-cee +xlog_fmt reject "" + +check_parse accept "no format arg, pseudo-variable message" \ + 'route { xlog("L_ERR", "msg $rU\n"); xlog("L_ERR", "msg $rU\n", "json"); }' + +# ---- 2. runtime ------------------------------------------------------------- + +is_plain() { grep -F "$1" "$2" | grep -v '^{' | grep -qF "$1"; } +is_json() { grep -F "$1" "$2" | grep '^{' | grep -qF '"message": "'; } +is_cee() { grep -F "$1" "$2" | grep '^{' | grep -qF '"msg": "'; } + +for configured in plain_text json json_cee; do + cfg="$TMP/$configured.cfg" + out="$TMP/$configured.out" + { echo "$CFG_PRELUDE"; echo "stderror_log_format = \"$configured\""; cat "$DIR/xlog-test.cfg"; } > "$cfg" + run_for 5 "$cfg" "$out" + + # no format argument: follows the configured format + case "$configured" in + plain_text) is_plain MARK_DEFAULT "$out" ;; + json) is_json MARK_DEFAULT "$out" ;; + json_cee) is_cee MARK_DEFAULT "$out" ;; + esac + result "runtime: configured=$configured, no override -> $configured" $? + + # explicit format argument: wins over the configured format + is_plain MARK_PLAIN "$out" + result "runtime: configured=$configured, override plain_text" $? + is_json MARK_JSON "$out" + result "runtime: configured=$configured, override json" $? + is_cee MARK_CEE "$out" + result "runtime: configured=$configured, override json_cee" $? + + if [ $fail -ne 0 ]; then + echo "--- output for configured=$configured:" + cat "$out" + fi +done + +finish diff --git a/test/script/xlog-format/xlog-test.cfg b/test/script/xlog-format/xlog-test.cfg new file mode 100644 index 00000000000..f5114a312a7 --- /dev/null +++ b/test/script/xlog-format/xlog-test.cfg @@ -0,0 +1,13 @@ +stderror_enabled = yes +log_level = 4 + +startup_route { + xlog("L_ERR", "MARK_DEFAULT\n"); + xlog("L_ERR", "MARK_PLAIN\n", "plain_text"); + xlog("L_ERR", "MARK_JSON\n", "json"); + xlog("L_ERR", "MARK_CEE\n", "json_cee"); +} + +route { + drop(); +} diff --git a/test/test_log_format.c b/test/test_log_format.c new file mode 100644 index 00000000000..1fa7ce533cc --- /dev/null +++ b/test/test_log_format.c @@ -0,0 +1,41 @@ +#include + +#include "../str.h" +#include "../dprint.h" + +#include "test_log_format.h" + +void test_log_format(void) +{ + /* format names -> enum log_format */ + ok(parse_log_format(_str(LOG_PLAIN_NAME)) == LOG_FORMAT_PLAIN, + "test-log-format-parse-0"); + ok(parse_log_format(_str(LOG_JSON_NAME)) == LOG_FORMAT_JSON, + "test-log-format-parse-1"); + ok(parse_log_format(_str(LOG_JSON_CEE_NAME)) == LOG_FORMAT_JSON_CEE, + "test-log-format-parse-2"); + ok(parse_log_format(_str("")) == -1, "test-log-format-parse-3"); + ok(parse_log_format(_str("bogus")) == -1, "test-log-format-parse-4"); + + /* no override keeps the configured format */ + ok(resolve_log_format(LOG_FORMAT_PLAIN, LOG_FMT_OVR_NONE) == + LOG_FORMAT_PLAIN, "test-log-format-resolve-0"); + ok(resolve_log_format(LOG_FORMAT_JSON, LOG_FMT_OVR_NONE) == + LOG_FORMAT_JSON, "test-log-format-resolve-1"); + ok(resolve_log_format(LOG_FORMAT_JSON_CEE, LOG_FMT_OVR_NONE) == + LOG_FORMAT_JSON_CEE, "test-log-format-resolve-2"); + + /* an override wins over any configured format */ + ok(resolve_log_format(LOG_FORMAT_JSON, LOG_FMT_OVR_PLAIN) == + LOG_FORMAT_PLAIN, "test-log-format-resolve-3"); + ok(resolve_log_format(LOG_FORMAT_PLAIN, LOG_FMT_OVR_JSON) == + LOG_FORMAT_JSON, "test-log-format-resolve-4"); + ok(resolve_log_format(LOG_FORMAT_PLAIN, LOG_FMT_OVR_JSON_CEE) == + LOG_FORMAT_JSON_CEE, "test-log-format-resolve-5"); + ok(resolve_log_format(LOG_FORMAT_JSON_CEE, LOG_FMT_OVR_JSON) == + LOG_FORMAT_JSON, "test-log-format-resolve-6"); + + /* unknown override values fall back to the configured format */ + ok(resolve_log_format(LOG_FORMAT_JSON, (enum log_fmt_override)99) == + LOG_FORMAT_JSON, "test-log-format-resolve-7"); +} diff --git a/test/test_log_format.h b/test/test_log_format.h new file mode 100644 index 00000000000..84964ceecd1 --- /dev/null +++ b/test/test_log_format.h @@ -0,0 +1,8 @@ +#ifndef TEST_LOG_FORMAT_H +#define TEST_LOG_FORMAT_H + +/* Test log format name parsing and per-call format override resolution */ + +void test_log_format(void); + +#endif diff --git a/test/unit_tests.c b/test/unit_tests.c index 3fccef3c19f..325d1735733 100644 --- a/test/unit_tests.c +++ b/test/unit_tests.c @@ -29,6 +29,7 @@ #include "../parser/test/test_parser.h" #include "../mem/test/test_malloc.h" #include "test_ut.h" +#include "test_log_format.h" #include "../str.h" #include "../lib/list.h" @@ -67,6 +68,7 @@ int run_unit_tests(void) test_lib_csv(); test_parser(); test_ut(); + test_log_format(); test_lib_digest_auth(); test_db(); diff --git a/xlog.c b/xlog.c index 58b4d71ed67..0b5458766fe 100644 --- a/xlog.c +++ b/xlog.c @@ -271,7 +271,11 @@ int xl_print_log(struct sip_msg* msg, pv_elem_p list, int *len) } -int xlog_2(struct sip_msg* msg, char* lev, char* frm) +/* Shared implementation for the 2- and 3-argument xlog() forms. + * @lev/@frm are the level and format args; @fmt_ovr selects a per-call output + * format override (LOG_FMT_OVR_NONE uses the configured format). */ +static int _xlog_impl(struct sip_msg* msg, char* lev, char* frm, + enum log_fmt_override fmt_ovr) { int log_len, ret; long level; @@ -307,7 +311,8 @@ int xlog_2(struct sip_msg* msg, char* lev, char* frm) set_proc_log_level( *xlog_level ); /* log_buf[log_len] = '\0'; */ - LM_GEN1((int)level, "%.*s", log_len, log_buf); + LM_GEN1_FMT_OVR(fmt_ovr, + (int)level, "%.*s", log_len, log_buf); reset_proc_log_level(); @@ -315,6 +320,23 @@ int xlog_2(struct sip_msg* msg, char* lev, char* frm) } +int xlog_2(struct sip_msg* msg, char* lev, char* frm) +{ + return _xlog_impl(msg, lev, frm, LOG_FMT_OVR_NONE); +} + + +/* Like xlog_2, but takes a 3rd argument selecting the output format for this + * one message, overriding the configured log format. The string given in the + * script ("plain_text", "json" or "json_cee") is resolved to @fmt_ovr when + * the config is parsed (see cfg.y). */ +int xlog_3(struct sip_msg* msg, char* lev, char* frm, + enum log_fmt_override fmt_ovr) +{ + return _xlog_impl(msg, lev, frm, fmt_ovr); +} + + int xlog_1(struct sip_msg* msg, char* frm) { int log_len, ret; diff --git a/xlog.h b/xlog.h index 24e712e3380..0cd808b2702 100644 --- a/xlog.h +++ b/xlog.h @@ -21,6 +21,7 @@ #ifndef _XLOG_H_ #define _XLOG_H_ +#include "dprint.h" #include "pvar.h" typedef struct _xl_level @@ -47,6 +48,7 @@ extern int *xlog_level; int xlog_1(struct sip_msg*, char*); int xlog_2(struct sip_msg*, char*, char*); +int xlog_3(struct sip_msg*, char*, char*, enum log_fmt_override); int xdbg(struct sip_msg*, char*); int pv_parse_color_name(pv_spec_p sp, const str *in);