run-tests.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/sh
  2. # -*- coding: utf-8 -*-
  3. basedir="$(realpath "$0" | xargs dirname)"
  4. basedir="$basedir/.."
  5. info()
  6. {
  7. echo "--- $*"
  8. }
  9. error()
  10. {
  11. echo "=== ERROR: $*" >&2
  12. }
  13. warning()
  14. {
  15. echo "=== WARNING: $*" >&2
  16. }
  17. die()
  18. {
  19. error "$*"
  20. exit 1
  21. }
  22. build_project()
  23. {
  24. info "Building project..."
  25. cd "$basedir" || die "cd failed"
  26. ./build.sh || die "Build failed"
  27. }
  28. cargo_clippy()
  29. {
  30. cargo clippy -- --deny warnings || die "cargo clippy failed"
  31. cargo clippy --tests -- --deny warnings || die "cargo clippy --tests failed"
  32. }
  33. run_tests_genkey()
  34. {
  35. info "### Running test: gen-key ###"
  36. local conf="$testdir/conf/udp.conf"
  37. local res="$("$target/letmein" --config "$conf" gen-key --user 12345678)"
  38. local user="$(echo "$res" | cut -d'=' -f1 | cut -d' ' -f1)"
  39. local key="$(echo "$res" | cut -d'=' -f2 | cut -d' ' -f2)"
  40. [ "$user" = "12345678" ] || die "Got invalid user"
  41. }
  42. run_tests_knock()
  43. {
  44. local test_type="$1"
  45. info "### Running test: knock $test_type ###"
  46. rm -rf "$rundir"
  47. local conf="$testdir/conf/$test_type.conf"
  48. info "Starting letmeinfwd..."
  49. "$target/letmeinfwd" \
  50. --test-mode \
  51. --no-systemd \
  52. --rundir "$rundir" \
  53. --config "$conf" &
  54. pid_letmeinfwd=$!
  55. info "Starting letmeind..."
  56. "$target/letmeind" \
  57. --no-systemd \
  58. --rundir "$rundir" \
  59. --config "$conf" &
  60. pid_letmeind=$!
  61. wait_for_pidfile letmeinfwd "$pid_letmeinfwd"
  62. wait_for_pidfile letmeind "$pid_letmeind"
  63. info "Knocking IPv6 + IPv4..."
  64. "$target/letmein" \
  65. --verbose \
  66. --config "$conf" \
  67. knock \
  68. --user 12345678 \
  69. localhost 42 \
  70. || die "letmein knock failed"
  71. info "Knocking IPv4..."
  72. "$target/letmein" \
  73. --verbose \
  74. --config "$conf" \
  75. knock \
  76. --user 12345678 \
  77. --ipv4 \
  78. localhost 42 \
  79. || die "letmein knock failed"
  80. info "Knocking IPv6..."
  81. "$target/letmein" \
  82. --verbose \
  83. --config "$conf" \
  84. knock \
  85. --user 12345678 \
  86. --ipv6 \
  87. localhost 42 \
  88. || die "letmein knock failed"
  89. kill_all_and_wait
  90. }
  91. wait_for_pidfile()
  92. {
  93. local name="$1"
  94. local pid="$2"
  95. for i in $(seq 0 29); do
  96. if [ -r "$rundir/$name/$name.pid" ]; then
  97. if [ "$pid" != "$(cat "$rundir/$name/$name.pid")" ]; then
  98. die "$name: Invalid PID-file."
  99. fi
  100. return
  101. fi
  102. sleep 0.1
  103. done
  104. die "$name PID-file is missing. Did $name fail to start?"
  105. }
  106. kill_all()
  107. {
  108. kill_letmeind
  109. kill_letmeinfwd
  110. }
  111. kill_all_and_wait()
  112. {
  113. kill_all
  114. wait
  115. }
  116. kill_letmeinfwd()
  117. {
  118. if [ -n "$pid_letmeinfwd" ]; then
  119. kill -TERM "$pid_letmeinfwd" >/dev/null 2>&1
  120. pid_letmeinfwd=
  121. fi
  122. }
  123. kill_letmeind()
  124. {
  125. if [ -n "$pid_letmeind" ]; then
  126. kill -TERM "$pid_letmeind" >/dev/null 2>&1
  127. pid_letmeind=
  128. fi
  129. }
  130. cleanup()
  131. {
  132. kill_all
  133. if [ -n "$tmpdir" ]; then
  134. rm -rf "$tmpdir"
  135. tmpdir=
  136. fi
  137. }
  138. cleanup_and_exit()
  139. {
  140. cleanup
  141. exit 1
  142. }
  143. pid_letmeinfwd=
  144. pid_letmeind=
  145. [ -n "$TMPDIR" ] || export TMPDIR=/tmp
  146. tmpdir="$(mktemp --tmpdir="$TMPDIR" -d letmein-test.XXXXXXXXXX)"
  147. [ -d "$tmpdir" ] || die "Failed to create temporary directory"
  148. rundir="$tmpdir/run"
  149. target="$basedir/target/debug"
  150. testdir="$basedir/tests"
  151. stubdir="$testdir/stubs"
  152. export PATH="$target:$PATH"
  153. trap cleanup_and_exit INT TERM
  154. trap cleanup EXIT
  155. info "Temporary directory is: $tmpdir"
  156. build_project
  157. cargo_clippy
  158. run_tests_genkey
  159. run_tests_knock tcp
  160. run_tests_knock udp
  161. info "All tests Ok."
  162. # vim: ts=4 sw=4 expandtab