You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

124 lines
3.6 KiB

#!/usr/bin/env bash
# No real need to use stderr here since it's just tests.
set -eE
shellcheck "$0"
# Before we build...
go fmt
go doc
# This comes later because it requires a running daemon
# go test
go fmt ./cmd/burnpaste/
go test ./cmd/burnpaste/ -v
go doc ./cmd/burnpaste/
go build
go build ./cmd/burnpaste/
strip -s burnpaste
mkdir test_dir
cd test_dir
../burnpaste &
PID=$!
# So, there's a bug in shellcheck where disabling SC2103 doesn't work.
# shellcheck disable=SC2103
# cd ..
cd "$(echo .. | grep .)"
cleanup() {
echo "Cleaning up."
kill "$PID"
rm -r test_dir
}
trap cleanup $(seq 1 15)
fail() {
echo "FAIL: $1"
cleanup
exit 1
}
sha256file() {
# Linux
# sha256filesum "$1" | cut -d ' ' -f 1
# FreeBSD
if [ "$1" = "-" ]; then
# stdin
sha256 | awk '{print $NF}'
else
sha256 "$1" | awk '{print $NF}'
fi
}
# Be sure we are running.
sleep 2
go test
# Test security, slightly.
echo 'This should give a 400.'
output=$(curl -s --show-error --fail "http://localhost:2323/read/README.md" || true)
[ ! -f 'README.md' ] && fail 'Whoops, we deleted README.md'
# Functional tests.
pasteid=$(echo test | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2323"/write)
[ -z "$pasteid" ] && fail 'pasteid empty.'
[ "$pasteid" != "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2" ] && fail 'pasteid does not match.'
output=$(curl -s --show-error --fail "http://localhost:2323/read/$pasteid")
[ "$output" != 'test' ] && fail 'Paste output inconsistent.'
echo 'This should give a 404.'
output=$(curl -s --show-error --fail "http://localhost:2323/read/$pasteid" || true)
[ "$output" = 'test' ] && fail 'Paste not deleted.'
# Now test with burnpaste.go
pasteid=$(curl -s --show-error --fail --data-urlencode data@- "http://localhost:2323"/write < burnpaste.go)
[ "$pasteid" != "$(sha256file burnpaste.go)" ] && fail 'pasteid does not match for burnpaste.go.'
output_checksum=$(curl -s --show-error --fail "http://localhost:2323/read/$pasteid" | sha256file -)
[ "$pasteid" != "$output_checksum" ] && fail 'output does not match for burnpaste.go.'
echo -n | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2323"/write && fail "Should error on empty input."
# This is.. approximate because it's URL encoded.
yes | head -c 4096 | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2323"/write || fail "Should allow a 4096 byte post."
yes | head -c 4097 | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2323"/write && fail "Should fail on anything greater than 4096 bytes."
[ "$(wc -l test_dir/309a1668b23adc98b0ec1b67d55bdca1e89e9d81c0930d5baf9b85df85d76ee0)" != "4096" ] || fail "4096 byte file wrong length or checksum???"
output_checksum=$(curl -s --show-error --fail "http://localhost:2323/read/309a1668b23adc98b0ec1b67d55bdca1e89e9d81c0930d5baf9b85df85d76ee0" | sha256file -)
[ "309a1668b23adc98b0ec1b67d55bdca1e89e9d81c0930d5baf9b85df85d76ee0" != "$output_checksum" ] && fail 'output does not match for 4096 byte paste'
for i in $(seq 1 128); do
yes | head -c "$i" | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2323"/write || fail "Should be able to add paste number $i"
done
echo test | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2323"/feedback && fail "Should not be able to make more than 128 feedbacks."
[ "$(find test_dir/ -type f | wc -l)" -ne 128 ] && fail "We don't have exactly 128 pastes??"
cleanup
sleep 0.1
echo "burnpaste.go sha256: $pasteid"
echo
echo Success.