| 1 |
# Copyright (C) 2011-2014 by Barry A. Warsaw |
| 2 |
# |
| 3 |
# This file is part of flufl.password |
| 4 |
# |
| 5 |
# flufl.password is free software: you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU Lesser General Public License as published by the |
| 7 |
# Free Software Foundation, version 3 of the License. |
| 8 |
# |
| 9 |
# flufl.password is distributed in the hope that it will be useful, but |
| 10 |
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 11 |
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 12 |
# License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU Lesser General Public License |
| 15 |
# along with flufl.password. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
"""Unit tests for the passwords module.""" |
| 18 |
|
| 19 |
from __future__ import absolute_import, print_function, unicode_literals |
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
__all__ = [ |
| 23 |
'TestCleartextPasswords', |
| 24 |
'TestNoPasswords', |
| 25 |
'TestPBKDF2Passwords', |
| 26 |
'TestPasswordGeneration', |
| 27 |
'TestSHAPasswords', |
| 28 |
'TestSSHAPasswords', |
| 29 |
'TestSchemeLookup', |
| 30 |
] |
| 31 |
|
| 32 |
|
| 33 |
import unittest |
| 34 |
|
| 35 |
# Python 3 does not have the izip form. |
| 36 |
try: |
| 37 |
from itertools import izip_longest as longest |
| 38 |
except ImportError: |
| 39 |
from itertools import zip_longest as longest |
| 40 |
|
| 41 |
from flufl.password._hash import make_secret |
| 42 |
from flufl.password._registry import BadPasswordSchemeError, lookup |
| 43 |
from flufl.password._verify import verify |
| 44 |
from flufl.password import schemes, generate |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class PasswordsTestBase: |
| 49 |
scheme = None |
| 50 |
|
| 51 |
def setUp(self): |
| 52 |
# The user's password, as a bytes |
| 53 |
self.pwbyte = b'abc' |
| 54 |
self.pwutf8 = 'abc\xc3\xbf' # 'abc\xff' |
| 55 |
# Bad passwords; bytes |
| 56 |
self.badbyte = b'def' |
| 57 |
|
| 58 |
def test_byte_passwords(self): |
| 59 |
secret = make_secret(self.pwbyte, self.scheme) |
| 60 |
self.assertTrue(verify(secret, self.pwbyte), self.scheme) |
| 61 |
self.assertFalse(verify(secret, self.badbyte), self.scheme) |
| 62 |
|
| 63 |
def test_utf8_passwords(self): |
| 64 |
secret = make_secret(self.pwutf8, self.scheme) |
| 65 |
self.assertTrue(verify(secret, self.pwutf8), self.scheme) |
| 66 |
self.assertFalse(verify(secret, self.badbyte), self.scheme) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
class TestNoPasswords(unittest.TestCase): |
| 71 |
def test_make_secret(self): |
| 72 |
self.assertEqual(schemes.NoPasswordScheme.make_secret('whatever'), b'') |
| 73 |
|
| 74 |
def test_check_response(self): |
| 75 |
self.assertFalse( |
| 76 |
schemes.NoPasswordScheme.check_response(b'foo', 'bar')) |
| 77 |
self.assertFalse(schemes.NoPasswordScheme.check_response(b'', 'bar')) |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
class TestCleartextPasswords(PasswordsTestBase, unittest.TestCase): |
| 82 |
scheme = schemes.ClearTextPasswordScheme |
| 83 |
|
| 84 |
|
| 85 |
class TestSHAPasswords(PasswordsTestBase, unittest.TestCase): |
| 86 |
scheme = schemes.SHAPasswordScheme |
| 87 |
|
| 88 |
|
| 89 |
class TestSSHAPasswords(PasswordsTestBase, unittest.TestCase): |
| 90 |
scheme = schemes.SSHAPasswordScheme |
| 91 |
|
| 92 |
|
| 93 |
class TestPBKDF2Passwords(PasswordsTestBase, unittest.TestCase): |
| 94 |
scheme = schemes.PBKDF2PasswordScheme |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
class TestSchemeLookup(unittest.TestCase): |
| 99 |
def test_scheme_name_lookup(self): |
| 100 |
self.assertEqual(lookup('NONE'), schemes.NoPasswordScheme) |
| 101 |
self.assertEqual(lookup('CLEARTEXT'), schemes.ClearTextPasswordScheme) |
| 102 |
self.assertEqual(lookup('SHA'), schemes.SHAPasswordScheme) |
| 103 |
self.assertEqual(lookup('SSHA'), schemes.SSHAPasswordScheme) |
| 104 |
self.assertEqual(lookup('PBKDF2'), schemes.PBKDF2PasswordScheme) |
| 105 |
|
| 106 |
def test_lookup_error(self): |
| 107 |
self.assertRaises(BadPasswordSchemeError, lookup, 'BOGUS') |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
# See itertools doc page examples. |
| 113 |
def _grouper(seq): |
| 114 |
args = [iter(seq)] * 2 |
| 115 |
return list(longest(*args)) |
| 116 |
|
| 117 |
|
| 118 |
class TestPasswordGeneration(unittest.TestCase): |
| 119 |
def test_provided_user_friendly_password_length(self): |
| 120 |
self.assertEqual(len(generate(12)), 12) |
| 121 |
|
| 122 |
def test_provided_odd_user_friendly_password_length(self): |
| 123 |
self.assertEqual(len(generate(15)), 15) |
| 124 |
|
| 125 |
def test_user_friendly_password(self): |
| 126 |
password = generate() |
| 127 |
for pair in _grouper(password): |
| 128 |
# There will always be one vowel and one non-vowel. |
| 129 |
vowel = (pair[0] if pair[0] in 'aeiou' else pair[1]) |
| 130 |
consonant = (pair[0] if pair[0] not in 'aeiou' else pair[1]) |
| 131 |
self.assertTrue(vowel in 'aeiou', vowel) |
| 132 |
self.assertTrue(consonant not in 'aeiou', consonant) |