test_parseconf.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. die() { echo "$1" >&2 ; exit 5; }
  40. # emulate realpath(), in case coreutils or equivalent is not installed.
  41. abspath() {
  42. f=$@
  43. if [ -d "$f" ]; then
  44. dir="$f"
  45. base=""
  46. else
  47. dir="$(dirname "$f")"
  48. base="/$(basename "$f")"
  49. fi
  50. dir="$(cd "$dir" && pwd)"
  51. echo "$dir$base"
  52. }
  53. # find the tor binary
  54. if [ $# -ge 1 ]; then
  55. TOR_BINARY="${1}"
  56. shift
  57. else
  58. TOR_BINARY="${TESTING_TOR_BINARY:-./src/app/tor}"
  59. fi
  60. TOR_BINARY="$(abspath "$TOR_BINARY")"
  61. # make a safe space for temporary files
  62. DATA_DIR=$(mktemp -d -t tor_parseconf_tests.XXXXXX)
  63. trap 'rm -rf "$DATA_DIR"' 0
  64. touch "${DATA_DIR}/EMPTY" || die "Couldn't create empty file."
  65. # This is where we look for examples
  66. EXAMPLEDIR="$(dirname "$0")"/conf_examples
  67. case "$(uname -s)" in
  68. CYGWIN*) WINDOWS=1;;
  69. MINGW*) WINDOWS=1;;
  70. MSYS*) WINDOWS=1;;
  71. *) WINDOWS=0;;
  72. esac
  73. if test "$WINDOWS" = 1; then
  74. FILTER="dos2unix"
  75. else
  76. FILTER="cat"
  77. fi
  78. for dir in "${EXAMPLEDIR}"/*; do
  79. if ! test -d "${dir}"; then
  80. # Only count directories.
  81. continue
  82. fi
  83. testname="$(basename "${dir}")"
  84. # We use printf since "echo -n" is not standard
  85. printf "%s: " "$testname"
  86. PREV_DIR="$(pwd)"
  87. cd "${dir}"
  88. if test -f "./torrc.defaults"; then
  89. DEFAULTS="./torrc.defaults"
  90. else
  91. DEFAULTS="${DATA_DIR}/EMPTY"
  92. fi
  93. if test -f "./cmdline"; then
  94. CMDLINE="$(cat ./cmdline)"
  95. else
  96. CMDLINE=""
  97. fi
  98. if test -f "./expected"; then
  99. if test -f "./error"; then
  100. echo "FAIL: Found both ${dir}/expected and ${dir}/error."
  101. echo "(Only one of these files should exist.)"
  102. exit 1
  103. fi
  104. # This case should succeed: run dump-config and see if it does.
  105. "${TOR_BINARY}" -f "./torrc" \
  106. --defaults-torrc "${DEFAULTS}" \
  107. --dump-config short \
  108. ${CMDLINE} \
  109. | "${FILTER}" > "${DATA_DIR}/output.${testname}" \
  110. || die "Failure: Tor exited."
  111. if cmp "./expected" "${DATA_DIR}/output.${testname}">/dev/null ; then
  112. # Check round-trip.
  113. "${TOR_BINARY}" -f "${DATA_DIR}/output.${testname}" \
  114. --defaults-torrc "${DATA_DIR}/empty" \
  115. --dump-config short \
  116. | "${FILTER}" \
  117. > "${DATA_DIR}/output_2.${testname}" \
  118. || die "Failure: Tor exited on round-trip."
  119. if ! cmp "${DATA_DIR}/output.${testname}" \
  120. "${DATA_DIR}/output_2.${testname}"; then
  121. echo "Failure: did not match on round-trip."
  122. exit 1
  123. fi
  124. echo "OK"
  125. else
  126. echo "FAIL"
  127. if test "$(wc -c < "${DATA_DIR}/output.${testname}")" = 0; then
  128. # There was no output -- probably we failed.
  129. "${TOR_BINARY}" -f "./torrc" \
  130. --defaults-torrc "${DEFAULTS}" \
  131. --verify-config \
  132. ${CMDLINE} || true
  133. fi
  134. diff -u "./expected" "${DATA_DIR}/output.${testname}"
  135. exit 1
  136. fi
  137. elif test -f "./error"; then
  138. # This case should fail: run verify-config and see if it does.
  139. "${TOR_BINARY}" --verify-config \
  140. -f ./torrc \
  141. --defaults-torrc "${DEFAULTS}" \
  142. ${CMDLINE} \
  143. > "${DATA_DIR}/output.${testname}" \
  144. && die "Failure: Tor did not report an error."
  145. expect_err="$(cat ./error)"
  146. if grep "${expect_err}" "${DATA_DIR}/output.${testname}" >/dev/null; then
  147. echo "OK"
  148. else
  149. echo "FAIL"
  150. echo "Expected error: ${expect_err}"
  151. echo "Tor said:"
  152. cat "${DATA_DIR}/output.${testname}"
  153. exit 1
  154. fi
  155. else
  156. # This case is not actually configured with a success or a failure.
  157. # call that an error.
  158. echo "FAIL: Did not find ${dir}/expected or ${dir}/error."
  159. exit 1
  160. fi
  161. cd "${PREV_DIR}"
  162. done