|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +THIS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 6 | +SCRIPTS_DIR="$(cd "${THIS_DIR}/.." && pwd)" |
| 7 | + |
| 8 | +# shellcheck source=../functions.sh |
| 9 | +source "${SCRIPTS_DIR}/functions.sh" |
| 10 | + |
| 11 | +failures=0 |
| 12 | + |
| 13 | +assert_equal() { |
| 14 | + local actual="$1" |
| 15 | + local expected="$2" |
| 16 | + local msg="$3" |
| 17 | + if [[ "$actual" != "$expected" ]]; then |
| 18 | + echo "FAIL: ${msg} (expected '${expected}', got '${actual}')" >&2 |
| 19 | + failures=$((failures + 1)) |
| 20 | + else |
| 21 | + echo "PASS: ${msg}" |
| 22 | + fi |
| 23 | +} |
| 24 | + |
| 25 | +assert_fails() { |
| 26 | + local msg="$1" |
| 27 | + shift |
| 28 | + local output_file |
| 29 | + output_file="$(mktemp)" |
| 30 | + if "$@" >"$output_file" 2>&1; then |
| 31 | + echo "FAIL: ${msg} (expected failure)" >&2 |
| 32 | + cat "$output_file" >&2 |
| 33 | + failures=$((failures + 1)) |
| 34 | + else |
| 35 | + cat "$output_file" |
| 36 | + echo "PASS: ${msg}" |
| 37 | + fi |
| 38 | + rm -f "$output_file" |
| 39 | +} |
| 40 | + |
| 41 | +# Successful cases |
| 42 | +assert_equal "$(sanitize_port 8080)" "8080" "sanitize_port accepts numeric port" |
| 43 | +assert_equal "$(sanitize_domain example.com)" "example.com" "sanitize_domain accepts fqdn" |
| 44 | +assert_equal "$(sanitize_domain '*.example.com')" "*.example.com" "sanitize_domain accepts wildcard" |
| 45 | +assert_equal "$(sanitize_target_endpoint http://service:80/path)" "http://service:80/path" "sanitize_target_endpoint accepts http" |
| 46 | +assert_equal "$(sanitize_target_endpoint grpc://svc:50051)" "grpc://svc:50051" "sanitize_target_endpoint accepts grpc" |
| 47 | +assert_equal "$(sanitize_client_max_body_size 50m)" "50m" "sanitize_client_max_body_size accepts suffix" |
| 48 | +assert_equal "$(sanitize_dns_label test_label)" "test_label" "sanitize_dns_label accepts lowercase" |
| 49 | +assert_equal "$(sanitize_dns_label test-label)" "test-label" "sanitize_dns_label accepts hyphen" |
| 50 | + |
| 51 | +# Failing cases |
| 52 | +assert_fails "sanitize_port rejects non-numeric" sanitize_port abc |
| 53 | +assert_fails "sanitize_domain rejects invalid domain" sanitize_domain bad_domain |
| 54 | +assert_fails "sanitize_target_endpoint rejects malformed URL" sanitize_target_endpoint "http:///broken" |
| 55 | +warning_output="$(sanitize_client_max_body_size "50mb" 2>&1 || true)" |
| 56 | +if [[ "$warning_output" == "Warning: Ignoring invalid CLIENT_MAX_BODY_SIZE value: 50mb" ]]; then |
| 57 | + echo "PASS: sanitize_client_max_body_size warns and returns empty" |
| 58 | +else |
| 59 | + echo "FAIL: sanitize_client_max_body_size warning unexpected" |
| 60 | + printf '%s\n' "$warning_output" |
| 61 | + failures=$((failures + 1)) |
| 62 | +fi |
| 63 | +assert_fails "sanitize_dns_label rejects invalid characters" sanitize_dns_label "bad*label" |
| 64 | + |
| 65 | +if [[ $failures -eq 0 ]]; then |
| 66 | + echo "All sanitizer tests passed" |
| 67 | +else |
| 68 | + echo "$failures sanitizer tests failed" >&2 |
| 69 | + exit 1 |
| 70 | +fi |
0 commit comments