summaryrefslogtreecommitdiff
path: root/passlib/tests/test_base.py
blob: 1275f0d553f0d5aaa8481f6aecc50f0d8cf8409c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""tests for passlib.pwhash -- (c) Assurance Technologies 2003-2009"""
#=========================================================
#imports
#=========================================================
from __future__ import with_statement
#core
import hashlib
from logging import getLogger
#site
#pkg
from passlib import hash
from passlib.base import CryptContext, CryptPolicy
from passlib.tests.utils import TestCase, mktemp
from passlib.drivers.md5_crypt import md5_crypt as AnotherHash
from passlib.tests.test_utils_drivers import UnsaltedHash, SaltedHash
#module
log = getLogger(__name__)

#=========================================================
#
#=========================================================
class CryptPolicyTest(TestCase):
    "test CryptPolicy object"

    #TODO: need to test user categories w/in all this

    case_prefix = "CryptPolicy"

    #=========================================================
    #sample crypt policies used for testing
    #=========================================================

    #-----------------------------------------------------
    #sample 1 - average config file
    #-----------------------------------------------------
    sample_config_1s = """\
[passlib]
schemes = des_crypt, md5_crypt, bsdi_crypt, sha512_crypt
default = md5_crypt
all.vary_rounds = 10%
bsdi_crypt.max_rounds = 30000
bsdi_crypt.default_rounds = 25000
sha512_crypt.max_rounds = 50000
sha512_crypt.min_rounds = 40000
"""

    sample_config_1pd = dict(
        schemes = [ "des_crypt", "md5_crypt", "bsdi_crypt", "sha512_crypt"],
        default = "md5_crypt",
        all__vary_rounds = "10%",
        bsdi_crypt__max_rounds = 30000,
        bsdi_crypt__default_rounds = 25000,
        sha512_crypt__max_rounds = 50000,
        sha512_crypt__min_rounds = 40000,
    )

    sample_config_1pid = {
        "schemes": "des_crypt, md5_crypt, bsdi_crypt, sha512_crypt",
        "default": "md5_crypt",
        "all.vary_rounds": "10%",
        "bsdi_crypt.max_rounds": 30000,
        "bsdi_crypt.default_rounds": 25000,
        "sha512_crypt.max_rounds": 50000,
        "sha512_crypt.min_rounds": 40000,
    }

    sample_config_1prd = dict(
        schemes = [ hash.des_crypt, hash.md5_crypt, hash.bsdi_crypt, hash.sha512_crypt],
        default = hash.md5_crypt,
        all__vary_rounds = "10%",
        bsdi_crypt__max_rounds = 30000,
        bsdi_crypt__default_rounds = 25000,
        sha512_crypt__max_rounds = 50000,
        sha512_crypt__min_rounds = 40000,
    )

    #-----------------------------------------------------
    #sample 2 - partial policy & result of overlay on sample 1
    #-----------------------------------------------------
    sample_config_2s = """\
