def test_nonascii_chars(self):
        passwordmetrics.configure()
        # In 8-bit strings, ö is a part of Latin-1
        entropy, unkown = passwordmetrics._character_entropy(b'abcdefgh\xf6')
        self.assertEqual(unkown, set())

        # But this is not!
        entropy, unkown = passwordmetrics._character_entropy('abcdefgh\N{LATIN CAPITAL LETTER H WITH STROKE}')
        self.assertEqual(unkown, set('\N{LATIN CAPITAL LETTER H WITH STROKE}'))

        # If you want these to work, you have to make a custom configuration.
        if 'unicode' not in locals():
            unicode = str
        groups = {'lowercase': set(unicode(string.ascii_lowercase)),
                  'uppercase': set(unicode(string.ascii_uppercase)),
                  'digits': set(unicode(string.digits)),
                  'punctuation': set(unicode(string.punctuation)),
                  'whitespace': set(unicode(string.whitespace)),
                  'non-printable': set(unicode((i)) for i in range(128) if chr(i) not in string.printable),
                  'extras': set('åäöö\N{LATIN CAPITAL LETTER H WITH STROKE}'),
                  }

        passwordmetrics.configure(groups=groups)
        # This will still raise an error,
        entropy, unkown = passwordmetrics._character_entropy('abcdefghö\N{LATIN CAPITAL LETTER H WITH STROKE}')
        self.assertEqual(unkown, set())
示例#2
0
 def test_verifygroups(self):
     passwordmetrics.configure()
     groups = passwordmetrics.config['groups']
     for name, g in groups.items():
         for c in g:
             for n, x in groups.items():
                 if n == name:
                     continue
                 if c in x:
                     print("Group %s and group %s both have character %s" % (name, n, c))
    def test_custom_wordlist(self):
        # You might want to include non-english words in the word list.
        words = {}
        # Open a toy wordlist
        with open('docs/ordlista_sv.txt', 'rt', encoding='latin-1') as wordlist:
            for line in wordlist.readlines():
                word, entropy = line.strip().split(' ')
                words[word] = float(entropy)

        passwordmetrics.configure(words=words)

        # It won't find English words
        res = passwordmetrics.metrics('correcthorsebatterystapler')
        self.assertEqual(res['words'], set())

        # It will find some Swedish words
        res = passwordmetrics.metrics('korrekthästbatterihäftapparat')
        self.assertEqual(res['words'], set([u'batteri', u'korrekt', u'h\xe4ftapparat', u'h\xe4st']))
 def setUpClass(cls):
     passwordmetrics.configure()
     cls.words = {'correct': 1, 'horse': 2, 'battery': 3, 'staple': 4, 'a': 1, 'incorrect': 1, 'bat': 1}