#!/bin/bash -x

function on_exit() {
    [ "$PID" ] && kill $PID && ! wait $PID
    [ -d "$TMP" ] && rm -rf $TMP
}

trap 'on_exit' EXIT
trap 'exit' ERR

export TMP=`mktemp -d`

PORT=`shuf -i 1024-65535 -n 1`
$SD_ACTIVATE -l 127.0.0.1:$PORT -a ${0%/*}/pin-httpd "$TMP" &
export PID=$!
sleep 0.25

cfg="{\"url\":\"http://localhost:${PORT}/foo\"}"
! clevis encrypt http "$cfg" <<< "hi"

cfg=`jose fmt -j "$cfg" -Oj true -s http -U -Oo-`
e=`echo -n hi | clevis encrypt http "$cfg"`
d=`echo -n "$e" | clevis decrypt`
test "$d" == "hi"

kill $PID
! wait $PID

! echo "$e" | clevis decrypt
