]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Added sample tests for the "users" file
authorAlan T. DeKok <aland@freeradius.org>
Mon, 22 Dec 2014 20:02:03 +0000 (15:02 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Mon, 22 Dec 2014 20:13:16 +0000 (15:13 -0500)
src/tests/keywords/all.mk
src/tests/modules/default-input.attrs [new file with mode: 0644]
src/tests/modules/files/all.mk
src/tests/modules/files/authorize [new file with mode: 0644]
src/tests/modules/files/files.unlang [new file with mode: 0644]
src/tests/modules/files/module.conf [new file with mode: 0644]
src/tests/modules/radiusd.conf [new file with mode: 0644]
src/tests/modules/subdir.mk [new file with mode: 0644]

index 2f2563ce0403dfde2878d30b36169b5d7f25f129..8972e7027502d23e037e175dc5a02b7ea9dacada 100644 (file)
@@ -25,9 +25,6 @@ BOOTSTRAP_EXISTS := $(addprefix $(DIR)/,$(addsuffix .attrs,$(KEYWORD_FILES)))
 BOOTSTRAP_NEEDS         := $(filter-out $(wildcard $(BOOTSTRAP_EXISTS)),$(BOOTSTRAP_EXISTS))
 BOOTSTRAP       := $(subst $(DIR),$(BUILD_DIR)/tests/keywords,$(BOOTSTRAP_NEEDS))
 
-BOOTSTRAP_HAS   := $(filter $(wildcard $(BOOTSTRAP_EXISTS)),$(BOOTSTRAP_EXISTS))
-BOOTSTRAP_COPY  := $(subst $(DIR),$(BUILD_DIR)/tests/keywords,$(BOOTSTRAP_NEEDS))
-
 #
 #  For each file, look for precursor test.
 #  Ensure that each test depends on its precursors.
diff --git a/src/tests/modules/default-input.attrs b/src/tests/modules/default-input.attrs
new file mode 100644 (file)
index 0000000..d24ac4b
--- /dev/null
@@ -0,0 +1,11 @@
+#
+#  Input packet
+#
+User-Name = "bob"
+User-Password = "hello"
+
+#
+#  Expected answer
+#
+Response-Packet-Type == Access-Accept
+Filter-Id == 'success'
index 74619dbd894fcfaa1d34509003e2bbba1c218c6a..a526965ae01b5d4bda366aff63d7455e0a2f268a 100644 (file)
@@ -5,3 +5,5 @@
 #  MODULE.test is the main target for this module.
 files.test:
        @echo OK: files.test
+
+include src/tests/modules/subdir.mk
diff --git a/src/tests/modules/files/authorize b/src/tests/modules/files/authorize
new file mode 100644 (file)
index 0000000..4d71158
--- /dev/null
@@ -0,0 +1,5 @@
+#
+#  Test if the "users" file works
+#
+bob    Cleartext-Password := "hello"
+       Filter-Id := "success"
diff --git a/src/tests/modules/files/files.unlang b/src/tests/modules/files/files.unlang
new file mode 100644 (file)
index 0000000..456c666
--- /dev/null
@@ -0,0 +1,4 @@
+#
+#  Run the "files" module
+#
+files
diff --git a/src/tests/modules/files/module.conf b/src/tests/modules/files/module.conf
new file mode 100644 (file)
index 0000000..12b46ac
--- /dev/null
@@ -0,0 +1,9 @@
+files {
+       # The default key attribute to use for matches.  The content
+       # of this attribute is used to match the "name" of the
+       # entry.
+       #key = "%{%{Stripped-User-Name}:-%{User-Name}}"
+
+       #  The old "users" style file is now located here.
+       filename = $ENV{MODULE_TEST_DIR}/authorize
+}
diff --git a/src/tests/modules/radiusd.conf b/src/tests/modules/radiusd.conf
new file mode 100644 (file)
index 0000000..f9561d5
--- /dev/null
@@ -0,0 +1,41 @@
+#
+#  Minimal radiusd.conf for testing modules
+#
+
+raddb          = raddb
+
+modconfdir     = ${raddb}/mods-config
+
+correct_escapes        = true
+
+#  Only for testing!
+#  Setting this on a production system is a BAD IDEA.
+security {
+       allow_vulnerable_openssl = yes
+}
+
+modules {
+       $INCLUDE ${raddb}/mods-enabled/always
+
+       $INCLUDE ${raddb}/mods-enabled/pap
+
+       $INCLUDE ${raddb}/mods-enabled/expr
+
+       $INCLUDE $ENV{MODULE_TEST_DIR}/module.conf
+}
+
+server default {
+       authorize {
+               #
+               # Include the test file specified by the
+               # KEYWORD environment variable.
+               #
+               $INCLUDE $ENV{MODULE_TEST_UNLANG}
+
+               pap
+       }
+
+       authenticate {
+               pap
+       }
+}
diff --git a/src/tests/modules/subdir.mk b/src/tests/modules/subdir.mk
new file mode 100644 (file)
index 0000000..a943f00
--- /dev/null
@@ -0,0 +1,115 @@
+#
+# Included by each module, after setting various flags
+#
+
+#
+#  Find out which test directory included us.  We can't do math in GNU make,
+#  so we get the lastword of the makefile list, then strip it out of the
+#  makefile list, then get the last word of the resulting list.  Then
+#  get the name of the directory which included us
+#
+MODULE_TEST := $(patsubst src/tests/modules/%/all.mk,%,$(lastword $(subst $(lastword ${MAKEFILE_LIST}),,${MAKEFILE_LIST})))
+
+#
+#  This is easier than re-typing them everywhere.
+#
+MODULE_DIR := modules/$(MODULE_TEST)
+TEST_DIR := $(BUILD_DIR)/tests/$(MODULE_DIR)
+
+#
+#  The test files are files without extensions.
+#  The list is unordered.  The order is added in the next step by looking
+#  at precursors.
+#
+MODULE_FILES := $(subst src/tests/$(MODULE_DIR)/,,$(wildcard src/tests/$(MODULE_DIR)/*.unlang))
+
+#
+#  Create the directory where the output files go, because GNU Make is
+#  too stupid to do that itself.
+#
+.PHONY: $(TEST_DIR)
+$(TEST_DIR):
+       @mkdir -p $@
+
+#
+#  For each file, look for precursor test.
+#  Ensure that each test depends on its precursors.
+#
+-include $(TEST_DIR)/depends.mk
+
+$(TEST_DIR)/depends.mk: $(addprefix src/tests/$(MODULE_DIR)/,$(MODULE_FILES)) | $(TEST_DIR)
+       @rm -f $@
+       @for x in $^; do \
+               y=`grep 'PRE: ' $$x | sed 's/.*://;s/  / /g;s, , $(TEST_DIR),g'`; \
+               if [ "$$y" != "" ]; then \
+                       z=`echo $$x | sed 's,src/,$(BUILD_DIR)/',`; \
+                       echo "$$z: $$y" >> $@; \
+                       echo "" >> $@; \
+               fi \
+       done
+
+MODULE_TEST_EXISTS := $(addprefix src/tests/$(MODULE_DIR)/,$(patsubst %.unlang,%.attrs,$(MODULE_FILES)))
+MODULE_TEST_NEEDS := $(filter-out $(wildcard $(MODULE_TEST_EXISTS)),$(MODULE_TEST_EXISTS))
+MODULE_TEST_COPY   := $(subst src/tests/$(MODULE_DIR),$(TEST_DIR),$(MODULE_TEST_NEEDS))
+
+#
+#  These ones get copied over from the default input
+#
+$(MODULE_TEST_COPY): src/tests/modules/default-input.attrs | $(TEST_DIR)
+       @cp $< $@
+
+#
+#  These ones get copied over from their original files
+#
+$(TEST_DIR)/%.attrs: src/tests/$(MODULE_DIR)/%.attrs | $(TEST_DIR)
+       @cp $< $@
+
+#
+#  Don't auto-remove the files copied by the rule just above.
+#  It's unnecessary, and it clutters the output with crap.
+#
+.PRECIOUS: $(TEST_DIR)/%.attrs
+
+#
+#  Files in the output dir depend on the unit tests
+#
+#      src/tests/$(MODULE_DIR)/FOO             unlang for the test
+#      src/tests/$(MODULE_DIR)/FOO.attrs       input RADIUS and output filter
+#      build/tests/$(MODULE_DIR)/FOO   updated if the test succeeds
+#      build/tests/$(MODULE_DIR)/FOO.log       debug output for the test
+#
+#  If the test fails, then look for ERROR in the input.  No error
+#  means it's unexpected, so we die.
+#
+#  Otherwise, check the log file for a parse error which matches the
+#  ERROR line in the input.
+#
+$(TEST_DIR)/%: src/tests/$(MODULE_DIR)/%.unlang $(TEST_DIR)/%.attrs $(TESTBINDIR)/unittest | $(TEST_DIR) build.raddb
+       @echo UNIT-TEST $(notdir $@)
+       @if ! MODULE_TEST_DIR=src/tests/$(MODULE_DIR) MODULE_TEST_UNLANG=src/tests/$(MODULE_DIR)/$(notdir $@).unlang $(TESTBIN)/unittest -D share -d src/tests/modules/ -i $@.attrs -f $@.attrs -xx > $@.log 2>&1; then \
+               if ! grep ERROR $< 2>&1 > /dev/null; then \
+                       cat $@.log; \
+                       echo "# $@.log"; \
+                       echo MODULE_TEST_DIR=src/tests/$(MODULE_DIR) MODULE_TEST_UNLANG=src/tests/$(MODULE_DIR)/$(notdir $@).unlang $(TESTBIN)/unittest -D share -d src/tests/modules/ -i $@.attrs -f $@.attrs -xx; \
+                       exit 1; \
+               fi; \
+               FOUND=$$(grep ^$< $@.log | head -1 | sed 's/:.*//;s/.*\[//;s/\].*//'); \
+               EXPECTED=$$(grep -n ERROR $< | sed 's/:.*//'); \
+               if [ "$$EXPECTED" != "$$FOUND" ]; then \
+                       cat $@.log; \
+                       echo "# $@.log"; \
+                       echo MODULE_TEST_DIR=src/tests/$(MODULE_DIR) MODULE_TEST_UNLANG=src/tests/$(MODULE_DIR)/$(notdir $@).unlang $(TESTBIN)/unittest -D share -d src/tests/modules/ -i $@.attrs -f $@.attrs -xx; \
+                       exit 1; \
+               fi \
+       fi
+       @touch $@
+
+#
+#  The input files get stripped of the ".unlang" suffix, and get the
+#  test directory added as a prefix.
+#
+$(MODULE_TEST).test: $(addprefix $(TEST_DIR)/,$(patsubst %.unlang,%,$(MODULE_FILES)))
+
+.PHONY: clean.$(MODULE_TEST).test
+clean.$(MODULE_TEST).test:
+       @rm -rf $(TEST_DIR)/