"""tests for passlib.util""" #============================================================================= # imports #============================================================================= # core from functools import partial import warnings # site # pkg # module from passlib.utils import is_ascii_safe, to_bytes from passlib.utils.compat import join_bytes, PYPY from passlib.tests.utils import TestCase, hb, run_with_fixed_seeds #============================================================================= # byte funcs #============================================================================= class MiscTest(TestCase): """tests various parts of utils module""" # NOTE: could test xor_bytes(), but it's exercised well enough by pbkdf2 test def test_compat(self): """test compat's lazymodule""" from passlib.utils import compat # "" self.assertRegex(repr(compat), r"^$") # test synthentic dir() dir(compat) # FIXME: find another lazy-loaded attr to check, all current ones removed after py2 comapt gone. # self.assertTrue('UnicodeIO' in dir(compat)) def test_classproperty(self): from passlib.utils.decor import classproperty def xprop_func(cls): return cls.xvar class test(object): xvar = 1 xprop = classproperty(xprop_func) self.assertEqual(test.xprop, 1) prop = test.__dict__['xprop'] self.assertIs(prop.__func__, xprop_func) def test_deprecated_function(self): from passlib.utils.decor import deprecated_function # NOTE: not comprehensive, just tests the basic behavior @deprecated_function(deprecated="1.6", removed="1.8") def test_func(*args): """test docstring""" return args self.assertTrue(".. deprecated::" in test_func.__doc__) with self.assertWarningList(dict(category=DeprecationWarning, message="the function passlib.tests.test_utils.test_func() " "is deprecated as of Passlib 1.6, and will be " "removed in Passlib 1.8." )): self.assertEqual(test_func(1,2), (1,2)) def test_memoized_property(self): from passlib.utils.decor import memoized_property class dummy(object): counter = 0 @memoized_property def value(self): value = self.counter self.counter = value+1 return value d = dummy() self.assertEqual(d.value, 0) self.assertEqual(d.value, 0) self.assertEqual(d.counter, 1) def test_getrandbytes(self): """getrandbytes()""" from passlib.utils import getrandbytes wrapper = partial(getrandbytes, self.getRandom()) self.assertEqual(len(wrapper(0)), 0) a = wrapper(10) b = wrapper(10) self.assertIsInstance(a, bytes) self.assertEqual(len(a), 10) self.assertEqual(len(b), 10) self.assertNotEqual(a, b) @run_with_fixed_seeds(count=1024) def test_getrandstr(self, seed): """getrandstr()""" from passlib.utils import getrandstr wrapper = partial(getrandstr, self.getRandom(seed=seed)) # count 0 self.assertEqual(wrapper('abc',0), '') # count <0 self.assertRaises(ValueError, wrapper, 'abc', -1) # letters 0 self.assertRaises(ValueError, wrapper, '', 0) # letters 1 self.assertEqual(wrapper('a', 5), 'aaaaa') # NOTE: the following parts are non-deterministic, # with a small chance of failure (outside chance it may pick # a string w/o one char, even more remote chance of picking # same string). to combat this, we run it against multiple # fixed seeds (using run_with_fixed_seeds decorator), # and hope that they're sufficient to test the range of behavior. # letters x = wrapper(u'abc', 32) y = wrapper(u'abc', 32) self.assertIsInstance(x, str) self.assertNotEqual(x,y) self.assertEqual(sorted(set(x)), [u'a',u'b',u'c']) # bytes x = wrapper(b'abc', 32) y = wrapper(b'abc', 32) self.assertIsInstance(x, bytes) self.assertNotEqual(x,y) # NOTE: decoding this due to py3 bytes self.assertEqual(sorted(set(x.decode("ascii"))), [u'a',u'b',u'c']) def test_generate_password(self): """generate_password()""" from passlib.utils import generate_password warnings.filterwarnings("ignore", "The function.*generate_password\(\) is deprecated") self.assertEqual(len(generate_password(15)), 15) def test_is_crypt_context(self): """test is_crypt_context()""" from passlib.utils import is_crypt_context from passlib.context import CryptContext cc = CryptContext(["des_crypt"]) self.assertTrue(is_crypt_context(cc)) self.assertFalse(not is_crypt_context(cc)) def test_genseed(self): """test genseed()""" import random from passlib.utils import genseed rng = random.Random(genseed()) a = rng.randint(0, 10**10) rng = random.Random(genseed()) b = rng.randint(0, 10**10) self.assertNotEqual(a,b) rng.seed(genseed(rng)) def test_crypt(self): """test crypt.crypt() wrappers""" from passlib.utils import has_crypt, safe_crypt, test_crypt from passlib.registry import get_supported_os_crypt_schemes, get_crypt_handler # test everything is disabled supported = get_supported_os_crypt_schemes() if not has_crypt: self.assertEqual(supported, ()) self.assertEqual(safe_crypt("test", "aa"), None) self.assertFalse(test_crypt("test", "aaqPiZY5xR5l.")) # des_crypt() hash of "test" raise self.skipTest("crypt.crypt() not available") # expect there to be something supported, if crypt() is present if not supported: # NOTE: failures here should be investigated. usually means one of: # 1) at least one of passlib's os_crypt detection routines is giving false negative # 2) crypt() ONLY supports some hash alg which passlib doesn't know about # 3) crypt() is present but completely disabled (never encountered this yet) raise self.fail("crypt() present, but no supported schemes found!") # pick cheap alg if possible, with minimum rounds, to speed up this test. # NOTE: trusting hasher class works properly (should have been verified using it's own UTs) for scheme in ("md5_crypt", "sha256_crypt"): if scheme in supported: break else: scheme = supported[-1] hasher = get_crypt_handler(scheme) if getattr(hasher, "min_rounds", None): hasher = hasher.using(rounds=hasher.min_rounds) # helpers to generate hashes & config strings to work with def get_hash(secret): assert isinstance(secret, str) hash = hasher.hash(secret) if isinstance(hash, bytes): # py2 hash = hash.decode("utf-8") assert isinstance(hash, str) return hash # test ascii password & return type s1 = u"test" h1 = get_hash(s1) result = safe_crypt(s1, h1) self.assertIsInstance(result, str) self.assertEqual(result, h1) self.assertEqual(safe_crypt(to_bytes(s1), to_bytes(h1)), h1) # make sure crypt doesn't just blindly return h1 for whatever we pass in h1x = h1[:-2] + 'xx' self.assertEqual(safe_crypt(s1, h1x), h1) # test utf-8 / unicode password s2 = u'test\u1234' h2 = get_hash(s2) self.assertEqual(safe_crypt(s2, h2), h2) self.assertEqual(safe_crypt(to_bytes(s2), to_bytes(h2)), h2) # test rejects null chars in password self.assertRaises(ValueError, safe_crypt, '\x00', h1) # check test_crypt() self.assertTrue(test_crypt("test", h1)) self.assertFalse(test_crypt("test", h1x)) # check crypt returning variant error indicators # some platforms return None on errors, others empty string, # The BSDs in some cases return ":" import passlib.utils as mod orig = mod._crypt try: retval = None mod._crypt = lambda secret, hash: retval for retval in [None, "", ":", ":0", "*0"]: self.assertEqual(safe_crypt("test", h1), None) self.assertFalse(test_crypt("test", h1)) retval = 'xxx' self.assertEqual(safe_crypt("test", h1), "xxx") self.assertFalse(test_crypt("test", h1)) finally: mod._crypt = orig def test_consteq(self): """test consteq()""" # NOTE: this test is kind of over the top, but that's only because # this is used for the critical task of comparing hashes for equality. from passlib.utils import consteq, str_consteq # ensure error raises for wrong types self.assertRaises(TypeError, consteq, u'', b'') self.assertRaises(TypeError, consteq, u'', 1) self.assertRaises(TypeError, consteq, u'', None) self.assertRaises(TypeError, consteq, b'', u'') self.assertRaises(TypeError, consteq, b'', 1) self.assertRaises(TypeError, consteq, b'', None) self.assertRaises(TypeError, consteq, None, u'') self.assertRaises(TypeError, consteq, None, b'') self.assertRaises(TypeError, consteq, 1, u'') self.assertRaises(TypeError, consteq, 1, b'') def consteq_supports_string(value): # compare_digest() only supports ascii unicode strings. # confirmed for: cpython 3.4, pypy3, pyston return (consteq is str_consteq or is_ascii_safe(value)) # check equal inputs compare correctly for value in [ u"a", u"abc", u"\xff\xa2\x12\x00"*10, ]: if consteq_supports_string(value): self.assertTrue(consteq(value, value), "value %r:" % (value,)) else: self.assertRaises(TypeError, consteq, value, value) self.assertTrue(str_consteq(value, value), "value %r:" % (value,)) value = value.encode("latin-1") self.assertTrue(consteq(value, value), "value %r:" % (value,)) # check non-equal inputs compare correctly for l,r in [ # check same-size comparisons with differing contents fail. (u"a", u"c"), (u"abcabc", u"zbaabc"), (u"abcabc", u"abzabc"), (u"abcabc", u"abcabz"), ((u"\xff\xa2\x12\x00"*10)[:-1] + u"\x01", u"\xff\xa2\x12\x00"*10), # check different-size comparisons fail. (u"", u"a"), (u"abc", u"abcdef"), (u"abc", u"defabc"), (u"qwertyuiopasdfghjklzxcvbnm", u"abc"), ]: if consteq_supports_string(l) and consteq_supports_string(r): self.assertFalse(consteq(l, r), "values %r %r:" % (l,r)) self.assertFalse(consteq(r, l), "values %r %r:" % (r,l)) else: self.assertRaises(TypeError, consteq, l, r) self.assertRaises(TypeError, consteq, r, l) self.assertFalse(str_consteq(l, r), "values %r %r:" % (l,r)) self.assertFalse(str_consteq(r, l), "values %r %r:" % (r,l)) l = l.encode("latin-1") r = r.encode("latin-1") self.assertFalse(consteq(l, r), "values %r %r:" % (l,r)) self.assertFalse(consteq(r, l), "values %r %r:" % (r,l)) # TODO: add some tests to ensure we take THETA(strlen) time. # this might be hard to do reproducably. # NOTE: below code was used to generate stats for analysis ##from math import log as logb ##import timeit ##multipliers = [ 1< encode() -> decode() -> raw # # generate some random bytes size = rng.randint(1 if saw_zero else 0, 12) if not size: saw_zero = True enc_size = (4*size+2)//3 raw = getrandbytes(rng, size) # encode them, check invariants encoded = engine.encode_bytes(raw) self.assertEqual(len(encoded), enc_size) # make sure decode returns original result = engine.decode_bytes(encoded) self.assertEqual(result, raw) # # test encoded -> decode() -> encode() -> encoded # # generate some random encoded data if size % 4 == 1: size += rng.choice([-1,1,2]) raw_size = 3*size//4 encoded = getrandstr(rng, engine.bytemap, size) # decode them, check invariants raw = engine.decode_bytes(encoded) self.assertEqual(len(raw), raw_size, "encoded %d:" % size) # make sure encode returns original (barring padding bits) result = engine.encode_bytes(raw) if size % 4: self.assertEqual(result[:-1], encoded[:-1]) else: self.assertEqual(result, encoded) def test_repair_unused(self): """test repair_unused()""" # NOTE: this test relies on encode_bytes() always returning clear # padding bits - which should be ensured by test vectors. from passlib.utils import getrandstr rng = self.getRandom() engine = self.engine check_repair_unused = self.engine.check_repair_unused i = 0 while i < 300: size = rng.randint(0,23) cdata = getrandstr(rng, engine.charmap, size).encode("ascii") if size & 3 == 1: # should throw error self.assertRaises(ValueError, check_repair_unused, cdata) continue rdata = engine.encode_bytes(engine.decode_bytes(cdata)) if rng.random() < .5: cdata = cdata.decode("ascii") rdata = rdata.decode("ascii") if cdata == rdata: # should leave unchanged ok, result = check_repair_unused(cdata) self.assertFalse(ok) self.assertEqual(result, rdata) else: # should repair bits self.assertNotEqual(size % 4, 0) ok, result = check_repair_unused(cdata) self.assertTrue(ok) self.assertEqual(result, rdata) i += 1 #=================================================================== # test transposed encode/decode - encoding independant #=================================================================== # NOTE: these tests assume normal encode/decode has been tested elsewhere. transposed = [ # orig, result, transpose map (b"\x33\x22\x11", b"\x11\x22\x33",[2,1,0]), (b"\x22\x33\x11", b"\x11\x22\x33",[1,2,0]), ] transposed_dups = [ # orig, result, transpose projection (b"\x11\x11\x22", b"\x11\x22\x33",[0,0,1]), ] def test_encode_transposed_bytes(self): """test encode_transposed_bytes()""" engine = self.engine for result, input, offsets in self.transposed + self.transposed_dups: tmp = engine.encode_transposed_bytes(input, offsets) out = engine.decode_bytes(tmp) self.assertEqual(out, result) self.assertRaises(TypeError, engine.encode_transposed_bytes, u"a", []) def test_decode_transposed_bytes(self): """test decode_transposed_bytes()""" engine = self.engine for input, result, offsets in self.transposed: tmp = engine.encode_bytes(input) out = engine.decode_transposed_bytes(tmp, offsets) self.assertEqual(out, result) def test_decode_transposed_bytes_bad(self): """test decode_transposed_bytes() fails if map is a one-way""" engine = self.engine for input, _, offsets in self.transposed_dups: tmp = engine.encode_bytes(input) self.assertRaises(TypeError, engine.decode_transposed_bytes, tmp, offsets) #=================================================================== # test 6bit handling #=================================================================== def check_int_pair(self, bits, encoded_pairs): """helper to check encode_intXX & decode_intXX functions""" rng = self.getRandom() engine = self.engine encode = getattr(engine, "encode_int%s" % bits) decode = getattr(engine, "decode_int%s" % bits) pad = -bits % 6 chars = (bits+pad)//6 upper = 1<