forked from keylime/keylime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_validators.py
More file actions
122 lines (88 loc) · 3.98 KB
/
Copy pathtest_validators.py
File metadata and controls
122 lines (88 loc) · 3.98 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import unittest
from keylime.common import validators
class TestValidRegex(unittest.TestCase):
"""Tests for valid_regex."""
def test_none(self):
"""Check that None is a valid regex."""
self.assertEqual(validators.valid_regex(None), (None, None))
def test_valid(self):
"""Check a well formed regex."""
value = validators.valid_regex(r"a.*")
assert value[0] is not None
self.assertEqual(value[0].pattern, r"a.*")
self.assertEqual(value[1], None)
def test_invalid(self):
"""Check a not valid regex."""
value = validators.valid_regex(r"a[")
self.assertEqual(value, (None, "Invalid regex: unterminated character set."))
class TestValidExcludeList(unittest.TestCase):
"""Tests for valid_exclude_list."""
def test_none(self):
"""Check that the empty list is valid."""
self.assertEqual(validators.valid_exclude_list(None), (None, None))
def test_single(self):
"""Check a single exclude list element."""
value = validators.valid_exclude_list([r"a.*"])
assert value[0] is not None
self.assertEqual(value[0].pattern, r"(a.*)")
self.assertEqual(value[1], None)
def test_multi(self):
"""Check a multiple elements exclude list."""
value = validators.valid_exclude_list([r"a.*", r"b.*"])
assert value[0] is not None
self.assertEqual(value[0].pattern, r"(a.*)|(b.*)")
self.assertEqual(value[1], None)
def test_invalid(self):
"""Check an invalid exclude list."""
value = validators.valid_exclude_list([r"a["])
self.assertEqual(value, (None, "Invalid regex: unterminated character set."))
class TestValidHex(unittest.TestCase):
"""Tests for valid_hex."""
def test_none(self):
"""Check that None is not valid."""
self.assertFalse(validators.valid_hex(None))
def test_empty(self):
"""Check that the empty string is not valid."""
self.assertFalse(validators.valid_hex(""))
def test_valid_lower(self):
"""Check a valid lower case hexadecimal number."""
self.assertTrue(validators.valid_hex("123abc"))
def test_valid_upper(self):
"""Check a valid upper case hexadecimal number."""
self.assertTrue(validators.valid_hex("123ABC"))
def test_invalid(self):
"""Check an invalid hexadecimal number."""
self.assertFalse(validators.valid_hex("123xyz"))
class TestValidUUID(unittest.TestCase):
"""Tests for valid_uuid."""
def test_none(self):
"""Check that None is not valid."""
self.assertFalse(validators.valid_uuid(None))
def test_empty(self):
"""Check that the empty string is not valid."""
self.assertFalse(validators.valid_uuid(""))
def test_valid(self):
"""Check a valid UUID that mix upper and lower case."""
self.assertTrue(validators.valid_uuid("74a93e15-da24-4ff1-ABC0-55beed02a16a"))
def test_invalid(self):
"""Check an invalid UUID that mix upper and lower case."""
self.assertFalse(validators.valid_uuid("some text"))
class TestValidAgentID(unittest.TestCase):
"""Tests for valid_agent_id."""
def test_none(self):
"""Check that None is not valid."""
self.assertFalse(validators.valid_agent_id(None))
def test_empty(self):
"""Check that the empty string is not valid."""
self.assertFalse(validators.valid_agent_id(""))
def test_valid_uuid(self):
"""Check a valid UUID that mix upper and lower case."""
self.assertTrue(validators.valid_agent_id("74a93e15-da24-4ff1-ABC0-55beed02a16a"))
def test_valid_hostname(self):
"""Check a valid hostname that mix upper and lower case."""
self.assertTrue(validators.valid_agent_id("my-Hostname.example.com"))
def test_invalid(self):
"""Check an invalid user ID with non valid characters."""
self.assertFalse(validators.valid_agent_id("rm -fr *"))
if __name__ == "__main__":
unittest.main()