beginning of a test suite
authorChristian Thaeter <ct@pipapo.org>
Thu, 25 Oct 2007 01:08:34 +0000 (03:08 +0200)
committerChristian Thaeter <ct@pipapo.org>
Thu, 25 Oct 2007 01:08:34 +0000 (03:08 +0200)
Makefile.am
tests/10basics.tests [new file with mode: 0644]
tests/20simplecommands.tests [new file with mode: 0644]
tests/files/example [new file with mode: 0644]
tests/files/policy [new file with mode: 0644]
tests/test.sh [new file with mode: 0755]

index 90be121..925d5ee 100644 (file)
@@ -20,6 +20,7 @@ AUTOMAKE_OPTIONS=gnu
 rxpd_srcdir = $(top_srcdir)/src
 
 bin_PROGRAMS = rxpd
+check_PROGRAMS =
 
 rxpd_CFLAGS = $(CFLAGS) -std=gnu99 -Wall -Wextra -Werror
 rxpd_LDADD = -lpth
@@ -38,6 +39,10 @@ rxpd_SOURCES =                                       \
        $(rxpd_srcdir)/rxpd_rule.c              \
        $(rxpd_srcdir)/rxpd_socket.c
 
+#### test suite
+
+tests_srcdir = $(top_srcdir)/tests
+TESTS = $(tests_srcdir)/test.sh
 
 #### maintainer targets
 
