[tor-commits] [stem/master] Using byte string for binascii.a2b_hex in python 3

atagar at torproject.org atagar at torproject.org
Sat Feb 2 18:20:49 UTC 2013


commit e6e5005b8afd9b7e0a1239a92d7c7a3f39e3ef42
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jan 27 12:28:59 2013 -0800

    Using byte string for binascii.a2b_hex in python 3
    
    Like base64.b64decode(), binascii.a2b_hex() expects a byte string.
    
    ======================================================================
    ERROR: test_valid_response
    ----------------------------------------------------------------------
    Traceback:
      File "/home/atagar/Desktop/stem/test/data/python3/test/unit/response/authchallenge.py", line 31, in test_valid_response
        stem.response.convert("AUTHCHALLENGE", control_message)
      File "/home/atagar/Desktop/stem/test/data/python3/stem/response/__init__.py", line 117, in convert
        message._parse_message(**kwargs)
      File "/home/atagar/Desktop/stem/test/data/python3/stem/response/authchallenge.py", line 40, in _parse_message
        self.server_hash = binascii.a2b_hex(value)
    TypeError: 'str' does not support the buffer interface
    
    ----------------------------------------------------------------------
    Ran 2 tests in 0.009s
---
 stem/connection.py                  |    7 ++++---
 stem/response/authchallenge.py      |    5 +++--
 test/unit/response/authchallenge.py |    4 ++--
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/stem/connection.py b/stem/connection.py
index 01da5b8..86d73d6 100644
--- a/stem/connection.py
+++ b/stem/connection.py
@@ -110,6 +110,7 @@ import stem.response
 import stem.socket
 import stem.util.connection
 import stem.util.enum
+import stem.util.str_tools
 import stem.util.system
 import stem.version
 
@@ -582,7 +583,7 @@ def authenticate_cookie(controller, cookie_path, suppress_ctl_errors = True):
   cookie_data = _read_cookie(cookie_path, False)
 
   try:
-    msg = "AUTHENTICATE %s" % binascii.b2a_hex(cookie_data)
+    msg = "AUTHENTICATE %s" % binascii.b2a_hex(stem.util.str_tools.to_bytes(cookie_data))
     auth_response = _msg(controller, msg)
 
     # if we got anything but an OK response then error
@@ -677,7 +678,7 @@ def authenticate_safecookie(controller, cookie_path, suppress_ctl_errors = True)
   client_nonce = os.urandom(32)
 
   try:
-    client_nonce_hex = binascii.b2a_hex(client_nonce)
+    client_nonce_hex = binascii.b2a_hex(stem.util.str_tools.to_bytes(client_nonce))
     authchallenge_response = _msg(controller, "AUTHCHALLENGE SAFECOOKIE %s" % client_nonce_hex)
 
     if not authchallenge_response.is_ok():
@@ -729,7 +730,7 @@ def authenticate_safecookie(controller, cookie_path, suppress_ctl_errors = True)
       CLIENT_HASH_CONSTANT,
       cookie_data + client_nonce + authchallenge_response.server_nonce)
 
-    auth_response = _msg(controller, "AUTHENTICATE %s" % (binascii.b2a_hex(client_hash)))
+    auth_response = _msg(controller, "AUTHENTICATE %s" % (binascii.b2a_hex(stem.util.str_tools.to_bytes(client_hash))))
   except stem.ControllerError, exc:
     try:
       controller.connect()
diff --git a/stem/response/authchallenge.py b/stem/response/authchallenge.py
index 5063b79..58bc48d 100644
--- a/stem/response/authchallenge.py
+++ b/stem/response/authchallenge.py
@@ -2,6 +2,7 @@ import binascii
 
 import stem.response
 import stem.socket
+import stem.util.str_tools
 import stem.util.tor_tools
 
 
@@ -37,7 +38,7 @@ class AuthChallengeResponse(stem.response.ControlMessage):
       if not stem.util.tor_tools.is_hex_digits(value, 64):
         raise stem.ProtocolError("SERVERHASH has an invalid value: %s" % value)
 
-      self.server_hash = binascii.a2b_hex(value)
+      self.server_hash = binascii.a2b_hex(stem.util.str_tools.to_bytes(value))
     else:
       raise stem.ProtocolError("Missing SERVERHASH mapping: %s" % line)
 
@@ -47,6 +48,6 @@ class AuthChallengeResponse(stem.response.ControlMessage):
       if not stem.util.tor_tools.is_hex_digits(value, 64):
         raise stem.ProtocolError("SERVERNONCE has an invalid value: %s" % value)
 
-      self.server_nonce = binascii.a2b_hex(value)
+      self.server_nonce = binascii.a2b_hex(stem.util.str_tools.to_bytes(value))
     else:
       raise stem.ProtocolError("Missing SERVERNONCE mapping: %s" % line)
diff --git a/test/unit/response/authchallenge.py b/test/unit/response/authchallenge.py
index a958322..5cdf347 100644
--- a/test/unit/response/authchallenge.py
+++ b/test/unit/response/authchallenge.py
@@ -14,8 +14,8 @@ VALID_RESPONSE = "250 AUTHCHALLENGE \
 SERVERHASH=B16F72DACD4B5ED1531F3FCC04B593D46A1E30267E636EA7C7F8DD7A2B7BAA05 \
 SERVERNONCE=653574272ABBB49395BD1060D642D653CFB7A2FCE6A4955BCFED819703A9998C"
 
-VALID_HASH = "\xb1or\xda\xcdK^\xd1S\x1f?\xcc\x04\xb5\x93\xd4j\x1e0&~cn\xa7\xc7\xf8\xddz+{\xaa\x05"
-VALID_NONCE = "e5t'*\xbb\xb4\x93\x95\xbd\x10`\xd6B\xd6S\xcf\xb7\xa2\xfc\xe6\xa4\x95[\xcf\xed\x81\x97\x03\xa9\x99\x8c"
+VALID_HASH = b"\xb1or\xda\xcdK^\xd1S\x1f?\xcc\x04\xb5\x93\xd4j\x1e0&~cn\xa7\xc7\xf8\xddz+{\xaa\x05"
+VALID_NONCE = b"e5t'*\xbb\xb4\x93\x95\xbd\x10`\xd6B\xd6S\xcf\xb7\xa2\xfc\xe6\xa4\x95[\xcf\xed\x81\x97\x03\xa9\x99\x8c"
 INVALID_RESPONSE = "250 AUTHCHALLENGE \
 SERVERHASH=FOOBARB16F72DACD4B5ED1531F3FCC04B593D46A1E30267E636EA7C7F8DD7A2B7BAA05 \
 SERVERNONCE=FOOBAR653574272ABBB49395BD1060D642D653CFB7A2FCE6A4955BCFED819703A9998C"





More information about the tor-commits mailing list