test-network.sh 13 KB

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