commit 0a28ada462c62fd3db4c2803036ab25cfdcb68b4 Author: Arturo Filastò arturo@filasto.net Date: Thu Jan 26 11:38:21 2017 +0000
Fix the is_public_ipv4_address function
Fixes #727 in ooniprobe Bump web_connectivity version This bug was introduced in: 4f0c2af37093070e4cec0593a0f57568a4db231c --- ooni/common/ip_utils.py | 17 +++++++++++------ ooni/nettests/blocking/web_connectivity.py | 2 +- ooni/tests/test_common.py | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/ooni/common/ip_utils.py b/ooni/common/ip_utils.py index afc5bf43..370670b5 100644 --- a/ooni/common/ip_utils.py +++ b/ooni/common/ip_utils.py @@ -2,16 +2,21 @@ from ipaddr import IPv4Address, IPv6Address from ipaddr import AddressValueError
-def is_public_ipv4_address(address): - return not is_private_ipv4_address(address) +def in_private_ip_space(address): + ip_address = IPv4Address(address) + return any( + [ip_address.is_private, ip_address.is_loopback] + )
+def is_public_ipv4_address(address): + try: + return not in_private_ip_space(address) + except AddressValueError: + return False
def is_private_ipv4_address(address): try: - ip_address = IPv4Address(address) - return any( - [ip_address.is_private, ip_address.is_loopback] - ) + return in_private_ip_space(address) except AddressValueError: return False
diff --git a/ooni/nettests/blocking/web_connectivity.py b/ooni/nettests/blocking/web_connectivity.py index 629ef1c9..c3877d4a 100644 --- a/ooni/nettests/blocking/web_connectivity.py +++ b/ooni/nettests/blocking/web_connectivity.py @@ -48,7 +48,7 @@ class WebConnectivityTest(httpt.HTTPTest, dnst.DNSTest): "connect to the resolved IPs and then fetching the page " "and comparing all these results with those of a control.") author = "Arturo Filastò" - version = "0.1.0" + version = "0.2.0"
contentDecoders = [('gzip', GzipDecoder)]
diff --git a/ooni/tests/test_common.py b/ooni/tests/test_common.py index 1cd77cf4..eb8e2432 100644 --- a/ooni/tests/test_common.py +++ b/ooni/tests/test_common.py @@ -1,5 +1,6 @@ from twisted.trial import unittest from ooni.common.http_utils import META_CHARSET_REGEXP +from ooni.common.ip_utils import is_public_ipv4_address, is_private_ipv4_address
class TestHTTPUtils(unittest.TestCase): def test_charset_detection(self): @@ -17,3 +18,16 @@ class TestHTTPUtils(unittest.TestCase): self.assertEqual(META_CHARSET_REGEXP.search( with_two_charsets).group(1), 'UTF-8') self.assertEqual(META_CHARSET_REGEXP.search(with_empty_charset), None) + +class TestIPUtils(unittest.TestCase): + def test_is_public_ipv4(self): + self.assertTrue(is_public_ipv4_address('8.8.8.8')) + self.assertFalse(is_public_ipv4_address('example.com')) + self.assertFalse(is_public_ipv4_address('127.0.0.1')) + self.assertFalse(is_public_ipv4_address('192.168.1.1')) + + def test_is_private_ipv4(self): + self.assertFalse(is_private_ipv4_address('8.8.8.8')) + self.assertFalse(is_private_ipv4_address('example.com')) + self.assertTrue(is_private_ipv4_address('127.0.0.1')) + self.assertTrue(is_private_ipv4_address('192.168.2.2'))
tor-commits@lists.torproject.org