[passlib]
bsdi_crypt.min_rounds = 29000
bsdi_crypt.max_rounds = 35000
bsdi_crypt.default_rounds = 31000
sha512_crypt.min_rounds = 45000
"""

    sample_config_2pd = dict(
        #using this to test full replacement of existing options
        bsdi_crypt__min_rounds = 29000,
        bsdi_crypt__max_rounds = 35000,
        bsdi_crypt__default_rounds = 31000,
        #using this to test partial replacement of existing options
        sha512_crypt__min_rounds=45000,
    )

    sample_config_12pd = dict(
        schemes = [ "des_crypt", "md5_crypt", "bsdi_crypt", "sha512_crypt"],
        default = "md5_crypt",
        all__vary_rounds = "10%",
        bsdi_crypt__min_rounds = 29000,
        bsdi_crypt__max_rounds = 35000,
        bsdi_crypt__default_rounds = 31000,
        sha512_crypt__max_rounds = 50000,
        sha512_crypt__min_rounds=45000,
    )

    #-----------------------------------------------------
    #sample 3 - just changing default
    #-----------------------------------------------------
    sample_config_3pd = dict(
        default="sha512_crypt",
    )

    sample_config_123pd = dict(
        schemes = [ "des_crypt", "md5_crypt", "bsdi_crypt", "sha512_crypt"],
        default = "sha512_crypt",
        all__vary_rounds = "10%",
        bsdi_crypt__min_rounds = 29000,
        bsdi_crypt__max_rounds = 35000,
        bsdi_crypt__default_rounds = 31000,
        sha512_crypt__max_rounds = 50000,
        sha512_crypt__min_rounds=45000,
    )

    #=========================================================
    #constructors
    #=========================================================
    def test_00_constructor(self):
        "test CryptPolicy() constructor"
        policy = CryptPolicy(**self.sample_config_1pd)
        self.assertEquals(policy.to_dict(), self.sample_config_1pd)

    def test_01_from_path(self):
        "test CryptPolicy.from_path() constructor"
        path = mktemp()
        with file(path, "w") as fh:
            fh.write(self.sample_config_1s)
        policy = CryptPolicy.from_path(path)
        self.assertEquals(policy.to_dict(), self.sample_config_1pd)

        #TODO: test if path missing

    def test_02_from_string(self):
        "test CryptPolicy.from_string() constructor"
        policy = CryptPolicy.from_string(self.sample_config_1s)
        self.assertEquals(policy.to_dict(), self.sample_config_1pd)

    def test_03_from_source(self):
        "test CryptPolicy.from_source() constructor"

        #pass it a path
        path = mktemp()
        with file(path, "w") as fh:
            fh.write(self.sample_config_1s)
        policy = CryptPolicy.from_source(path)
        self.assertEquals(policy.to_dict(), self.sample_config_1pd)

        #pass it a string
        policy = CryptPolicy.from_source(self.sample_config_1s)
        self.assertEquals(policy.to_dict(), self.sample_config_1pd)

        #pass it a dict (NOTE: make a copy to detect in-place modifications)
        policy = CryptPolicy.from_source(self.sample_config_1pd.copy())
        self.assertEquals(policy.to_dict(), self.sample_config_1pd)

        #pass it existing policy
        p2 = CryptPolicy.from_source(policy)
        self.assertIs(policy, p2)

        #pass it something wrong
        self.assertRaises(TypeError, CryptPolicy.from_source, 1)
        self.assertRaises(TypeError, CryptPolicy.from_source, [])

    def test_04_from_sources(self):
        "test CryptPolicy.from_sources() constructor"

        #pass it empty list
        self.assertRaises(ValueError, CryptPolicy.from_sources, [])

        #pass it one-element list
        policy = CryptPolicy.from_sources([self.sample_config_1s])
        self.assertEquals(policy.to_dict(), self.sample_config_1pd)

        #pass multiple sources
        path = mktemp()
        with file(path, "w") as fh:
            fh.write(self.sample_config_1s)
        policy = CryptPolicy.from_sources([
            path,
            self.sample_config_2s,
            self.sample_config_3pd,
            ])
        self.assertEquals(policy.to_dict(), self.sample_config_123pd)

    def test_05_replace(self):
        "test CryptPolicy.replace() constructor"

        p1 = CryptPolicy(**self.sample_config_1pd)

        #check overlaying sample 2
        p2 = p1.replace(**self.sample_config_2pd)
        self.assertEquals(p2.to_dict(), self.sample_config_12pd)

        #check repeating overlay makes no change
        p2b = p2.replace(**self.sample_config_2pd)
        self.assertEquals(p2b.to_dict(), self.sample_config_12pd)

        #check overlaying sample 3
        p3 = p2.replace(self.sample_config_3pd)
        self.assertEquals(p3.to_dict(), self.sample_config_123pd)

    #=========================================================
    #reading
    #=========================================================
    def test_10_has_handlers(self):
        "test has_handlers() method"

        p1 = CryptPolicy(**self.sample_config_1pd)
        self.assert_(p1.has_handlers())

        p3 = CryptPolicy(**self.sample_config_3pd)
        self.assert_(not p3.has_handlers())

    def test_11_iter_handlers(self):
        "test iter_handlers() method"

        p1 = CryptPolicy(**self.sample_config_1pd)
        s = self.sample_config_1prd['schemes'][::-1]
        self.assertEquals(list(p1.iter_handlers()), s)

        p3 = CryptPolicy(**self.sample_config_3pd)
        self.assertEquals(list(p3.iter_handlers()), [])

    def test_12_get_handler(self):
        "test get_handler() method"

        p1 = CryptPolicy(**self.sample_config_1pd)

        #check by name
        self.assertIs(p1.get_handler("bsdi_crypt"), hash.bsdi_crypt)

        #check by missing name
        self.assertIs(p1.get_handler("sha256_crypt"), None)
        self.assertRaises(KeyError, p1.get_handler, "sha256_crypt", required=True)

        #check default
        self.assertIs(p1.get_handler(), hash.md5_crypt)

    def test_13_get_options(self):
        "test get_options() method"

        p12 = CryptPolicy(**self.sample_config_12pd)

        self.assertEquals(p12.get_options("bsdi_crypt"),dict(
            vary_rounds = "10%",
            min_rounds = 29000,
            max_rounds = 35000,
            default_rounds = 31000,
        ))

        self.assertEquals(p12.get_options("sha512_crypt"),dict(
            vary_rounds = "10%",
            min_rounds = 45000,
            max_rounds = 50000,
        ))

    def test_14_handler_is_deprecated(self):
        "test handler_is_deprecated() method"
        pa = CryptPolicy(**self.sample_config_1pd)
        pb = pa.replace(deprecated=["des_crypt", "bsdi_crypt"])

        self.assert_(not pa.handler_is_deprecated("des_crypt"))
        self.assert_(not pa.handler_is_deprecated(hash.bsdi_crypt))
        self.assert_(not pa.handler_is_deprecated("sha512_crypt"))

        self.assert_(pb.handler_is_deprecated("des_crypt"))
        self.assert_(pb.handler_is_deprecated(hash.bsdi_crypt))
        self.assert_(not pb.handler_is_deprecated("sha512_crypt"))

    #TODO: test this.
    ##def test_gen_min_verify_time(self):
    ##    "test get_min_verify_time() method"

    #=========================================================
    #serialization
    #=========================================================
    def test_20_iter_config(self):
        "test iter_config() method"
        p1 = CryptPolicy(**self.sample_config_1pd)
        self.assertEquals(dict(p1.iter_config()), self.sample_config_1pd)
        self.assertEquals(dict(p1.iter_config(resolve=True)), self.sample_config_1prd)
        self.assertEquals(dict(p1.iter_config(ini=True)), self.sample_config_1pid)

    def test_21_to_dict(self):
        "test to_dict() method"
        p1 = CryptPolicy(**self.sample_config_1pd)
        self.assertEquals(p1.to_dict(), self.sample_config_1pd)
        self.assertEquals(p1.to_dict(resolve=True), self.sample_config_1prd)

    def test_22_to_string(self):
        "test to_string() method"
        pa = CryptPolicy(**self.sample_config_1pd)
        s = pa.to_string() #NOTE: can't compare string directly, ordering etc may not match
        pb = CryptPolicy.from_string(s)
        self.assertEquals(pb.to_dict(), self.sample_config_1pd)

    #=========================================================
    #
    #=========================================================

#=========================================================
#CryptContext
#=========================================================
class CryptContextTest(TestCase):
    "test CryptContext object's behavior"
    case_prefix = "CryptContext"

    #=========================================================
    #constructor
    #=========================================================
    def test_00_constructor(self):
        "test CryptContext simple constructor"
        #create crypt context using handlers
        cc = CryptContext([UnsaltedHash, SaltedHash, hash.md5_crypt])
        c, b, a = cc.policy.iter_handlers()
        self.assertIs(a, UnsaltedHash)
        self.assertIs(b, SaltedHash)
        self.assertIs(c, hash.md5_crypt)

        #create context using names
        cc = CryptContext([UnsaltedHash, SaltedHash, "md5_crypt"])
        c, b, a = cc.policy.iter_handlers()
        self.assertIs(a, UnsaltedHash)
        self.assertIs(b, SaltedHash)
        self.assertIs(c, hash.md5_crypt)

    #TODO: test policy & other options

    #=========================================================
    #policy adaptation
    #=========================================================
    #TODO:
    #norm_handler_settings
    #hash_is_compliant

    #=========================================================
    #identify
    #=========================================================
    def test_20_basic(self):
        "test basic encrypt/identify/verify functionality"
        handlers = [UnsaltedHash, SaltedHash, AnotherHash]
        cc = CryptContext(handlers, policy=None)

        #run through handlers
        for crypt in handlers:
            h = cc.encrypt("test", scheme=crypt.name)
            self.assertEquals(cc.identify(h), crypt.name)
            self.assertEquals(cc.identify(h, resolve=True), crypt)
            self.assert_(cc.verify('test', h))
            self.assert_(not cc.verify('notest', h))

        #test default
        h = cc.encrypt("test")
        self.assertEquals(cc.identify(h), AnotherHash.name)

    def test_21_identify(self):
        "test identify() border cases"
        handlers = [UnsaltedHash, SaltedHash, AnotherHash]
        cc = CryptContext(handlers, policy=None)

        #check unknown hash
        self.assertEquals(cc.identify('$9$232323123$1287319827'), None)
        self.assertRaises(ValueError, cc.identify, '$9$232323123$1287319827', required=True)

        #make sure "None" is accepted
        self.assertEquals(cc.identify(None), None)
        self.assertRaises(ValueError, cc.identify, None, required=True)

    def test_22_verify(self):
        "test verify() scheme kwd"
        handlers = [UnsaltedHash, SaltedHash, AnotherHash]
        cc = CryptContext(handlers, policy=None)

        h = AnotherHash.encrypt("test")

        #check base verify
        self.assert_(cc.verify("test", h))
        self.assert_(not cc.verify("notest", h))

        #check verify using right alg
        self.assert_(cc.verify('test', h, scheme='md5_crypt'))
        self.assert_(not cc.verify('notest', h, scheme='md5_crypt'))

        #check verify using wrong alg
        self.assertRaises(ValueError, cc.verify, 'test', h, scheme='salted_example')

    def test_23_verify_empty_hash(self):
        "test verify() allows hash=None"
        handlers = [UnsaltedHash, SaltedHash, AnotherHash]
        cc = CryptContext(handlers, policy=None)
        self.assert_(not cc.verify("test", None))
        for handler in handlers:
            self.assert_(not cc.verify("test", None, scheme=handler.name))

    #=========================================================
    #eoc
    #=========================================================

#=========================================================
#EOF
#=========================================================