test-network.sh 13 KB

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