• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / AuthEncoding / 45058194495

17 Sep 2026 07:10AM UTC coverage: 90.253% (-6.5%) from 96.751%
45058194495

push

github

dataflake
Version bump [ci skip]

34 of 38 branches covered (89.47%)

Branch coverage included in aggregate %.

216 of 239 relevant lines covered (90.38%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/src/AuthEncoding/tests/test_AuthEncoding.py
1
##############################################################################
2
#
3
# Copyright (c) 2002, 2015 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE
11
#
12
##############################################################################
13
"""Test of AuthEncoding
14
"""
15

16
import pytest
1 ✔
17

18
from .. import AuthEncoding
1 ✔
19
from ..compat import to_bytes
1 ✔
20
from ..compat import to_string
1 ✔
21

22

23
def testListSchemes():
1 ✔
24
    assert len(AuthEncoding.listSchemes()) > 0  # At least one must exist!
1 ✔
25

26

27
@pytest.mark.parametrize('schema_id', AuthEncoding.listSchemes())
1 ✔
28
@pytest.mark.parametrize(
1 ✔
29
    'password',
30
    ['good_pw', 'gööd_pw', 'g€€d_pw', to_bytes('gööd_pw')])
31
def testGoodPassword(schema_id, password):
1 ✔
32
    enc = AuthEncoding.pw_encrypt(password, schema_id)
1 ✔
33
    assert enc != password
1 ✔
34
    assert AuthEncoding.pw_validate(enc, password)
1 ✔
35
    assert AuthEncoding.pw_validate(to_string(enc), password)
1 ✔
36
    assert AuthEncoding.is_encrypted(enc)
1 ✔
37
    assert not AuthEncoding.is_encrypted(password)
1 ✔
38

39

40
@pytest.mark.parametrize('schema_id', AuthEncoding.listSchemes())
1 ✔
41
@pytest.mark.parametrize(
1 ✔
42
    'password',
43
    ['OK_pa55w0rd \n', 'OK_pä55w0rd \n', to_bytes('OK_pä55w0rd \n')])
44
def testBadPassword(schema_id, password):
1 ✔
45
    enc = AuthEncoding.pw_encrypt(password, schema_id)
1 ✔
46
    assert enc != password
1 ✔
47
    assert not AuthEncoding.pw_validate(enc, 'xxx')
1 ✔
48
    assert not AuthEncoding.pw_validate(enc, b'xxx')
1 ✔
49
    assert not AuthEncoding.pw_validate(to_string(enc), 'xxx')
1 ✔
50
    assert not AuthEncoding.pw_validate(enc, enc)
1 ✔
51
    if schema_id != 'CRYPT':
1 !
52
        # crypt truncates passwords and would fail this test.
53
        assert not AuthEncoding.pw_validate(enc, password[:-1])
1 ✔
54
    assert not AuthEncoding.pw_validate(enc, password[1:])
1 ✔
55
    assert AuthEncoding.pw_validate(enc, password)
1 ✔
56

57

58
@pytest.mark.parametrize('schema_id', AuthEncoding.listSchemes())
1 ✔
59
def testShortPassword(schema_id):
1 ✔
60
    pw = '1'
1 ✔
61
    enc = AuthEncoding.pw_encrypt(pw, schema_id)
1 ✔
62
    assert AuthEncoding.pw_validate(enc, pw)
1 ✔
63
    assert not AuthEncoding.pw_validate(enc, enc)
1 ✔
64
    assert not AuthEncoding.pw_validate(enc, 'xxx')
1 ✔
65

66

67
long_password_parameters = [
1 ✔
68
    x for x in AuthEncoding.listSchemes() if x != 'CRYPT']
69
if 'CRYPT' in AuthEncoding.listSchemes():
1 !
70
    long_password_parameters.append(
×
71
        pytest.param('CRYPT',
72
                     marks=pytest.mark.xfail(reason='CRYPT fails on GHA')))
73

74

75
@pytest.mark.parametrize('schema_id', long_password_parameters)
1 ✔
76
def testLongPassword(schema_id):
1 ✔
77
    pw = 'Pw' * 2000
1 ✔
78
    if schema_id == 'BCRYPT':
1 ✔
79
        pw = pw[:72]  # bcrypt has a max length of 72 bytes
1 ✔
80
    enc = AuthEncoding.pw_encrypt(pw, schema_id)
1 ✔
81
    assert AuthEncoding.pw_validate(enc, pw)
1 ✔
82
    assert not AuthEncoding.pw_validate(enc, enc)
1 ✔
83
    assert not AuthEncoding.pw_validate(enc, 'xxx')
1 ✔
84
    if 'CRYPT' not in schema_id:
1 ✔
85
        # crypt and bcrypt truncates passwords and would fail these tests.
86
        assert not AuthEncoding.pw_validate(enc, pw[:-2])
1 ✔
87
        assert not AuthEncoding.pw_validate(enc, pw[2:])
1 ✔
88

89

90
@pytest.mark.parametrize('schema_id', AuthEncoding.listSchemes())
1 ✔
91
def testBlankPassword(schema_id):
1 ✔
92
    pw = ''
1 ✔
93
    enc = AuthEncoding.pw_encrypt(pw, schema_id)
1 ✔
94
    assert enc != pw
1 ✔
95
    assert AuthEncoding.pw_validate(enc, pw)
1 ✔
96
    assert not AuthEncoding.pw_validate(enc, enc)
1 ✔
97
    assert not AuthEncoding.pw_validate(enc, 'xxx')
1 ✔
98

99

100
def testUnencryptedPassword():
1 ✔
101
    # Sanity check
102
    pw = 'my-password'
1 ✔
103
    assert AuthEncoding.pw_validate(pw, pw)
1 ✔
104
    assert not AuthEncoding.pw_validate(pw, pw + 'asdf')
1 ✔
105

106

107
def testEncryptWithNotSupportedScheme():
1 ✔
108
    with pytest.raises(ValueError) as err:
1 ✔
109
        AuthEncoding.pw_encrypt('asdf', 'MD1')
1 ✔
110
    assert 'Not supported: MD1' == str(err.value)
1 ✔
111

112

113
def testEncryptAcceptsTextAndBinaryEncodingNames():
1 ✔
114
    assert (AuthEncoding.pw_encrypt('asdf', b'SHA') ==
1 ✔
115
            AuthEncoding.pw_encrypt('asdf', 'SHA'))
116

117

118
def testIsEncryptedAcceptsTextAndBinary():
1 ✔
119
    assert AuthEncoding.is_encrypted(b'{SHA}')
1 ✔
120
    assert AuthEncoding.is_encrypted('{SHA}')
1 ✔
121
    assert not AuthEncoding.is_encrypted(b'foo')
1 ✔
122
    assert not AuthEncoding.is_encrypted('foo')
1 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc