]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add basic trigger tests
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 8 Sep 2021 23:30:10 +0000 (18:30 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 8 Sep 2021 23:30:30 +0000 (18:30 -0500)
src/lib/server/trigger.c
src/lib/server/trigger.h
src/lib/unlang/xlat_builtin.c
src/modules/rlm_test/rlm_test.c
src/tests/modules/test/all.mk [new file with mode: 0644]
src/tests/modules/test/module.conf [new file with mode: 0644]
src/tests/modules/test/trigger.attrs [new file with mode: 0644]
src/tests/modules/test/trigger.unlang [new file with mode: 0644]
src/tests/modules/test/triggers.conf [new file with mode: 0644]

index 6e4cc33db5b464547a55a43b45dea10c6ad03901..68293fa0100166143d0f6dfd126cdab82138e051 100644 (file)
@@ -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.
index f83ab1a266498dfcbd572eb7205a6d38a35c1fd4..1c3dcacd3d3824d550cb48145bbf78806f26bbb2 100644 (file)
@@ -36,8 +36,10 @@ extern "C" {
 #include <freeradius-devel/util/pair.h>
 #include <freeradius-devel/util/talloc.h>
 
+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);
index 76d0099d6ba4cdd8d9be05ed77be7c2ff186ef8b..64f443c3f7f9fb44d4b2c88ac67c7377bb660dac 100644 (file)
@@ -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
index 86c72d0907021885251049e2321d545634bcae45..ad4cb4d1f9a4c9aede98adf1d54f42c598fcf299 100644 (file)
@@ -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 (file)
index 0000000..ca38a22
--- /dev/null
@@ -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 (file)
index 0000000..2b0bace
--- /dev/null
@@ -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 (file)
index 0000000..da24310
--- /dev/null
@@ -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 (file)
index 0000000..77c029e
--- /dev/null
@@ -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 (file)
index 0000000..03bcd75
--- /dev/null
@@ -0,0 +1,3 @@
+trigger {
+       test = "/bin/echo hello"
+}