From: Alan T. DeKok Date: Mon, 22 Dec 2014 20:02:03 +0000 (-0500) Subject: Added sample tests for the "users" file X-Git-Tag: release_3_0_7~419 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7427bc6ff8d86819a1db060cef110d846754b48c;p=thirdparty%2Ffreeradius-server.git Added sample tests for the "users" file --- diff --git a/src/tests/keywords/all.mk b/src/tests/keywords/all.mk index 2f2563ce040..8972e702750 100644 --- a/src/tests/keywords/all.mk +++ b/src/tests/keywords/all.mk @@ -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 index 00000000000..d24ac4b378e --- /dev/null +++ b/src/tests/modules/default-input.attrs @@ -0,0 +1,11 @@ +# +# Input packet +# +User-Name = "bob" +User-Password = "hello" + +# +# Expected answer +# +Response-Packet-Type == Access-Accept +Filter-Id == 'success' diff --git a/src/tests/modules/files/all.mk b/src/tests/modules/files/all.mk index 74619dbd894..a526965ae01 100644 --- a/src/tests/modules/files/all.mk +++ b/src/tests/modules/files/all.mk @@ -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 index 00000000000..4d711585e65 --- /dev/null +++ b/src/tests/modules/files/authorize @@ -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 index 00000000000..456c666dbc1 --- /dev/null +++ b/src/tests/modules/files/files.unlang @@ -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 index 00000000000..12b46acd9f9 --- /dev/null +++ b/src/tests/modules/files/module.conf @@ -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 index 00000000000..f9561d592f5 --- /dev/null +++ b/src/tests/modules/radiusd.conf @@ -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 index 00000000000..a943f00e4a5 --- /dev/null +++ b/src/tests/modules/subdir.mk @@ -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)/