From 89c80947df964b6853ce22dba1f20e246bdcf48d Mon Sep 17 00:00:00 2001 From: Joseph Blackman Date: Wed, 17 Mar 2021 03:09:13 -0700 Subject: [PATCH] Improve regexes for ipv4 matching Signed-off-by: jbzdarkid --- automated install/basic-install.sh | 4 +-- test/test_automated_install.py | 58 ++++++++++++++---------------- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 762ee110..4c9352b8 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -1018,9 +1018,9 @@ valid_ip() { local stat=1 # Regex matching one IPv4 component, i.e. an integer from 0 to 255. - local ipv4elem="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)"; + local ipv4elem="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})"; # Regex matching an optional port beginning with # matching optional port number starting '#' with range of 1 to 65536 - local portelem="(#([1-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-6]))?" + local portelem="(:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[0-9]{1,4}))?"; # Build a full IPv4 regex from the above subexpressions local regex="^${ipv4elem}\.${ipv4elem}\.${ipv4elem}\.${ipv4elem}${portelem}$" diff --git a/test/test_automated_install.py b/test/test_automated_install.py index f6b5a87e..20dadc26 100644 --- a/test/test_automated_install.py +++ b/test/test_automated_install.py @@ -524,43 +524,37 @@ def test_IPv6_ULA_GUA_test(Pihole): assert expected_stdout in detectPlatform.stdout -def test_validate_ip_valid(Pihole): +def test_validate_ip(Pihole): ''' - Given a valid IP address, valid_ip returns success + Tests valid_ip for various IP addresses ''' - output = Pihole.run(''' - source /opt/pihole/basic-install.sh - valid_ip "192.168.1.1" - ''') + def test_address(addr, success=True): + output = Pihole.run(''' + source /opt/pihole/basic-install.sh + valid_ip "{addr}" + '''.format(addr=addr)) - assert output.rc == 0 + assert output.rc == 0 if success else 1 - -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 + test_address('192.168.1.1') + test_address('127.0.0.1') + test_address('255.255.255.255') + test_address('255.255.255.256', False) + test_address('255.255.256.255', False) + test_address('255.256.255.255', False) + test_address('256.255.255.255', False) + test_address('1092.168.1.1', False) + test_address('not an IP', False) + test_address('8.8.8.8:', False) + test_address('8.8.8.8:0') + test_address('8.8.8.8:1') + test_address('8.8.8.8:42') + test_address('8.8.8.8:888') + test_address('8.8.8.8:1337') + test_address('8.8.8.8:65535') + test_address('8.8.8.8:65536', False) + test_address('8.8.8.8:-1', False) def test_os_check_fails(Pihole):