diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2012-01-18 20:01:20 -0500 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2012-01-18 20:01:20 -0500 |
| commit | bebb8be9cca116e1331cdb9154b225a69fa9b8b7 (patch) | |
| tree | c34ef49378a1c9f8059ceadf9f39493dff181ef9 /passlib/tests | |
| parent | c1927edb87df4f22c5d5471e88f42b085a1a946a (diff) | |
| download | passlib-bebb8be9cca116e1331cdb9154b225a69fa9b8b7.tar.gz | |
misc bugfixes from round of changes
* added str_to_[ub]ascii to wrap hexdigest() calls
* fixed some h64big calls I missed
* some py3 fixes
* removed utils.compat.aliases, using overlay
to replace real compat module instead
(to agree w/ imports already in code)
Diffstat (limited to 'passlib/tests')
| -rw-r--r-- | passlib/tests/test_handlers.py | 6 | ||||
| -rw-r--r-- | passlib/tests/test_utils.py | 30 | ||||
| -rw-r--r-- | passlib/tests/test_utils_handlers.py | 37 |
3 files changed, 40 insertions, 33 deletions
diff --git a/passlib/tests/test_handlers.py b/passlib/tests/test_handlers.py index 702c1e3..414be37 100644 --- a/passlib/tests/test_handlers.py +++ b/passlib/tests/test_handlers.py @@ -1097,9 +1097,9 @@ class ScramTest(HandlerCase): # return appropriate value or throw KeyError h = "$scram$10$AAAAAA$sha-1=AQ,bbb=Ag,ccc=Aw" s = b('\x00')*4 - self.assertEqual(edi(h,"SHA1"), (s,10,'\x01')) - self.assertEqual(edi(h,"bbb"), (s,10,'\x02')) - self.assertEqual(edi(h,"ccc"), (s,10,'\x03')) + self.assertEqual(edi(h,"SHA1"), (s,10, b('\x01'))) + self.assertEqual(edi(h,"bbb"), (s,10, b('\x02'))) + self.assertEqual(edi(h,"ccc"), (s,10, b('\x03'))) self.assertRaises(KeyError, edi, h, "ddd") # config strings should cause value error. diff --git a/passlib/tests/test_utils.py b/passlib/tests/test_utils.py index a4e021d..d0609b7 100644 --- a/passlib/tests/test_utils.py +++ b/passlib/tests/test_utils.py @@ -12,7 +12,7 @@ import warnings #pkg #module from passlib.utils.compat import b, bytes, bascii_to_str, irange, PY2, PY3, u, \ - unicode + unicode, bjoin from passlib.tests.utils import TestCase, Params as ak, enable_option, catch_warnings def hb(source): @@ -350,8 +350,6 @@ class CodecTest(TestCase): #check byte inputs ignores enocding self.assertEqual(to_bytes(b('\x00\xc3\xbf'), "latin-1"), b('\x00\xc3\xbf')) - self.assertEqual(to_bytes(b('\x00\xc3\xbf'), None, "utf-8"), - b('\x00\xc3\xbf')) #check bytes transcoding self.assertEqual(to_bytes(b('\x00\xc3\xbf'), "latin-1", "utf-8"), @@ -388,8 +386,8 @@ class CodecTest(TestCase): from passlib.utils import to_native_str # test plain ascii - self.assertEqual(to_native_str(u('abc', 'ascii')), 'abc') - self.assertEqual(to_native_str(b('abc', 'ascii')), 'abc') + self.assertEqual(to_native_str(u('abc'), 'ascii'), 'abc') + self.assertEqual(to_native_str(b('abc'), 'ascii'), 'abc') # test invalid ascii if PY3: @@ -540,7 +538,7 @@ class _Base64Test(TestCase): # helper to generate bytemap-specific strings def m(self, *offsets): "generate byte string from offsets" - return b("").join(self.engine.bytemap[o:o+1] for o in offsets) + return bjoin(self.engine.bytemap[o:o+1] for o in offsets) #========================================================= # test encode_bytes @@ -712,12 +710,14 @@ class _Base64Test(TestCase): encode = getattr(engine, "encode_int%s" % bits) decode = getattr(engine, "decode_int%s" % bits) pad = -bits % 6 - chars = (bits+pad)/6 + chars = (bits+pad)//6 upper = 1<<bits # test encode func for value, encoded in encoded_pairs: - self.assertEqual(encode(value), encoded) + result = encode(value) + self.assertIsInstance(result, bytes) + self.assertEqual(result, encoded) self.assertRaises(ValueError, encode, -1) self.assertRaises(ValueError, encode, upper) @@ -785,11 +785,11 @@ class _Base64Test(TestCase): if not self.encoded_ints: raise self.skipTests("none defined for class") engine = self.engine - for encoded, value, bits in self.encoded_ints: + for data, value, bits in self.encoded_ints: encode = getattr(engine, "encode_int%d" % bits) decode = getattr(engine, "decode_int%d" % bits) - self.assertEqual(encode(value), encoded) - self.assertEqual(decode(encoded), value) + self.assertEqual(encode(value), data) + self.assertEqual(decode(data), value) #========================================================= # eoc @@ -820,8 +820,8 @@ class H64_Test(_Base64Test): ] encoded_ints = [ - ("z.", 63, 12), - (".z", 4032, 12), + (b("z."), 63, 12), + (b(".z"), 4032, 12), ] class H64Big_Test(_Base64Test): @@ -845,8 +845,8 @@ class H64Big_Test(_Base64Test): ] encoded_ints = [ - (".z", 63, 12), - ("z.", 4032, 12), + (b(".z"), 63, 12), + (b("z."), 4032, 12), ] #========================================================= diff --git a/passlib/tests/test_utils_handlers.py b/passlib/tests/test_utils_handlers.py index 9028df4..af7d540 100644 --- a/passlib/tests/test_utils_handlers.py +++ b/passlib/tests/test_utils_handlers.py @@ -14,7 +14,8 @@ from passlib.hash import ldap_md5, sha256_crypt from passlib.registry import _unload_handler_name as unload_handler_name, \ register_crypt_handler, get_crypt_handler from passlib.utils import getrandstr, JYTHON, rng, to_unicode, MissingBackendError -from passlib.utils.compat import b, bytes, bascii_to_str, uascii_to_str, unicode +from passlib.utils.compat import b, bytes, bascii_to_str, str_to_uascii, \ + uascii_to_str, unicode import passlib.utils.handlers as uh from passlib.tests.utils import HandlerCase, TestCase, catch_warnings, \ dummy_handler_in_registry @@ -42,11 +43,11 @@ class SkeletonTest(TestCase): def genhash(cls, secret, hash, flag=False): if isinstance(hash, bytes): hash = hash.decode("ascii") - if hash not in (u('a'),u('b')): - raise ValueError + if hash not in (u('a'), u('b'), None): + raise ValueError("unknown hash %r" % (hash,)) return 'b' if flag else 'a' - #check default identify method + # check default identify method self.assertTrue(d1.identify(u('a'))) self.assertTrue(d1.identify(b('a'))) self.assertTrue(d1.identify(u('b'))) @@ -55,20 +56,26 @@ class SkeletonTest(TestCase): self.assertFalse(d1.identify(u(''))) self.assertFalse(d1.identify(None)) - #check default genconfig method + # check default genconfig method self.assertIs(d1.genconfig(), None) d1._stub_config = u('b') self.assertEqual(d1.genconfig(), 'b') - #check default verify method - self.assertTrue(d1.verify('s','a')) + # check config string is rejected + self.assertRaises(ValueError, d1.verify, 's', b('b')) + self.assertRaises(ValueError, d1.verify, 's', u('b')) + del d1._stub_config + + # check default verify method + self.assertTrue(d1.verify('s', b('a'))) self.assertTrue(d1.verify('s',u('a'))) - self.assertFalse(d1.verify('s','b')) + self.assertFalse(d1.verify('s', b('b'))) self.assertFalse(d1.verify('s',u('b'))) - self.assertTrue(d1.verify('s', 'b', flag=True)) - self.assertRaises(ValueError, d1.verify, 's', 'c') + self.assertTrue(d1.verify('s', b('b'), flag=True)) + self.assertRaises(ValueError, d1.verify, 's', b('c')) + self.assertRaises(ValueError, d1.verify, 's', u('c')) - #check default encrypt method + # check default encrypt method self.assertEqual(d1.encrypt('s'), 'a') self.assertEqual(d1.encrypt('s'), 'a') self.assertEqual(d1.encrypt('s', flag=True), 'b') @@ -411,9 +418,9 @@ class UnsaltedHash(uh.StaticHandler): if isinstance(secret, unicode): secret = secret.encode("utf-8") data = b("boblious") + secret - return bascii_to_str(hashlib.sha1(data).hexdigest()) + return hashlib.sha1(data).hexdigest() -class SaltedHash(uh.HasSalt, uh.GenericHandler): +class SaltedHash(uh.HasStubChecksum, uh.HasSalt, uh.GenericHandler): "test algorithm with a salt" name = "salted_test_hash" setting_kwds = ("salt",) @@ -435,7 +442,7 @@ class SaltedHash(uh.HasSalt, uh.GenericHandler): hash = hash.decode("ascii") return cls(salt=hash[5:-40], checksum=hash[-40:], strict=True) - _stub_checksum = '0' * 40 + _stub_checksum = u('0') * 40 def to_string(self): hash = u("@salt%s%s") % (self.salt, self.checksum or self._stub_checksum) @@ -445,7 +452,7 @@ class SaltedHash(uh.HasSalt, uh.GenericHandler): if isinstance(secret, unicode): secret = secret.encode("utf-8") data = self.salt.encode("ascii") + secret + self.salt.encode("ascii") - return hashlib.sha1(data).hexdigest().decode("ascii") + return str_to_uascii(hashlib.sha1(data).hexdigest()) #========================================================= #test sample algorithms - really a self-test of HandlerCase |
