Skip to content

Commit 9ebe9ff

Browse files
committed
more tests for password.py
Test error conditions for: empty passwords and verify setPassword inherits default scheme pbkdf2(), pbkdf2_sha512(), encodePassword()
1 parent 9ddfed0 commit 9ebe9ff

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

test/test_security.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,26 @@ def test_pbkdf2_unpack_errors(self):
440440
with self.assertRaises(roundup.password.PasswordValueError) as ctx:
441441
pbkdf2_unpack("fred$salt$password")
442442

443-
self.assertEqual(ctx.exception.args[0],
443+
self.assertEqual(ctx.exception.args[0],
444444
'invalid PBKDF2 hash (invalid rounds)')
445445

446+
def test_empty_passwords(self):
447+
448+
p = roundup.password.Password()
449+
450+
with self.assertRaises(ValueError) as ctx:
451+
p == "foo"
452+
453+
self.assertEqual(ctx.exception.args[0],
454+
'Password not set')
455+
456+
p = roundup.password.Password()
457+
458+
# make sure it uses the default scheme
459+
default_scheme = roundup.password.Password.default_scheme
460+
p.setPassword("sekret", config=self.db.config)
461+
self.assertEqual(p.scheme, default_scheme)
462+
446463
def test_pbkdf2_migrate_rounds(self):
447464
'''Check that migration happens when number of rounds in
448465
config is larger than number of rounds in current password.
@@ -458,6 +475,54 @@ def test_pbkdf2_migrate_rounds(self):
458475
self.assertEqual(p.needs_migration(config=self.db.config), True)
459476
del(os.environ["PYTEST_USE_CONFIG"])
460477

478+
def test_encodePassword_errors(self):
479+
self.db.config.PASSWORD_PBKDF2_DEFAULT_ROUNDS = 999
480+
481+
os.environ["PYTEST_USE_CONFIG"] = "True"
482+
with self.assertRaises(roundup.password.PasswordValueError) as ctx:
483+
p = roundup.password.encodePassword('sekrit', 'PBKDF2',
484+
config=self.db.config)
485+
486+
self.assertEqual(ctx.exception.args[0],
487+
'invalid PBKDF2 hash (rounds too low)')
488+
489+
del(os.environ["PYTEST_USE_CONFIG"])
490+
491+
with self.assertRaises(roundup.password.PasswordValueError) as ctx:
492+
p = roundup.password.encodePassword('sekrit', 'fred',
493+
config=self.db.config)
494+
495+
self.assertEqual(ctx.exception.args[0],
496+
"Unknown encryption scheme 'fred'")
497+
498+
def test_pbkdf2_errors(self):
499+
500+
with self.assertRaises(ValueError) as ctx:
501+
roundup.password.pbkdf2('sekret', b'saltandpepper', 0, 41)
502+
503+
self.assertEqual(ctx.exception.args[0],
504+
"key length too large")
505+
506+
with self.assertRaises(ValueError) as ctx:
507+
roundup.password.pbkdf2('sekret', b'saltandpepper', 0, 40)
508+
509+
self.assertEqual(ctx.exception.args[0],
510+
"rounds must be positive number")
511+
512+
def test_pbkdf2_sha512_errors(self):
513+
514+
with self.assertRaises(ValueError) as ctx:
515+
roundup.password.pbkdf2_sha512('sekret', b'saltandpepper', 0, 65)
516+
517+
self.assertEqual(ctx.exception.args[0],
518+
"key length too large")
519+
520+
with self.assertRaises(ValueError) as ctx:
521+
roundup.password.pbkdf2_sha512('sekret', b'saltandpepper', 0, 64)
522+
523+
self.assertEqual(ctx.exception.args[0],
524+
"rounds must be positive number")
525+
461526

462527
def test_encodePasswordNoConfig(self):
463528
# should run cleanly as we are in a test.

0 commit comments

Comments
 (0)