-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_random_strings.py
150 lines (128 loc) · 4.88 KB
/
test_random_strings.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import re
import unittest
from random_strings import random_hex, random_string, random_uuid, _DEFAULT_ENTROPY
class TestRandomStrings(unittest.TestCase):
UPPER_LOWER_DIGIT_REGEX = r"^[A-Za-z0-9]+$"
NOUPPER_LOWER_DIGIT_REGEX = r"^[a-z0-9]+$"
UPPER_NOLOWER_DIGIT_REGEX = r"^[A-Z0-9]+$"
UPPER_LOWER_NODIGIT_REGEX = r"^[A-Za-z]+$"
NOUPPER_NOLOWER_DIGIT_REGEX = r"^[0-9]+$"
NOUPPER_LOWER_NODIGIT_REGEX = r"^[a-z]+$"
UPPER_NOLOWER_NODIGIT_REGEX = r"^[A-Z]+$"
valid_lengths = [8, 16, 32, 64, 512, 2048, True] # True returns length 1
invalid_lengths = [0, -5, "hello", 3.14, {}, None, False, "", " "]
def _string_lengths(self, length, *args, regex="", **kwargs):
string = random_string(length, *args, **kwargs)
match = re.fullmatch(regex, string)
self.assertIsNotNone(match)
self.assertEqual(len(string), length)
def _invalid_string_lengths(self, length, *args, regex="", **kwargs):
with self.assertRaises(ValueError):
self._string_lengths(length, *args, regex=regex, **kwargs)
def test_valid_string_lengths(self):
for length in self.valid_lengths:
self._string_lengths(
length,
regex=self.UPPER_LOWER_DIGIT_REGEX
)
def test_invalid_string_lengths(self):
for length in self.invalid_lengths:
self._invalid_string_lengths(
length,
regex=self.UPPER_LOWER_DIGIT_REGEX
)
def test_upper_false(self):
for length in self.valid_lengths:
self._string_lengths(
length,
regex=self.NOUPPER_LOWER_DIGIT_REGEX,
upper=False
)
def test_lower_false(self):
for length in self.valid_lengths:
self._string_lengths(
length,
regex=self.UPPER_NOLOWER_DIGIT_REGEX,
lower=False
)
def test_digit_false(self):
for length in self.valid_lengths:
self._string_lengths(
length,
regex=self.UPPER_LOWER_NODIGIT_REGEX,
digit=False
)
def test_upper_lower_false(self):
for length in self.valid_lengths:
self._string_lengths(
length,
regex=self.NOUPPER_NOLOWER_DIGIT_REGEX,
upper=False,
lower=False
)
def test_upper_digit_false(self):
for length in self.valid_lengths:
self._string_lengths(
length,
regex=self.NOUPPER_LOWER_NODIGIT_REGEX,
upper=False,
digit=False
)
def test_lower_digit_false(self):
for length in self.valid_lengths:
self._string_lengths(
length,
regex=self.UPPER_NOLOWER_NODIGIT_REGEX,
lower=False,
digit=False
)
def test_upper_lower_digit_false(self):
for length in self.valid_lengths:
with self.assertRaises(ValueError):
random_string(
length,
upper=False,
lower=False,
digit=False
)
def test_custom_character_string(self):
for length in self.valid_lengths:
self._string_lengths(
length,
regex=r"^\S+$",
character_string="#$^%&@*()[]"
)
string = random_string(
length,
character_string="#$^%&@*()[]"
)
self.assertIsNone(re.fullmatch(
self.UPPER_LOWER_DIGIT_REGEX, string))
string = random_string(
length,
character_string="ABCD"
)
self.assertIsNotNone(re.fullmatch(r"^[ABCD]+$", string))
class TestRandomHexAndUUID(unittest.TestCase):
HEX_REGEX = r"^[0-9a-f]+$"
UUID_REGEX_DASH = r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
UUID_REGEX_NODASH = r"^[0-9a-f]{32}$"
def test_random_hex(self):
for bytes_len in [False, None, 1, 3, 5, 8, 14, 25, 100, 2500]:
generated_hex = random_hex(nbytes=bytes_len)
match = re.fullmatch(self.HEX_REGEX, generated_hex)
if not bytes_len:
bytes_len = _DEFAULT_ENTROPY
self.assertIsNotNone(match)
self.assertEqual(len(generated_hex), bytes_len * 2)
def test_random_uuid(self):
generated_uuid = random_uuid()
match = re.fullmatch(self.UUID_REGEX_DASH, generated_uuid)
self.assertIsNotNone(match)
def test_random_uuid_nodash(self):
generated_uuid = random_uuid(dashes=False)
match = re.fullmatch(self.UUID_REGEX_NODASH, generated_uuid)
self.assertIsNotNone(match)
if __name__ == "__main__":
print("running tests")
unittest.main()