[tor-commits] [stem/master] Python 3 compatability for mapaddress test

atagar at torproject.org atagar at torproject.org
Mon Oct 14 00:42:16 UTC 2013


commit 6c81b101dd69cf9f4b9170aa089d98e38220f383
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Oct 13 17:25:34 2013 -0700

    Python 3 compatability for mapaddress test
    
    Quite a few byte vs unicode gotchas. Also, the behavior of indexing into byte
    strings changed in python 3 (it provides ints rather than characters).
    
    ======================================================================
    ERROR: test_mapaddress
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/atagar/Desktop/stem/test/data/python3/test/integ/control/controller.py", line 813, in test_mapaddress
        test.network.negotiate_socks(s, '1.2.1.2', 80)
      File "/home/atagar/Desktop/stem/test/data/python3/test/network.py", line 321, in negotiate_socks
        request = "\x04\x01" + struct.pack("!H", port) + "\x00\x00\x00\x01" + "\x00" + host + "\x00"
    TypeError: Can't convert 'bytes' object to str implicitly
---
 test/integ/control/controller.py |    7 ++++---
 test/network.py                  |    6 ++++--
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 69112cf..1c0e468 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -16,6 +16,7 @@ import stem.descriptor.reader
 import stem.descriptor.router_status_entry
 import stem.response.protocolinfo
 import stem.socket
+import stem.util.str_tools
 import stem.version
 import test.network
 import test.runner
@@ -811,7 +812,7 @@ class TestController(unittest.TestCase):
           s.settimeout(30)
           s.connect(('127.0.0.1', int(controller.get_conf('SocksListenAddress').rsplit(':', 1)[1])))
           test.network.negotiate_socks(s, '1.2.1.2', 80)
-          s.sendall(test.network.ip_request)  # make the http request for the ip address
+          s.sendall(stem.util.str_tools._to_bytes(test.network.ip_request))  # make the http request for the ip address
           response = s.recv(1000)
 
           if response:
@@ -826,9 +827,9 @@ class TestController(unittest.TestCase):
 
       # everything after the blank line is the 'data' in a HTTP response.
       # The response data for our request for request should be an IP address + '\n'
-      ip_addr = response[response.find("\r\n\r\n"):].strip()
+      ip_addr = response[response.find(b"\r\n\r\n"):].strip()
 
-      self.assertTrue(stem.util.connection.is_valid_ipv4_address(ip_addr))
+      self.assertTrue(stem.util.connection.is_valid_ipv4_address(stem.util.str_tools._to_unicode(ip_addr)))
 
   def test_get_microdescriptor(self):
     """
diff --git a/test/network.py b/test/network.py
index 78ebf21..fbb378a 100644
--- a/test/network.py
+++ b/test/network.py
@@ -20,6 +20,8 @@ import socket
 import struct
 
 import stem.util.connection
+import stem.util.str_tools
+
 from stem import ProtocolError, SocketError
 
 # Store a reference to the original class so we can find it after
@@ -318,11 +320,11 @@ def negotiate_socks(sock, host, port):
   """
 
   # SOCKS4a request here - http://en.wikipedia.org/wiki/SOCKS#Protocol
-  request = "\x04\x01" + struct.pack("!H", port) + "\x00\x00\x00\x01" + "\x00" + host + "\x00"
+  request = b"\x04\x01" + struct.pack("!H", port) + b"\x00\x00\x00\x01" + b"\x00" + stem.util.str_tools._to_bytes(host) + b"\x00"
   sock.sendall(request)
   response = sock.recv(8)
 
-  if len(response) != 8 or response[0] != "\x00" or response[1] != "\x5a":
+  if len(response) != 8 or response[0:2] != b"\x00\x5a":
     sock.close()
     raise ProtocolError(error_msgs.get(response[1], "SOCKS server returned unrecognized error code"))
 





More information about the tor-commits mailing list