test_parseconf.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/sh
  2. # Copyright 2019, The Tor Project, Inc.
  3. # See LICENSE for licensing information
  4. # Integration test script for verifying that Tor configurations are parsed as
  5. # we expect.
  6. #
  7. # Valid configurations are tested with --dump-config, which parses and
  8. # validates the configuration before writing it out. We then make sure that
  9. # the result is what we expect, before parsing and dumping it again to make
  10. # sure that there is no change.
  11. #
  12. # Invalid configurations are tested with --verify-config, which parses
  13. # and validates the configuration. We capture its output and make sure that
  14. # it contains the error message we expect.
  15. # This script looks for its test cases as individual directories in
  16. # src/test/conf_examples/. Each test may have these files:
  17. #
  18. # torrc -- Usually needed. This file is passed to Tor on the command line
  19. # with the "-f" flag. (If you omit it, you'll test Tor's behavior when
  20. # it receives a nonexistent configuration file.)
  21. #
  22. # torrc.defaults -- Optional. If present, it is passed to Tor on the command
  23. # line with the --defaults-torrc option. If this file is absent, an empty
  24. # file is passed instead to prevent Tor from reading the system defaults.
  25. #
  26. # cmdline -- Optional. If present, it contains command-line arguments that
  27. # will be passed to Tor.
  28. #
  29. # expected -- If this file is present, then it should be the expected result
  30. # of "--dump-config short" for this test case. Exactly one of
  31. # "expected" or "error" must be present, or the test will fail.
  32. #
  33. # error -- If this file is present, then it contains a regex that must be
  34. # matched by some line in the output of "--verify-config", which must
  35. # fail. Exactly one of "expected" or "error" must be present, or the
  36. # test will fail.
  37. umask 077
  38. set -e
  39. # emulate realpath(), in case coreutils or equivalent is not installed.
  40. abspath() {
  41. f="$*"
  42. if [ -d "$f" ]; then
  43. dir="$f"
  44. base=""
  45. else
  46. dir="$(dirname "$f")"
  47. base="/$(basename "$f")"
  48. fi
  49. dir="$(cd "$dir" && pwd)"
  50. echo "$dir$base"
  51. }
  52. # find the tor binary
  53. if [ $# -ge 1 ]; then
  54. TOR_BINARY="${1}"
  55. shift
  56. else
  57. TOR_BINARY="${TESTING_TOR_BINARY:-./src/app/tor}"
  58. fi
  59. TOR_BINARY="$(abspath "$TOR_BINARY")"
  60. # make a safe space for temporary files
  61. DATA_DIR=$(mktemp -d -t tor_parseconf_tests.XXXXXX)
  62. trap 'rm -rf "$DATA_DIR"' 0
  63. # This is where we look for examples
  64. EXAMPLEDIR="$(dirname "$0")"/conf_examples
  65. case "$(uname -s)" in
  66. CYGWIN*) WINDOWS=1;;
  67. MINGW*) WINDOWS=1;;
  68. MSYS*) WINDOWS=1;;
  69. *) WINDOWS=0;;
  70. esac
  71. ####
  72. # BUG WORKAROUND FOR 31757:
  73. # On Appveyor, it seems that Tor sometimes randomly fails to produce
  74. # output with --dump-config. Whil we are figuring this out, do not treat
  75. # windows errors as hard failures.
  76. ####
  77. if test "$WINDOWS" = 1; then
  78. EXITCODE=0
  79. else
  80. EXITCODE=1
  81. fi
  82. die() { echo "$1" >&2 ; exit "$EXITCODE"; }
  83. if test "$WINDOWS" = 1; then
  84. FILTER="dos2unix"
  85. else
  86. FILTER="cat"
  87. fi
  88. touch "${DATA_DIR}/EMPTY" || die "Couldn't create empty file."
  89. for dir in "${EXAMPLEDIR}"/*; do
  90. if ! test -d "${dir}"; then
  91. # Only count directories.
  92. continue
  93. fi
  94. testname="$(basename "${dir}")"
  95. # We use printf since "echo -n" is not standard
  96. printf "%s: " "$testname"
  97. PREV_DIR="$(pwd)"
  98. cd "${dir}"
  99. if test -f "./torrc.defaults"; then
  100. DEFAULTS="./torrc.defaults"
  101. else
  102. DEFAULTS="${DATA_DIR}/EMPTY"
  103. fi
  104. if test -f "./cmdline"; then
  105. CMDLINE="$(cat ./cmdline)"
  106. else
  107. CMDLINE=""
  108. fi
  109. if test -f "./expected"; then
  110. if test -f "./error"; then
  111. echo "FAIL: Found both ${dir}/expected and ${dir}/error."
  112. echo "(Only one of these files should exist.)"
  113. exit $EXITCODE
  114. fi
  115. # This case should succeed: run dump-config and see if it does.
  116. "${TOR_BINARY}" -f "./torrc" \
  117. --defaults-torrc "${DEFAULTS}" \
  118. --dump-config short \
  119. ${CMDLINE} \
  120. | "${FILTER}" > "${DATA_DIR}/output.${testname}" \
  121. || die "Failure: Tor exited."
  122. if cmp "./expected" "${DATA_DIR}/output.${testname}">/dev/null ; then
  123. # Check round-trip.
  124. "${TOR_BINARY}" -f "${DATA_DIR}/output.${testname}" \
  125. --defaults-torrc "${DATA_DIR}/empty" \
  126. --dump-config short \
  127. | "${FILTER}" \
  128. > "${DATA_DIR}/output_2.${testname}" \
  129. || die "Failure: Tor exited on round-trip."
  130. if ! cmp "${DATA_DIR}/output.${testname}" \
  131. "${DATA_DIR}/output_2.${testname}"; then
  132. echo "Failure: did not match on round-trip."
  133. exit $EXITCODE
  134. fi
  135. echo "OK"
  136. else
  137. echo "FAIL"
  138. if test "$(wc -c < "${DATA_DIR}/output.${testname}")" = 0; then
  139. # There was no output -- probably we failed.
  140. "${TOR_BINARY}" -f "./torrc" \
  141. --defaults-torrc "${DEFAULTS}" \
  142. --verify-config \
  143. ${CMDLINE} || true
  144. fi
  145. diff -u "./expected" "${DATA_DIR}/output.${testname}" || /bin/true
  146. exit $EXITCODE
  147. fi
  148. elif test -f "./error"; then
  149. # This case should fail: run verify-config and see if it does.
  150. "${TOR_BINARY}" --verify-config \
  151. -f ./torrc \
  152. --defaults-torrc "${DEFAULTS}" \
  153. ${CMDLINE} \
  154. > "${DATA_DIR}/output.${testname}" \
  155. && die "Failure: Tor did not report an error."
  156. expect_err="$(cat ./error)"
  157. if grep "${expect_err}" "${DATA_DIR}/output.${testname}" >/dev/null; then
  158. echo "OK"
  159. else
  160. echo "FAIL"
  161. echo "Expected error: ${expect_err}"
  162. echo "Tor said:"
  163. cat "${DATA_DIR}/output.${testname}"
  164. exit $EXITCODE
  165. fi
  166. else
  167. # This case is not actually configured with a success or a failure.
  168. # call that an error.
  169. echo "FAIL: Did not find ${dir}/expected or ${dir}/error."
  170. exit $EXITCODE
  171. fi
  172. cd "${PREV_DIR}"
  173. done