-
-
Notifications
You must be signed in to change notification settings - Fork 341
/
test_regex_identifier.py
93 lines (77 loc) · 2.67 KB
/
test_regex_identifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import pytest
from pywhat import regex_identifier
from pywhat.filter import Distribution, Filter
from pywhat.helper import load_regexes
database = load_regexes()
r = regex_identifier.RegexIdentifier()
dist = Distribution(Filter({"MinRarity": 0.0}))
def test_regex_successfully_parses():
regexes = r.distribution.get_regexes()
assert type(regexes) == list
assert len(regexes) != 0
assert all([type(regex) == dict for regex in regexes])
def regex_valid_match(name: str, match: str) -> bool:
return any(
name in matched["Regex Pattern"]["Name"]
for matched in r.check([match], dist=dist)
)
@pytest.mark.parametrize(
"name,match",
[
(regex["Name"], match)
for regex in database
for match in regex.get("Examples", {}).get("Valid", [])
],
)
def test_regex_valid_match(name: str, match: str):
assert regex_valid_match(name, match)
@pytest.mark.parametrize(
"name,match",
[
(regex["Name"], match)
for regex in database
for match in regex.get("Examples", {}).get("Invalid", [])
],
)
def test_regex_invalid_match(name: str, match: str):
assert not regex_valid_match(name, match)
@pytest.mark.skip(
reason="Fails because not a valid TLD. If presented in punycode, it works."
)
def test_international_url():
assert regex_valid_match(
"Uniform Resource Locator (URL)", r.check(["http://папироска.рф"])
)
@pytest.mark.parametrize(
"match, description",
[
# EUI-48 Identifier (Ethernet, WiFi, Bluetooth, etc)
("00:00:00:00:00:00", "Xerox Corp"),
("00-00-00-00-00-00", "Xerox Corp"),
("0000.0000.0000", "Xerox Corp"),
# MasterCard Number
("5409010000000004", "UNION NATIONAL BANK"),
("5409 0100 0000 0004", "UNION NATIONAL BANK"),
# Phone Number
("+1-202-555-0156", "United States"),
("+662025550156", "Thailand"),
("+356 202 555 0156", "Malta"),
],
)
def test_match_description(match: str, description: str):
assert description in r.check([match])[0]["Regex Pattern"]["Description"]
@pytest.mark.parametrize(
"match, exploit",
[
(
"xoxp-514654431830-843187921057-792480346180-d44d2r9b71f954o8z2k5llt41ovpip6v",
"https://slack.com/api/auth.test?token=xoxp-514654431830-843187921057-792480346180-d44d2r9b71f954o8z2k5llt41ovpip6v",
),
(
"xoxb-51465443183-hgvhXVd2ISC2x7gaoRWBOUdQ",
"https://slack.com/api/auth.test?token=xoxb-51465443183-hgvhXVd2ISC2x7gaoRWBOUdQ",
),
],
)
def test_match_exploit(match: str, exploit: str):
assert exploit in r.check([match])[0]["Regex Pattern"]["Exploit"]