diff --git a/tests/10basics.tests b/tests/10basics.tests
new file mode 100644 (file)
index 0000000..02baae9
--- /dev/null
@@ -0,0 +1,15 @@
+TESTING "startup and options" ./rxpd
+
+TEST "usage test" -h <<END
+out: rxpd [OPTIONS] RULES..
+out:  -v          increase verbosity level
+out:  -V          show version
+out:  -d          daemonize into background
+out:  -D          debug mode
+out:  -b dir      basedir for rules
+out:  -q          be quiet
+out:  -t port     listen on tcp port
+out:  -p policy   define a list for access policies
+out:  -h          this usage information
+return: 0
+END
diff --git a/tests/20simplecommands.tests b/tests/20simplecommands.tests
new file mode 100644 (file)
index 0000000..5b2ea26
--- /dev/null
@@ -0,0 +1,86 @@
+# we randomize ports to minimize collisions,
+# this needs to be fixed someday (use netstat and check which port is free)
+port=$((RANDOM+1500))
+./rxpd -dq -b $srcdir/files -p policy -t $port example
+
+function rxpd_client()
+{
+    nc localhost $port;
+}
+
+TESTING "basic protcol tests" rxpd_client
+
+
+# this test has to be updated with each new version
+TEST "version information" <<END
+in: VERSION:
+out: rxpd 0.2pre1
+out: #
+out: # Copyright (C)
+out: #   2007,               Christian Thaeter <ct@pipapo.org>
+out: #
+out: # This is free software.  You may redistribute copies of it under the terms of
+out: # the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
+out: # There is NO WARRANTY, to the extent permitted by law.
+out: #
+out: # http://www.pipapo.org/pipawiki/RegexPolicyDaemon
+END
+
+
+TEST "buildin protocol help" <<END
+in: HELP:
+out: # Available commands:
+out: #
+out: # CHECK data against regular expressions.
+out: # APPEND new rules to a list.
+out: # PREPEND new rules in front of a list.
+out: # REMOVE rules from a list.
+out: # REPLACE a rule in a list with new rules.
+out: # DELETE a list from memory.
+out: # LOAD a list from disk.
+out: # SAVE a list to disk.
+out: # FETCH a list from a remote server.
+out: # SYNC atimes from other lists.
+out: # EXPIRE aged rules from a list.
+out: # DUMP rules in a list.
+out: # LIST all existing lists.
+out: # VERSION of this rxpd is 0.2pre1.
+out: # HELP is what you see right now.
+out: # SHUTDOWN the daemon.
+out: #
+out: # general syntax is: 'COMMAND:listname\n..data..'
+END
+
+
+TEST "show loaded files" <<END
+in: LIST:
+out: example
+out: policy
+END
+
+
+TEST "dump a file" <<END
+in: DUMP:policy
+out: # syntax:
+out: # [atime]:name:command:list:proto:address
+out: #
+out: # Allow dumping of the 'policy' list itself
+out: :ACCEPT:DUMP:policy
+out: # allow localhost
+out: :ACCEPT:.*:tcp.:127.0.0.1$
+out: # Clients from local network are allowed to do anything
+out: :ACCEPT:.*:tcp.:10\..*$
+out: # Forbid all others to do anything else with the policy
+out: :REJECT:.*:policy
+out: # Hey and no one shall remotely shutdown this daemon!
+out: :REJECT:SHUTDOWN
+out: # Finally allow anything else
+out: :ACCEPT:.*
+END
+
+
+TEST "daemon shutdown" <<END
+in: SHUTDOWN:
+out: #OK:
+END
+
diff --git a/tests/files/example b/tests/files/example
new file mode 100644 (file)
index 0000000..2f72906
--- /dev/null
@@ -0,0 +1,7 @@
+# Free things are good!
+0:accept:GNU|Linux
+0:accept:FreeBSD
+# Bad things
+:reject:M.*soft
+# test for error in regex
+:error in regex:[
diff --git a/tests/files/policy b/tests/files/policy
new file mode 100644 (file)
index 0000000..bccf84d
--- /dev/null
@@ -0,0 +1,15 @@
+# syntax:
+# [atime]:name:command:list:proto:address
+#
+# Allow dumping of the 'policy' list itself
+:ACCEPT:DUMP:policy
+# allow localhost
+:ACCEPT:.*:tcp.:127.0.0.1$
+# Clients from local network are allowed to do anything
+:ACCEPT:.*:tcp.:10\..*$
+# Forbid all others to do anything else with the policy
+:REJECT:.*:policy
+# Hey and no one shall remotely shutdown this daemon!
+:REJECT:SHUTDOWN
+# Finally allow anything else
+:ACCEPT:.*
diff --git a/tests/test.sh b/tests/test.sh
new file mode 100755 (executable)
index 0000000..d231c08
--- /dev/null
@@ -0,0 +1,228 @@
+#!/bin/bash 
+#  Copyright (C)         CinelerraCV
+#    2007,               Christian Thaeter <ct@pipapo.org>
+#                        Hermann Vosseler <Ichthyostega@web.de>
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License as
+#  published by the Free Software Foundation; either version 2 of the
+#  License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software
+#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+# TESTMODE=FULL yet unimplemented
+#   run all tests, PLANNED which fail count as error
+#
+# TESTMODE=FAST
+#   run only tests which recently failed
+#
+# TESTMODE=FIRSTFAIL
+#   stop testing on the first failure
+
+export LC_ALL=C
+
+arg0="$0"
+srcdir=$(dirname "$arg0")
+
+ulimit -S -t 1 -v 524288
+valgrind=""
+
+# gnu-pth & valgrind don't cooperate nicely
+
+#if [ "$VALGRINDFLAGS" = 'DISABLE' ]; then
+#    echo "valgrind explicit disabled"
+#else
+#    if [ "$(which valgrind)" ]; then
+#        valgrind="$(which valgrind) --leak-check=yes --show-reachable=yes -q $VALGRINDFLAGS"
+#      ulimit -S -t 10
+#    else
+#        echo "no valgrind found, go without it"
+#    fi
+#fi
+
+echo
+echo ================ ${0##*/} ================
+
+TESTCNT=0
+SKIPCNT=0
+FAILCNT=0
+
+# the old testlog if existing will be used to check for previous test states
+if test -f ,testlog; then
+    mv ,testlog ,testlog.pre
+else
+    touch ,testlog.pre
+fi
+
+date >,testlog
+
+function TEST()
+{
+        name="$1"
+       shift
+        rm -f ,send_stdin
+        rm -f ,expect_stdout
+        rm -f ,expect_stderr
+
+       while read -r line; do
+            cmd="${line%%:*}"
+            arg="${line#*: }"
+            expect_return=0
+
+            case $cmd in
+            'in')
+                echo "$arg" >>,send_stdin
+                ;;
+            'out')
+                echo "$arg" >>,expect_stdout
+                ;;
+            'err')
+                echo "$arg" >>,expect_stderr
+                ;;
+            'return')
+                expect_return=$arg
+                ;;
+            *)
+                echo "UNKOWN TEST COMMAND '$cmd'" 1>&2
+                exit
+                ;;
+            esac
+        done
+       echo -n "TEST $name: "
+       echo -en "\nTEST $name: $* " >>,testlog
+
+        case $TESTMODE in
+        *FAST*)
+            if grep "^TEST $name: .* FAILED" ,testlog.pre >&/dev/null; then
+                MSGOK=" (fixed)"
+                MSGFAIL=" (still broken)"
+            elif grep "^TEST $name: .* \\(SKIPPED (ok)\\|OK\\)" ,testlog.pre >&/dev/null; then
+                echo ".. SKIPPED (ok)"
+                echo ".. SKIPPED (ok)" >>,testlog
+                SKIPCNT=$(($SKIPCNT + 1))
+                TESTCNT=$(($TESTCNT + 1))
+                return
+            else
+                MSGOK=" (new)"
+                MSGFAIL=" (new)"
+            fi
+            ;;
+        *)
+            MSGOK=""
+            MSGFAIL=""
+            ;;
+        esac
+
+        TESTCNT=$(($TESTCNT + 1))
+
+        fails=0
+
+
+        if test -f ,send_stdin; then
+            cat ,send_stdin | $valgrind $TESTBIN "$@" 2>,stderr >,stdout
+        else
+            $valgrind $TESTBIN "$@" 2>,stderr >,stdout
+        fi &>/dev/null
+        return=$?
+
+        echo -n >,testtmp
+
+        if test -f ,expect_stdout; then
+            if ! cmp ,expect_stdout ,stdout &>/dev/null; then
+                echo "unexpected data on stdout" >>,testtmp
+                grep -v ': \(TRACE\|INFO\|NOTICE\|WARNING\|ERR\):' <,stdout >,tmp
+                diff -ua ,expect_stdout ,tmp >>,testtmp
+                rm ,tmp
+                ((fails+=1))
+            fi
+        fi
+
+        if test -f ,expect_stderr; then
+            if ! cmp ,expect_stderr ,stderr &>/dev/null; then
+                echo "unexpected data on stderr" >>,testtmp
+                grep -v ': \(TRACE\|INFO\|NOTICE\|WARNING\|ERR\):' <,stderr >,tmp
+                diff -ua ,expect_stderr ,tmp >>,testtmp
+                rm ,tmp
+                ((fails+=1))
+            fi
+        fi
+
+        if test $expect_return != $return; then
+            echo "unexpected return value $return" >>,testtmp
+            ((fails+=1))
+        fi
+
+       if test $fails -eq 0; then
+            echo ".. OK$MSGOK"
+            echo ".. OK$MSGOK" >>,testlog
+        else
+            echo ".. FAILED$MSGFAIL";
+            echo ".. FAILED$MSGFAIL" >>,testlog
+            cat ,testtmp >>,testlog
+            rm ,testtmp
+            echo "stderr was:" >>,testlog
+            cat ,stderr >>,testlog
+            echo END >>,testlog
+            FAILCNT=$(($FAILCNT + 1))
+           case $TESTMODE in
+           *FIRSTFAIL*)
+                break 2
+                ;;
+            esac
+        fi
+}
+
+function PLANNED()
+{
+       echo -n "PLANNED $1: "
+       echo -en "\nPLANNED $* " >>,testlog
+        echo ".. SKIPPED (planned)"
+        echo ".. SKIPPED (planned)" >>,testlog
+        SKIPCNT=$(($SKIPCNT + 1))
+        TESTCNT=$(($TESTCNT + 1))
+}
+
+function RUNTESTS()
+{
+    if test \( ! "${TESTSUITES/*,*/}" \) -a "$TESTSUITES"; then
+        TESTSUITES="{$TESTSUITES}"
+    fi
+    for t in $(eval echo $srcdir/*$TESTSUITES*.tests); do
+        echo "$t"
+    done | sort | uniq | {
+        while read i; do
+            if test -f $i; then
+                source $i
+            fi
+        done
+        echo
+        if [ $FAILCNT = 0 ]; then
+            echo " ... PASSED $(($TESTCNT - $SKIPCNT)) TESTS, $SKIPCNT SKIPPED"
+            #rm ,testlog
+        else
+            echo " ... SUCCEDED $(($TESTCNT - $FAILCNT - $SKIPCNT)) TESTS"
+            echo " ... FAILED $FAILCNT TESTS"
+            echo " ... SKIPPED $SKIPCNT TESTS"
+            echo " see ',testlog' for details"
+            exit 1
+        fi
+    }
+}
+
+function TESTING()
+{
+    echo
+    echo "$1 [${i##*/}]"
+    TESTBIN=$2
+}
+
+TESTSUITES="${TESTSUITES}${1:+${TESTSUITES:+,}$1}"
+
+RUNTESTS