From fa8751f9ad89cbe1e9eb32784bdbb99e213ef390 Mon Sep 17 00:00:00 2001 From: Mark Drobnak Date: Wed, 10 Jul 2019 19:42:51 -0700 Subject: [PATCH 1/3] Fix error when checking if IP address is valid During install in `valid_ip`, we split up the IP address into octets to verify it is valid (each is <= 255). This validation was broken in #2743 when a variable usage was quoted where it should have stayed unquoted: ``` ./automated install/basic-install.sh: line 942: [[: 192.241.211.120: syntax error: invalid arithmetic operator (error token is ".241.211.120") ``` Due to this error, `127.0.0.1` would be used instead of the requested IP address. Also, this prevented the user from entering a custom DNS server as it would be marked as an invalid IP address. Signed-off-by: Mark Drobnak --- automated install/basic-install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index a4adac94..cb6783a2 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -934,7 +934,7 @@ valid_ip() { # and set the new one to a dot (period) IFS='.' # Put the IP into an array - ip=("${ip}") + ip=(${ip}) # Restore the IFS to what it was IFS=${OIFS} ## Evaluate each octet by checking if it's less than or equal to 255 (the max for each octet) From c156af020c017e0a0429724cba695a4d1984bba1 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Wed, 10 Jul 2019 19:52:17 -0700 Subject: [PATCH 2/3] Use suggested array creation to fix linter error Signed-off-by: Mcat12 --- automated install/basic-install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index cb6783a2..17bdde31 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -934,7 +934,7 @@ valid_ip() { # and set the new one to a dot (period) IFS='.' # Put the IP into an array - ip=(${ip}) + read -r -a ip <<< "${ip}" # Restore the IFS to what it was IFS=${OIFS} ## Evaluate each octet by checking if it's less than or equal to 255 (the max for each octet) From 1d5755a4c2712156bd16d30fe61fcaef229714c9 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Wed, 10 Jul 2019 21:18:58 -0700 Subject: [PATCH 3/3] Add tests for valid_ip Signed-off-by: Mcat12 --- test/test_automated_install.py | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/test_automated_install.py b/test/test_automated_install.py index 4bfb0f6a..cce11857 100644 --- a/test/test_automated_install.py +++ b/test/test_automated_install.py @@ -700,3 +700,42 @@ def test_IPv6_ULA_GUA_test(Pihole): ''') expected_stdout = 'Found IPv6 ULA address, using it for blocking IPv6 ads' assert expected_stdout in detectPlatform.stdout + + +def test_validate_ip_valid(Pihole): + ''' + Given a valid IP address, valid_ip returns success + ''' + + output = Pihole.run(''' + source /opt/pihole/basic-install.sh + valid_ip "192.168.1.1" + ''') + + assert output.rc == 0 + + +def test_validate_ip_invalid_octet(Pihole): + ''' + Given an invalid IP address (large octet), valid_ip returns an error + ''' + + output = Pihole.run(''' + source /opt/pihole/basic-install.sh + valid_ip "1092.168.1.1" + ''') + + assert output.rc == 1 + + +def test_validate_ip_invalid_letters(Pihole): + ''' + Given an invalid IP address (contains letters), valid_ip returns an error + ''' + + output = Pihole.run(''' + source /opt/pihole/basic-install.sh + valid_ip "not an IP" + ''') + + assert output.rc == 1