test-network.sh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #! /bin/sh
  2. ECHO_N="/bin/echo -n"
  3. # Output is prefixed with the name of the script
  4. myname=$(basename "$0")
  5. until [ -z "$1" ]
  6. do
  7. case "$1" in
  8. # the path to the chutney directory
  9. --chutney-path)
  10. export CHUTNEY_PATH="$2"
  11. shift
  12. ;;
  13. --tor-path)
  14. # the path of a tor build directory
  15. # --tor-path overrides --tor and --tor-gencert
  16. export TOR_DIR="$2"
  17. shift
  18. ;;
  19. --tor)
  20. # the name or path of a tor binary
  21. export CHUTNEY_TOR="$2"
  22. shift
  23. ;;
  24. --tor-gencert)
  25. # the name or path of a tor-gencert binary
  26. export CHUTNEY_TOR_GENCERT="$2"
  27. shift
  28. ;;
  29. --flavor|--flavour|--network-flavor|--network-flavour)
  30. export NETWORK_FLAVOUR="$2"
  31. shift
  32. ;;
  33. # The amount of time chutney will wait before starting to verify
  34. # If negative, chutney exits straight after launching the network
  35. --start-time)
  36. export CHUTNEY_START_TIME="$2"
  37. shift
  38. ;;
  39. # The amount of time chutney will try to verify, before failing
  40. # If negative, chutney exits without verifying
  41. --delay|--sleep|--bootstrap-time|--time|--verify-time)
  42. # This isn't the best name for this variable, but we kept it the same
  43. # for backwards compatibility
  44. export CHUTNEY_BOOTSTRAP_TIME="$2"
  45. shift
  46. ;;
  47. # The amount of time chutney will wait after successfully verifying
  48. # If negative, chutney exits without stopping
  49. --stop-time)
  50. export CHUTNEY_STOP_TIME="$2"
  51. shift
  52. ;;
  53. # Environmental variables used by chutney verify performance tests
  54. # Send this many bytes per client connection (10 KBytes)
  55. --data|--data-bytes|--data-byte|--bytes|--byte)
  56. export CHUTNEY_DATA_BYTES="$2"
  57. shift
  58. ;;
  59. # Make this many connections per client (1)
  60. # Note: If you create 7 or more connections to a hidden service from
  61. # a single Tor 0.2.7 client, you'll likely get a verification failure due
  62. # to #15937. This is fixed in 0.2.8.
  63. --connections|--connection|--connection-count|--count)
  64. export CHUTNEY_CONNECTIONS="$2"
  65. shift
  66. ;;
  67. # Make each client connect to each HS (0)
  68. # 0 means a single client connects to each HS
  69. # 1 means every client connects to every HS
  70. --hs-multi-client|--hs-multi-clients|--hs-client|--hs-clients)
  71. export CHUTNEY_HS_MULTI_CLIENT="$2"
  72. shift
  73. ;;
  74. # The IPv4 address to bind to, defaults to 127.0.0.1
  75. --ipv4|--v4|-4|--ip)
  76. export CHUTNEY_LISTEN_ADDRESS="$2"
  77. shift
  78. ;;
  79. # The IPv6 address to bind to, default is not to bind to an IPv6 address
  80. --ipv6|--v6|-6)
  81. export CHUTNEY_LISTEN_ADDRESS_V6="$2"
  82. shift
  83. ;;
  84. --coverage)
  85. export USE_COVERAGE_BINARY=true
  86. ;;
  87. --dry-run)
  88. # process arguments, but don't call any other scripts
  89. export NETWORK_DRY_RUN=true
  90. ;;
  91. *)
  92. echo "$myname: Sorry, I don't know what to do with '$1'."
  93. # continue processing arguments during a dry run
  94. if [ "$NETWORK_DRY_RUN" != true ]; then
  95. exit 2
  96. fi
  97. ;;
  98. esac
  99. shift
  100. done
  101. # optional: $TOR_DIR is the tor build directory
  102. # it's used to find the location of tor binaries
  103. # if it's not set:
  104. # - set it ro $BUILDDIR, or
  105. # - if $PWD looks like a tor build directory, set it to $PWD, or
  106. # - unset $TOR_DIR, and let chutney fall back to finding tor binaries in $PATH
  107. if [ ! -d "$TOR_DIR" ]; then
  108. if [ -d "$BUILDDIR/src/or" -a -d "$BUILDDIR/src/tools" ]; then
  109. # Choose the build directory
  110. # But only if it looks like one
  111. echo "$myname: \$TOR_DIR not set, trying \$BUILDDIR"
  112. export TOR_DIR="$BUILDDIR"
  113. elif [ -d "$PWD/src/or" -a -d "$PWD/src/tools" ]; then
  114. # Guess the tor directory is the current directory
  115. # But only if it looks like one
  116. echo "$myname: \$TOR_DIR not set, trying \$PWD"
  117. export TOR_DIR="$PWD"
  118. elif [ -d "$PWD/../tor" -a -d "$PWD/../tor/src/or" -a \
  119. -d "$PWD/../tor/src/tools" ]; then
  120. # Guess the tor directory is next to the current directory
  121. # But only if it looks like one
  122. echo "$myname: \$TOR_DIR not set, trying \$PWD/../tor"
  123. export TOR_DIR="$PWD/../tor"
  124. else
  125. echo "$myname: no \$TOR_DIR, chutney will use \$PATH for tor binaries"
  126. unset TOR_DIR
  127. fi
  128. fi
  129. # make TOR_DIR absolute
  130. if [ -d "$PWD/$TOR_DIR" ]; then
  131. export TOR_DIR="$PWD/$TOR_DIR"
  132. fi
  133. # mandatory: $CHUTNEY_PATH is the path to the chutney launch script
  134. # if it's not set:
  135. # - if $PWD looks like a chutney directory, set it to $PWD, or
  136. # - set it based on $TOR_DIR, expecting chutney to be next to tor, or
  137. # - fail and tell the user how to clone the chutney repository
  138. if [ ! -d "$CHUTNEY_PATH" -o ! -x "$CHUTNEY_PATH/chutney" -o \
  139. ! -f "$CHUTNEY_PATH/chutney" ]; then
  140. if [ -x "$PWD/chutney" -a -f "$PWD/chutney" ]; then
  141. echo "$myname: \$CHUTNEY_PATH not valid, trying \$PWD"
  142. export CHUTNEY_PATH="$PWD"
  143. elif [ -d "`dirname \"$0\"`/.." -a \
  144. -x "`dirname \"$0\"`/../chutney" -a \
  145. -f "`dirname \"$0\"`/../chutney" ]; then
  146. echo "$myname: \$CHUTNEY_PATH not valid, using this script's location"
  147. export CHUTNEY_PATH="`dirname \"$0\"`/.."
  148. elif [ -d "$TOR_DIR" -a -d "$TOR_DIR/../chutney" -a \
  149. -x "$TOR_DIR/../chutney/chutney" -a \
  150. -f "$TOR_DIR/../chutney/chutney" ]; then
  151. echo "$myname: \$CHUTNEY_PATH not valid, trying \$TOR_DIR/../chutney"
  152. export CHUTNEY_PATH="$TOR_DIR/../chutney"
  153. else
  154. # TODO: work out how to package and install chutney,
  155. # so users can find it in $PATH
  156. echo "$myname: missing 'chutney' in \$CHUTNEY_PATH ($CHUTNEY_PATH)"
  157. echo "$myname: Get chutney: git clone https://git.torproject.org/\
  158. chutney.git"
  159. echo "$myname: Set \$CHUTNEY_PATH to a non-standard location: export \
  160. CHUTNEY_PATH=\`pwd\`/chutney"
  161. unset CHUTNEY_PATH
  162. exit 1
  163. fi
  164. fi
  165. # make chutney path absolute
  166. if [ -d "$PWD/$CHUTNEY_PATH" ]; then
  167. export CHUTNEY_PATH="$PWD/$CHUTNEY_PATH"
  168. fi
  169. # For picking up the right tor binaries.
  170. # If $TOR_DIR isn't set, chutney looks for tor binaries by name or path
  171. # using $CHUTNEY_TOR and $CHUTNEY_TOR_GENCERT, and then falls back to
  172. # looking for tor and tor-gencert in $PATH
  173. if [ -d "$TOR_DIR" ]; then
  174. tor_name=tor
  175. tor_gencert_name=tor-gencert
  176. if [ "$USE_COVERAGE_BINARY" = true ]; then
  177. tor_name=tor-cov
  178. fi
  179. export CHUTNEY_TOR="${TOR_DIR}/src/or/${tor_name}"
  180. export CHUTNEY_TOR_GENCERT="${TOR_DIR}/src/tools/${tor_gencert_name}"
  181. else
  182. # make binary paths absolute
  183. if [ -x "$PWD/$CHUTNEY_TOR" ]; then
  184. export CHUTNEY_TOR="$PWD/$CHUTNEY_TOR"
  185. fi
  186. if [ -x "$PWD/$CHUTNEY_TOR_GENCERT" ]; then
  187. export CHUTNEY_TOR_GENCERT="$PWD/$CHUTNEY_TOR_GENCERT"
  188. fi
  189. fi
  190. # Set the variables for the chutney network flavour
  191. export NETWORK_FLAVOUR=${NETWORK_FLAVOUR:-"bridges+hs"}
  192. export CHUTNEY_NETWORK="$CHUTNEY_PATH/networks/$NETWORK_FLAVOUR"
  193. # And finish up if we're doing a dry run
  194. if [ "$NETWORK_DRY_RUN" = true ]; then
  195. # we can't exit here, it breaks argument processing
  196. return
  197. fi
  198. # Chutney must be launched at $CHUTNEY_PATH, at least until #21521 is fixed
  199. cd "$CHUTNEY_PATH"
  200. "$CHUTNEY_PATH/tools/bootstrap-network.sh" "$NETWORK_FLAVOUR" || exit 2
  201. # chutney starts verifying after 20 seconds, keeps on trying for 60 seconds,
  202. # and then stops immediately (by default)
  203. # Even the fastest chutney networks take 5-10 seconds for their first consensus
  204. # and then 10 seconds after that for relays to bootstrap and upload descriptors
  205. export CHUTNEY_START_TIME=${CHUTNEY_START_TIME:-20}
  206. export CHUTNEY_BOOTSTRAP_TIME=${CHUTNEY_BOOTSTRAP_TIME:-60}
  207. export CHUTNEY_STOP_TIME=${CHUTNEY_STOP_TIME:-0}
  208. CHUTNEY="$CHUTNEY_PATH/chutney"
  209. if [ "$CHUTNEY_START_TIME" -ge 0 ]; then
  210. echo "Waiting ${CHUTNEY_START_TIME} seconds for a consensus containing relays to be generated..."
  211. sleep "$CHUTNEY_START_TIME"
  212. else
  213. echo "Chutney network launched and running. To stop the network, use:"
  214. echo "$CHUTNEY stop $CHUTNEY_NETWORK"
  215. CHUTNEY_WARNINGS_IGNORE_EXPECTED=1 "$CHUTNEY_PATH/tools/warnings.sh"
  216. exit 0
  217. fi
  218. if [ "$CHUTNEY_BOOTSTRAP_TIME" -ge 0 ]; then
  219. # Chutney will try to verify for $CHUTNEY_BOOTSTRAP_TIME seconds
  220. "$CHUTNEY" verify "$CHUTNEY_NETWORK"
  221. VERIFY_EXIT_STATUS="$?"
  222. else
  223. echo "Chutney network ready and running. To stop the network, use:"
  224. echo "$CHUTNEY" stop "$CHUTNEY_NETWORK"
  225. CHUTNEY_WARNINGS_IGNORE_EXPECTED=1 "$CHUTNEY_PATH/tools/warnings.sh"
  226. exit 0
  227. fi
  228. if [ "$CHUTNEY_STOP_TIME" -ge 0 ]; then
  229. if [ "$CHUTNEY_STOP_TIME" -gt 0 ]; then
  230. echo "Waiting ${CHUTNEY_STOP_TIME} seconds before stopping the network..."
  231. fi
  232. sleep "$CHUTNEY_STOP_TIME"
  233. # work around a bug/feature in make -j2 (or more)
  234. # where make hangs if any child processes are still alive
  235. "$CHUTNEY" stop "$CHUTNEY_NETWORK"
  236. exit "$VERIFY_EXIT_STATUS"
  237. else
  238. echo "Chutney network verified and running. To stop the network, use:"
  239. echo "$CHUTNEY stop $CHUTNEY_NETWORK"
  240. CHUTNEY_WARNINGS_IGNORE_EXPECTED=1 "$CHUTNEY_PATH/tools/warnings.sh"
  241. exit 0
  242. fi