#!/usr/bin/env bash
#
# Some unit-tests
# (require bash and socat to be installed)

set -ue

port=${1:-12345}
pidfile=/tmp/sockproc-test.pid
sockfile=/tmp/sockproc-test.sock


function cleanup() {
    kill `cat $pidfile` && rm -f $pidfile
}
trap cleanup EXIT


function runtests() {
    dest=$1
    title=$2

    echo -e "======= $title =================="

    # simple commands
    echo -e "uname -a\r\n0\r\n" | socat -t10 - $dest
    echo -e "id\r\n0\r\n" | socat -t10 - $dest

    # long-running command
    echo -e "date +%s; sleep 1; date +%s\r\n0\r\n" | socat -t10 - $dest

    # commands with some input
    echo -e "wc -l\r\n12\r\nline1\r\nline2" | socat -t10 - $dest
    echo -e "grep line\r\n20\r\nline1\r\nline2\r\nfoobar" | socat -t10 - $dest

    # bad command.expecting non-empty error stream
    echo -e "thisshouldfail\r\n0\r\n" | socat -t10 - $dest

    # this should have data in both output and error streams
    echo -e "echo hello output && echo hello error >&2\r\n0\r\n" | socat -t10 - $dest

    # long command line strings
    echo -e "echo "\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "12345678901234567890123456789012345678901234567890"\
    "1234567890123456789012345678901234567890"\
    "\r\n0\r\n" | socat -t10 - $dest
}

./sockproc $port $pidfile
runtests tcp-connect:127.0.0.1:$port "TCP mode"

cleanup

./sockproc $sockfile $pidfile
runtests $sockfile "UNIX-socket mode"

echo -e "================================"
