[tor-commits] [bridgedb/develop] Fix captcha tests

phw at torproject.org phw at torproject.org
Wed Feb 19 18:27:18 UTC 2020


commit 62e5f00925ad8d19a6395e930e0a1869ea5d84e4
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Jan 18 17:01:24 2020 -0800

    Fix captcha tests
    
    Nothing too particularly interesting...
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_captcha.py", line 63, in test_get
          if os.environ.has_key(envkey):
      builtins.AttributeError: '_Environ' object has no attribute 'has_key'
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_captcha.py", line 191, in test_get
          image, challenge = c.get()
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/captcha.py", line 391, in get
          self.image = imageFile.read()
        File "/usr/lib/python3.5/codecs.py", line 321, in decode
          (result, consumed) = self._buffer_decode(data, self.errors, final)
      builtins.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_captcha.py", line 163, in test_createChallenge_hmacValid
          challenge = c.createChallenge('ShouldHaveAValidHMAC')
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/captcha.py", line 369, in createChallenge
          encBlob = self.publicKey.encrypt(blob)
        File "/usr/local/lib/python3.5/dist-packages/Crypto/Cipher/PKCS1_OAEP.py", line 121, in encrypt
          db = lHash + ps + b'\x01' + _copy_bytes(None, None, message)
      builtins.TypeError: can't concat bytes to str
    
    Test results changed as follows...
    
      before: FAILED (skips=114, failures=21, errors=52, successes=797)
      after:  FAILED (skips=115, failures=29, errors=34, successes=806)
---
 bridgedb/captcha.py                  | 4 ++--
 bridgedb/distributors/moat/server.py | 4 ++--
 bridgedb/test/test_captcha.py        | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/bridgedb/captcha.py b/bridgedb/captcha.py
index a4753fa..fe0b876 100644
--- a/bridgedb/captcha.py
+++ b/bridgedb/captcha.py
@@ -366,7 +366,7 @@ class GimpCaptcha(Captcha):
         """
         timestamp = str(int(time.time())).zfill(12)
         blob = timestamp + answer
-        encBlob = self.publicKey.encrypt(blob)
+        encBlob = self.publicKey.encrypt(blob.encode('utf-8'))
         hmac = crypto.getHMAC(self.hmacKey, encBlob)
         challenge = urlsafe_b64encode(hmac + encBlob)
         return challenge
@@ -387,7 +387,7 @@ class GimpCaptcha(Captcha):
         try:
             imageFilename = random.choice(os.listdir(self.cacheDir))
             imagePath = os.path.join(self.cacheDir, imageFilename)
-            with open(imagePath) as imageFile:
+            with open(imagePath, 'rb') as imageFile:
                 self.image = imageFile.read()
         except IndexError:
             raise GimpCaptchaError("CAPTCHA cache dir appears empty: %r"
diff --git a/bridgedb/distributors/moat/server.py b/bridgedb/distributors/moat/server.py
index c2f59c1..4dde385 100644
--- a/bridgedb/distributors/moat/server.py
+++ b/bridgedb/distributors/moat/server.py
@@ -368,7 +368,7 @@ class CaptchaFetchResource(CaptchaResource):
             logging.error("Unhandled error while retrieving Gimp captcha!")
             logging.error(impossible)
 
-        return (capt.image, capt.challenge)
+        return (capt.image, capt.challenge.decode('utf-8') if isinstance(capt.challenge, bytes) else capt.challenge)
 
     def getPreferredTransports(self, supportedTransports):
         """Choose which transport a client should request, based on their list
@@ -476,7 +476,7 @@ class CaptchaFetchResource(CaptchaResource):
         }
 
         try:
-            data["data"][0]["image"] = base64.b64encode(image)
+            data["data"][0]["image"] = base64.b64encode(image).decode('utf-8')
         except Exception as impossible:
             logging.error("Could not construct or encode captcha!")
             logging.error(impossible)
diff --git a/bridgedb/test/test_captcha.py b/bridgedb/test/test_captcha.py
index 92cc716..cc0c99c 100644
--- a/bridgedb/test/test_captcha.py
+++ b/bridgedb/test/test_captcha.py
@@ -60,7 +60,7 @@ class ReCaptchaTests(unittest.TestCase):
         # Force urllib.request to do anything less idiotic than the defaults:
         envkey = 'HTTPS_PROXY'
         oldkey = None
-        if os.environ.has_key(envkey):
+        if envkey in os.environ:
             oldkey = os.environ[envkey]
         os.environ[envkey] = '127.0.0.1:9150'
         # This stupid thing searches the environment for ``<protocol>_PROXY``
@@ -179,7 +179,7 @@ class GimpCaptchaTests(unittest.TestCase):
         self.assertEqual(hmac, correctHMAC)
 
         decrypted = self.sekrit.decrypt(orig)
-        timestamp = int(decrypted[:12].lstrip('0'))
+        timestamp = int(decrypted[:12].lstrip(b'0'))
         # The timestamp should be within 30 seconds of right now.
         self.assertApproximates(timestamp, int(time.time()), 30)
         self.assertEqual('ThisAnswerShouldDecryptToThis', decrypted[12:])
@@ -238,7 +238,7 @@ class GimpCaptchaTests(unittest.TestCase):
         c = captcha.GimpCaptcha(self.publik, self.sekrit, self.hmacKey,
                                 self.cacheDir)
         image, challenge = c.get()
-        challengeBadB64 = challenge.rstrip('==') + "\x42\x42\x42"
+        challengeBadB64 = challenge.decode('utf-8').rstrip('==') + "\x42\x42\x42"
         self.assertEquals(
             c.check(challenge, c.answer, c.secretKey, c.hmacKey),
             True)





More information about the tor-commits mailing list