Commits

Kelvin Wong committed aab3202 Draft

Test for base64 linefeed cleaner. Test to ensure verify function works with linefeeds and without

Comments (0)

Files changed (1)

tests/test_django_scrypt.py

 
 import django_scrypt
 from django_scrypt.hashers import (
-    ScryptPasswordHasher, base64, stringify)
+    ScryptPasswordHasher, enbase64, debase64, stringify,
+    clean_base64_scrypt_hash)
 
 try:
     import scrypt
         self.utf8_encoded = b'a\xc2\xac\xe1\x88\xb4\xe2\x82\xac\xe8\x80\x80'
         self.unicode_str = utf8(self.utf8_encoded)
         self.test_str = b'Test'
-        self.test_str_base64 = b'VGVzdA==\n'
+        self.test_str_base64 = b'VGVzdA=='
+        self.test_str_base64_lf = b'VGVzdA==\n'
 
-    def test_base64(self):
+    def test_enbase64(self):
         """Returns a Base64 encoded string"""
-        encoded = base64(self.test_str)
+        encoded = enbase64(self.test_str)
         expected = self.test_str_base64
         self.assertEqual(encoded, expected)
         self.assertTrue(isinstance(encoded, bytes))
 
+    def test_debase64(self):
+        """Decodes a Base64 encoded string"""
+        decoded = debase64(self.test_str_base64)
+        expected = self.test_str
+        self.assertEqual(decoded, expected)
+        self.assertTrue(isinstance(decoded, bytes))
+        decoded = debase64(self.test_str_base64_lf)
+        expected = self.test_str
+        self.assertEqual(decoded, expected)
+        self.assertTrue(isinstance(decoded, bytes))
+
     @skipUnless(not PYTHON3, "Python 2 required")
     def test_stringify_py2(self):
         """String returned is suitable for use by hasher"""
         # Unicode string is encoded to UTF-8
         self.assertTrue(isinstance(stringify(self.unicode_str), str))
 
+    def test_clean_base64_scrypt_hash(self):
+        """Hashes are cleaned of linefeeds"""
+        hash_lf = (
+            'scrypt$h64FF0EdQ1f9$14$8$1$64$refoMN0aZci6vt0VOPKB7tfgPS/wgAWXxz'
+            'e1z8CSzTzVSUV0hT3ByyE3YKPCpOegxJfZ6CZgQ+DS\nhd3G65p24Q==')
+        hash = (
+            'scrypt$h64FF0EdQ1f9$14$8$1$64$refoMN0aZci6vt0VOPKB7tfgPS/wgAWXxz'
+            'e1z8CSzTzVSUV0hT3ByyE3YKPCpOegxJfZ6CZgQ+DShd3G65p24Q==')
+        unicode_pwd = 'a\xac\u1234\u20ac\u8000'
+        h = clean_base64_scrypt_hash(hash_lf)
+        self.assertTrue(check_password(unicode_pwd, h))
+        h = clean_base64_scrypt_hash(hash)
+        self.assertTrue(check_password(unicode_pwd, h))
+
 
 @skipUnless(scrypt, "Uninstalled scrypt module needed to generate hash")
 class TestDjangoScrypt(TestCase):
             'e1z8CSzTzVSUV0hT3ByyE3YKPCpOegxJfZ6CZgQ+DS\nhd3G65p24Q==')
         unicode_pwd = 'a\xac\u1234\u20ac\u8000'
         self.assertTrue(check_password(unicode_pwd, py26_hash))
+
+    def test_base64_linefeed_indifferent(self):
+        """Hashes with linefeeds work the same as hashes without"""
+        py26_hash_lf = (
+            'scrypt$h64FF0EdQ1f9$14$8$1$64$refoMN0aZci6vt0VOPKB7tfgPS/wgAWXxz'
+            'e1z8CSzTzVSUV0hT3ByyE3YKPCpOegxJfZ6CZgQ+DS\nhd3G65p24Q==')
+        py26_hash = (
+            'scrypt$h64FF0EdQ1f9$14$8$1$64$refoMN0aZci6vt0VOPKB7tfgPS/wgAWXxz'
+            'e1z8CSzTzVSUV0hT3ByyE3YKPCpOegxJfZ6CZgQ+DShd3G65p24Q==')
+        unicode_pwd = 'a\xac\u1234\u20ac\u8000'
+        self.assertTrue(check_password(unicode_pwd, py26_hash))
+        self.assertTrue(check_password(unicode_pwd, py26_hash_lf))