test-network.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. --connections|--connection|--connection-count|--count)
  82. export CHUTNEY_CONNECTIONS="$2"
  83. shift
  84. ;;
  85. # Run this many verification rounds (1)
  86. --rounds)
  87. export CHUTNEY_ROUNDS="$2"
  88. shift
  89. ;;
  90. # Make each client connect to each HS (0)
  91. # 0 means a single client connects to each HS
  92. # 1 means every client connects to every HS
  93. --hs-multi-client|--hs-multi-clients|--hs-client|--hs-clients)
  94. export CHUTNEY_HS_MULTI_CLIENT="$2"
  95. shift
  96. ;;
  97. # The IPv4 address to bind to, defaults to 127.0.0.1
  98. --ipv4|--v4|-4|--ip)
  99. export CHUTNEY_LISTEN_ADDRESS="$2"
  100. shift
  101. ;;
  102. # The IPv6 address to bind to, default is not to bind to an IPv6 address
  103. --ipv6|--v6|-6)
  104. export CHUTNEY_LISTEN_ADDRESS_V6="$2"
  105. shift
  106. ;;
  107. # The DNS server config for Tor Exits. Chutney's default is
  108. # /etc/resolv.conf, even if tor's compile time default is different.
  109. --dns-conf)
  110. export CHUTNEY_DNS_CONF="$2"
  111. shift
  112. ;;
  113. # Do not make any DNS queries. This is incompatible with external
  114. # controllers that use SETCONF.
  115. --offline)
  116. export CHUTNEY_DNS_CONF="/dev/null"
  117. ;;
  118. # Use tor's compile-time default for ServerDNSResolvConfFile.
  119. --dns-conf-default)
  120. export CHUTNEY_DNS_CONF=""
  121. ;;
  122. # Warning Options
  123. # we summarise unexpected warnings by default
  124. # this shows all warnings per-node
  125. --all-warnings)
  126. export CHUTNEY_WARNINGS_IGNORE_EXPECTED=false
  127. export CHUTNEY_WARNINGS_SUMMARY=false
  128. ;;
  129. # this doesn't run chutney, and only logs warnings
  130. --only-warnings)
  131. export CHUTNEY_WARNINGS_ONLY=true
  132. ;;
  133. # this skips warnings entirely
  134. --no-warnings)
  135. export CHUTNEY_WARNINGS_SKIP=true
  136. ;;
  137. # Expert options
  138. # Code Coverage Binary
  139. --coverage)
  140. export USE_COVERAGE_BINARY=true
  141. ;;
  142. # Do Nothing (but process arguments and set environmental variables)
  143. --dry-run)
  144. # process arguments, but don't call any other scripts
  145. export NETWORK_DRY_RUN=true
  146. ;;
  147. # The net directory, usually chutney/net
  148. --net-dir)
  149. export CHUTNEY_DATA_DIR="$2"
  150. shift
  151. ;;
  152. # Try not to say anything (applies only to this script)
  153. --quiet)
  154. export ECHO=true
  155. ;;
  156. # Oops
  157. *)
  158. $ECHO "$myname: Sorry, I don't know what to do with '$1'."
  159. $ECHO "$UPDATE_YOUR_CHUTNEY"
  160. # continue processing arguments during a dry run
  161. if [ "$NETWORK_DRY_RUN" != true ]; then
  162. exit 2
  163. fi
  164. ;;
  165. esac
  166. shift
  167. done
  168. # If the DNS server doesn't work, tor exits may reject all exit traffic, and
  169. # chutney may fail
  170. if [ "$CHUTNEY_WARNINGS_ONLY" != true ]; then
  171. $ECHO "$myname: using CHUTNEY_DNS_CONF '$CHUTNEY_DNS_CONF'"
  172. fi
  173. # optional: $TOR_DIR is the tor build directory
  174. # it's used to find the location of tor binaries
  175. # if it's not set:
  176. # - set it to $BUILDDIR, or
  177. # - if $PWD looks like a tor build directory, set it to $PWD, or
  178. # - unset $TOR_DIR, and let chutney fall back to finding tor binaries in $PATH
  179. if [ ! -d "$TOR_DIR" ]; then
  180. if [ -d "$BUILDDIR/src/or" -a -d "$BUILDDIR/src/tools" ]; then
  181. # Choose the build directory
  182. # But only if it looks like one
  183. $ECHO "$myname: \$TOR_DIR not set, trying \$BUILDDIR"
  184. export TOR_DIR="$BUILDDIR"
  185. elif [ -d "$PWD/src/or" -a -d "$PWD/src/tools" ]; then
  186. # Guess the tor directory is the current directory
  187. # But only if it looks like one
  188. $ECHO "$myname: \$TOR_DIR not set, trying \$PWD"
  189. export TOR_DIR="$PWD"
  190. elif [ -d "$PWD/../tor" -a -d "$PWD/../tor/src/or" -a \
  191. -d "$PWD/../tor/src/tools" ]; then
  192. # Guess the tor directory is next to the current directory
  193. # But only if it looks like one
  194. $ECHO "$myname: \$TOR_DIR not set, trying \$PWD/../tor"
  195. export TOR_DIR="$PWD/../tor"
  196. else
  197. $ECHO "$myname: no \$TOR_DIR, chutney will use \$PATH for tor binaries"
  198. unset TOR_DIR
  199. fi
  200. fi
  201. # make TOR_DIR absolute
  202. if [ -d "$PWD/$TOR_DIR" -a -d "$PWD/$TOR_DIR/src/or" -a \
  203. -d "$PWD/$TOR_DIR/src/tools" ]; then
  204. export TOR_DIR="$PWD/$TOR_DIR"
  205. fi
  206. # mandatory: $CHUTNEY_PATH is the path to the chutney launch script
  207. # if it's not set:
  208. # - if $PWD looks like a chutney directory, set it to $PWD, or
  209. # - set it based on $TOR_DIR, expecting chutney to be next to tor, or
  210. # - fail and tell the user how to clone the chutney repository
  211. if [ ! -d "$CHUTNEY_PATH" -o ! -x "$CHUTNEY_PATH/chutney" -o \
  212. ! -f "$CHUTNEY_PATH/chutney" ]; then
  213. if [ -x "$PWD/chutney" -a -f "$PWD/chutney" ]; then
  214. $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$PWD"
  215. export CHUTNEY_PATH="$PWD"
  216. elif [ -d "`dirname \"$0\"`/.." -a \
  217. -x "`dirname \"$0\"`/../chutney" -a \
  218. -f "`dirname \"$0\"`/../chutney" ]; then
  219. $ECHO "$myname: \$CHUTNEY_PATH not valid, using this script's location"
  220. export CHUTNEY_PATH="`dirname \"$0\"`/.."
  221. elif [ -d "$TOR_DIR" -a -d "$TOR_DIR/../chutney" -a \
  222. -x "$TOR_DIR/../chutney/chutney" -a \
  223. -f "$TOR_DIR/../chutney/chutney" ]; then
  224. $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$TOR_DIR/../chutney"
  225. export CHUTNEY_PATH="$TOR_DIR/../chutney"
  226. else
  227. # TODO: work out how to package and install chutney,
  228. # so users can find it in $PATH
  229. $ECHO "$myname: missing 'chutney' in \$CHUTNEY_PATH ($CHUTNEY_PATH)"
  230. $ECHO "$myname: Get chutney: git clone https://git.torproject.org/\
  231. chutney.git"
  232. $ECHO "$myname: Set \$CHUTNEY_PATH to a non-standard location: export \
  233. CHUTNEY_PATH=\`pwd\`/chutney"
  234. unset CHUTNEY_PATH
  235. exit 1
  236. fi
  237. fi
  238. # make chutney path absolute
  239. if [ -d "$PWD/$CHUTNEY_PATH" -a -x "$PWD/$CHUTNEY_PATH/chutney" ]; then
  240. export CHUTNEY_PATH="$PWD/$CHUTNEY_PATH"
  241. fi
  242. # For picking up the right tor binaries
  243. # Choose coverage binaries, if selected
  244. tor_name=tor
  245. tor_gencert_name=tor-gencert
  246. if [ "$USE_COVERAGE_BINARY" = true ]; then
  247. tor_name=tor-cov
  248. fi
  249. # If $TOR_DIR isn't set, chutney looks for tor binaries by name or path
  250. # using $CHUTNEY_TOR and $CHUTNEY_TOR_GENCERT, and then falls back to
  251. # looking for tor and tor-gencert in $PATH
  252. if [ -d "$TOR_DIR" ]; then
  253. # TOR_DIR is absolute, so these are absolute paths
  254. export CHUTNEY_TOR="${TOR_DIR}/src/or/${tor_name}"
  255. export CHUTNEY_TOR_GENCERT="${TOR_DIR}/src/tools/${tor_gencert_name}"
  256. else
  257. # these are binary names, they will be searched for in $PATH
  258. export CHUTNEY_TOR="${tor_name}"
  259. export CHUTNEY_TOR_GENCERT="${tor_gencert_name}"
  260. fi
  261. # Set the variables for the chutney network flavour
  262. export NETWORK_FLAVOUR=${NETWORK_FLAVOUR:-"bridges+hs"}
  263. export CHUTNEY_NETWORK="$CHUTNEY_PATH/networks/$NETWORK_FLAVOUR"
  264. if [ "$CHUTNEY_WARNINGS_SKIP" = true ]; then
  265. WARNINGS=true
  266. else
  267. WARNINGS="$CHUTNEY_PATH/tools/warnings.sh"
  268. fi
  269. # And finish up if we're doing a dry run
  270. if [ "$NETWORK_DRY_RUN" = true -o "$CHUTNEY_WARNINGS_ONLY" = true ]; then
  271. if [ "$CHUTNEY_WARNINGS_ONLY" = true ]; then
  272. "$WARNINGS"
  273. fi
  274. # This breaks sourcing this script: that is intentional, as the previous
  275. # behaviour only worked with bash as /bin/sh
  276. exit
  277. fi
  278. "$CHUTNEY_PATH/tools/bootstrap-network.sh" "$NETWORK_FLAVOUR" || exit 3
  279. # chutney starts verifying after 20 seconds, keeps on trying for 60 seconds,
  280. # and then stops immediately (by default)
  281. # Even the fastest chutney networks take 5-10 seconds for their first consensus
  282. # and then 10 seconds after that for relays to bootstrap and upload descriptors
  283. export CHUTNEY_START_TIME=${CHUTNEY_START_TIME:-20}
  284. export CHUTNEY_BOOTSTRAP_TIME=${CHUTNEY_BOOTSTRAP_TIME:-60}
  285. export CHUTNEY_STOP_TIME=${CHUTNEY_STOP_TIME:-0}
  286. CHUTNEY="$CHUTNEY_PATH/chutney"
  287. if [ "$CHUTNEY_START_TIME" -ge 0 ]; then
  288. $ECHO "Waiting ${CHUTNEY_START_TIME} seconds for a consensus containing relays to be generated..."
  289. sleep "$CHUTNEY_START_TIME"
  290. else
  291. $ECHO "Chutney network launched and running. To stop the network, use:"
  292. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  293. "$WARNINGS"
  294. exit 0
  295. fi
  296. if [ "$CHUTNEY_BOOTSTRAP_TIME" -ge 0 ]; then
  297. # Chutney will try to verify for $CHUTNEY_BOOTSTRAP_TIME seconds each round
  298. n_rounds=0
  299. VERIFY_EXIT_STATUS=0
  300. # Run CHUTNEY_ROUNDS verification rounds
  301. $ECHO "Running $CHUTNEY_ROUNDS verify rounds..."
  302. while [ "$CHUTNEY_ROUNDS" -gt "$n_rounds" \
  303. -a "$VERIFY_EXIT_STATUS" -eq 0 ]; do
  304. "$CHUTNEY" verify "$CHUTNEY_NETWORK"
  305. VERIFY_EXIT_STATUS="$?"
  306. n_rounds=$((n_rounds+1))
  307. done
  308. $ECHO "Completed $n_rounds of $CHUTNEY_ROUNDS verify rounds."
  309. else
  310. $ECHO "Chutney network ready and running. To stop the network, use:"
  311. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  312. "$WARNINGS"
  313. exit 0
  314. fi
  315. if [ "$CHUTNEY_STOP_TIME" -ge 0 ]; then
  316. if [ "$CHUTNEY_STOP_TIME" -gt 0 ]; then
  317. $ECHO "Waiting ${CHUTNEY_STOP_TIME} seconds before stopping the network..."
  318. fi
  319. sleep "$CHUTNEY_STOP_TIME"
  320. # work around a bug/feature in make -j2 (or more)
  321. # where make hangs if any child processes are still alive
  322. "$CHUTNEY" stop "$CHUTNEY_NETWORK"
  323. # Give tor time to exit gracefully
  324. sleep 3
  325. "$WARNINGS"
  326. exit "$VERIFY_EXIT_STATUS"
  327. else
  328. $ECHO "Chutney network verified and running. To stop the network, use:"
  329. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  330. "$WARNINGS"
  331. exit 0
  332. fi