-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
81 lines (63 loc) · 2.19 KB
/
tests.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
import unittest
import re
from keeper import match_config
from keeper import resolve_ip
from keeper import discover_my_ip
class TestConfRegex(unittest.TestCase):
def test_matches(self):
# List of strings that should match
matches = [
"_wildcard.example.com.conf",
"example.com.conf",
"_wildcard.test-site.com.conf",
"test.example-site.com.conf",
"_wildcard.sub.test.com.conf",
"sub.test.com.conf"
]
for match in matches:
with self.subTest(match=match):
self.assertTrue(match_config(match))
def test_non_matches(self):
# List of strings that should not match
non_matches = [
"example.com",
"_wildcard.example.com",
"_wildcardexample.com.conf",
"example_com.conf",
"example.com.conf."
]
for non_match in non_matches:
with self.subTest(non_match=non_match):
self.assertFalse(match_config(non_match))
class TestResolveIP(unittest.TestCase):
def test_resolve(self):
# List of strings that should match
domains = [
"google.com",
"apple.com",
"microsoft.com"
]
for domain in domains:
with self.subTest(should_resolve=domain):
self.assertTrue(resolve_ip(domain))
def test_not_resolve(self):
# List of strings that should match
domains = [
"this-doma1n-sh0uld-n0t-res0lve-pl3333ase.com",
"doma1n-n0t-res0lve-pl3333ase.net",
"test.non-existing-gtld"
]
for domain in domains:
with self.subTest(should_not_resolve=domain):
self.assertFalse(resolve_ip(domain))
class TestDiscoverMyIP(unittest.TestCase):
def test_ipv4_or_unknown(self):
ip = discover_my_ip()
ipv4_pattern = re.compile(r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$")
self.assertTrue(ipv4_pattern.match(ip) or ip == "unknown")
def test_cache(self):
ip1 = discover_my_ip()
ip2 = discover_my_ip()
self.assertEqual(ip1, ip2)
if __name__ == '__main__':
unittest.main()