Add test for paircmp
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 5 Nov 2014 21:07:02 +0000 (16:07 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 5 Nov 2014 21:08:47 +0000 (16:08 -0500)
src/modules/rlm_example/rlm_example.c
src/tests/keywords/if-paircmp [new file with mode: 0644]
src/tests/keywords/radiusd.conf

index 3b250c4..c0c78c0 100644 (file)
@@ -25,6 +25,7 @@ RCSID("$Id$")
 
 #include <freeradius-devel/radiusd.h>
 #include <freeradius-devel/modules.h>
+#include <freeradius-devel/rad_assert.h>
 
 /*
  *     Define a structure for our module configuration.
@@ -52,6 +53,16 @@ static const CONF_PARSER module_config[] = {
        { NULL, -1, 0, NULL, NULL }             /* end the list */
 };
 
+static int rlm_example_cmp(UNUSED void *instance, REQUEST *request, UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
+                          UNUSED VALUE_PAIR *check_pairs, UNUSED VALUE_PAIR **reply_pairs)
+{
+       rad_assert(check->da->type == PW_TYPE_STRING);
+
+       RINFO("Example-Paircmp called with \"%s\"", check->vp_strvalue);
+
+       if (strcmp(check->vp_strvalue, "yes") == 0) return 0;
+       return 1;
+}
 
 /*
  *     Do any per-module initialization that is separate to each
@@ -66,7 +77,9 @@ static const CONF_PARSER module_config[] = {
 static int mod_instantiate(CONF_SECTION *conf, void *instance)
 {
        rlm_example_t *inst = instance;
+       ATTR_FLAGS flags;
 
+       memset(&flags, 0, sizeof(flags));
        /*
         *      Do more work here
         */
@@ -75,6 +88,15 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
                return -1;
        }
 
+       if (dict_addattr("Example-Paircmp", -1, 0, PW_TYPE_STRING, flags) < 0) {
+               ERROR("Failed creating paircmp attribute", fr_strerror());
+
+               return -1;
+       }
+
+       paircompare_register(dict_attrbyname("Example-Paircmp"), dict_attrbyvalue(PW_USER_NAME, 0), false,
+                            rlm_example_cmp, inst);
+
        return 0;
 }
 
@@ -91,7 +113,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_authorize(UNUSED void *instance, UNUSED
        /*
         *  Look for the 'state' attribute.
         */
-       state =  pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
+       state = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
        if (state != NULL) {
                RDEBUG("Found reply to access challenge");
                return RLM_MODULE_OK;
diff --git a/src/tests/keywords/if-paircmp b/src/tests/keywords/if-paircmp
new file mode 100644 (file)
index 0000000..0efcc83
--- /dev/null
@@ -0,0 +1,27 @@
+#
+# PRE: update if
+#
+update {
+       control:Cleartext-Password := 'hello'
+       reply:Filter-Id := 'filter'
+}
+
+#
+#  Paircmp
+#
+
+#
+#  Passing 'yes' causes the example paircmp to return match
+#  Passing 'no' causes the example paircmp to return a non-match
+#
+if (&Example-Paircmp != 'yes') {
+       update reply {
+               Filter-Id += 'fail 1'
+       }
+}
+
+if (&Example-Paircmp == 'no') {
+       update reply {
+               Filter-Id += 'fail 2'
+       }
+}
index 4273c39..85cc347 100644 (file)
@@ -21,6 +21,10 @@ modules {
        $INCLUDE ${raddb}/mods-enabled/pap
 
        $INCLUDE ${raddb}/mods-enabled/expr
+
+       example {
+               boolean = yes
+       }
 }
 
 server default {