-
-
Save latagore/3fba7cec2665dd5cd81b to your computer and use it in GitHub Desktop.
Test IPv6 regular expression without dependency on custom scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
test_regex () { | |
REGEX="$1"; | |
TEST_STRINGS="$2"; | |
[ -n "$3" ] && [ "$3" = "expect_no_match" ] && SHOW_NON_MATCHING="-v"; | |
# find IP addresses anywhere in the string | |
# but avoid certain characters in the beginning and end | |
# so we don't get partial matches like | |
# 1.2.3.4 in 1.2.3.4.5. | |
# but we can match comma delimited strings like | |
# 1.2.3.4,6.7.8.9 | |
BEGIN_REGEX='(?<![a-zA-Z0-9:.])' | |
END_REGEX='(?![a-zA-Z0-9:.%])' | |
# test regex on strings and | |
# color code pass and fail messages | |
matches=`echo "$TEST_STRINGS" \ | |
|grep -C 10000 --label=test -H -P $SHOW_NON_MATCHING "${BEGIN_REGEX}(${REGEX})$END_REGEX" \ | |
|sed -e ''/test:/s//\`printf "\033[32mpass\033[0m--"\`/'' -e ''/test-/s//\`printf "\033[32mfail\033[0m--"\`/''` | |
echo "$matches" | |
} | |
#test strings expected to match the IPV4 regex | |
RE_IPV4="((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])" | |
IPV4_TEST_STRINGS=`cat <<EOF | |
127.0.0.1 | |
10.0.0.1 | |
192.168.1.1 | |
0.0.0.0 | |
255.255.255.255 | |
EOF` | |
results=`test_regex "$RE_IPV4" "$IPV4_TEST_STRINGS"` | |
echo "IPV4 Positive Test Cases:" | |
echo "$results" | |
echo | |
# test strings expected not to match the IPV4 regex | |
IPV4_NEG_TEST_STRINGS=`cat <<EOF | |
10002.3.4 | |
1.2.3.4.5 | |
256.0.0.0 | |
260.0.0.0 | |
EOF` | |
results=`test_regex "$RE_IPV4" "${IPV4_NEG_TEST_STRINGS}" "expect_no_match"` | |
echo "IPV4 Negative Test Cases (expected not to match the regex):" | |
echo "$results" | |
echo | |
#IPV6 tests | |
SEG="[0-9a-fA-F]{1,4}" | |
RE_IPV6="(${SEG}:){7,7}${SEG}|" # TEST: 1:2:3:4:5:6:7:8 | |
RE_IPV6="${RE_IPV6}(${SEG}:){1,7}:|" # TEST: 1:: 1:2:3:4:5:6:7:: | |
RE_IPV6="${RE_IPV6}(${SEG}:){1,6}:${SEG}|" # TEST: 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8 | |
RE_IPV6="${RE_IPV6}(${SEG}:){1,5}(:${SEG}){1,2}|" # TEST: 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8 | |
RE_IPV6="${RE_IPV6}(${SEG}:){1,4}(:${SEG}){1,3}|" # TEST: 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8 | |
RE_IPV6="${RE_IPV6}(${SEG}:){1,3}(:${SEG}){1,4}|" # TEST: 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8 | |
RE_IPV6="${RE_IPV6}(${SEG}:){1,2}(:${SEG}){1,5}|" # TEST: 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8 | |
RE_IPV6="${RE_IPV6}${SEG}:((:${SEG}){1,6})|" # TEST: 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8 | |
RE_IPV6="${RE_IPV6}:((:${SEG}){1,7}|:)|" # TEST: ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 :: | |
RE_IPV6="${RE_IPV6}fe08:(:${SEG}){2,2}%[0-9a-zA-Z]{1,}|" # TEST: fe08::7:8%eth0 fe08::7:8%1 (link-local IPv6 addresses with zone index) | |
RE_IPV6="${RE_IPV6}::(ffff(:0{1,4})?:)?${RE_IPV4}|" # TEST: ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255 (IPv4-mapped IPv6 addresses and IPv4-translated addresses) | |
RE_IPV6="${RE_IPV6}(${SEG}:){1,4}:${RE_IPV4}" # TEST: 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33 (IPv4-Embedded IPv6 Address) | |
#IPV6 positive test strings | |
IPV6_TEST_STRINGS=`cat <<EOF | |
1:2:3:4:5:6:7:8 | |
::ffff:10.0.0.1 | |
::ffff:1.2.3.4 | |
::ffff:0.0.0.0 | |
1:2:3:4:5:6:77:88 | |
::ffff:255.255.255.255 | |
fe08::7:8 | |
ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff | |
` | |
results=`test_regex "$RE_IPV6" "$IPV6_TEST_STRINGS"` | |
echo "IPV6 Test Cases:" | |
echo "$results" | |
echo | |
IPV6_NEG_TEST_STRINGS=`cat <<EOF | |
1:2:3:4:5:6:7:8:9 | |
1:2:3:4:5:6::7:8 | |
:1:2:3:4:5:6:7:8 | |
1:2:3:4:5:6:7:8: | |
::1:2:3:4:5:6:7:8 | |
1:2:3:4:5:6:7:8:: | |
1:2:3:4:5:6:7:88888 | |
2001:db8:3:4:5::192.0.2.33 | |
fe08::7:8% | |
fe08::7:8i | |
fe08::7:8interface | |
` | |
results=`test_regex "$RE_IPV6" "$IPV6_NEG_TEST_STRINGS" "expect_no_match"` | |
echo "IPV4 Negative Test Cases (expected not to match the regex):" | |
echo "$results" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment