Skip to content

Commit

Permalink
Add test to find invalid IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
duhow committed Oct 28, 2023
1 parent 4fae7c1 commit d0f77ef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
30 changes: 28 additions & 2 deletions pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
_fail(){ echo "::error::$@"; FAIL=1; }
_fail_exit(){ _fail "$@"; exit 1; }

test_list_has_unique_ip_addrs(){
FILE="source"
FILE="source"

test_0_list_has_unique_ip_addrs(){
REPEATED=`grep -ve '^#' -ve '^$' $FILE | sort | uniq -d`
if [ -n "$REPEATED" ]; then
echo "${REPEATED}"
Expand All @@ -18,6 +18,32 @@ test_list_has_unique_ip_addrs(){
# Filter:
# grep -vFf duplicated_list.txt new_list.txt | awk '{print $1}' > final_list.txt

test_1_ip_is_valid(){
# extract only up to first space, exclude comments (hopefully)
IP_LIST=`grep -ve '^#' -ve '^$' $FILE | awk '{print $1}'`
while IFS= read -r ip; do
# count how many dots are in string
dots="${ip//[^.]/}"
if [ "${#dots}" -ne 3 ]; then
_fail "Invalid IP address: $ip"
continue
fi

IFS='.' read -r -a blocks <<< "$ip"
for block in "${blocks[@]}"; do
if ! [[ $block =~ ^[0-9]+(\/[0-9]+)?$ ]]; then
_fail "Invalid IP address: $ip"
continue 2 # Skip to the next IP address
fi

if ((block < 0 || block > 255)); then
_fail "Invalid IP address: $ip"
continue 2 # Skip to the next IP address
fi
done
done <<< $IP_LIST
}

# ---

FAIL=0
Expand Down
1 change: 0 additions & 1 deletion source
Original file line number Diff line number Diff line change
Expand Up @@ -2765,7 +2765,6 @@
68.132.154.22
79.52.3.40
95.95.177.143
4.52.168
104.224.54.179
111.242.34.86
113.124.84.254
Expand Down

0 comments on commit d0f77ef

Please sign in to comment.