test-network.sh 11 KB

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