Просмотр исходного кода

ref(py3): Clarify hashlib usage between 2/3 (#20474)

Evan Purkhiser 4 лет назад
Родитель
Сommit
ea8936ed94
1 измененных файлов с 13 добавлено и 1 удалено
  1. 13 1
      tests/sentry/utils/hashlib/tests.py

+ 13 - 1
tests/sentry/utils/hashlib/tests.py

@@ -17,7 +17,12 @@ HASHLIB_VALUES_TESTS = (
     ("seed", six.binary_type("test".encode("utf-8")), "334e3fd2f66966a5c785d825c5f03494"),
     ("seed", six.text_type("test"), "ce35c0ce0d38976f61a5ca951de74a16"),
     ("seed", (4, 2), "d03b32e798444249d726158594d370f6"),
-    ("seed", {"test": 42}, "c545cf1c4ab09eff4a1e0fa5209f1645"),
+    ("seed", {six.text_type("test"): 42}, "ca094da15d323155e3954cff7ca373c4"),
+    # XXX: It should be noted these cases EXPLICLTY exclude the fact that
+    # python2 and python3 CANNOT hash to the same values whne using the `str`
+    # and not the text_type, since they will map to different cases (py2 will
+    # encode with the 0x06 'byte' marker and py3 will encode using the 0x07
+    # 'unicode' marker).
 )
 
 
@@ -26,6 +31,13 @@ def test_hash_values(seed, value, hash):
     assert hash_values([value], seed=seed) == hash
 
 
+def test_hashvalues_python23_strings():
+    if six.PY2:
+        assert hash_values(["test"], seed="seed") == "334e3fd2f66966a5c785d825c5f03494"
+    else:
+        assert hash_values(["test"], seed="seed") == "ce35c0ce0d38976f61a5ca951de74a16"
+
+
 class HashlibTest(TestCase):
     def test_simple(self):
         md5_text("x").hexdigest() == "9dd4e461268c8034f5c8564e155c67a6"