Skip to content
suit Artifact [acdee765e2]
Login

Artifact [acdee765e2]

Artifact acdee765e26c679586bc744ed6761a80c30dbe75a69addb96b8d548f2811c569:


#!/bin/sh
# Shell tests for sync, clone and tend over https (src/tls.c, src/anchors.c),
# against servers on 127.0.0.1 only: stock fossil serving TLS with
# certificates made at test time (test/https_certs.sh), and test/badsrv.c, a
# BearSSL server that misbehaves on purpose. Every server runs under
# build/helpers/deadline, which kills it within 15 minutes even if this
# script dies, and is stopped at the end. Needs fossil, openssl and nc; a
# case that cannot run says `skip`.
# SUIT names the binary under test (default build/dev/suit).

SUIT=${SUIT:-build/dev/suit}
case $SUIT in /*) ;; *) SUIT=$PWD/$SUIT ;; esac
ROOT=$PWD
H=$ROOT/build/helpers
FIXTURE=$ROOT/test/fixtures/wardrobe.fossil
mkdir -p build/tmp
T=$(mktemp -d "$ROOT/build/tmp/https.XXXXXX") || exit 1

total=0
failed=0
pass() { total=$((total + 1)); echo "ok $1"; }
bad() {
    total=$((total + 1)); failed=$((failed + 1))
    echo "FAIL $1${2:+: $2}"
}
skip() { echo "skip $1: $2"; }

export USER=tester
unset LOGNAME SSL_CERT_FILE SUIT_NOW SUIT_FAULT SUIT_NET_TIMEOUT SUIT_NET_DEADLINE

# check NAME WANT_RC [STDERR_SUBSTRING] -- COMMAND...   (output in $T/out, $T/err)
check() {
    cname=$1; cwant=$2; cmsg=$3; shift 4
    "$@" >"$T/out" 2>"$T/err"; crc=$?
    if [ "$crc" != "$cwant" ]; then bad "$cname" "exit $crc, want $cwant: $(head -c 600 "$T/err")"
    elif [ -n "$cmsg" ] && ! grep -qF -- "$cmsg" "$T/err"; then bad "$cname" "want '$cmsg' in: $(head -c 600 "$T/err")"
    else pass "$cname"; fi
}

# ---- what runs without a server ------------------------------------------------

export SUIT_HOME=$T/plain
mkdir -p "$SUIT_HOME"
printf 'not a certificate\n' > "$T/notpem.pem"
: > "$T/empty.pem"
check remote-ca-needs-https 2 "--ca is for an https:// address" -- "$SUIT" remote wd http://h/p --ca "$T/empty.pem"
check remote-ca-relative-path 2 "a certificate file's path is absolute" -- "$SUIT" remote wd https://h/p --ca rel.pem
check remote-ca-empty-path 2 "1 to 1024 characters" -- "$SUIT" remote wd https://h/p --ca ""
check remote-ca-space-in-path 2 "printable characters only" -- "$SUIT" remote wd https://h/p --ca "/a b.pem"
check remote-ca-missing-file 2 "no certificate file: none of $T/nope.pem exists" -- "$SUIT" remote wd https://h/p --ca "$T/nope.pem"
check remote-ca-not-pem 2 "holds no certificate suit can use" -- "$SUIT" remote wd https://h/p --ca "$T/notpem.pem"
check remote-ca-bad-address-first 2 "starts with file://" -- "$SUIT" remote wd ftp://h/p --ca "$T/empty.pem"
check remote-ca-bad-option 2 "usage:" -- "$SUIT" remote wd https://h/p --cx "$T/empty.pem"
check remote-ca-option-without-file 2 "usage:" -- "$SUIT" remote wd https://h/p --ca
[ ! -e "$SUIT_HOME/remotes" ] && [ ! -e "$SUIT_HOME/cafiles" ] && pass remote-ca-refusals-write-nothing || bad remote-ca-refusals-write-nothing
check clone-ca-needs-https 2 "--ca is for an https:// address" -- "$SUIT" clone wd http://h/p --ca /a.pem
check clone-ca-relative-path 2 "a certificate file's path is absolute" -- "$SUIT" clone wd https://h/p --ca a.pem
check clone-ca-bad-option 2 "usage:" -- "$SUIT" clone wd https://h/p --cx /a.pem
# the cafiles file is parsed as strictly as the remotes file
printf 'wd https://h/p\n' > "$SUIT_HOME/remotes"
for c in "wd rel.pem|1: a certificate file's path is absolute" "wd|1: expected NAME PATH" "a/b /x.pem|1: not a wardrobe name" \
         "wd /a.pem
wd /b.pem|2: duplicate CA file for wd"; do
    printf '%s\n' "${c%|*}" > "$SUIT_HOME/cafiles"
    check "cafiles-bad-$total" 1 "cafiles:${c#*|}" -- "$SUIT" remote wd
done
for i in $(seq 1 257); do echo "w$i /a.pem"; done > "$SUIT_HOME/cafiles"
check cafiles-too-many 1 "too many CA files (limit 256)" -- "$SUIT" remote wd
check cafiles-bad-file-stops-a-set 1 "too many CA files" -- "$SUIT" remote wd https://h/p
# a cafiles file that would grow past its limit is not written
awk 'BEGIN { t = 0; for (i = 1; i <= 64; i++) { p = "/"; k = (i < 64) ? 1019 : 65530 - t - length("w" i " /") - 1; for (j = 0; j < k; j++) p = p "a"; line = "w" i " " p; print line; t += length(line) + 1 } }' > "$SUIT_HOME/cafiles"
cp "$ROOT/test/fixtures/ec-ca.pem" "$T/ec-ca.pem"; chmod 644 "$T/ec-ca.pem"
check cafiles-too-big-to-write 1 "cannot write $SUIT_HOME/cafiles" -- "$SUIT" remote wd https://h/p --ca "$T/ec-ca.pem"
printf 'wd /a.pem\n' > "$SUIT_HOME/cafiles"
check cafiles-read-fails 1 "cafiles:0: cannot read" -- env SUIT_FAULT=open:2 "$SUIT" remote wd
# a CA file anyone else could change is refused when it is set
check remote-ca-group-writable 2 "ec-ca.pem is not trusted: it can be written by group or others" -- sh -c "chmod 664 '$T/ec-ca.pem'; exec '$SUIT' remote wd https://h/p --ca '$T/ec-ca.pem'"
chmod 644 "$T/ec-ca.pem"
check remote-ca-of-another-user 2 "it belongs to another user" -- env SUIT_UID=4242 "$SUIT" remote wd https://h/p --ca "$T/ec-ca.pem"
mkdir -p "$T/shared"; cp "$T/ec-ca.pem" "$T/shared/ca.pem"; chmod 1777 "$T/shared"
check remote-ca-in-a-shared-folder 2 "shared/ca.pem is not trusted: its folder can be written by group or others" -- "$SUIT" remote wd https://h/p --ca "$T/shared/ca.pem"
chmod 700 "$T/shared"
ln -s "$T/ec-ca.pem" "$T/link.pem"
check remote-ca-symlink 2 "cannot read $T/link.pem" -- "$SUIT" remote wd https://h/p --ca "$T/link.pem"
mkfifo "$T/fifo.pem"
t0=$(date +%s)
check remote-ca-fifo 2 "fifo.pem is not trusted: it is not a regular file" -- "$SUIT" remote wd https://h/p --ca "$T/fifo.pem"
[ $(($(date +%s) - t0)) -le 5 ] && pass remote-ca-fifo-at-once || bad remote-ca-fifo-at-once
check remote-ca-folder 2 "is not trusted: it is not a regular file" -- "$SUIT" remote wd https://h/p --ca "$T/shared"
check remote-ca-trusted 0 "" -- "$SUIT" remote wd https://h/p --ca "$T/ec-ca.pem"
# no core file may hold the secret
check login-without-core-files-off 1 "cannot turn off core files" -- sh -c "printf 'x1234\n' | SUIT_FAULT=setrlimit:1 '$SUIT' login wd alice"
[ ! -e "$SUIT_HOME/credentials" ] && pass login-wrote-nothing || bad login-wrote-nothing
check logout-without-core-files-off 1 "cannot turn off core files" -- env SUIT_FAULT=setrlimit:1 "$SUIT" logout wd
rm -rf "$SUIT_HOME"; mkdir -p "$SUIT_HOME"

if ! command -v fossil >/dev/null 2>&1 || ! command -v openssl >/dev/null 2>&1 || ! command -v nc >/dev/null 2>&1; then
    skip https-servers "needs fossil, openssl and nc on the path"
    rm -rf "$T"
    echo "https.sh: $total cases, $failed failed"
    [ "$failed" -eq 0 ]
    exit
fi
if [ ! -x "$H/deadline" ] || [ ! -x "$H/badsrv" ]; then
    bad https-helpers "build/helpers/deadline and badsrv are missing: make builds them"
    echo "https.sh: $total cases, $failed failed"
    exit 1
fi

# ---- the servers ----------------------------------------------------------------

STARTED=
stop_all() {
    for n in $STARTED; do
        [ -f "$T/$n.pid" ] && kill -TERM -- "-$(cat "$T/$n.pid")" 2>/dev/null
    done
    sleep 1
    for n in $STARTED; do
        [ -f "$T/$n.pid" ] && kill -KILL -- "-$(cat "$T/$n.pid")" 2>/dev/null
        p=$(cat "$T/$n.port" 2>/dev/null)
        # a connection's fossil process may have left the server's group
        [ -n "$p" ] && pkill -KILL -f "fossil server .*--port $p " 2>/dev/null
    done
}
trap 'stop_all; rm -rf "$T"' EXIT
trap 'exit 130' INT TERM

C=$T/certs
if ! sh "$ROOT/test/https_certs.sh" "$C" >"$T/certs.log" 2>&1; then
    bad https-certs "$(tail -3 "$T/certs.log")"
fi

cp "$FIXTURE" "$T/S.fossil"
SECRET=s3cr3t-https-7q
fossil user new alice contact "$SECRET" -R "$T/S.fossil" >/dev/null 2>&1
fossil user capabilities alice oi -R "$T/S.fossil" >/dev/null 2>&1
fossil settings backoffice-disable 1 -R "$T/S.fossil" >/dev/null 2>&1

# start NAME COMMAND...: on a free port (the word PORT in COMMAND), under the
# watchdog; the port is left in $PORT and $T/NAME.port.
start() {
    sn=$1; shift
    for try in 1 2 3 4 5 6; do
        p=$(awk 'BEGIN { srand(); print 20000 + int(rand() * 30000) + '$try' * 13 }')
        nc -z 127.0.0.1 "$p" >/dev/null 2>&1 && continue
        cmd=
        for a in "$@"; do [ "$a" = PORT ] && a=$p; cmd="$cmd '$a'"; done
        DEADLINE_PIDFILE=$T/$sn.pid eval "\"\$H/deadline\" 840 $cmd" >"$T/$sn.log" 2>&1 </dev/null &
        STARTED="$STARTED $sn"
        echo "$p" > "$T/$sn.port"
        i=0
        while [ $i -lt 50 ] && ! nc -z 127.0.0.1 "$p" >/dev/null 2>&1; do i=$((i + 1)); sleep 0.2; done
        if nc -z 127.0.0.1 "$p" >/dev/null 2>&1; then PORT=$p; return 0; fi
        [ -f "$T/$sn.pid" ] && kill -KILL -- "-$(cat "$T/$sn.pid")" 2>/dev/null
    done
    bad "server-$sn-did-not-start" "$(tail -3 "$T/$sn.log")"
    PORT=1
    return 1
}
fsrv() { # NAME CERT: fossil serving S.fossil over TLS with CERT's chain and key
    start "$1" fossil server --localhost --max-latency 60 --port PORT --cert "$C/$2.chain.pem" --pkey "$C/$2.key" "$T/S.fossil"
}

start http fossil server --localhost --max-latency 60 --port PORT "$T/S.fossil"; P_HTTP=$PORT
fsrv tls leaf; P_TLS=$PORT
fsrv ca2 leafca2; P_CA2=$PORT
if [ -f "$C/expired.key" ]; then fsrv expired expired; P_EXP=$PORT; else P_EXP=; fi
for m in nothing midclose truncated noclen noclen-unclean chunked redirect error500 garbage cbc tls13; do
    start "bad-$m" "$H/badsrv" PORT "$m" "$C/leaf.chain.pem" "$C/leaf.key" "$P_HTTP" 830
    eval "P_$(echo "$m" | tr - _)=\$PORT"
done
start bad-rsakex "$H/badsrv" PORT rsakex "$C/rsa.chain.pem" "$C/rsa.key" "$P_HTTP" 830; P_rsakex=$PORT

# ---- checks with stock fossil ------------------------------------------------------

tip() { fossil sql -R "$1" "SELECT uuid FROM blob JOIN event ON objid=rid WHERE type='ci' ORDER BY mtime DESC LIMIT 1" 2>/dev/null; }
whole() { fossil test-integrity -R "$1" 2>&1 | tr '\r' '\n' | grep -cE ' 0 errors|integrity-check: ok' | grep -qx 2; }
# same NAME REPO OTHER: REPO is whole and has OTHER's tip
same() {
    if whole "$2" && [ -n "$(tip "$2")" ] && [ "$(tip "$2")" = "$(tip "$3")" ]; then pass "$1"
    else bad "$1" "tip $(tip "$2") vs $(tip "$3")"; fi
}
# keep REPO, then unchanged NAME: REPO is whole and its tip is what it was
keep() { KEPT_REPO=$1; KEPT_TIP=$(tip "$1"); }
unchanged() {
    if whole "$KEPT_REPO" && [ "$(tip "$KEPT_REPO")" = "$KEPT_TIP" ]; then pass "$1"
    else bad "$1" "tip $(tip "$KEPT_REPO"), was $KEPT_TIP"; fi
}
# server-side check-in with stock fossil: FILE TEXT
server_commit() {
    [ -d "$T/sck" ] || { mkdir -p "$T/sck"; (cd "$T/sck" && fossil open "$T/S.fossil" trunk >/dev/null 2>&1 && fossil settings autosync off >/dev/null 2>&1); }
    (cd "$T/sck" && { fossil update --user alice >/dev/null 2>&1; true; } && echo "$2" > "$1" && fossil add "$1" >/dev/null 2>&1 && fossil commit -m "server: $1" --user alice --no-warnings >/dev/null 2>&1)
}
since() { echo $(($(date +%s) - $1)); }

export SUIT_CA_BUNDLE=$C/ca1.pem
U=https://localhost:$P_TLS/

# ---- clone, pull, push, a second clone ----------------------------------------------

export SUIT_HOME=$T/A
check clone 0 "" -- "$SUIT" clone wd "$U"
[ "$(cat "$T/out")" = "cloned wd" ] && pass clone-says-cloned || bad clone-says-cloned "$(cat "$T/out")"
same clone-matches-the-server "$T/A/wardrobes/wd.fossil" "$T/S.fossil"
[ "$(cat "$T/A/remotes")" = "wd $U" ] && [ ! -e "$T/A/cafiles" ] && pass clone-records-the-remote || bad clone-records-the-remote
printf '%s\n' "$SECRET" | "$SUIT" login wd alice
check no-change-sync 0 "" -- "$SUIT" sync wd
[ ! -s "$T/out" ] && [ ! -s "$T/err" ] && pass no-change-sync-is-quiet || bad no-change-sync-is-quiet "$(cat "$T/out" "$T/err")"
same no-change-sync-matches "$T/A/wardrobes/wd.fossil" "$T/S.fossil"

export SUIT_HOME=$T/B
check second-clone 0 "" -- "$SUIT" clone wd "$U"

server_commit from-server.txt "made on the server"
export SUIT_HOME=$T/A
check pull 0 "" -- "$SUIT" sync wd
same pull-matches-the-server "$T/A/wardrobes/wd.fossil" "$T/S.fossil"
[ "$(cat "$T/A/store/wd/from-server.txt")" = "made on the server" ] && pass pull-updates-the-store || bad pull-updates-the-store

echo "pushed" > "$T/A/store/wd/pushed.txt"
"$SUIT" commit wd -m push >/dev/null 2>&1
check push 0 "" -- "$SUIT" sync wd
same push-reached-the-server "$T/S.fossil" "$T/A/wardrobes/wd.fossil"
[ "$(fossil cat pushed.txt -r tip -R "$T/S.fossil" 2>/dev/null)" = pushed ] && pass push-content-on-the-server || bad push-content-on-the-server

export SUIT_HOME=$T/B
printf '%s\n' "$SECRET" | "$SUIT" login wd alice
check pull-into-second-clone 0 "" -- "$SUIT" sync wd
same second-clone-matches "$T/B/wardrobes/wd.fossil" "$T/S.fossil"
[ -f "$T/B/store/wd/pushed.txt" ] && [ -f "$T/B/store/wd/from-server.txt" ] && pass second-clone-has-both || bad second-clone-has-both

echo "tended" > "$T/B/store/wd/tended.txt"
check tend 0 "" -- "$SUIT" tend wd
same tend-reached-the-server "$T/S.fossil" "$T/B/wardrobes/wd.fossil"
[ "$(fossil cat tended.txt -r tip -R "$T/S.fossil" 2>/dev/null)" = tended ] && pass tend-content-on-the-server || bad tend-content-on-the-server

# a push larger than the TLS buffer
head -c 150000 /dev/urandom | od -An -tx1 > "$T/B/store/wd/big.txt"
"$SUIT" commit wd -m big >/dev/null 2>&1
check big-push 0 "" -- "$SUIT" sync wd
same big-push-reached-the-server "$T/S.fossil" "$T/B/wardrobes/wd.fossil"

# tend with no name: two https wardrobes, two servers
"$SUIT" clone w2 "$U" >/dev/null 2>&1
printf '%s\n' "$SECRET" | "$SUIT" login w2 alice
check tend-all 0 "" -- "$SUIT" tend

# ---- where the trusted certificates come from ------------------------------------

export SUIT_HOME=$T/A
check ssl-cert-file 0 "" -- env SUIT_CA_BUNDLE="$T/none.pem" SSL_CERT_FILE="$C/ca1.pem" "$SUIT" sync wd
check ssl-cert-file-missing-falls-through 0 "" -- env SSL_CERT_FILE="$T/none.pem" "$SUIT" sync wd
check ssl-cert-file-empty-is-unset 0 "" -- env SSL_CERT_FILE= "$SUIT" sync wd
keep "$T/A/wardrobes/wd.fossil"
check no-bundle-at-all 18 "sync wd: no certificate file: none of $T/none.pem exists; set SSL_CERT_FILE" -- env SUIT_CA_BUNDLE="$T/none.pem" "$SUIT" sync wd
unchanged no-bundle-leaves-the-repository
check ssl-cert-file-not-pem 18 "$T/notpem.pem holds no certificate suit can use" -- env SSL_CERT_FILE="$T/notpem.pem" "$SUIT" sync wd
check ssl-cert-file-a-folder 18 "$T is not trusted: it is not a regular file" -- env SSL_CERT_FILE="$T" "$SUIT" sync wd
head -c 100 "$C/ca1.pem" > "$T/half.pem"
check ssl-cert-file-bad-pem 18 "half.pem: it is not PEM text" -- env SSL_CERT_FILE="$T/half.pem" "$SUIT" sync wd
sed '$d' "$C/ca1.pem" > "$T/noend.pem"
check ssl-cert-file-no-end 18 "noend.pem: its last PEM object has no END line" -- env SSL_CERT_FILE="$T/noend.pem" "$SUIT" sync wd
# with no override, the system's own bundle: it does not trust the test CA
check system-bundle-does-not-trust-the-test-ca 18 "" -- env -u SUIT_CA_BUNDLE "$SUIT" sync wd
unchanged system-bundle-leaves-the-repository
check sync-without-core-files-off 1 "cannot turn off core files" -- env SUIT_FAULT=setrlimit:1 "$SUIT" sync wd
check clone-without-core-files-off 1 "cannot turn off core files" -- env SUIT_FAULT=setrlimit:1 SUIT_HOME="$T/nocore" "$SUIT" clone wd "$U"
[ ! -e "$T/nocore/wardrobes/wd.fossil" ] && pass clone-without-core-files-off-leaves-nothing || bad clone-without-core-files-off-leaves-nothing
# a CA file that became writable by others after it was set is refused
export SUIT_HOME=$T/A
cp "$C/ca1.pem" "$T/a-ca.pem"; chmod 644 "$T/a-ca.pem"
"$SUIT" remote wd "$U" --ca "$T/a-ca.pem"
chmod 666 "$T/a-ca.pem"
check ca-file-made-writable-is-refused 18 "a-ca.pem is not trusted: it can be written by group or others" -- "$SUIT" sync wd
unchanged writable-ca-file-leaves-the-repository
"$SUIT" remote wd "$U"

# a remote's own CA file is used in place of the bundle
export SUIT_HOME=$T/C
check clone-with-a-ca-file 0 "" -- env SUIT_CA_BUNDLE="$T/none.pem" "$SUIT" clone wd "https://localhost:$P_CA2/" --ca "$C/ca2.pem"
same clone-with-a-ca-file-matches "$T/C/wardrobes/wd.fossil" "$T/S.fossil"
check remote-shows-the-ca-file 0 "" -- "$SUIT" remote wd
[ "$(cat "$T/out")" = "https://localhost:$P_CA2/
ca $C/ca2.pem" ] && pass remote-prints-the-ca-line || bad remote-prints-the-ca-line "$(cat "$T/out")"
[ "$(cat "$T/C/cafiles")" = "wd $C/ca2.pem" ] && pass cafiles-holds-the-path || bad cafiles-holds-the-path "$(cat "$T/C/cafiles")"
printf '%s\n' "$SECRET" | "$SUIT" login wd alice
check sync-with-a-ca-file 0 "" -- env SUIT_CA_BUNDLE="$T/none.pem" "$SUIT" sync wd
keep "$T/C/wardrobes/wd.fossil"
check ca-file-replaces-the-bundle 0 "" -- "$SUIT" remote wd "$U" --ca "$C/ca2.pem"
check ca-file-is-not-added-to-the-bundle 18 "not signed by a CA suit trusts" -- "$SUIT" sync wd
unchanged ca-file-refusal-leaves-the-repository
check remote-without-ca-forgets-it 0 "" -- "$SUIT" remote wd "$U"
[ ! -s "$T/C/cafiles" ] && pass cafiles-entry-removed || bad cafiles-entry-removed "$(cat "$T/C/cafiles")"
check sync-after-forgetting-the-ca-file 0 "" -- "$SUIT" sync wd
check remote-set-ca-again 0 "" -- "$SUIT" remote wd "https://localhost:$P_CA2/" --ca "$C/ca2.pem"
check remote-replace-ca 0 "" -- "$SUIT" remote wd "https://localhost:$P_CA2/" --ca "$C/leafca2.chain.pem"
[ "$(cat "$T/C/cafiles")" = "wd $C/leafca2.chain.pem" ] && pass cafiles-entry-replaced || bad cafiles-entry-replaced "$(cat "$T/C/cafiles")"
check ca-file-that-is-a-chain 0 "" -- "$SUIT" sync wd
check remote-none-forgets-the-ca 0 "" -- "$SUIT" remote wd --none
[ ! -s "$T/C/cafiles" ] && pass none-removes-the-cafiles-entry || bad none-removes-the-cafiles-entry
"$SUIT" remote wd "https://localhost:$P_CA2/" --ca "$C/ca2.pem"
check cafiles-write-fails 1 "cannot write $T/C/cafiles" -- env SUIT_FAULT=rename:1 "$SUIT" remote wd "https://localhost:$P_CA2/" --ca "$C/ca2.pem"
for i in $(seq 1 256); do echo "w$i /a.pem"; done > "$T/C/cafiles"
check cafiles-full 1 "too many CA files (limit 256)" -- "$SUIT" remote wd "$U" --ca "$C/ca1.pem"
"$SUIT" remote w1 "$U" >/dev/null 2>&1
check cafiles-full-still-replaces 0 "" -- "$SUIT" remote w1 "$U" --ca "$C/ca1.pem"
rm -f "$T/C/cafiles"
check clone-ca-file-unusable 18 "holds no certificate suit can use" -- "$SUIT" clone w3 "$U" --ca "$T/notpem.pem"
[ ! -e "$T/C/wardrobes/w3.fossil" ] && pass clone-ca-file-unusable-leaves-nothing || bad clone-ca-file-unusable-leaves-nothing

# ---- the certificate's checks ---------------------------------------------------

export SUIT_HOME=$T/A
keep "$T/A/wardrobes/wd.fossil"
"$SUIT" remote wd "https://127.0.0.1:$P_TLS/"
check host-name-mismatch 18 "the server's certificate is for another host name" -- "$SUIT" sync wd
unchanged host-name-mismatch-leaves-the-repository
"$SUIT" remote wd "https://localhost:$P_CA2/"
check untrusted-ca 18 "the server's certificate is not signed by a CA suit trusts" -- "$SUIT" sync wd
unchanged untrusted-ca-leaves-the-repository
"$SUIT" remote wd "$U"
check expired-by-the-clock 18 "the server's certificate has expired or is not valid yet" -- env SUIT_NOW=4102444800 "$SUIT" sync wd
unchanged expired-leaves-the-repository
if [ -n "$P_EXP" ]; then
    "$SUIT" remote wd "https://localhost:$P_EXP/"
    check expired-certificate 18 "has expired or is not valid yet" -- "$SUIT" sync wd
    unchanged expired-certificate-leaves-the-repository
else
    skip expired-certificate "this openssl cannot backdate a certificate (-not_before)"
fi
"$SUIT" remote wd "https://localhost:$P_HTTP/"
check plain-http-server-is-not-tls 18 "TLS error" -- "$SUIT" sync wd
"$SUIT" remote wd "https://localhost:$P_rsakex/"
check rsa-key-exchange-only-is-refused 18 "TLS alert 40: suit offers only TLS 1.2 with ECDHE" -- "$SUIT" sync wd
"$SUIT" remote wd "https://localhost:$P_cbc/"
check cbc-only-is-refused 18 "TLS alert 40" -- "$SUIT" sync wd
"$SUIT" remote wd "https://localhost:$P_tls13/"
check tls13-only-is-named 18 "TLS alert 70 (protocol version): it may offer only TLS 1.3" -- "$SUIT" sync wd
"$SUIT" remote wd "https://localhost:$P_garbage/"
check not-a-tls-record 18 "TLS error" -- "$SUIT" sync wd
unchanged tls-refusals-leave-the-repository

# ---- logins ------------------------------------------------------------------------

"$SUIT" remote wd "$U"
printf '%s\n' "wrong-secret-9" | "$SUIT" login wd alice
check wrong-password 19 "" -- "$SUIT" sync wd
grep -q "wrong-secret-9" "$T/out" "$T/err" && bad wrong-password-not-printed || pass wrong-password-not-printed
"$SUIT" logout wd
echo anon > "$T/A/store/wd/anon.txt"; "$SUIT" commit wd -m anon >/dev/null 2>&1
check anonymous-push-is-refused 19 "not authorized to write" -- "$SUIT" sync wd
[ -z "$(fossil sql -R "$T/S.fossil" "SELECT 1 FROM filename WHERE name='anon.txt'" 2>/dev/null)" ] && pass anonymous-push-left-the-server-alone || bad anonymous-push-left-the-server-alone
whole "$T/A/wardrobes/wd.fossil" && pass refused-login-leaves-the-repository || bad refused-login-leaves-the-repository
printf '%s\n' "$SECRET" | "$SUIT" login wd alice
check push-after-the-login 0 "" -- "$SUIT" sync wd
grep -rl "$SECRET" "$T/A" | grep -v '/credentials$' | grep -q . && bad secret-only-in-credentials || pass secret-only-in-credentials

# ---- replies that are wrong ------------------------------------------------------------

keep "$T/A/wardrobes/wd.fossil"
"$SUIT" remote wd "https://localhost:$P_midclose/"
check server-drops-mid-reply 17 "" -- "$SUIT" sync wd
unchanged mid-reply-drop-leaves-the-repository
"$SUIT" remote wd "https://localhost:$P_truncated/"
check reply-shorter-than-its-length 17 "" -- "$SUIT" sync wd
unchanged short-reply-leaves-the-repository
"$SUIT" remote wd "https://localhost:$P_noclen_unclean/"
check close-delimited-without-close-notify 17 "" -- "$SUIT" sync wd
unchanged unclean-close-leaves-the-repository
"$SUIT" remote wd "https://localhost:$P_noclen/"
check close-delimited-with-close-notify 0 "" -- "$SUIT" sync wd
"$SUIT" remote wd "https://localhost:$P_chunked/"
check chunked-reply 0 "" -- "$SUIT" sync wd
same chunked-matches-the-server "$T/A/wardrobes/wd.fossil" "$T/S.fossil"
keep "$T/A/wardrobes/wd.fossil"
"$SUIT" remote wd "https://localhost:$P_redirect/"
check redirect-is-refused 17 "to https://localhost:$P_HTTP/" -- "$SUIT" sync wd
grep -qF "the remote moved; if you trust the address above, set it as the remote with: suit remote wd URL" "$T/err" && pass redirect-says-to-set-the-remote || bad redirect-says-to-set-the-remote "$(cat "$T/err")"
"$SUIT" remote wd "https://localhost:$P_error500/"
check http-500 17 "500" -- "$SUIT" sync wd
unchanged bad-replies-leave-the-repository
t0=$(date +%s)
"$SUIT" remote wd "https://localhost:$P_nothing/"
check silent-server-times-out 17 "No response from localhost within 2 seconds" -- env SUIT_NET_TIMEOUT=2 "$SUIT" sync wd
[ "$(since "$t0")" -le 12 ] && pass silent-server-within-the-limit || bad silent-server-within-the-limit "$(since "$t0") s"
t0=$(date +%s)
check whole-transfer-deadline 17 "the transfer took longer than 2 seconds" -- env SUIT_NET_DEADLINE=2 "$SUIT" sync wd
[ "$(since "$t0")" -le 12 ] && pass whole-transfer-deadline-holds || bad whole-transfer-deadline-holds "$(since "$t0") s"
"$SUIT" remote wd "https://localhost:1/"
check closed-port 17 "cannot connect to localhost:1" -- "$SUIT" sync wd
# no connection is made: the port is all this is about
"$SUIT" remote wd "https://localhost/r"
check default-port-is-443 17 "cannot connect to localhost:443" -- env SUIT_FAULT=connect:1,connect:2 "$SUIT" sync wd
unchanged network-failures-leave-the-repository

# ---- the name lookup ----------------------------------------------------------------

"$SUIT" remote wd "$U"
t0=$(date +%s)
check lookup-that-never-answers 17 "the name lookup sent nothing for 2 seconds" -- env SUIT_NET_TIMEOUT=2 SUIT_FAULT=getaddrinfo:1:hang "$SUIT" sync wd
[ "$(since "$t0")" -le 12 ] && pass lookup-deadline-holds || bad lookup-deadline-holds "$(since "$t0") s"
check lookup-fails 17 "cannot find localhost:" -- env SUIT_FAULT=getaddrinfo:1 "$SUIT" sync wd
check lookup-socketpair-fails 17 "socketpair:" -- env SUIT_FAULT=socketpair:1 "$SUIT" sync wd
check lookup-fork-fails 17 "fork:" -- env SUIT_FAULT=fork:1 "$SUIT" sync wd
check lookup-answer-unread 17 "the name lookup of localhost gave no answer" -- env SUIT_FAULT=recv:1 "$SUIT" sync wd
check lookup-answer-unsent 17 "the name lookup of localhost gave no answer" -- env SUIT_FAULT=send:1,send:2 "$SUIT" sync wd
check lookup-poll-fails 17 "poll:" -- env SUIT_FAULT=poll:1 "$SUIT" sync wd
check lookup-poll-interrupted 0 "" -- env SUIT_FAULT=poll:1:EINTR "$SUIT" sync wd
check lookup-child-not-signalled 0 "" -- env SUIT_FAULT=signal:1 "$SUIT" sync wd
unchanged lookup-failures-leave-the-repository
printf 'wd\n' > "$T/A/cafiles"
check cafiles-unreadable-stops-the-sync 1 "cafiles:1: expected NAME PATH" -- "$SUIT" sync wd
rm -f "$T/A/cafiles"

# ---- every failure of a call, one at a time -------------------------------------------

check entropy-fails 18 "cannot get random bytes for TLS" -- env SUIT_FAULT=entropy:1 "$SUIT" sync wd
check socket-fails 17 "socket:" -- env SUIT_FAULT=socket:1,socket:2 "$SUIT" sync wd
check fcntl-fails 17 "fcntl:" -- env SUIT_FAULT=fcntl:1,fcntl:2 "$SUIT" sync wd
check connect-fails 17 "cannot connect to localhost:$P_TLS" -- env SUIT_FAULT=connect:1,connect:2 "$SUIT" sync wd
check getsockopt-fails 17 "cannot connect to localhost:$P_TLS" -- env SUIT_FAULT=getsockopt:1,getsockopt:2 "$SUIT" sync wd
# found: the Nth call that hits a given spot is counted, not assumed
found() { # NAME WANT_RC MESSAGE FAULT_PREFIX MAX
    fn=$1 fw=$2 fm=$3 fp=$4
    for i in $(seq 1 "$5"); do
        env SUIT_FAULT="$fp:$i" "$SUIT" sync wd >"$T/out" 2>"$T/err"
        if [ $? = "$fw" ] && grep -qF -- "$fm" "$T/err"; then pass "$fn"; return; fi
    done
    bad "$fn" "no $fp:1..$5 gave exit $fw with '$fm'"
}
found wall-clock-fails 18 "cannot read the date to check certificates" wall 6
found monotonic-clock-fails 17 "cannot read the clock" clock_gettime 12
# sweep NAME MAX [KIND]: NAME:1..MAX[:KIND], each on a sync; it ends 0, 17 or
# 18 and the repository is whole and where it was or at the server's tip
sweep() {
    sw_ok=true
    for i in $(seq 1 "$2"); do
        env SUIT_FAULT="$1:$i${3:+:$3}" "$SUIT" sync wd >"$T/out" 2>"$T/err"; rc=$?
        case $rc in 0|17|18) ;; *) sw_ok=false; bad "sweep-$1-$i${3:+-$3}" "exit $rc: $(head -c 300 "$T/err")" ;; esac
        whole "$T/A/wardrobes/wd.fossil" || { sw_ok=false; bad "sweep-$1-$i${3:+-$3}-repository"; }
    done
    $sw_ok && pass "sweep-$1${3:+-$3}"
}
echo again > "$T/A/store/wd/again.txt"; "$SUIT" commit wd -m again >/dev/null 2>&1
for f in recv send poll; do sweep $f 12; done
for k in EINTR EAGAIN short; do sweep recv 8 $k; sweep send 8 $k; done
check sync-after-the-sweeps 0 "" -- "$SUIT" sync wd
same sweeps-end-at-the-server "$T/A/wardrobes/wd.fossil" "$T/S.fossil"
# a big push, with the send failing at each call in turn
head -c 150000 /dev/urandom | od -An -tx1 > "$T/A/store/wd/big2.txt"
"$SUIT" commit wd -m big2 >/dev/null 2>&1
sweep send 40
check big-push-after-the-sweep 0 "" -- "$SUIT" sync wd
same big-push-sweep-ends-at-the-server "$T/A/wardrobes/wd.fossil" "$T/S.fossil"

# a clone that fails at any call leaves nothing behind
export SUIT_HOME=$T/D
cl_ok=true
for spec in recv:2 recv:5 send:3 send:6 poll:3 connect:1,connect:2 entropy:1 fork:1; do
    rm -rf "$T/D"
    env SUIT_FAULT=$spec "$SUIT" clone wd "$U" >"$T/out" 2>"$T/err"; rc=$?
    case $rc in 0) ;; 17|18) [ -z "$(ls "$T/D/wardrobes" 2>/dev/null)" ] && [ ! -e "$T/D/store/wd" ] && [ ! -e "$T/D/remotes" ] || { cl_ok=false; bad "clone-fault-$spec-left-something"; } ;;
        *) cl_ok=false; bad "clone-fault-$spec" "exit $rc: $(head -c 300 "$T/err")" ;; esac
done
$cl_ok && pass clone-failures-leave-nothing

stop_all
leftover=
for n in $STARTED; do
    p=$(cat "$T/$n.port" 2>/dev/null)
    [ -n "$p" ] && nc -z 127.0.0.1 "$p" >/dev/null 2>&1 && leftover="$leftover $p"
done
[ -z "$leftover" ] && pass servers-stopped || bad servers-stopped "still listening:$leftover"

echo "https.sh: $total cases, $failed failed"
[ "$failed" -eq 0 ]