#!/bin/bash
set -ex

if [ "x$AUTOPKGTEST_NORMAL_USER" = x ]; then
    exit 77
fi

user=$AUTOPKGTEST_NORMAL_USER

port=7
addr='[::1]'
token=cQFoveQKSgwPdcKQ
 
touch /etc/authbind/byport/7
chmod 544 /etc/authbind/byport/7
chown "$user" /etc/authbind/byport/7

really -u $user \
authbind \
socat "TCP6-LISTEN:$port,bind=$addr" "exec:echo $token" &

pid=$!

# Ideally we'd wait for socat to get its act together, but that's hard.
sleep 5

token_got=$(socat "TCP6:$addr:$port" STDIO </dev/null)

test "x$token_got" = "x$token"
wait $pid

echo ok.
