[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Accept bytes in is_valid_ipv4_address



commit bd2df36146bfc2bb38fac4d7888ca84f7eb70e8c
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Feb 25 17:03:59 2018 -0800

    Accept bytes in is_valid_ipv4_address
    
    With python3 byte arguments failed with...
    
      ======================================================================
      ERROR: test_is_valid_ipv4_address
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/stem/test/unit/util/connection.py", line 464, in test_is_valid_ipv4_address
          self.assertTrue(stem.util.connection.is_valid_ipv4_address(address))
        File "/home/atagar/Desktop/stem/stem/util/connection.py", line 415, in is_valid_ipv4_address
          if address.count('.') != 3:
      TypeError: a bytes-like object is required, not 'str'
---
 stem/util/connection.py      | 9 +++++++--
 test/unit/util/connection.py | 3 +++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/stem/util/connection.py b/stem/util/connection.py
index d989713a..3395a3a5 100644
--- a/stem/util/connection.py
+++ b/stem/util/connection.py
@@ -64,7 +64,7 @@ import re
 import stem.util.proc
 import stem.util.system
 
-from stem.util import str_type, conf, enum, log
+from stem.util import str_type, conf, enum, log, str_tools
 
 # Connection resolution is risky to log about since it's highly likely to
 # contain sensitive information. That said, it's also difficult to get right in
@@ -407,7 +407,9 @@ def is_valid_ipv4_address(address):
   :returns: **True** if input is a valid IPv4 address, **False** otherwise
   """
 
-  if not isinstance(address, (bytes, str_type)):
+  if isinstance(address, bytes):
+    address = str_tools._to_unicode(address)
+  elif not isinstance(address, str_type):
     return False
 
   # checks if theres four period separated values
@@ -435,6 +437,9 @@ def is_valid_ipv6_address(address, allow_brackets = False):
   :returns: **True** if input is a valid IPv6 address, **False** otherwise
   """
 
+  if isinstance(address, bytes):
+    address = str_tools._to_unicode(address)
+
   if allow_brackets:
     if address.startswith('[') and address.endswith(']'):
       address = address[1:-1]
diff --git a/test/unit/util/connection.py b/test/unit/util/connection.py
index 58a5cb82..a2162029 100644
--- a/test/unit/util/connection.py
+++ b/test/unit/util/connection.py
@@ -444,8 +444,10 @@ class TestConnection(unittest.TestCase):
     valid_addresses = (
       '0.0.0.0',
       '1.2.3.4',
+      '1.2.3.4',
       '192.168.0.1',
       '255.255.255.255',
+      b'255.255.255.255',
     )
 
     invalid_addresses = (
@@ -455,6 +457,7 @@ class TestConnection(unittest.TestCase):
       '1.2.3.-1',
       '0.0.0.a',
       'a.b.c.d',
+      b'a.b.c.d',
     )
 
     for address in valid_addresses:



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits