| 1 | #!/bin/sh |
| 2 | |
| 3 | . "$(dirname "$0")/e2e-test-subrs" |
| 4 | |
| 5 | # |
| 6 | # Validate that mosh produces expected output, using screen captures |
| 7 | # in tmux. |
| 8 | # |
| 9 | |
| 10 | log() |
| 11 | { |
| 12 | printf "$@" |
| 13 | } |
| 14 | |
| 15 | error() |
| 16 | { |
| 17 | printf "$@" >&2 |
| 18 | } |
| 19 | |
| 20 | dump_logs() |
| 21 | { |
| 22 | dir=$1 |
| 23 | shift |
| 24 | testname=$(basename "$dir" .d) |
| 25 | for logfile in $dir/*.tmux.log; do |
| 26 | printf "travis_fold:start:%s-%s\n" "$testname" "$(basename "$logfile")" |
| 27 | cat "$logfile" |
| 28 | printf "travis_fold:end:%s-%s\n" "$testname" "$(basename "$logfile")" |
| 29 | done |
| 30 | } |
| 31 | |
| 32 | test_success() |
| 33 | { |
| 34 | exit 0 |
| 35 | } |
| 36 | test_failure() |
| 37 | { |
| 38 | error "$@" |
| 39 | exit 1 |
| 40 | } |
| 41 | test_skipped() |
| 42 | { |
| 43 | error "$@" |
| 44 | exit 77 |
| 45 | } |
| 46 | test_error() |
| 47 | { |
| 48 | error "$@" |
| 49 | exit 99 |
| 50 | } |
| 51 | test_exitstatus() |
| 52 | { |
| 53 | status=$1 |
| 54 | shift |
| 55 | error "$@" |
| 56 | exit "$status" |
| 57 | } |
| 58 | |
| 59 | |
| 60 | ssh_localhost_check() |
| 61 | { |
| 62 | ssh localhost : |
| 63 | if [ $? -ne 0 ]; then |
| 64 | error "ssh to localhost failed\n" |
| 65 | return 1 |
| 66 | fi |
| 67 | return 0 |
| 68 | } |
| 69 | |
| 70 | # These two functions are wrappers for mosh-client/mosh-server to turn |
| 71 | # on verbosity and log stderr. |
| 72 | mosh_client() |
| 73 | { |
| 74 | if [ -z "$MOSH_CLIENT" ] || [ -z "$MOSH_E2E_TEST" ]; then |
| 75 | test_error "mosh_client: variables missing\n" |
| 76 | fi |
| 77 | exec 2> "${MOSH_E2E_TEST}.client.stderr" |
| 78 | exec "$MOSH_CLIENT" $MOSH_CLIENT_ARGS "$@" |
| 79 | } |
| 80 | |
| 81 | mosh_server() |
| 82 | { |
| 83 | if [ -z "$MOSH_SERVER" ] || [ -z "$MOSH_E2E_TEST" ]; then |
| 84 | test_error "mosh_server: variables missing\n" |
| 85 | fi |
| 86 | exec 2> "${MOSH_E2E_TEST}.server.stderr" |
| 87 | exec "$MOSH_SERVER" new -vv $MOSH_SERVER_ARGS -@ "$@" |
| 88 | } |
| 89 | |
| 90 | # main |
| 91 | # Set up environment |
| 92 | if [ -z "$srcdir" ]; then |
| 93 | export srcdir=$PWD |
| 94 | else |
| 95 | srcdir="$(cd "$srcdir" && pwd)" |
| 96 | if [ $? -ne 0 ]; then |
| 97 | error "can't cd to srcdir: %s\n" "$srcdir" |
| 98 | exit 99 |
| 99 | fi |
| 100 | fi |
| 101 | |
| 102 | # Wrappers. |
| 103 | case "$(basename "$0")" in |
| 104 | mosh-client) |
| 105 | mosh_client "$@" |
| 106 | exit |
| 107 | ;; |
| 108 | mosh-server) |
| 109 | mosh_server "$@" |
| 110 | exit |
| 111 | ;; |
| 112 | *) |
| 113 | ;; |
| 114 | esac |
| 115 | |
| 116 | if [ $# -lt 2 ]; then |
| 117 | test_error "not enough args\n" |
| 118 | fi |
| 119 | |
| 120 | # Get arguments (only one so far) |
| 121 | test_name=$1 |
| 122 | shift |
| 123 | test_args=$@ |
| 124 | # XXX could use AM testsubdir macro instead |
| 125 | test_dir=$(basename "${test_name}").d |
| 126 | test_script="${test_name}" |
| 127 | |
| 128 | tests_dir=$(dirname "${test_name}") |
| 129 | if ! set_locale "${tests_dir}"; then |
| 130 | test_error "e2e-test: no usable locale\n" |
| 131 | fi |
| 132 | |
| 133 | # XXX this ignores $TMPDIR, because it results in an overlong pathname on OS X |
| 134 | tmux_socket="/tmp/.tmux-mosh-test-$$" |
| 135 | |
| 136 | # need version 1.8 for capture-pane |
| 137 | if ! tmux_check 1 8; then |
| 138 | test_skipped "tmux unavailable\n" |
| 139 | fi |
| 140 | |
| 141 | rm -rf "${test_dir}" |
| 142 | mkdir "${test_dir}" |
| 143 | |
| 144 | |
| 145 | on_exit() { |
| 146 | rv=$? |
| 147 | if test $rv -ne 0; then |
| 148 | dump_logs "$test_dir" $test_args |
| 149 | fi |
| 150 | exit $rv |
| 151 | } |
| 152 | trap on_exit EXIT |
| 153 | |
| 154 | # Set up tests to run. |
| 155 | server_tests= |
| 156 | compare_tests= |
| 157 | for i in $test_args; do |
| 158 | case $i in |
| 159 | baseline|direct|variant) |
| 160 | server_tests="$server_tests $i";; |
| 161 | verify|same|different) |
| 162 | compare_tests="$compare_tests $i";; |
| 163 | tmux) |
| 164 | tmux=1;; |
| 165 | client) |
| 166 | client=1;; |
| 167 | server) |
| 168 | server=1;; |
| 169 | post) |
| 170 | post=1;; |
| 171 | mosh-args) |
| 172 | mosh_args=$("${test_script}" mosh-args);; |
| 173 | client-args) |
| 174 | MOSH_CLIENT_ARGS=$("${test_script}" client-args) |
| 175 | export MOSH_CLIENT_ARGS;; |
| 176 | server-args) |
| 177 | MOSH_SERVER_ARGS=$("${test_script}" server-args) |
| 178 | export MOSH_SERVER_ARGS;; |
| 179 | *) |
| 180 | error 'unknown test type argument %s\n' "$i" |
| 181 | exit 99 |
| 182 | ;; |
| 183 | esac |
| 184 | done |
| 185 | |
| 186 | # Run test(s). |
| 187 | client_wrapper= |
| 188 | if [ -n "$client" ]; then |
| 189 | client_wrapper="${test_script} client" |
| 190 | fi |
| 191 | |
| 192 | server_wrapper="\"${srcdir}/e2e-test-server\"" |
| 193 | if [ -n "$server" ]; then |
| 194 | server_wrapper="\"${srcdir}/${test_script}\" server" |
| 195 | fi |
| 196 | tmux_stdin="${srcdir}/hold-stdin" |
| 197 | if [ -n "$tmux" ]; then |
| 198 | tmux_stdin="${test_script} tmux" |
| 199 | fi |
| 200 | |
| 201 | ln -fs "${tmux_socket}" "${test_dir}/tmux-socket" |
| 202 | |
| 203 | for run in $server_tests; do |
| 204 | log "Running server test %s.\n" "$run" |
| 205 | # These three variables are for the benefit of the mosh-client and mosh-server wrappers. |
| 206 | export MOSH_CLIENT="$PWD/../frontend/mosh-client" |
| 207 | export MOSH_SERVER="$PWD/../frontend/mosh-server" |
| 208 | export MOSH_E2E_TEST="$PWD/${test_dir}/${run}" |
| 209 | # XXX need to quote special chars in server pathname here somehow |
| 210 | sut="../../scripts/mosh --client=${srcdir}/mosh-client --server=${srcdir}/mosh-server --local --bind-server=127.0.0.1 ${mosh_args} 127.0.0.1" |
| 211 | if [ "$run" = "direct" ]; then |
| 212 | sut="" |
| 213 | fi |
| 214 | # Actually execute code under test |
| 215 | # XXX tmux 1.8 requires shell command as a single arg; once we move to 2.0, undo these quotes |
| 216 | # tmux <= 2.5 ignore -x/-y, but the client sets the session to 80x24. |
| 217 | # tmux from 2017-05-27 and later should default to an 80x24 session, |
| 218 | # but do use -x/-y on control-master clients. |
| 219 | ${tmux_stdin} tmux -f /dev/null -S "${tmux_socket}" -C new-session -x 80 -y 24 "${srcdir}/print-exitstatus ${client_wrapper} ${sut} ${server_wrapper} \"${PWD}/${test_dir}/${run}\" \"${PWD}/${test_script} ${run}\"" > "${test_dir}/${run}.tmux.log" |
| 220 | rv=$? |
| 221 | rm -f "${tmux_socket}" "${test_dir}/tmux-socket" |
| 222 | if [ $rv -ne 0 ]; then |
| 223 | test_error "tmux failure on test %s\n" "$run" |
| 224 | fi |
| 225 | # Check for mosh failures |
| 226 | if ! grep -q "@@@ exitstatus: 0 @@@" "${test_dir}/${run}.tmux.log"; then |
| 227 | test_error "mosh-client had non-zero exitstatus\n" |
| 228 | fi |
| 229 | |
| 230 | # Check for server harness failures |
| 231 | if [ -z "$server" ]; then |
| 232 | if [ ! -s "${test_dir}/${run}.capture" ] \ |
| 233 | || [ ! -s "${test_dir}/${run}.exitstatus" ]; then |
| 234 | test_error "server harness failure on test %s\n" "$run" |
| 235 | fi |
| 236 | read -r server_rv < "${test_dir}/${run}.exitstatus" |
| 237 | if [ "$server_rv" -ne 0 ]; then |
| 238 | test_error "server harness exited with status %s\n" "$server_rv" |
| 239 | fi |
| 240 | fi |
| 241 | if [ "${run}" != "direct" ]; then |
| 242 | # Check for "round-trip" failures |
| 243 | if grep -q "round-trip Instruction verification failed" "${test_dir}/${run}.server.stderr"; then |
| 244 | test_error "Round-trip Instruction verification failed on server during %s\n" "$run" |
| 245 | fi |
| 246 | # Check for 0-timeout select() issue |
| 247 | if egrep -q "(polls, rate limiting|consecutive polls)" "${test_dir}/${run}.server.stderr"; then |
| 248 | if [ "osx" != "${TRAVIS_OS_NAME}" ]; then |
| 249 | test_error "select() with zero timeout called too often on server during %s\n" "$run" |
| 250 | fi |
| 251 | fi |
| 252 | # Check for assert() |
| 253 | if egrep -q "assertion.*failed" "${test_dir}/${run}.server.stderr"; then |
| 254 | test_error "assertion during %s\n" "$run" |
| 255 | fi |
| 256 | fi |
| 257 | # XXX We'd also like to check for "target state Instruction |
| 258 | # verification failed", a new check, but tmux's lack of BCE |
| 259 | # support forces mosh to clear lines with spaces and change a |
| 260 | # framebuffer in a way that causes this to fire spuriously. |
| 261 | done |
| 262 | |
| 263 | for compare in $compare_tests; do |
| 264 | log "Running server comparison %s.\n" "$compare" |
| 265 | # Compare captures |
| 266 | if [ "$compare" = verify ]; then |
| 267 | test1="direct" |
| 268 | test2="baseline" |
| 269 | else |
| 270 | test1="baseline" |
| 271 | test2="variant" |
| 272 | fi |
| 273 | if diff -q "${test_dir}/${test1}.capture" "${test_dir}/${test2}.capture"; then |
| 274 | differ=n |
| 275 | else |
| 276 | differ=y |
| 277 | fi |
| 278 | if [ "$compare" = different ]; then |
| 279 | desired=y |
| 280 | badresult=same |
| 281 | else |
| 282 | desired=n |
| 283 | badresult=different |
| 284 | fi |
| 285 | if [ $differ != $desired ]; then |
| 286 | test_failure "Output is %s between tests %s and %s\n" "$badresult" "$test1" "$test2" |
| 287 | fi |
| 288 | done |
| 289 | |
| 290 | # Run a post script (usually a custom validation of results) |
| 291 | if [ -n "$post" ]; then |
| 292 | "${test_script}" post |
| 293 | status=$? |
| 294 | if [ $status -ne 0 ]; then |
| 295 | test_exitstatus $status "Post test failed with exitstatus %d\n" $status |
| 296 | fi |
| 297 | fi |