Files
troupe-standup/wizard-work/spark/ipv4_validator_artifact.md
T

1.8 KiB

IPv4 Address Validator Artifact

Files Created

  1. validate_ipv4.sh - Bash script that validates IPv4 addresses

    • Uses regex to match 4 octets (1-3 digits each)
    • Validates each octet is in range 0-255
    • Returns exit code 0 for valid, 1 for invalid
    • Outputs "valid" or "invalid"
  2. test_ipv4_validator.sh - Test suite with 24 test cases

    • 9 valid IPv4 addresses
    • 15 invalid edge cases (out of range, malformed, etc.)

Test Results

=== IPv4 Validator Test Suite ===

PASS: '192.168.1.1' -> valid (exit 0)
PASS: '0.0.0.0' -> valid (exit 0)
PASS: '255.255.255.255' -> valid (exit 0)
PASS: '10.0.0.1' -> valid (exit 0)
PASS: '172.16.0.1' -> valid (exit 0)
PASS: '8.8.8.8' -> valid (exit 0)
PASS: '127.0.0.1' -> valid (exit 0)
PASS: '1.2.3.4' -> valid (exit 0)
PASS: '200.200.200.200' -> valid (exit 0)
PASS: '256.1.1.1' -> invalid (exit 1)
PASS: '1.2.3' -> invalid (exit 1)
PASS: '1.2.3.4.5' -> invalid (exit 1)
PASS: 'a.b.c.d' -> invalid (exit 1)
PASS: '192.168.1.300' -> invalid (exit 1)
PASS: '999.999.999.999' -> invalid (exit 1)
PASS: '192.168.1.256' -> invalid (exit 1)
PASS: '255.255.255.256' -> invalid (exit 1)
PASS: '-1.2.3.4' -> invalid (exit 1)
PASS: '192.168.1.-1' -> invalid (exit 1)
PASS: '' -> invalid (exit 1)
PASS: '192.168.1.1.' -> invalid (exit 1)
PASS: '.192.168.1.1' -> invalid (exit 1)
PASS: '192.168.1.1a' -> invalid (exit 1)

=== Results: 0 failures ===
ALL TESTS PASSED

Implementation Details

The validator uses:

  1. String regex ^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$ to match the format
  2. Bash BASH_REMATCH array to capture octets
  3. Arithmetic comparison (( i > 255 )) to validate octet ranges

This approach correctly handles edge cases like leading zeros, negative numbers, and malformed addresses.