[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Add a test script to try parsing and encoding Tor configurations



commit 7c7e8402b599515846cbb77ab4cbb5520f8463d8
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Wed Sep 11 14:50:06 2019 -0400

    Add a test script to try parsing and encoding Tor configurations
    
    This script takes a set of example torrcs and command-lines from
    src/test/conf_examples.  If a success is expected, it runs "tor
    --dump-config" and compares the result with the one we expect.  If a
    failure is expected, it runs "tor --verify-config" and greps for the
    error we expect.
---
 src/test/conf_examples/1/expected |   2 +
 src/test/conf_examples/1/torrc    |   5 ++
 src/test/conf_examples/2/error    |   1 +
 src/test/conf_examples/2/torrc    |   1 +
 src/test/conf_examples/3/cmdline  |   1 +
 src/test/conf_examples/3/expected |   1 +
 src/test/conf_examples/3/torrc    |   0
 src/test/include.am               |   6 +-
 src/test/test_parseconf.sh        | 162 ++++++++++++++++++++++++++++++++++++++
 9 files changed, 177 insertions(+), 2 deletions(-)

diff --git a/src/test/conf_examples/1/expected b/src/test/conf_examples/1/expected
new file mode 100644
index 000000000..9d6688a56
--- /dev/null
+++ b/src/test/conf_examples/1/expected
@@ -0,0 +1,2 @@
+ContactInfo tor_tellini@xxxxxxxxxxx
+SocksPort 80
diff --git a/src/test/conf_examples/1/torrc b/src/test/conf_examples/1/torrc
new file mode 100644
index 000000000..bff7fa0aa
--- /dev/null
+++ b/src/test/conf_examples/1/torrc
@@ -0,0 +1,5 @@
+
+# Here is a simple example torrc.
+  SocksPort  80
+
+ContactInfo "tor_tellini@xxxxxxxxxxx"
diff --git a/src/test/conf_examples/2/error b/src/test/conf_examples/2/error
new file mode 100644
index 000000000..ce18b68db
--- /dev/null
+++ b/src/test/conf_examples/2/error
@@ -0,0 +1 @@
+Unknown option 'JumpingJellyjars'
diff --git a/src/test/conf_examples/2/torrc b/src/test/conf_examples/2/torrc
new file mode 100644
index 000000000..8ec8133b2
--- /dev/null
+++ b/src/test/conf_examples/2/torrc
@@ -0,0 +1 @@
+JumpingJellyjars 1
diff --git a/src/test/conf_examples/3/cmdline b/src/test/conf_examples/3/cmdline
new file mode 100644
index 000000000..5b2fadceb
--- /dev/null
+++ b/src/test/conf_examples/3/cmdline
@@ -0,0 +1 @@
+--socksport 99
diff --git a/src/test/conf_examples/3/expected b/src/test/conf_examples/3/expected
new file mode 100644
index 000000000..867fb8bcc
--- /dev/null
+++ b/src/test/conf_examples/3/expected
@@ -0,0 +1 @@
+SocksPort 99
diff --git a/src/test/conf_examples/3/torrc b/src/test/conf_examples/3/torrc
new file mode 100644
index 000000000..e69de29bb
diff --git a/src/test/include.am b/src/test/include.am
index 931e05498..2dd4d8c58 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -24,7 +24,8 @@ TESTSCRIPTS = \
 	src/test/test_workqueue_pipe2.sh \
 	src/test/test_workqueue_socketpair.sh \
 	src/test/test_switch_id.sh \
-	src/test/test_cmdline.sh
+	src/test/test_cmdline.sh \
+	src/test/test_parseconf.sh
 
 if USE_RUST
 TESTSCRIPTS += \
@@ -414,7 +415,8 @@ EXTRA_DIST += \
 	src/test/test_workqueue_pipe.sh \
 	src/test/test_workqueue_pipe2.sh \
 	src/test/test_workqueue_socketpair.sh \
-	src/test/test_cmdline.sh
+	src/test/test_cmdline.sh \
+	src/test/test_parseconf.sh
 
 test-rust:
 	$(TESTS_ENVIRONMENT) "$(abs_top_srcdir)/src/test/test_rust.sh"
diff --git a/src/test/test_parseconf.sh b/src/test/test_parseconf.sh
new file mode 100755
index 000000000..dc80fc36f
--- /dev/null
+++ b/src/test/test_parseconf.sh
@@ -0,0 +1,162 @@
+#!/bin/sh
+# Copyright 2019, The Tor Project, Inc.
+# See LICENSE for licensing information
+
+# Integration test script for verifying that Tor configurations are parsed as
+# we expect.
+#
+# Valid configurations are tested with --dump-config, which parses and
+# validates the configuration before writing it out.  We then make sure that
+# the result is what we expect, before parsing and dumping it again to make
+# sure that there is no change.
+#
+# Invalid configurations are tested with --verify-config, which parses
+# and validates the configuration. We capture its output and make sure that
+# it contains the error message we expect.
+
+# This script looks for its test cases as individual directories in
+# src/test/conf_examples/.  Each test may have these files:
+#
+# torrc -- Usually needed. This file is passed to Tor on the command line
+#      with the "-f" flag. (If you omit it, you'll test Tor's behavior when
+#      it receives a nonexistent configuration file.)
+#
+# torrc.defaults -- Optional. If present, it is passed to Tor on the command
+#      line with the --defaults-torrc option. If this file is absent, an empty
+#      file is passed instead to prevent Tor from reading the system defaults.
+#
+# cmdline -- Optional. If present, it contains command-line arguments that
+#      will be passed to Tor.
+#
+# expected -- If this file is present, then it should be the expected result
+#      of "--dump-config short" for this test case.  Exactly one of
+#      "expected" or "error" must be present, or the test will fail.
+#
+# error -- If this file is present, then it contains a regex that must be
+#      matched by some line in the output of "--verify-config", which must
+#      fail. Exactly one of "expected" or "error" must be present, or the
+#      test will fail.
+
+umask 077
+set -e
+die() { echo "$1" >&2 ; exit 5; }
+
+# find the tor binary
+if [ $# -ge 1 ]; then
+  TOR_BINARY="${1}"
+  shift
+else
+  TOR_BINARY="${TESTING_TOR_BINARY:-./src/app/tor}"
+fi
+
+# make a safe space for temporary files
+DATA_DIR=$(mktemp -d -t tor_parseconf_tests.XXXXXX)
+trap 'rm -rf "$DATA_DIR"' 0
+touch "${DATA_DIR}/EMPTY" || die "Couldn't create empty file."
+
+# This is where we look for examples
+EXAMPLEDIR="$(dirname "$0")"/conf_examples
+
+case "$(uname -s)" in
+    CYGWIN*) WINDOWS=1;;
+    MINGW*) WINDOWS=1;;
+    MSYS*) WINDOWS=1;;
+    *) WINDOWS=0;;
+esac
+
+if test "$WINDOWS" = 1; then
+    FILTER="dos2unix"
+else
+    FILTER="cat"
+fi
+
+for dir in "${EXAMPLEDIR}"/*; do
+    if ! test -d "${dir}"; then
+       # Only count directories.
+       continue
+    fi
+
+    testname="$(basename "${dir}")"
+    # We use printf since "echo -n" is not standard
+    printf "%s: " "$testname"
+
+    if test -f "${dir}/torrc.defaults"; then
+        DEFAULTS="${dir}/torrc.defaults"
+    else
+        DEFAULTS="${DATA_DIR}/EMPTY"
+    fi
+
+    if test -f "${dir}/cmdline"; then
+        CMDLINE="$(cat "${dir}"/cmdline)"
+    else
+        CMDLINE=""
+    fi
+
+    if test -f "${dir}/expected"; then
+        if test -f "${dir}/error"; then
+            echo "FAIL: Found both ${dir}/expected and ${dir}/error."
+            echo "(Only one of these files should exist.)"
+            exit 1
+        fi
+
+        # This case should succeed: run dump-config and see if it does.
+
+        "${TOR_BINARY}" -f "${dir}"/torrc \
+                        --defaults-torrc "${DEFAULTS}" \
+                        --dump-config short \
+                        ${CMDLINE} \
+                        | "${FILTER}" > "${DATA_DIR}/output.${testname}" \
+                        || die "Failure: Tor exited."
+
+        if cmp "${dir}/expected" "${DATA_DIR}/output.${testname}">/dev/null ; then
+            # Check round-trip.
+            "${TOR_BINARY}" -f "${DATA_DIR}/output.${testname}" \
+                            --defaults-torrc "${DATA_DIR}/empty" \
+                            --dump-config short \
+                            | "${FILTER}" \
+                            > "${DATA_DIR}/output_2.${testname}" \
+                        || die "Failure: Tor exited on round-trip."
+
+            if ! cmp "${DATA_DIR}/output.${testname}" \
+                 "${DATA_DIR}/output_2.${testname}"; then
+                echo "Failure: did not match on round-trip."
+                exit 1
+            fi
+
+            echo "OK"
+        else
+            echo "FAIL"
+            diff -u "${dir}/expected" "${DATA_DIR}/output.${testname}"
+            exit 1
+        fi
+
+    elif test -f "${dir}/error"; then
+        # This case should fail: run verify-config and see if it does.
+
+        "${TOR_BINARY}" --verify-config \
+                        -f "${dir}"/torrc \
+                        --defaults-torrc "${DEFAULTS}" \
+                        ${CMDLINE} \
+                        > "${DATA_DIR}/output.${testname}" \
+                        && die "Failure: Tor did not report an error."
+
+        expect_err="$(cat "${dir}"/error)"
+        if grep "${expect_err}" "${DATA_DIR}/output.${testname}" >/dev/null; then
+            echo "OK"
+        else
+            echo "FAIL"
+            echo "Expected error: ${expect_err}"
+            echo "Tor said:"
+            cat "${DATA_DIR}/output.${testname}"
+            exit 1
+        fi
+
+    else
+        # This case is not actually configured with a success or a failure.
+        # call that an error.
+
+        echo "FAIL: Did not find ${dir}/expected or ${dir}/error."
+        exit 1
+    fi
+
+done



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits