-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pattern_generation.py
More file actions
117 lines (105 loc) · 3.69 KB
/
test_pattern_generation.py
File metadata and controls
117 lines (105 loc) · 3.69 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
#!/usr/bin/env python3
"""
Test script for pattern-based password generation
"""
import itertools
import string
def generate_from_pattern(pattern, char_sets):
"""Generate passwords from a pattern using character sets"""
# Parse pattern into list of character sets
sets = []
i = 0
while i < len(pattern):
if pattern[i] == '?':
if i + 1 < len(pattern):
char_type = pattern[i + 1]
if char_type in char_sets:
sets.append(char_sets[char_type])
i += 2
else:
# Invalid, treat as literal
sets.append([pattern[i]])
i += 1
else:
sets.append([pattern[i]])
i += 1
else:
sets.append([pattern[i]])
i += 1
# Generate all combinations
for combo in itertools.product(*sets):
yield ''.join(combo)
def test_pattern_generation():
"""Test pattern-based generation"""
char_sets = {
'l': string.ascii_lowercase,
'u': string.ascii_uppercase,
'd': string.digits,
's': '!@#$%^&*'
}
# Test 1: Simple pattern with lowercase and digits
print("Test 1: Pattern '?l?l?d?d' (2 lowercase + 2 digits)")
pattern = "?l?l?d?d"
count = 0
for pw in generate_from_pattern(pattern, char_sets):
if count < 5:
print(f" {pw}")
count += 1
print(f" Total combinations: {count}")
expected = 26 * 26 * 10 * 10 # 67600
print(f" Expected: {expected}")
assert count == expected, f"Expected {expected}, got {count}"
print(" ✓ PASSED\n")
# Test 2: Pattern with literal characters
print("Test 2: Pattern 'pass?d?d' (literal 'pass' + 2 digits)")
pattern = "pass?d?d"
count = 0
for pw in generate_from_pattern(pattern, char_sets):
if count < 5:
print(f" {pw}")
count += 1
print(f" Total combinations: {count}")
expected = 10 * 10 # 100
print(f" Expected: {expected}")
assert count == expected, f"Expected {expected}, got {count}"
print(" ✓ PASSED\n")
# Test 3: Pattern with uppercase
print("Test 3: Pattern '?u?u?u' (3 uppercase)")
pattern = "?u?u?u"
count = 0
for pw in generate_from_pattern(pattern, char_sets):
if count < 5:
print(f" {pw}")
count += 1
print(f" Total combinations: {count}")
expected = 26 * 26 * 26 # 17576
print(f" Expected: {expected}")
assert count == expected, f"Expected {expected}, got {count}"
print(" ✓ PASSED\n")
# Test 4: Pattern with special characters
print("Test 4: Pattern '?l?s?d' (lowercase + special + digit)")
pattern = "?l?s?d"
count = 0
for pw in generate_from_pattern(pattern, char_sets):
if count < 5:
print(f" {pw}")
count += 1
print(f" Total combinations: {count}")
expected = 26 * 8 * 10 # 2080
print(f" Expected: {expected}")
assert count == expected, f"Expected {expected}, got {count}"
print(" ✓ PASSED\n")
# Test 5: Empty pattern (generates one empty string)
print("Test 5: Empty pattern ''")
pattern = ""
count = 0
for pw in generate_from_pattern(pattern, char_sets):
count += 1
print(f" Total combinations: {count}")
expected = 1 # Empty pattern generates one empty string
print(f" Expected: {expected}")
assert count == expected, f"Expected {expected}, got {count}"
print(" ✓ PASSED\n")
print("All tests passed! ✓")
if __name__ == "__main__":
test_pattern_generation()