#!/usr/bin/env bash # End-to-end test of block-wise media streaming over CoAP/UDP. Self-contained: # generates test media, starts its own server, streams both files back and # compares them byte for byte. # ./test-stream.sh (needs: cargo build --release, ffmpeg, python3) # COAP_PORT picks the test server's port (default 5699). set -u cd "$(dirname "$0")" PORT=${COAP_PORT:-5699} DIR=$(mktemp -d) pass=0; fail=0 server_pid= cleanup() { [[ -n $server_pid ]] && kill "$server_pid" 2>/dev/null; rm -rf "$DIR"; } trap cleanup EXIT ok() { printf ' ok %s\n' "$1"; ((pass++)); } bad() { printf ' FAIL %s\n' "$1"; [[ -n ${2:-} ]] && printf '%s\n' "$2" | sed 's/^/ /'; ((fail++)); } for bin in coap-hello-udp stream client; do [[ -x target/release/$bin ]] || { echo "build first: cargo build --release"; exit 1; } done command -v ffmpeg >/dev/null || { echo "ffmpeg is needed to generate test media"; exit 1; } ffmpeg -hide_banner -loglevel error -f lavfi -i "sine=frequency=440:duration=10" \ -ac 1 -ar 16000 -c:a pcm_s16le "$DIR/audio.wav" ffmpeg -hide_banner -loglevel error -f lavfi -i "testsrc=size=320x240" \ -frames:v 1 -f rawvideo -pix_fmt rgb565le "$DIR/image.rgb565" MEDIA_DIR=$DIR COAP_PORT=$PORT target/release/coap-hello-udp >"$DIR/server.log" 2>&1 & server_pid=$! for _ in $(seq 50); do ss -lun | grep -q ":$PORT " && break sleep 0.1 done ss -lun | grep -q ":$PORT " || { echo "server did not start:"; cat "$DIR/server.log"; exit 1; } echo "block-wise streaming tests -> udp://127.0.0.1:$PORT" check_stream() { # check_stream local media=$1 src=$2 out="$DIR/received.$1" log local size; size=$(stat -c %s "$src") local blocks=$(( (size + 1023) / 1024 )) if ! log=$(COAP_PORT=$PORT target/release/stream "$media" --no-play --out "$out" 2>&1); then bad "stream $media" "$log"; return fi if cmp -s "$src" "$out"; then ok "stream $media: $size B received byte-identical" else bad "stream $media: received file differs from the source" fi if grep -q "received $size B in $blocks blocks" <<<"$log"; then ok "stream $media: $blocks blocks, $(grep -o '[0-9]* retransmits' <<<"$log")" else bad "stream $media: expected $size B in $blocks blocks" "$log" fi } check_stream image "$DIR/image.rgb565" check_stream audio "$DIR/audio.wav" reply=$(COAP_PORT=$PORT python3 coap-inspect.py get /image --block 150 2>&1) if grep -q "4.02 Bad Option" <<<"$reply"; then ok "block 150 of a 150-block image -> 4.02 Bad Option" else bad "block past the end -> 4.02" "$(tail -4 <<<"$reply")" fi reply=$(COAP_PORT=$PORT target/release/client post /image x 2>&1) if [[ $reply == "<- 4.05 "* ]]; then ok "POST /image -> 4.05"; else bad "POST /image -> 4.05" "$reply"; fi if out=$(COAP_PORT=$PORT ./test-coap.sh 2>&1); then ok "test-coap.sh on the same server: $(tail -1 <<<"$out")" else bad "test-coap.sh on the same server" "$(grep FAIL <<<"$out")" fi echo "$pass passed, $fail failed" [[ $fail -eq 0 ]]