#!/bin/sh
set -eu

check_out() {
    if [ "$OUT" != "$(cat /etc/os-release)" ]; then
        echo "ssh command output does not match /etc/os-release:" >&2
        echo "$OUT" >&2
        exit 1
    fi
}

# change to debian/tests so that mock-sshd can find its keys
cd $(dirname $(realpath $0))
SSHD="$AUTOPKGTEST_TMP/mock-sshd"

# build server
cc -O0 -g -o "$SSHD" mock-sshd.c $(pkg-config --cflags --libs glib-2.0 libssh) -lutil

echo "====== password authentication ======"

# run server
$SSHD --user=joe --password=$(./mock-ssh-askpass) --port 1234 -v &
SERVER_PID=$!

OUT=$(DISPLAY= SSH_ASKPASS=`pwd`/mock-ssh-askpass setsid ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 1234 joe@localhost cat /etc/os-release)

wait $SERVER_PID
check_out

echo "====== pubkey authentication ======"

# run server
$SSHD --user=joe --port 1234 -v &
SERVER_PID=$!

chmod 600 test_rsa
OUT=$(ssh -i test_rsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 1234 joe@localhost cat /etc/os-release)

wait $SERVER_PID
check_out

