test-network.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 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. # what we say when we fail
  16. UPDATE_YOUR_CHUTNEY="Please update your chutney using 'git pull'."
  17. until [ -z "$1" ]
  18. do
  19. case "$1" in
  20. # the path to the chutney directory
  21. --chutney-path)
  22. export CHUTNEY_PATH="$2"
  23. shift
  24. ;;
  25. --tor-path)
  26. # the path of a tor build directory
  27. # --tor-path overrides --tor and --tor-gencert
  28. export TOR_DIR="$2"
  29. shift
  30. ;;
  31. --tor)
  32. # the name or path of a tor binary
  33. export CHUTNEY_TOR="$2"
  34. shift
  35. ;;
  36. --tor-gencert)
  37. # the name or path of a tor-gencert binary
  38. export CHUTNEY_TOR_GENCERT="$2"
  39. shift
  40. ;;
  41. --debug)
  42. export CHUTEY_DEBUG="yes"
  43. ;;
  44. --flavor|--flavour|--network-flavor|--network-flavour)
  45. export NETWORK_FLAVOUR="$2"
  46. shift
  47. ;;
  48. # The amount of time chutney will wait before starting to verify
  49. # If negative, chutney exits straight after launching the network
  50. --start-time)
  51. export CHUTNEY_START_TIME="$2"
  52. shift
  53. ;;
  54. # The amount of time chutney will try to verify, before failing
  55. # If negative, chutney exits without verifying
  56. --delay|--sleep|--bootstrap-time|--time|--verify-time)
  57. # This isn't the best name for this variable, but we kept it the same
  58. # for backwards compatibility
  59. export CHUTNEY_BOOTSTRAP_TIME="$2"
  60. shift
  61. ;;
  62. # The amount of time chutney will wait after successfully verifying
  63. # If negative, chutney exits without stopping
  64. --stop-time)
  65. export CHUTNEY_STOP_TIME="$2"
  66. shift
  67. ;;
  68. # If all of the CHUTNEY_*_TIME options are positive, chutney will ask tor
  69. # to exit when this PID exits. Set to 1 or lower to disable.
  70. --controlling-pid)
  71. export CHUTNEY_CONTROLLING_PID="$2"
  72. shift
  73. ;;
  74. # Environmental variables used by chutney verify performance tests
  75. # Send this many bytes per client connection (10 KBytes)
  76. --data|--data-bytes|--data-byte|--bytes|--byte)
  77. export CHUTNEY_DATA_BYTES="$2"
  78. shift
  79. ;;
  80. # Make this many simultaneous connections per client (1)
  81. # Note: If you create 7 or more connections to a hidden service from
  82. # a single Tor 0.2.7 client, you'll likely get a verification failure due
  83. # to #15937. This is fixed in 0.2.8.
  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 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. # Try not to say anything (applies only to this script)
  156. --quiet)
  157. export ECHO=true
  158. ;;
  159. # Oops
  160. *)
  161. $ECHO "$myname: Sorry, I don't know what to do with '$1'."
  162. $ECHO "$UPDATE_YOUR_CHUTNEY"
  163. # continue processing arguments during a dry run
  164. if [ "$NETWORK_DRY_RUN" != true ]; then
  165. exit 2
  166. fi
  167. ;;
  168. esac
  169. shift
  170. done
  171. # If the DNS server doesn't work, tor exits may reject all exit traffic, and
  172. # chutney may fail
  173. if [ "$CHUTNEY_WARNINGS_ONLY" != true ]; then
  174. $ECHO "$myname: using CHUTNEY_DNS_CONF '$CHUTNEY_DNS_CONF'"
  175. fi
  176. # optional: $TOR_DIR is the tor build directory
  177. # it's used to find the location of tor binaries
  178. # if it's not set:
  179. # - set it to $BUILDDIR, or
  180. # - if $PWD looks like a tor build directory, set it to $PWD, or
  181. # - unset $TOR_DIR, and let chutney fall back to finding tor binaries in $PATH
  182. if [ ! -d "$TOR_DIR" ]; then
  183. if [ -d "$BUILDDIR/src/or" -a -d "$BUILDDIR/src/tools" ]; then
  184. # Choose the build directory
  185. # But only if it looks like one
  186. $ECHO "$myname: \$TOR_DIR not set, trying \$BUILDDIR"
  187. export TOR_DIR="$BUILDDIR"
  188. elif [ -d "$PWD/src/or" -a -d "$PWD/src/tools" ]; then
  189. # Guess the tor directory is the current directory
  190. # But only if it looks like one
  191. $ECHO "$myname: \$TOR_DIR not set, trying \$PWD"
  192. export TOR_DIR="$PWD"
  193. elif [ -d "$PWD/../tor" -a -d "$PWD/../tor/src/or" -a \
  194. -d "$PWD/../tor/src/tools" ]; then
  195. # Guess the tor directory is next to the current directory
  196. # But only if it looks like one
  197. $ECHO "$myname: \$TOR_DIR not set, trying \$PWD/../tor"
  198. export TOR_DIR="$PWD/../tor"
  199. else
  200. $ECHO "$myname: no \$TOR_DIR, chutney will use \$PATH for tor binaries"
  201. unset TOR_DIR
  202. fi
  203. fi
  204. # make TOR_DIR absolute
  205. if [ -d "$PWD/$TOR_DIR" -a -d "$PWD/$TOR_DIR/src/or" -a \
  206. -d "$PWD/$TOR_DIR/src/tools" ]; then
  207. export TOR_DIR="$PWD/$TOR_DIR"
  208. fi
  209. # mandatory: $CHUTNEY_PATH is the path to the chutney launch script
  210. # if it's not set:
  211. # - if $PWD looks like a chutney directory, set it to $PWD, or
  212. # - set it based on $TOR_DIR, expecting chutney to be next to tor, or
  213. # - fail and tell the user how to clone the chutney repository
  214. if [ ! -d "$CHUTNEY_PATH" -o ! -x "$CHUTNEY_PATH/chutney" -o \
  215. ! -f "$CHUTNEY_PATH/chutney" ]; then
  216. if [ -x "$PWD/chutney" -a -f "$PWD/chutney" ]; then
  217. $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$PWD"
  218. export CHUTNEY_PATH="$PWD"
  219. elif [ -d "`dirname \"$0\"`/.." -a \
  220. -x "`dirname \"$0\"`/../chutney" -a \
  221. -f "`dirname \"$0\"`/../chutney" ]; then
  222. $ECHO "$myname: \$CHUTNEY_PATH not valid, using this script's location"
  223. export CHUTNEY_PATH="`dirname \"$0\"`/.."
  224. elif [ -d "$TOR_DIR" -a -d "$TOR_DIR/../chutney" -a \
  225. -x "$TOR_DIR/../chutney/chutney" -a \
  226. -f "$TOR_DIR/../chutney/chutney" ]; then
  227. $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$TOR_DIR/../chutney"
  228. export CHUTNEY_PATH="$TOR_DIR/../chutney"
  229. else
  230. # TODO: work out how to package and install chutney,
  231. # so users can find it in $PATH
  232. $ECHO "$myname: missing 'chutney' in \$CHUTNEY_PATH ($CHUTNEY_PATH)"
  233. $ECHO "$myname: Get chutney: git clone https://git.torproject.org/\
  234. chutney.git"
  235. $ECHO "$myname: Set \$CHUTNEY_PATH to a non-standard location: export \
  236. CHUTNEY_PATH=\`pwd\`/chutney"
  237. unset CHUTNEY_PATH
  238. exit 1
  239. fi
  240. fi
  241. # make chutney path absolute
  242. if [ -d "$PWD/$CHUTNEY_PATH" -a -x "$PWD/$CHUTNEY_PATH/chutney" ]; then
  243. export CHUTNEY_PATH="$PWD/$CHUTNEY_PATH"
  244. fi
  245. # For picking up the right tor binaries
  246. # Choose coverage binaries, if selected
  247. tor_name=tor
  248. tor_gencert_name=tor-gencert
  249. if [ "$USE_COVERAGE_BINARY" = true ]; then
  250. tor_name=tor-cov
  251. fi
  252. # If $TOR_DIR isn't set, chutney looks for tor binaries by name or path
  253. # using $CHUTNEY_TOR and $CHUTNEY_TOR_GENCERT, and then falls back to
  254. # looking for tor and tor-gencert in $PATH
  255. if [ -d "$TOR_DIR" ]; then
  256. # TOR_DIR is absolute, so these are absolute paths
  257. export CHUTNEY_TOR="${TOR_DIR}/src/or/${tor_name}"
  258. export CHUTNEY_TOR_GENCERT="${TOR_DIR}/src/tools/${tor_gencert_name}"
  259. else
  260. # these are binary names, they will be searched for in $PATH
  261. export CHUTNEY_TOR="${tor_name}"
  262. export CHUTNEY_TOR_GENCERT="${tor_gencert_name}"
  263. fi
  264. # Set the variables for the chutney network flavour
  265. export NETWORK_FLAVOUR=${NETWORK_FLAVOUR:-"bridges+hs"}
  266. export CHUTNEY_NETWORK="$CHUTNEY_PATH/networks/$NETWORK_FLAVOUR"
  267. if [ "$CHUTNEY_WARNINGS_SKIP" = true ]; then
  268. WARNINGS=true
  269. else
  270. WARNINGS="$CHUTNEY_PATH/tools/warnings.sh"
  271. fi
  272. # And finish up if we're doing a dry run
  273. if [ "$NETWORK_DRY_RUN" = true -o "$CHUTNEY_WARNINGS_ONLY" = true ]; then
  274. if [ "$CHUTNEY_WARNINGS_ONLY" = true ]; then
  275. "$WARNINGS"
  276. fi
  277. # This breaks sourcing this script: that is intentional, as the previous
  278. # behaviour only worked with bash as /bin/sh
  279. exit
  280. fi
  281. "$CHUTNEY_PATH/tools/bootstrap-network.sh" "$NETWORK_FLAVOUR" || exit 3
  282. # chutney starts verifying after 20 seconds, keeps on trying for 60 seconds,
  283. # and then stops immediately (by default)
  284. # Even the fastest chutney networks take 5-10 seconds for their first consensus
  285. # and then 10 seconds after that for relays to bootstrap and upload descriptors
  286. export CHUTNEY_START_TIME=${CHUTNEY_START_TIME:-20}
  287. export CHUTNEY_BOOTSTRAP_TIME=${CHUTNEY_BOOTSTRAP_TIME:-60}
  288. export CHUTNEY_STOP_TIME=${CHUTNEY_STOP_TIME:-0}
  289. CHUTNEY="$CHUTNEY_PATH/chutney"
  290. if [ "$CHUTNEY_START_TIME" -ge 0 ]; then
  291. $ECHO "Waiting ${CHUTNEY_START_TIME} seconds for a consensus containing relays to be generated..."
  292. sleep "$CHUTNEY_START_TIME"
  293. else
  294. $ECHO "Chutney network launched and running. To stop the network, use:"
  295. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  296. "$WARNINGS"
  297. exit 0
  298. fi
  299. if [ "$CHUTNEY_BOOTSTRAP_TIME" -ge 0 ]; then
  300. # Chutney will try to verify for $CHUTNEY_BOOTSTRAP_TIME seconds each round
  301. n_rounds=0
  302. VERIFY_EXIT_STATUS=0
  303. # Run CHUTNEY_ROUNDS verification rounds
  304. $ECHO "Running $CHUTNEY_ROUNDS verify rounds..."
  305. while [ "$CHUTNEY_ROUNDS" -gt "$n_rounds" \
  306. -a "$VERIFY_EXIT_STATUS" -eq 0 ]; do
  307. "$CHUTNEY" verify "$CHUTNEY_NETWORK"
  308. VERIFY_EXIT_STATUS="$?"
  309. n_rounds=$((n_rounds+1))
  310. done
  311. $ECHO "Completed $n_rounds of $CHUTNEY_ROUNDS verify rounds."
  312. else
  313. $ECHO "Chutney network ready and running. To stop the network, use:"
  314. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  315. "$WARNINGS"
  316. exit 0
  317. fi
  318. if [ "$CHUTNEY_STOP_TIME" -ge 0 ]; then
  319. if [ "$CHUTNEY_STOP_TIME" -gt 0 ]; then
  320. $ECHO "Waiting ${CHUTNEY_STOP_TIME} seconds before stopping the network..."
  321. fi
  322. sleep "$CHUTNEY_STOP_TIME"
  323. # work around a bug/feature in make -j2 (or more)
  324. # where make hangs if any child processes are still alive
  325. "$CHUTNEY" stop "$CHUTNEY_NETWORK"
  326. # Give tor time to exit gracefully
  327. sleep 3
  328. "$WARNINGS"
  329. exit "$VERIFY_EXIT_STATUS"
  330. else
  331. $ECHO "Chutney network verified and running. To stop the network, use:"
  332. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  333. "$WARNINGS"
  334. exit 0
  335. fi