# Parts of this code are based on work Copyright (c) 2003 by Domenico
# Andreoli.
#
-# Copyright (c) 2008, 2009 Jan Dittberner <jan@dittberner.info>
+# Copyright (c) 2008, 2009, 2012 Jan Dittberner <jan@dittberner.info>
#
# This file is part of cracklib.
#
import string
from _cracklib import FascistCheck
+__version__ = '2.8.19'
+
ASCII_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ASCII_LOWERCASE = "abcdefghijklmnopqrstuvwxyz"
"""
if old != None:
if new == old:
- raise ValueError, "is the same as the old one"
+ raise ValueError("is the same as the old one")
oldmono = old.lower()
newmono = new.lower()
wrapped = old + old
if newmono == oldmono:
- raise ValueError, "case changes only"
+ raise ValueError("case changes only")
if wrapped.find(new) != -1:
- raise ValueError, "is rotated"
+ raise ValueError("is rotated")
if similar(oldmono, newmono):
- raise ValueError, "is too similar to the old one"
+ raise ValueError("is too similar to the old one")
if dictpath == None:
FascistCheck(new)
FascistCheck(new, dictpath)
if palindrome(new):
- raise ValueError, "is a palindrome"
+ raise ValueError("is a palindrome")
if simple(new):
- raise ValueError, "is too simple"
+ raise ValueError("is too simple")
return new
+
+
+def test(verbosity=1, repeat=1):
+ """Test cracklib methods."""
+ import test_cracklib
+ import sys
+ result = test_cracklib.run(verbosity=verbosity, repeat=repeat)
+ if result.wasSuccessful():
+ sys.exit(0)
+ sys.exit(1)
--- /dev/null
+# -*- python -*-
+# -*- coding: utf-8 -*-
+"""
+Test suite for cracklib's Python binding.
+"""
+import os
+import sys
+import unittest
+import cracklib
+
+__version__ = '2.8.19'
+
+tests = []
+
+
+class TestModuleFunctions(unittest.TestCase):
+ def test_VeryFascistCheck(self):
+ try:
+ cracklib.VeryFascistCheck('test')
+ self.fail('expected ValueError')
+ except ValueError:
+ pass
+ try:
+ cracklib.VeryFascistCheck('LhIRI6JXpKhUqBjT')
+ except ValueError:
+ self.fail('password should be good enough')
+
+ def test_palindrome(self):
+ try:
+ cracklib.VeryFascistCheck('ot23#xyx#32to')
+ self.fail('expected ValueError')
+ except ValueError:
+ e = sys.exc_info()[1]
+ self.assertEqual('is a palindrome', str(e))
+
+ def test_same(self):
+ try:
+ cracklib.VeryFascistCheck('test', 'test')
+ self.fail('expected ValueError')
+ except ValueError:
+ e = sys.exc_info()[1]
+ self.assertEqual('is the same as the old one', str(e))
+
+ def test_case_change(self):
+ try:
+ cracklib.VeryFascistCheck('test', 'TeSt')
+ self.fail('expected ValueError')
+ except ValueError:
+ e = sys.exc_info()[1]
+ self.assertEqual('case changes only', str(e))
+
+ def test_similar(self):
+ try:
+ cracklib.VeryFascistCheck('test12', 'test34')
+ self.fail('expected ValueError')
+ except ValueError:
+ e = sys.exc_info()[1]
+ self.assertEqual('is too similar to the old one', str(e))
+
+ def test_simple(self):
+ try:
+ cracklib.VeryFascistCheck('t3sx24')
+ self.fail('expected ValueError')
+ except ValueError:
+ e = sys.exc_info()[1]
+ self.assertEqual('is too simple', str(e))
+
+ def test_simple_lower(self):
+ for passwd in ['t' * i for i in range(
+ cracklib.MIN_LENGTH - cracklib.LOW_CREDIT)]:
+ self.assertEquals(
+ 1, cracklib.simple(passwd),
+ 'password {0} should be detected as too simple'.format(
+ passwd))
+ self.assertEquals(0, cracklib.simple(
+ 't' * (cracklib.MIN_LENGTH - cracklib.LOW_CREDIT)))
+
+ def test_simple_upper(self):
+ for passwd in ['T' * i for i in range(
+ cracklib.MIN_LENGTH - cracklib.UP_CREDIT)]:
+ self.assertEquals(
+ 1, cracklib.simple(passwd),
+ 'password {0} should be detected as too simple'.format(
+ passwd))
+ self.assertEquals(0, cracklib.simple(
+ 'T' * (cracklib.MIN_LENGTH - cracklib.UP_CREDIT)))
+
+ def test_simple_digit(self):
+ for passwd in ['1' * i for i in range(
+ cracklib.MIN_LENGTH - cracklib.DIG_CREDIT)]:
+ self.assertEquals(
+ 1, cracklib.simple(passwd),
+ 'password {0} should be detected as too simple'.format(
+ passwd))
+ self.assertEquals(0, cracklib.simple(
+ '1' * (cracklib.MIN_LENGTH - cracklib.DIG_CREDIT)))
+
+ def test_simple_other(self):
+ for passwd in ['#' * i for i in range(
+ cracklib.MIN_LENGTH - cracklib.OTH_CREDIT)]:
+ self.assertEquals(
+ 1, cracklib.simple(passwd),
+ 'password {0} should be detected as too simple'.format(
+ passwd))
+ self.assertEquals(0, cracklib.simple(
+ '#' * (cracklib.MIN_LENGTH - cracklib.OTH_CREDIT)))
+
+ def test_simple_combinations(self):
+ testset = '#a' * (cracklib.MIN_LENGTH // 2)
+ for passwd in [testset[:i] for i in range(
+ cracklib.MIN_LENGTH - cracklib.LOW_CREDIT - cracklib.OTH_CREDIT)]:
+ self.assertEquals(
+ 1, cracklib.simple(passwd),
+ 'password {0} should be detected as too simple'.format(
+ passwd))
+ self.assertEquals(0, cracklib.simple(
+ testset[:(cracklib.MIN_LENGTH - cracklib.LOW_CREDIT -
+ cracklib.OTH_CREDIT)]))
+
+
+tests.append(TestModuleFunctions)
+
+
+def run(verbosity=1, repeat=1):
+ print(('cracklib is installed in: ' + os.path.dirname(__file__)))
+ print(('cracklib version: ' + __version__))
+ print((sys.version))
+
+ suite = unittest.TestSuite()
+ for cls in tests:
+ for _ in range(repeat):
+ suite.addTest(unittest.makeSuite(cls))
+ runner = unittest.TextTestRunner(verbosity=verbosity)
+ return runner.run(suite)