test-network.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #!/bin/sh
  2. export ECHO="${ECHO:-echo}"
  3. # Output is prefixed with the name of the script
  4. myname=$(basename "$0")
  5. # default to one round
  6. export CHUTNEY_ROUNDS=${CHUTNEY_ROUNDS:-1}
  7. # default to summarising unexpected warnings
  8. export CHUTNEY_WARNINGS_IGNORE_EXPECTED=${CHUTNEY_WARNINGS_IGNORE_EXPECTED:-true}
  9. export CHUTNEY_WARNINGS_SUMMARY=${CHUTNEY_WARNINGS_SUMMARY:-true}
  10. # default to exiting when this script exits
  11. export CHUTNEY_CONTROLLING_PID=${CHUTNEY_CONTROLLING_PID:-$$}
  12. # default to allowing zero failures
  13. export CHUTNEY_ALLOW_FAILURES=${CHUTNEY_ALLOW_FAILURES:-0}
  14. # default to no DNS: this is a safe, working default for most users
  15. # If a custom test expects DNS, it needs to set CHUTNEY_DNS_CONF
  16. export CHUTNEY_DNS_CONF=${CHUTNEY_DNS_CONF:-/dev/null}
  17. # what we say when we fail
  18. UPDATE_YOUR_CHUTNEY="Please update your chutney using 'git pull'."
  19. until [ -z "$1" ]
  20. do
  21. case "$1" in
  22. # the path to the chutney directory
  23. --chutney-path)
  24. export CHUTNEY_PATH="$2"
  25. shift
  26. ;;
  27. --tor-path)
  28. # the path of a tor build directory
  29. # --tor-path overrides --tor and --tor-gencert
  30. export TOR_DIR="$2"
  31. shift
  32. ;;
  33. --tor)
  34. # the name or path of a tor binary
  35. export CHUTNEY_TOR="$2"
  36. shift
  37. ;;
  38. --tor-gencert)
  39. # the name or path of a tor-gencert binary
  40. export CHUTNEY_TOR_GENCERT="$2"
  41. shift
  42. ;;
  43. --debug)
  44. export CHUTNEY_DEBUG="yes"
  45. ;;
  46. --flavor|--flavour|--network-flavor|--network-flavour)
  47. export NETWORK_FLAVOUR="$2"
  48. shift
  49. ;;
  50. # The amount of time chutney will wait before starting to verify
  51. # If negative, chutney exits straight after launching the network
  52. --start-time)
  53. export CHUTNEY_START_TIME="$2"
  54. shift
  55. ;;
  56. # The amount of time chutney will try to verify, before failing
  57. # If negative, chutney exits without verifying
  58. --delay|--sleep|--bootstrap-time|--time|--verify-time)
  59. # This isn't the best name for this variable, but we kept it the
  60. # same for backwards compatibility
  61. export CHUTNEY_BOOTSTRAP_TIME="$2"
  62. shift
  63. ;;
  64. # The amount of time chutney will wait after successfully verifying
  65. # If negative, chutney exits without stopping
  66. --stop-time)
  67. export CHUTNEY_STOP_TIME="$2"
  68. shift
  69. ;;
  70. # If all of the CHUTNEY_*_TIME options are positive, chutney will ask
  71. # tor to exit when this PID exits. Set to 1 or lower to disable.
  72. --controlling-pid)
  73. export CHUTNEY_CONTROLLING_PID="$2"
  74. shift
  75. ;;
  76. # Environmental variables used by chutney verify performance tests
  77. # Send this many bytes per client connection (10 KBytes)
  78. --data|--data-bytes|--data-byte|--bytes|--byte)
  79. export CHUTNEY_DATA_BYTES="$2"
  80. shift
  81. ;;
  82. # Make this many simultaneous connections per client (1)
  83. --connections|--connection|--connection-count|--count)
  84. export CHUTNEY_CONNECTIONS="$2"
  85. shift
  86. ;;
  87. # Run this many verification rounds (1)
  88. --rounds)
  89. export CHUTNEY_ROUNDS="$2"
  90. shift
  91. ;;
  92. # Make each client connect to each HS (0)
  93. # 0 means a single client connects to each HS
  94. # 1 means every client connects to every HS
  95. --hs-multi-client|--hs-multi-clients|--hs-client|--hs-clients)
  96. export CHUTNEY_HS_MULTI_CLIENT="$2"
  97. shift
  98. ;;
  99. # The IPv4 address to bind to, defaults to 127.0.0.1
  100. --ipv4|--v4|-4|--ip)
  101. export CHUTNEY_LISTEN_ADDRESS="$2"
  102. shift
  103. ;;
  104. # The IPv6 address to bind to, default is not to bind to an
  105. # IPv6 address
  106. --ipv6|--v6|-6)
  107. export CHUTNEY_LISTEN_ADDRESS_V6="$2"
  108. shift
  109. ;;
  110. # The DNS server config for Tor Exits. Chutney's default is
  111. # /etc/resolv.conf, even if tor's compile time default is different.
  112. --dns-conf)
  113. export CHUTNEY_DNS_CONF="$2"
  114. shift
  115. ;;
  116. # Do not make any DNS queries. This is incompatible with external
  117. # controllers that use SETCONF.
  118. --offline)
  119. export CHUTNEY_DNS_CONF="/dev/null"
  120. ;;
  121. # Use tor's compile-time default for ServerDNSResolvConfFile.
  122. --dns-conf-default)
  123. export CHUTNEY_DNS_CONF=""
  124. ;;
  125. # Warning Options
  126. # we summarise unexpected warnings by default
  127. # this shows all warnings per-node
  128. --all-warnings)
  129. export CHUTNEY_WARNINGS_IGNORE_EXPECTED=false
  130. export CHUTNEY_WARNINGS_SUMMARY=false
  131. ;;
  132. # this doesn't run chutney, and only logs warnings
  133. --only-warnings)
  134. export CHUTNEY_WARNINGS_ONLY=true
  135. ;;
  136. # this skips warnings entirely
  137. --no-warnings)
  138. export CHUTNEY_WARNINGS_SKIP=true
  139. ;;
  140. # Expert options
  141. # Code Coverage Binary
  142. --coverage)
  143. export USE_COVERAGE_BINARY=true
  144. ;;
  145. # Do Nothing (but process arguments and set environmental variables)
  146. --dry-run)
  147. # process arguments, but don't call any other scripts
  148. export NETWORK_DRY_RUN=true
  149. ;;
  150. # The net directory, usually chutney/net
  151. --net-dir)
  152. export CHUTNEY_DATA_DIR="$2"
  153. shift
  154. ;;
  155. # How many failures should we allow? Defaults to 0.
  156. --allow-failures)
  157. export CHUTNEY_ALLOW_FAILURES="$2"
  158. shift
  159. ;;
  160. # Try not to say anything (applies only to this script)
  161. --quiet)
  162. export ECHO=true
  163. ;;
  164. # Oops
  165. *)
  166. $ECHO "$myname: Sorry, I don't know what to do with '$1'."
  167. $ECHO "$UPDATE_YOUR_CHUTNEY"
  168. # continue processing arguments during a dry run
  169. if [ "$NETWORK_DRY_RUN" != true ]; then
  170. exit 1
  171. fi
  172. ;;
  173. esac
  174. shift
  175. done
  176. # If the DNS server doesn't work, tor exits may reject all exit traffic, and
  177. # chutney may fail
  178. if [ "$CHUTNEY_WARNINGS_ONLY" != true ]; then
  179. $ECHO "$myname: using CHUTNEY_DNS_CONF '$CHUTNEY_DNS_CONF'"
  180. fi
  181. # optional: $TOR_DIR is the tor build directory
  182. # it's used to find the location of tor binaries
  183. # if it's not set:
  184. # - set it to $BUILDDIR, or
  185. # - if $PWD looks like a tor build directory, set it to $PWD, or
  186. # - unset $TOR_DIR, and let chutney fall back to finding tor binaries in
  187. # $CHUTNEY_TOR and $CHUTNEY_TOR_GENCERT, or $PATH
  188. #
  189. # Find the Tor build dir using the src/tools dir
  190. if [ ! -d "$TOR_DIR" ]; then
  191. if [ -d "$BUILDDIR/src/tools" ]; then
  192. # Choose the build directory
  193. # But only if it looks like one
  194. $ECHO "$myname: \$TOR_DIR not set, trying \$BUILDDIR"
  195. export TOR_DIR="$BUILDDIR"
  196. elif [ -d "$PWD/src/tools" ]; then
  197. # Guess the tor directory is the current directory
  198. # But only if it looks like one
  199. $ECHO "$myname: \$TOR_DIR not set, trying \$PWD"
  200. export TOR_DIR="$PWD"
  201. elif [ -d "$PWD/../tor" ] && [ -d "$PWD/../tor/src/tools" ]; then
  202. # Guess the tor directory is next to the current directory
  203. # But only if it looks like one
  204. $ECHO "$myname: \$TOR_DIR not set, trying \$PWD/../tor"
  205. export TOR_DIR="$PWD/../tor"
  206. else
  207. $ECHO "$myname: no \$TOR_DIR, chutney will use \$CHUTNEY_TOR and \$CHUTNEY_TOR_GENCERT as tor binary paths, or search \$PATH for tor binary names"
  208. unset TOR_DIR
  209. fi
  210. fi
  211. # Now find the name of the Tor app dir, which changed in Tor 0.3.5
  212. if [ -d "$TOR_DIR" ]; then
  213. if [ -d "$TOR_DIR/src/app" ] && [ -d "$TOR_DIR/src/or" ]; then
  214. $ECHO "$myname: \$TOR_DIR has a Tor 0.3.5 or later build directory, and a Tor 0.3.4 or earlier build directory"
  215. $ECHO "$myname: Please remove $TOR_DIR/src/app or $TOR_DIR/src/or, or set \$CHUTNEY_TOR"
  216. exit 1
  217. elif [ -d "$TOR_DIR/src/app" ]; then
  218. $ECHO "$myname: \$TOR_DIR is a Tor 0.3.5 or later build directory"
  219. TOR_APP_DIR="$TOR_DIR/src/app"
  220. elif [ -d "$TOR_DIR/src/or" ]; then
  221. $ECHO "$myname: \$TOR_DIR is a Tor 0.3.4 or earlier build directory"
  222. TOR_APP_DIR="$TOR_DIR/src/or"
  223. else
  224. $ECHO "$myname: \$TOR_DIR has no src/app or src/or, looking elsewhere"
  225. unset TOR_DIR
  226. fi
  227. fi
  228. # make TOR_DIR and TOR_APP_DIR absolute
  229. if [ -d "$PWD/$TOR_DIR" ] && [ -d "$PWD/$TOR_APP_DIR" ] && \
  230. [ -d "$PWD/$TOR_DIR/src/tools" ]; then
  231. export TOR_DIR="$PWD/$TOR_DIR"
  232. export TOR_APP_DIR="$PWD/$TOR_APP_DIR"
  233. fi
  234. TOOLS_DIR=$(dirname "$0")
  235. # mandatory: $CHUTNEY_PATH is the path to the chutney launch script
  236. # if it's not set:
  237. # - if $PWD looks like a chutney directory, set it to $PWD, or
  238. # - set it based on $TOR_DIR, expecting chutney to be next to tor, or
  239. # - fail and tell the user how to clone the chutney repository
  240. if [ ! -d "$CHUTNEY_PATH" ] || [ ! -x "$CHUTNEY_PATH/chutney" ] || \
  241. [ ! -f "$CHUTNEY_PATH/chutney" ]; then
  242. if [ -x "$PWD/chutney" ] && [ -f "$PWD/chutney" ]; then
  243. $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$PWD"
  244. export CHUTNEY_PATH="$PWD"
  245. elif [ -d "$TOOLS_DIR/.." ] && \
  246. [ -x "$TOOLS_DIR)/../chutney" ] && \
  247. [ -f "$TOOLS_DIR/../chutney" ]; then
  248. $ECHO "$myname: \$CHUTNEY_PATH not valid, using this script's location"
  249. export CHUTNEY_PATH="$TOOLS_DIR/.."
  250. elif [ -d "$TOR_DIR" ] && \
  251. [ -d "$TOR_DIR/../chutney" ] && \
  252. [ -x "$TOR_DIR/../chutney/chutney" ] && \
  253. [ -f "$TOR_DIR/../chutney/chutney" ]; then
  254. $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$TOR_DIR/../chutney"
  255. export CHUTNEY_PATH="$TOR_DIR/../chutney"
  256. else
  257. # TODO: work out how to package and install chutney,
  258. # so users can find it in $PATH
  259. $ECHO "$myname: missing 'chutney' in \$CHUTNEY_PATH ($CHUTNEY_PATH)"
  260. $ECHO "$myname: Get chutney: git clone https://git.torproject.org/chutney.git"
  261. $ECHO "$myname: Set \$CHUTNEY_PATH to a non-standard location: export CHUTNEY_PATH=\`pwd\`/chutney"
  262. unset CHUTNEY_PATH
  263. exit 1
  264. fi
  265. fi
  266. # make chutney path absolute
  267. if [ -d "$PWD/$CHUTNEY_PATH" ] && [ -x "$PWD/$CHUTNEY_PATH/chutney" ]; then
  268. export CHUTNEY_PATH="$PWD/$CHUTNEY_PATH"
  269. fi
  270. # For picking up the right tor binaries
  271. # Choose coverage binaries, if selected
  272. tor_name=tor
  273. tor_gencert_name=tor-gencert
  274. if [ "$USE_COVERAGE_BINARY" = true ]; then
  275. tor_name=tor-cov
  276. fi
  277. # If $TOR_DIR isn't set, chutney looks for tor binaries by name or path
  278. # using $CHUTNEY_TOR and $CHUTNEY_TOR_GENCERT, and then falls back to
  279. # looking for tor and tor-gencert in $PATH
  280. if [ -d "$TOR_DIR" ]; then
  281. $ECHO "$myname: Setting \$CHUTNEY_TOR and \$CHUTNEY_TOR_GENCERT based on TOR_DIR: '$TOR_DIR'"
  282. # TOR_DIR is absolute, so these are absolute paths
  283. export CHUTNEY_TOR="$TOR_APP_DIR/$tor_name"
  284. export CHUTNEY_TOR_GENCERT="$TOR_DIR/src/tools/$tor_gencert_name"
  285. else
  286. if [ -x "$CHUTNEY_TOR" ]; then
  287. $ECHO "$myname: Assuming \$CHUTNEY_TOR is a path to a binary"
  288. elif [ -n "$CHUTNEY_TOR" ]; then
  289. $ECHO "$myname: Assuming \$CHUTNEY_TOR is a binary name in \$PATH"
  290. else
  291. $ECHO "$myname: Setting \$CHUTNEY_TOR to the standard binary name in \$PATH"
  292. export CHUTNEY_TOR="$tor_name"
  293. fi
  294. if [ -x "$CHUTNEY_TOR_GENCERT" ]; then
  295. $ECHO "$myname: Assuming \$CHUTNEY_TOR_GENCERT is a path to a binary"
  296. elif [ -n "$CHUTNEY_TOR_GENCERT" ]; then
  297. $ECHO "$myname: Assuming \$CHUTNEY_TOR_GENCERT is a binary name in \$PATH"
  298. else
  299. $ECHO "$myname: Setting \$CHUTNEY_TOR_GENCERT to the standard binary name in \$PATH"
  300. export CHUTNEY_TOR_GENCERT="$tor_gencert_name"
  301. fi
  302. fi
  303. $ECHO "$myname: Using \$CHUTNEY_TOR: '$CHUTNEY_TOR' and \$CHUTNEY_TOR_GENCERT: '$CHUTNEY_TOR_GENCERT'"
  304. # Set the variables for the chutney network flavour
  305. export NETWORK_FLAVOUR=${NETWORK_FLAVOUR:-"bridges+hs-v2"}
  306. export CHUTNEY_NETWORK="$CHUTNEY_PATH/networks/$NETWORK_FLAVOUR"
  307. export WARNING_COMMAND="$CHUTNEY_PATH/tools/warnings.sh"
  308. if [ "$CHUTNEY_WARNINGS_SKIP" = true ]; then
  309. export WARNINGS=true
  310. else
  311. export WARNINGS="$WARNING_COMMAND"
  312. fi
  313. # And finish up if we're doing a dry run
  314. if [ "$NETWORK_DRY_RUN" = true ] || [ "$CHUTNEY_WARNINGS_ONLY" = true ]; then
  315. if [ "$CHUTNEY_WARNINGS_ONLY" = true ]; then
  316. "$WARNINGS"
  317. fi
  318. $ECHO "Finished dry run"
  319. # This breaks sourcing this script: that is intentional, as the previous
  320. # behaviour only worked with bash as /bin/sh
  321. exit 0
  322. fi
  323. n_attempts=0
  324. max_attempts=$((CHUTNEY_ALLOW_FAILURES+1))
  325. while [ "$n_attempts" -lt "$max_attempts" ]; do
  326. n_attempts=$((n_attempts+1))
  327. $ECHO "==== Running tests: attempt $n_attempts/$max_attempts"
  328. if "$CHUTNEY_PATH/tools/test-network-impl.sh"; then
  329. $ECHO "==== Chutney succeeded after $n_attempts attempt(s)."
  330. exit 0
  331. fi
  332. if test "$?" = 77; then
  333. exit 77
  334. fi
  335. done
  336. $ECHO "Chutney failed $n_attempts times; we may have a problem here."
  337. exit 1