Skip to content

Commit

Permalink
Added sample tests for the "users" file
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok authored and arr2036 committed Dec 22, 2014
1 parent 4bd06f5 commit 2ae2f95
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/tests/keywords/all.mk
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions 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'
2 changes: 2 additions & 0 deletions src/tests/modules/files/all.mk
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/tests/modules/files/authorize
@@ -0,0 +1,5 @@
#
# Test if the "users" file works
#
bob Cleartext-Password := "hello"
Filter-Id := "success"
4 changes: 4 additions & 0 deletions src/tests/modules/files/files.unlang
@@ -0,0 +1,4 @@
#
# Run the "files" module
#
files
9 changes: 9 additions & 0 deletions 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
}
41 changes: 41 additions & 0 deletions 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
}
}
115 changes: 115 additions & 0 deletions 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)/

0 comments on commit 2ae2f95

Please sign in to comment.