summaryrefslogtreecommitdiff
path: root/passlib/tests
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2011-12-28 17:15:31 -0500
committerEli Collins <elic@assurancetechnologies.com>2011-12-28 17:15:31 -0500
commit9d1ca56acb757a034d66eb0b97acf0ca06245146 (patch)
tree4a08b829d3a70d0430aa5dfbb03cac96238dcec5 /passlib/tests
parentdce7529fa18ab75cc42df175abee660836326ea5 (diff)
downloadpasslib-9d1ca56acb757a034d66eb0b97acf0ca06245146.tar.gz
deprecated to_hash_str, replaced all instances with to_native_str
decided that to_hash_str will always return native string, feature of hashes being returned as unicode under python 2 is better done through a CryptContext option.
Diffstat (limited to 'passlib/tests')
-rw-r--r--passlib/tests/test_apache.py8
-rw-r--r--passlib/tests/test_drivers.py2
-rw-r--r--passlib/tests/test_utils.py37
-rw-r--r--passlib/tests/test_utils_handlers.py16
-rw-r--r--passlib/tests/utils.py12
5 files changed, 41 insertions, 34 deletions
diff --git a/passlib/tests/test_apache.py b/passlib/tests/test_apache.py
index 9d69001..fa78a77 100644
--- a/passlib/tests/test_apache.py
+++ b/passlib/tests/test_apache.py
@@ -11,7 +11,7 @@ import time
#site
#pkg
from passlib import apache
-from passlib.utils import b, native_str, bytes
+from passlib.utils import b, bytes
from passlib.utils.compat import irange, unicode
from passlib.tests.utils import TestCase, mktemp, gae_env, get_file, set_file
from passlib.utils.compat import u
@@ -167,7 +167,7 @@ class HtpasswdFileTest(TestCase):
#check users() returns native string by default
ht = apache.HtpasswdFile._from_string(self.sample_01)
- self.assertIsInstance(ht.users()[0], native_str)
+ self.assertIsInstance(ht.users()[0], str)
#check returns unicode if encoding explicitly set
ht = apache.HtpasswdFile._from_string(self.sample_01, encoding="utf-8")
@@ -354,8 +354,8 @@ class HtdigestFileTest(TestCase):
#check users() returns native string by default
ht = apache.HtdigestFile._from_string(self.sample_01)
- self.assertIsInstance(ht.realms()[0], native_str)
- self.assertIsInstance(ht.users("realm")[0], native_str)
+ self.assertIsInstance(ht.realms()[0], str)
+ self.assertIsInstance(ht.users("realm")[0], str)
#check returns unicode if encoding explicitly set
ht = apache.HtdigestFile._from_string(self.sample_01, encoding="utf-8")
diff --git a/passlib/tests/test_drivers.py b/passlib/tests/test_drivers.py
index 1890074..5dc8d4f 100644
--- a/passlib/tests/test_drivers.py
+++ b/passlib/tests/test_drivers.py
@@ -635,7 +635,7 @@ class _Md5CryptTest(HandlerCase):
known_correct_hashes = [
#NOTE: would need to patch HandlerCase to coerce hashes
- #to_hash_str() for this first one to work under py3.
+ #to_native_str() for this first one to work under py3.
## ('', b('$1$dOHYPKoP$tnxS1T8Q6VVn3kpV8cN6o.')),
('', '$1$dOHYPKoP$tnxS1T8Q6VVn3kpV8cN6o.'),
(' ', '$1$m/5ee7ol$bZn0kIBFipq39e.KDXX8I0'),
diff --git a/passlib/tests/test_utils.py b/passlib/tests/test_utils.py
index 08dd321..dfd5f39 100644
--- a/passlib/tests/test_utils.py
+++ b/passlib/tests/test_utils.py
@@ -14,7 +14,7 @@ import warnings
from passlib.context import CryptContext
from passlib import utils
from passlib.utils import h64, des, Undef, bytes, b, \
- native_str, to_bytes, to_unicode, to_native_str, to_hash_str, \
+ to_bytes, to_unicode, to_native_str, \
is_same_codec, is_ascii_safe, safe_os_crypt, md4 as md4_mod
from passlib.utils.compat import unicode, PY3
from passlib.tests.utils import TestCase, Params as ak, \
@@ -320,7 +320,7 @@ class CodecTest(TestCase):
"tests bytes/unicode helpers in passlib.utils"
def test_bytes(self):
- "test b() helper, bytes and native_str types"
+ "test b() helper, bytes and native str type"
if PY3:
import builtins
self.assertIs(bytes, builtins.bytes)
@@ -328,8 +328,6 @@ class CodecTest(TestCase):
import __builtin__ as builtins
self.assertIs(bytes, builtins.str)
- self.assertIs(native_str, builtins.str)
-
self.assertIsInstance(b(''), bytes)
self.assertIsInstance(b('\x00\xff'), bytes)
if PY3:
@@ -392,20 +390,29 @@ class CodecTest(TestCase):
def test_to_native_str(self):
"test to_native_str()"
- self.assertEqual(to_native_str(u('abc')), 'abc')
- self.assertEqual(to_native_str(b('abc')), 'abc')
+ # ascii goes to native string w/o problems
+ self.assertEqual(to_native_str(u('abc')), 'abc')
+ self.assertEqual(to_native_str(b('abc')), 'abc')
+
+ # other types rejected
self.assertRaises(TypeError, to_native_str, None)
- self.assertEqual(to_native_str(u('\x00\xff'), 'latin-1'), '\x00\xff')
- self.assertEqual(to_native_str(b('\x00\xff'), 'latin-1'), '\x00\xff')
+ # non-ascii unicode should throw error under py2, unless codec specified
+ if PY3:
+ self.assertEqual(to_native_str(u('\x00\xff')), '\x00\xff')
+ else:
+ self.assertRaises(UnicodeEncodeError, to_native_str, u('\x00\xff'))
+ self.assertEqual(to_native_str(u('\x00\xff'), 'utf-8'), '\x00\xc3\xbf')
+
+ # non-ascii bytes should throw error under py3, unless codec specified
if PY3:
- self.assertEqual(to_native_str(u('\x00\xff')), '\x00\xff')
- self.assertEqual(to_native_str(b('\x00\xc3\xbf')), '\x00\xff')
+ self.assertRaises(UnicodeDecodeError, to_native_str, b('\x00\xff'))
else:
- self.assertEqual(to_native_str(u('\x00\xff')), '\x00\xc3\xbf')
- self.assertEqual(to_native_str(b('\x00\xc3\xbf')), '\x00\xc3\xbf')
+ self.assertEqual(to_native_str(b('\x00\xff')), '\x00\xff')
- #TODO: test to_hash_str()
+ # latin-1 should work if explicitly chosen
+ self.assertEqual(to_native_str(u('\x00\xff'), 'latin-1'), '\x00\xff')
+ self.assertEqual(to_native_str(b('\x00\xff'), 'latin-1'), '\x00\xff')
def test_is_ascii_safe(self):
"test is_ascii_safe()"
@@ -676,8 +683,8 @@ class _MD4_Test(TestCase):
"test md4 digest()"
md4 = self.hash
for input, hex in self.vectors:
- out = md4(input).digest()
- self.assertEqual(to_native_str(hexlify(out)), hex)
+ out = to_native_str(hexlify(md4(input).digest()))
+ self.assertEqual(out, hex)
def test_md4_copy(self):
"test md4 copy()"
diff --git a/passlib/tests/test_utils_handlers.py b/passlib/tests/test_utils_handlers.py
index 62ac529..3847745 100644
--- a/passlib/tests/test_utils_handlers.py
+++ b/passlib/tests/test_utils_handlers.py
@@ -14,7 +14,7 @@ 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 rng, getrandstr, handlers as uh, bytes, b, \
- to_hash_str, to_unicode, MissingBackendError, jython_vm
+ to_native_str, to_unicode, MissingBackendError, jython_vm
from passlib.utils.compat import unicode
from passlib.tests.utils import HandlerCase, TestCase, catch_warnings, \
dummy_handler_in_registry
@@ -44,7 +44,7 @@ class SkeletonTest(TestCase):
hash = hash.decode("ascii")
if hash not in (u('a'),u('b')):
raise ValueError
- return to_hash_str(u('b') if flag else u('a'))
+ return to_native_str(u('b') if flag else u('a'))
#check default identify method
self.assertTrue(d1.identify(u('a')))
@@ -58,7 +58,7 @@ class SkeletonTest(TestCase):
#check default genconfig method
self.assertIs(d1.genconfig(), None)
d1._stub_config = u('b')
- self.assertEqual(d1.genconfig(), to_hash_str('b'))
+ self.assertEqual(d1.genconfig(), to_native_str('b'))
#check default verify method
self.assertTrue(d1.verify('s','a'))
@@ -69,9 +69,9 @@ class SkeletonTest(TestCase):
self.assertRaises(ValueError, d1.verify, 's', 'c')
#check default encrypt method
- self.assertEqual(d1.encrypt('s'), to_hash_str('a'))
- self.assertEqual(d1.encrypt('s'), to_hash_str('a'))
- self.assertEqual(d1.encrypt('s', flag=True), to_hash_str('b'))
+ self.assertEqual(d1.encrypt('s'), to_native_str('a'))
+ self.assertEqual(d1.encrypt('s'), to_native_str('a'))
+ self.assertEqual(d1.encrypt('s', flag=True), to_native_str('b'))
#=========================================================
#GenericHandler & mixins
@@ -411,7 +411,7 @@ class UnsaltedHash(uh.StaticHandler):
if isinstance(secret, unicode):
secret = secret.encode("utf-8")
data = b("boblious") + secret
- return to_hash_str(hashlib.sha1(data).hexdigest())
+ return to_native_str(hashlib.sha1(data).hexdigest())
class SaltedHash(uh.HasSalt, uh.GenericHandler):
"test algorithm with a salt"
@@ -439,7 +439,7 @@ class SaltedHash(uh.HasSalt, uh.GenericHandler):
def to_string(self):
hash = u("@salt%s%s") % (self.salt, self.checksum or self._stub_checksum)
- return to_hash_str(hash)
+ return to_native_str(hash)
def calc_checksum(self, secret):
if isinstance(secret, unicode):
diff --git a/passlib/tests/utils.py b/passlib/tests/utils.py
index cb72143..4e5f2fb 100644
--- a/passlib/tests/utils.py
+++ b/passlib/tests/utils.py
@@ -33,7 +33,7 @@ if ut_version < 2:
from passlib import registry, utils
from passlib.utils import classproperty, handlers as uh, \
has_rounds_info, has_salt_info, MissingBackendError, \
- rounds_cost_values, b, bytes, native_str, NoneType
+ rounds_cost_values, b, bytes, NoneType
from passlib.utils.compat import iteritems, irange, callable, sb_types, \
exc_err, unicode
#local
@@ -564,7 +564,7 @@ class HandlerCase(TestCase):
name = ga("name")
self.assertTrue(name, "name not defined:")
- self.assertIsInstance(name, native_str, "name must be native str")
+ self.assertIsInstance(name, str, "name must be native str")
self.assertTrue(name.lower() == name, "name not lower-case:")
self.assertTrue(re.match("^[a-z0-9_]+$", name), "name must be alphanum + underscore: %r" % (name,))
@@ -811,8 +811,8 @@ class HandlerCase(TestCase):
raise self.skipTest("handler doesn't have salt")
c1 = self.do_genconfig()
c2 = self.do_genconfig()
- self.assertIsInstance(c1, native_str, "genconfig() must return native str:")
- self.assertIsInstance(c2, native_str, "genconfig() must return native str:")
+ self.assertIsInstance(c1, str, "genconfig() must return native str:")
+ self.assertIsInstance(c2, str, "genconfig() must return native str:")
self.assertNotEqual(c1,c2)
def test_31_genconfig_minsalt(self):
@@ -933,14 +933,14 @@ class HandlerCase(TestCase):
#check it handles unicode password
secret = u("\u20AC\u00A5$")
result = self.do_encrypt(secret)
- self.assertIsInstance(result, native_str, "encrypt must return native str:")
+ self.assertIsInstance(result, str, "encrypt must return native str:")
self.assertTrue(self.do_identify(result))
self.assertTrue(self.do_verify(secret, result))
#check it handles bytes password as well
secret = b('\xe2\x82\xac\xc2\xa5$')
result = self.do_encrypt(secret)
- self.assertIsInstance(result, native_str, "encrypt must return native str:")
+ self.assertIsInstance(result, str, "encrypt must return native str:")
self.assertTrue(self.do_identify(result))
self.assertTrue(self.do_verify(secret, result))