示例#1
0
文件: tests.py 项目: taxpon/pyzxcvbn
    def test_dictionary_guesses(self):

        # Case
        match = {
            "token": "aaaaa",
            "rank": 32
        }
        msg = "base guesses == the rank"
        self.assertEqual(scoring.dictionary_guesses(match), 32, msg)

        # Case
        match = {
            "token": "AAAaaa",
            "rank": 32
        }
        msg = "extra guesses are added for capitalization"
        self.assertEqual(scoring.dictionary_guesses(match), 32 * scoring.uppercase_variations(match), msg)

        # Case
        match = {
            "token": "aaa",
            "rank": 32,
            "reversed": True
        }
        msg = "guesses are doubled when word is reversed"
        self.assertEqual(scoring.dictionary_guesses(match), 32 * 2, msg)

        # Case
        match = {
            "token": 'aaa@@@',
            "rank": 32,
            "l33t": True,
            "sub": {'@': 'a'}
        }
        msg = "extra guesses are added for common l33t substitutions"
        self.assertEqual(scoring.dictionary_guesses(match), 32 * scoring.l33t_variations(match), msg)

        # Case
        match = {
            "token": 'AaA@@@',
            "rank": 32,
            "l33t": True,
            "sub": {'@': 'a'}
        }
        msg = "extra guesses are added for both capitalization and common l33t substitutions"
        expected = 32 * scoring.l33t_variations(match) * scoring.uppercase_variations(match)
        self.assertEqual(scoring.dictionary_guesses(match), expected, msg)
示例#2
0
    def test_dictionary_guesses(self):

        # Case
        match = {
            "token": "aaaaa",
            "rank": 32
        }
        msg = "base guesses == the rank"
        self.assertEqual(scoring.dictionary_guesses(match), 32, msg)

        # Case
        match = {
            "token": "AAAaaa",
            "rank": 32
        }
        msg = "extra guesses are added for capitalization"
        self.assertEqual(scoring.dictionary_guesses(match), 32 * scoring.uppercase_variations(match), msg)

        # Case
        match = {
            "token": "aaa",
            "rank": 32,
            "reversed": True
        }
        msg = "guesses are doubled when word is reversed"
        self.assertEqual(scoring.dictionary_guesses(match), 32 * 2, msg)

        # Case
        match = {
            "token": 'aaa@@@',
            "rank": 32,
            "l33t": True,
            "sub": {'@': 'a'}
        }
        msg = "extra guesses are added for common l33t substitutions"
        self.assertEqual(scoring.dictionary_guesses(match), 32 * scoring.l33t_variations(match), msg)

        # Case
        match = {
            "token": 'AaA@@@',
            "rank": 32,
            "l33t": True,
            "sub": {'@': 'a'}
        }
        msg = "extra guesses are added for both capitalization and common l33t substitutions"
        expected = 32 * scoring.l33t_variations(match) * scoring.uppercase_variations(match)
        self.assertEqual(scoring.dictionary_guesses(match), expected, msg)
示例#3
0
    def test_l33t_variations(self):

        # Case
        match = {"l33t": False}
        self.assertEqual(scoring.l33t_variations(match), 1,
                         "1 variant for non-l33t matches")

        # Case
        pattern_list = [['', 1, {}], ['a', 1, {}], ['4', 2, {
            '4': 'a'
        }], ['4pple', 2, {
            '4': 'a'
        }], ['abcet', 1, {}], ['4bcet', 2, {
            '4': 'a'
        }], ['a8cet', 2, {
            '8': 'b'
        }], ['abce+', 2, {
            '+': 't'
        }], ['48cet', 4, {
            '4': 'a',
            '8': 'b'
        }], ['a4a4aa', binom(6, 2) + binom(6, 1), {
            '4': 'a'
        }], ['4a4a44', binom(6, 2) + binom(6, 1), {
            '4': 'a'
        }],
                        [
                            'a44att+',
                            (binom(4, 2) + binom(4, 1)) * binom(3, 1), {
                                '4': 'a',
                                '+': 't'
                            }
                        ]]
        for word, variants, sub in pattern_list:
            match = {"token": word, "sub": sub, "l33t": not is_empty(sub)}
            msg = "extra l33t guesses of {} is {}".format(word, variants)
            self.assertEqual(scoring.l33t_variations(match), variants, msg)

        # Case
        match = {"token": 'Aa44aA', "l33t": True, "sub": {'4': 'a'}}
        variants = binom(6, 2) + binom(6, 1)
        msg = "capitalization doesn't affect extra l33t guesses calc"
        self.assertEqual(scoring.l33t_variations(match), variants, msg)
示例#4
0
    def test_l33t_variations(self):

        # Case
        match = {"l33t": False}
        self.assertEqual(scoring.l33t_variations(match), 1, "1 variant for non-l33t matches")

        # Case
        pattern_list = [
            ['',  1, {}],
            ['a', 1, {}],
            ['4', 2, {'4': 'a'}],
            ['4pple', 2, {'4': 'a'}],
            ['abcet', 1, {}],
            ['4bcet', 2, {'4': 'a'}],
            ['a8cet', 2, {'8': 'b'}],
            ['abce+', 2, {'+': 't'}],
            ['48cet', 4, {'4': 'a', '8': 'b'}],
            ['a4a4aa',  binom(6, 2) + binom(6, 1), {'4': 'a'}],
            ['4a4a44',  binom(6, 2) + binom(6, 1), {'4': 'a'}],
            ['a44att+', (binom(4, 2) + binom(4, 1)) * binom(3, 1), {'4': 'a', '+': 't'}]
        ]
        for word, variants, sub in pattern_list:
            match = {
                "token": word,
                "sub": sub,
                "l33t": not is_empty(sub)
            }
            msg = "extra l33t guesses of {} is {}".format(word, variants)
            self.assertEqual(scoring.l33t_variations(match), variants, msg)

        # Case
        match = {
            "token": 'Aa44aA',
            "l33t": True,
            "sub": {'4': 'a'}
        }
        variants = binom(6, 2) + binom(6, 1)
        msg = "capitalization doesn't affect extra l33t guesses calc"
        self.assertEqual(scoring.l33t_variations(match), variants, msg)