示例#1
0
 def dm(pw):
     return matching.dictionary_match(pw, test_dicts)
示例#2
0
文件: tests.py 项目: taxpon/pyzxcvbn
 def dm(pw):
     return matching.dictionary_match(pw, test_dicts)
示例#3
0
    def test_dictionary_match(self):
        test_dicts = {
            "d1": {
                "motherboard": 1,
                "mother": 2,
                "board": 3,
                "abcd": 4,
                "cdef": 5
            },
            "d2": {
                "z": 1,
                "8": 2,
                "99": 3,
                "$": 4,
                "asdf1234&*": 5
            }
        }

        def dm(pw):
            return matching.dictionary_match(pw, test_dicts)

        # Case
        matches = dm("motherboard")
        patterns = ["mother", "motherboard", "board"]
        msg = "matches words that contain other words"
        self.check_matches(msg, matches, "dictionary", patterns, [[0, 5], [0, 10], [6, 10]], {
            "matched_word": ["mother", "motherboard", "board"],
            "rank": [2, 1, 3],
            "dictionary_name": ["d1", "d1", "d1"]
        })

        # Case
        matches = dm("abcdef")
        patterns = ["abcd", "cdef"]
        msg = "matches multiple words when they overlap"
        self.check_matches(msg, matches, "dictionary", patterns, [[0, 3], [2, 5]], {
            "matched_word": ["abcd", "cdef"],
            "rank": [4, 5],
            "dictionary_name": ["d1", "d1"]
        })

        # Case
        matches = dm("BoaRdZ")
        patterns = ["BoaRd", "Z"]
        msg = "ignores uppercasing"
        self.check_matches(msg, matches, "dictionary", patterns, [[0, 4], [5, 5]], {
            "matched_word": ["board", "z"],
            "rank": [3, 1],
            "dictionary_name": ["d1", "d2"]
        })

        # Case
        prefixes = ["q", "%%"]
        suffixes = ["%", "qq"]
        word = "asdf1234&*"
        for password, i, j in self.genpws(word, prefixes, suffixes):
            matches = dm(password)
            msg = "identifies words surrounded by non-words"
            self.check_matches(msg, matches, "dictionary", [word], [[i, j]], {
                "matched_word": [word],
                "rank": [5],
                "dictionary_name": ["d2"]
            })

        # Case
        for name, dic in test_dicts.items():
            for word, rank in dic.items():
                if word is "motherboard":
                    continue  # skip words that contain others
                matches = dm(word)
                msg = "matches against all words in provided dictionaries"
                self.check_matches(msg, matches, "dictionary", [word], [[0, len(word) - 1]], {
                    "matched_word": [word],
                    "rank": [rank],
                    "dictionary_name": [name]
                })

        # Case
        # test the default dictionaries
        matches = matching.dictionary_match("rosebud")
        patterns = ["ros", "rose", "rosebud", "bud"]
        ijs = [[0, 2], [0, 3], [0, 6], [4, 6]]
        msg = "default dictionaries"
        self.check_matches(msg, matches, "dictionary", patterns, ijs, {
            "matched_word": patterns,
            "rank": [13085, 65, 245, 786],
            "dictionary_name": ["surnames", "female_names", "passwords", "male_names"]
        })

        # Case
        matching.set_user_input_dictionary(["foo", "bar"])
        matches = matching.dictionary_match("foobar")
        matches = filter(lambda m: m["dictionary_name"] == "user_inputs", matches)
        msg = "matches with provided user input dictionary"
        self.check_matches(msg, matches, "dictionary", ["foo", "bar"], [[0, 2], [3, 5]], {
            "matched_word": ["foo", "bar"],
            "rank": [1, 2]
        })
示例#4
0
文件: tests.py 项目: taxpon/pyzxcvbn
    def test_dictionary_match(self):
        test_dicts = {
            "d1": {
                "motherboard": 1,
                "mother": 2,
                "board": 3,
                "abcd": 4,
                "cdef": 5
            },
            "d2": {
                "z": 1,
                "8": 2,
                "99": 3,
                "$": 4,
                "asdf1234&*": 5
            }
        }

        def dm(pw):
            return matching.dictionary_match(pw, test_dicts)

        # Case
        matches = dm("motherboard")
        patterns = ["mother", "motherboard", "board"]
        msg = "matches words that contain other words"
        self.check_matches(msg, matches, "dictionary", patterns, [[0, 5], [0, 10], [6, 10]], {
            "matched_word": ["mother", "motherboard", "board"],
            "rank": [2, 1, 3],
            "dictionary_name": ["d1", "d1", "d1"]
        })

        # Case
        matches = dm("abcdef")
        patterns = ["abcd", "cdef"]
        msg = "matches multiple words when they overlap"
        self.check_matches(msg, matches, "dictionary", patterns, [[0, 3], [2, 5]], {
            "matched_word": ["abcd", "cdef"],
            "rank": [4, 5],
            "dictionary_name": ["d1", "d1"]
        })

        # Case
        matches = dm("BoaRdZ")
        patterns = ["BoaRd", "Z"]
        msg = "ignores uppercasing"
        self.check_matches(msg, matches, "dictionary", patterns, [[0, 4], [5, 5]], {
            "matched_word": ["board", "z"],
            "rank": [3, 1],
            "dictionary_name": ["d1", "d2"]
        })

        # Case
        prefixes = ["q", "%%"]
        suffixes = ["%", "qq"]
        word = "asdf1234&*"
        for password, i, j in self.genpws(word, prefixes, suffixes):
            matches = dm(password)
            msg = "identifies words surrounded by non-words"
            self.check_matches(msg, matches, "dictionary", [word], [[i, j]], {
                "matched_word": [word],
                "rank": [5],
                "dictionary_name": ["d2"]
            })

        # Case
        for name, dic in test_dicts.items():
            for word, rank in dic.items():
                if word is "motherboard":
                    continue  # skip words that contain others
                matches = dm(word)
                msg = "matches against all words in provided dictionaries"
                self.check_matches(msg, matches, "dictionary", [word], [[0, len(word) - 1]], {
                    "matched_word": [word],
                    "rank": [rank],
                    "dictionary_name": [name]
                })

        # Case
        # test the default dictionaries
        matches = matching.dictionary_match("rosebud")
        patterns = ["ros", "rose", "rosebud", "bud"]
        ijs = [[0, 2], [0, 3], [0, 6], [4, 6]]
        msg = "default dictionaries"
        self.check_matches(msg, matches, "dictionary", patterns, ijs, {
            "matched_word": patterns,
            "rank": [13085, 65, 245, 786],
            "dictionary_name": ["surnames", "female_names", "passwords", "male_names"]
        })

        # Case
        matching.set_user_input_dictionary(["foo", "bar"])
        matches = matching.dictionary_match("foobar")
        matches = filter(lambda m: m["dictionary_name"] == "user_inputs", matches)
        msg = "matches with provided user input dictionary"
        self.check_matches(msg, matches, "dictionary", ["foo", "bar"], [[0, 2], [3, 5]], {
            "matched_word": ["foo", "bar"],
            "rank": [1, 2]
        })