spark: add IPv4 address validator with test suite
This commit is contained in:
1 parent
e3b613366a
commit
a99fdd04dd
2 files changed
+104
No files matched your search
Executable
+71
@@ -0,0 +1,71 @@
|
||||
#!/bin/bash
|
||||
# test_ipv4_validator.sh - Automated tests for validate_ipv4.sh
|
||||
# Run this to verify the IPv4 validator works correctly
|
||||
|
||||
SCRIPT="/root/.hermes/wizard-outputs/spark/validate_ipv4.sh"
|
||||
ERRORS=0
|
||||
|
||||
test_case() {
|
||||
local input="$1"
|
||||
local expected="$2"
|
||||
local output
|
||||
output=$($SCRIPT "$input" 2>/dev/null)
|
||||
local exit_code=$?
|
||||
|
||||
if [[ "$expected" == "valid" ]]; then
|
||||
if [[ "$output" == "valid" && $exit_code -eq 0 ]]; then
|
||||
echo "PASS: '$input' -> valid (exit 0)"
|
||||
else
|
||||
echo "FAIL: '$input' expected valid, got '$output' (exit $exit_code)"
|
||||
((ERRORS++))
|
||||
fi
|
||||
else
|
||||
if [[ "$output" == "invalid" && $exit_code -ne 0 ]]; then
|
||||
echo "PASS: '$input' -> invalid (exit $exit_code)"
|
||||
else
|
||||
echo "FAIL: '$input' expected invalid, got '$output' (exit $exit_code)"
|
||||
((ERRORS++))
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
echo "=== IPv4 Validator Test Suite ==="
|
||||
echo ""
|
||||
|
||||
# Valid addresses
|
||||
test_case "192.168.1.1" "valid"
|
||||
test_case "0.0.0.0" "valid"
|
||||
test_case "255.255.255.255" "valid"
|
||||
test_case "10.0.0.1" "valid"
|
||||
test_case "172.16.0.1" "valid"
|
||||
test_case "8.8.8.8" "valid"
|
||||
test_case "127.0.0.1" "valid"
|
||||
test_case "1.2.3.4" "valid"
|
||||
test_case "200.200.200.200" "valid"
|
||||
|
||||
# Invalid addresses
|
||||
test_case "256.1.1.1" "invalid"
|
||||
test_case "1.2.3" "invalid"
|
||||
test_case "1.2.3.4.5" "invalid"
|
||||
test_case "a.b.c.d" "invalid"
|
||||
test_case "192.168.1.300" "invalid"
|
||||
test_case "999.999.999.999" "invalid"
|
||||
test_case "192.168.1.256" "invalid"
|
||||
test_case "255.255.255.256" "invalid"
|
||||
test_case "-1.2.3.4" "invalid"
|
||||
test_case "192.168.1.-1" "invalid"
|
||||
test_case "" "invalid"
|
||||
test_case "192.168.1.1." "invalid"
|
||||
test_case ".192.168.1.1" "invalid"
|
||||
test_case "192.168.1.1a" "invalid"
|
||||
|
||||
echo ""
|
||||
echo "=== Results: $ERRORS failures ==="
|
||||
|
||||
if [[ $ERRORS -eq 0 ]]; then
|
||||
echo "ALL TESTS PASSED"
|
||||
exit 0
|
||||
else
|
||||
echo "SOME TESTS FAILED"
|
||||
exit 1
|
||||
fi
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
# validate_ipv4.sh - Check if a string is a valid IPv4 address
|
||||
# Usage: validate_ipv4.sh <IP_ADDRESS>
|
||||
# Returns: 0 if valid, 1 if invalid
|
||||
# Output: "valid" or "invalid"
|
||||
|
||||
validate_ipv4() {
|
||||
local ip="$1"
|
||||
local regex='^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$'
|
||||
|
||||
if [[ ! $ip =~ $regex ]]; then
|
||||
echo "invalid"
|
||||
return 1
|
||||
fi
|
||||
|
||||
for i in "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}" "${BASH_REMATCH[4]}"; do
|
||||
if (( i > 255 )); then
|
||||
echo "invalid"
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
|
||||
echo "valid"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Main execution
|
||||
if [[ $# -ne 1 ]]; then
|
||||
echo "Usage: $0 <IP_ADDRESS>" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
validate_ipv4 "$1"
|
||||
Reference in new issue
Block a user