From: Arran Cudbard-Bell Date: Wed, 8 Sep 2021 23:30:10 +0000 (-0500) Subject: Add basic trigger tests X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e6999f0a65a67a13f5e3bffaca9edf3831a5de87;p=thirdparty%2Ffreeradius-server.git Add basic trigger tests --- diff --git a/src/lib/server/trigger.c b/src/lib/server/trigger.c index 6e4cc33db5..68293fa010 100644 --- a/src/lib/server/trigger.c +++ b/src/lib/server/trigger.c @@ -53,6 +53,11 @@ typedef struct { fr_time_t last_fired; //!< When this trigger last fired. } trigger_last_fired_t; +xlat_arg_parser_t const trigger_xlat_args[] = { + { .required = true, .single = true, .type = FR_TYPE_STRING }, + XLAT_ARG_PARSER_TERMINATOR +}; + /** Retrieve attributes from a special trigger list * */ @@ -258,7 +263,7 @@ static unlang_action_t trigger_run(rlm_rcode_t *p_result, UNUSED int *priority, * trigger will be executed synchronously. * * @param[in] request The current request. - * @param[in] cs to search for triggers in. + * @param[in] cs to search for triggers in. * If cs is not NULL, the portion after the last '.' in name is used for the trigger. * If cs is NULL, the entire name is used to find the trigger in the global trigger * section. diff --git a/src/lib/server/trigger.h b/src/lib/server/trigger.h index f83ab1a266..1c3dcacd3d 100644 --- a/src/lib/server/trigger.h +++ b/src/lib/server/trigger.h @@ -36,8 +36,10 @@ extern "C" { #include #include +extern xlat_arg_parser_t const trigger_xlat_args[]; + xlat_action_t trigger_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, request_t *request, - UNUSED void const *xlat_inst, UNUSED void *xlat_thread_inst, + UNUSED void const *xlat_inst, UNUSED void *xlat_thread_inst, fr_value_box_list_t *in); int trigger_exec_init(CONF_SECTION const *cs); diff --git a/src/lib/unlang/xlat_builtin.c b/src/lib/unlang/xlat_builtin.c index 76d0099d6b..64f443c3f7 100644 --- a/src/lib/unlang/xlat_builtin.c +++ b/src/lib/unlang/xlat_builtin.c @@ -3045,11 +3045,6 @@ static xlat_action_t xlat_func_urlunquote(TALLOC_CTX *ctx, fr_dcursor_t *out, return XLAT_ACTION_DONE; } -static xlat_arg_parser_t const trigger_xlat_args[] = { - { .required = true, .single = true, .type = FR_TYPE_STRING }, - XLAT_ARG_PARSER_TERMINATOR -}; - static xlat_arg_parser_t const protocol_decode_xlat_args[] = { { .single = true, .variadic = true, .type = FR_TYPE_VOID }, XLAT_ARG_PARSER_TERMINATOR diff --git a/src/modules/rlm_test/rlm_test.c b/src/modules/rlm_test/rlm_test.c index 86c72d0907..ad4cb4d1f9 100644 --- a/src/modules/rlm_test/rlm_test.c +++ b/src/modules/rlm_test/rlm_test.c @@ -199,77 +199,6 @@ static int rlm_test_cmp(UNUSED void *instance, request_t *request, UNUSED fr_pai return 1; } -static int mod_thread_instantiate(UNUSED CONF_SECTION const *cs, UNUSED void *instance, UNUSED fr_event_list_t *el, - void *thread) -{ - rlm_test_thread_t *t = thread; - - t->value = pthread_self(); - INFO("Performing instantiation for thread %p (ctx %p)", (void *)t->value, t); - - return 0; -} - -static int mod_thread_detach(UNUSED fr_event_list_t *el, void *thread) -{ - rlm_test_thread_t *t = thread; - - INFO("Performing detach for thread %p", (void *)t->value); - - if (!fr_cond_assert(t->value == pthread_self())) return -1; - - return 0; -} - -/* - * Do any per-module bootstrapping that is separate to each - * configured instance of the module. e.g. set up connections - * to external databases, read configuration files, set up - * dictionary entries, etc. - * - * If configuration information is given in the config section - * that must be referenced in later calls, store a handle to it - * in *instance otherwise put a null pointer there. - */ -static int mod_bootstrap(void *instance, UNUSED CONF_SECTION *conf) -{ - rlm_test_t *inst = instance; - - if (paircmp_register_by_name("Test-Paircmp", attr_user_name, false, - rlm_test_cmp, inst) < 0) { - PERROR("Failed registering \"Test-Paircmp\""); - return -1; - } - - /* - * Log some messages - */ - INFO("Informational message"); - WARN("Warning message"); - ERROR("Error message"); - DEBUG("Debug message"); - DEBUG2("Debug2 message"); - DEBUG3("Debug3 message"); - DEBUG4("Debug4 message"); - - /* - * Output parsed tmpls - */ - if (inst->tmpl) { - INFO("%s", inst->tmpl->name); - } else { - INFO("inst->tmpl is NULL"); - } - - if (inst->tmpl_m) { - talloc_foreach(inst->tmpl_m, item) INFO("%s", item->name); - } else { - INFO("inst->tmpl_m is NULL"); - } - - return 0; -} - /* * Find the named user in this modules database. Create the set * of attribute-value pairs to check and reply with for this user @@ -406,7 +335,6 @@ static void mod_retry_signal(UNUSED module_ctx_t const *mctx, request_t *request } - /* * Test retries */ @@ -415,6 +343,113 @@ static unlang_action_t CC_HINT(nonnull) mod_retry(UNUSED rlm_rcode_t *p_result, return unlang_module_yield(request, mod_retry_resume, mod_retry_signal, NULL); } + +static xlat_arg_parser_t const trigger_test_xlat_args[] = { + { .required = true, .single = true, .type = FR_TYPE_STRING }, + XLAT_ARG_PARSER_TERMINATOR +}; + + +/** Run a trigger (useful for testing) + * + */ +static xlat_action_t trigger_test_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, request_t *request, + UNUSED void const *xlat_inst, UNUSED void *xlat_thread_inst, + fr_value_box_list_t *in) +{ + fr_value_box_t *in_head = fr_dlist_head(in); + fr_value_box_t *vb; + + MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL, false)); + fr_dcursor_append(out, vb); + + if (trigger_exec(unlang_interpret_get(request), request, NULL, in_head->vb_strvalue, false, NULL) < 0) { + RPEDEBUG("Running trigger failed"); + vb->vb_bool = false; + return XLAT_ACTION_FAIL; + } + + vb->vb_bool = true; + + return XLAT_ACTION_DONE; +} + + +static int mod_thread_instantiate(UNUSED CONF_SECTION const *cs, UNUSED void *instance, UNUSED fr_event_list_t *el, + void *thread) +{ + rlm_test_thread_t *t = thread; + + t->value = pthread_self(); + INFO("Performing instantiation for thread %p (ctx %p)", (void *)t->value, t); + + return 0; +} + +static int mod_thread_detach(UNUSED fr_event_list_t *el, void *thread) +{ + rlm_test_thread_t *t = thread; + + INFO("Performing detach for thread %p", (void *)t->value); + + if (!fr_cond_assert(t->value == pthread_self())) return -1; + + return 0; +} + +/* + * Do any per-module bootstrapping that is separate to each + * configured instance of the module. e.g. set up connections + * to external databases, read configuration files, set up + * dictionary entries, etc. + * + * If configuration information is given in the config section + * that must be referenced in later calls, store a handle to it + * in *instance otherwise put a null pointer there. + */ +static int mod_bootstrap(void *instance, UNUSED CONF_SECTION *conf) +{ + xlat_t *xlat; + rlm_test_t *inst = instance; + + if (paircmp_register_by_name("Test-Paircmp", attr_user_name, false, + rlm_test_cmp, inst) < 0) { + PERROR("Failed registering \"Test-Paircmp\""); + return -1; + } + + /* + * Log some messages + */ + INFO("Informational message"); + WARN("Warning message"); + ERROR("Error message"); + DEBUG("Debug message"); + DEBUG2("Debug2 message"); + DEBUG3("Debug3 message"); + DEBUG4("Debug4 message"); + + /* + * Output parsed tmpls + */ + if (inst->tmpl) { + INFO("%s", inst->tmpl->name); + } else { + INFO("inst->tmpl is NULL"); + } + + if (inst->tmpl_m) { + talloc_foreach(inst->tmpl_m, item) INFO("%s", item->name); + } else { + INFO("inst->tmpl_m is NULL"); + } + + if (!(xlat = xlat_register(instance, "test_trigger", trigger_test_xlat, false))) return -1; + xlat_func_args(xlat, trigger_test_xlat_args); + + return 0; +} + static int mod_detach(UNUSED void *instance) { /* free things here */ diff --git a/src/tests/modules/test/all.mk b/src/tests/modules/test/all.mk new file mode 100644 index 0000000000..ca38a22228 --- /dev/null +++ b/src/tests/modules/test/all.mk @@ -0,0 +1,3 @@ +# +# Test the "trigger" module +# diff --git a/src/tests/modules/test/module.conf b/src/tests/modules/test/module.conf new file mode 100644 index 0000000000..2b0baceff8 --- /dev/null +++ b/src/tests/modules/test/module.conf @@ -0,0 +1,2 @@ +test testing { +} diff --git a/src/tests/modules/test/trigger.attrs b/src/tests/modules/test/trigger.attrs new file mode 100644 index 0000000000..da24310f20 --- /dev/null +++ b/src/tests/modules/test/trigger.attrs @@ -0,0 +1,9 @@ +Packet-Type = Access-Request + +# +# Expected answer +# +# There's not an Accounting-Failed packet type in RADIUS... +# +Packet-Type == Access-Accept + diff --git a/src/tests/modules/test/trigger.unlang b/src/tests/modules/test/trigger.unlang new file mode 100644 index 0000000000..77c029e0e5 --- /dev/null +++ b/src/tests/modules/test/trigger.unlang @@ -0,0 +1,5 @@ +if ("%(test_trigger:test)" == false) { + test_fail +} + +test_pass diff --git a/src/tests/modules/test/triggers.conf b/src/tests/modules/test/triggers.conf new file mode 100644 index 0000000000..03bcd75398 --- /dev/null +++ b/src/tests/modules/test/triggers.conf @@ -0,0 +1,3 @@ +trigger { + test = "/bin/echo hello" +}