|
1 | | -from zkregex_fuzzer.utils import is_valid_regex |
| 1 | +from zkregex_fuzzer.utils import is_valid_regex, has_lazy_quantifier, correct_carret_position, check_zkregex_rules_basic |
2 | 2 |
|
3 | 3 |
|
4 | 4 | def test_valid_regex(): |
@@ -26,3 +26,131 @@ def test_invalid_regex(): |
26 | 26 | ] |
27 | 27 | for pattern in invalid_patterns: |
28 | 28 | assert not is_valid_regex(pattern), f"Expected {pattern} to be invalid" |
| 29 | + |
| 30 | + |
| 31 | +def test_has_lazy_quantifier(): |
| 32 | + """Test that has_lazy_quantifier returns True for patterns with lazy quantifiers.""" |
| 33 | + patterns = [ |
| 34 | + (r"ab*c", False), |
| 35 | + (r"a+?", True), |
| 36 | + (r"(abc){2,5}?", True), |
| 37 | + (r"xyz", False), |
| 38 | + (r"[a-z]*", False), |
| 39 | + (r".+?", True), |
| 40 | + ] |
| 41 | + for pattern, expected in patterns: |
| 42 | + assert has_lazy_quantifier(pattern) == expected, f"Expected {pattern} to have lazy quantifier: {expected}" |
| 43 | + |
| 44 | + |
| 45 | +def test_correct_carret_position(): |
| 46 | + """ |
| 47 | + Test the correct_carret_position function with various corner cases. |
| 48 | + """ |
| 49 | + # Test cases with expected results |
| 50 | + test_cases = [ |
| 51 | + # Basic cases |
| 52 | + (r"^abc", True), # Start of regex |
| 53 | + (r"abc", True), # No caret |
| 54 | + (r"abc^", False), # Invalid position at end |
| 55 | + |
| 56 | + # Capturing group cases |
| 57 | + (r"(^abc)", False), # Start of capturing group |
| 58 | + (r"(|^)", True), # Alternative with caret |
| 59 | + (r"(abc|^def)", False), # Caret in middle of alternative |
| 60 | + (r"(|^)", True), # Simple alternative with caret |
| 61 | + (r"(\n|^)", True), # Newline alternative |
| 62 | + (r"abc(\n|^)", False), # Not at start of regex |
| 63 | + (r"(\r|^)", True), # Carriage return alternative |
| 64 | + (r"(\r\n|^)", True), # CRLF alternative |
| 65 | + (r"(\n\r|^)", True), # CRLF alternative |
| 66 | + (r"( |^)", True), # Spaces before alternative |
| 67 | + |
| 68 | + # Character class cases |
| 69 | + (r"[^abc]", True), # Simple negated character class |
| 70 | + (r"abc[^xyz]def", True), # Negated character class in middle |
| 71 | + (r"[abc^]", False), # Caret not at start of character class |
| 72 | + (r"[[^]]", True), # Nested character class |
| 73 | + (r"[^]", True), # Empty negated character class |
| 74 | + |
| 75 | + # Multiple caret cases |
| 76 | + (r"^abc[^xyz]", True), # Valid multiple carets |
| 77 | + (r"^abc^", False), # Invalid multiple carets |
| 78 | + (r"[^abc][^xyz]", True), # Multiple negated character classes |
| 79 | + |
| 80 | + # Edge cases |
| 81 | + (r"", True), # Empty string |
| 82 | + (r"^", True), # Just caret |
| 83 | + (r"[]^]", False), # Invalid character class |
| 84 | + (r"(^)|^", False), # Multiple start anchors |
| 85 | + (r"(^abc|^def)", False), # Multiple start anchors in group |
| 86 | + |
| 87 | + # Complex cases |
| 88 | + (r"(|^)abc[^xyz]123", True), # Combination of valid cases |
| 89 | + (r"^abc[^xyz](|^)def", False), # Invalid multiple start anchors |
| 90 | + (r"[^abc]^[^xyz]", False), # Invalid caret between character classes |
| 91 | + (r"( \r\n |^)abc", True), # Complex whitespace before alternative |
| 92 | + |
| 93 | + # Escaped caret cases |
| 94 | + (r"abc\^", True), |
| 95 | + (r"abc\^def", True), |
| 96 | + ] |
| 97 | + for regex, expected in test_cases: |
| 98 | + assert correct_carret_position(regex) == expected, f"Expected {regex} to have correct caret position: {expected}" |
| 99 | + |
| 100 | + |
| 101 | +def test_check_zkregex_rules_basic(): |
| 102 | + """ |
| 103 | + Test the check_zkregex_rules_basic function with various test cases. |
| 104 | + """ |
| 105 | + # Test cases with expected results |
| 106 | + test_cases = [ |
| 107 | + # 1. Dollar sign tests |
| 108 | + (r"abc$", (True, True)), # Valid dollar sign at end, |
| 109 | + (r"abc$def", (True, True)), # Valid dollar sign in middle |
| 110 | + (r"abc", (True, True)), # No dollar sign |
| 111 | + (r"$abc", (True, True)), # Dollar sign at start |
| 112 | + |
| 113 | + # 2. Caret position tests |
| 114 | + (r"^abc", (True, True)), # Valid caret at start |
| 115 | + (r"(|^)abc", (True, True)), # Valid caret in alternative |
| 116 | + (r"(\r\n|^)abc", (True, True)), # Valid caret with CRLF alternative |
| 117 | + (r"[^abc]", (True, True)), # Valid caret in character class |
| 118 | + (r"abc^", (False, True)), # Invalid caret at end |
| 119 | + (r"abc^def", (False, True)), # Invalid caret in middle |
| 120 | + |
| 121 | + # 3. Lazy quantifier tests |
| 122 | + (r"abc*", (True, True)), # Valid greedy quantifier |
| 123 | + (r"abc*?", (False, True)), # Invalid lazy star quantifier |
| 124 | + (r"abc+?", (False, True)), # Invalid lazy plus quantifier |
| 125 | + (r"abc??", (False, True)), # Invalid lazy question mark quantifier |
| 126 | + (r"abc{1,2}?", (False, True)), # Invalid lazy range quantifier |
| 127 | + |
| 128 | + # 4. Combined valid cases |
| 129 | + (r"^abc$", (True, True)), # Valid start and end anchors |
| 130 | + (r"(|^)abc$", (True, True)), # Valid alternative and end anchor |
| 131 | + (r"[^abc].*$", (True, True)), # Valid character class and end anchor |
| 132 | + |
| 133 | + # 5. Combined invalid cases |
| 134 | + (r"^abc$def", (True, True)), # Valid dollar position with caret |
| 135 | + (r"abc^def$", (False, True)), # Invalid caret with dollar |
| 136 | + (r"[^abc]*?$", (False, True)), # Invalid lazy quantifier with valid anchors |
| 137 | + |
| 138 | + # 6. Complex cases |
| 139 | + (r"(|^)abc[^xyz]*$", (True, True)), # Complex valid regex |
| 140 | + (r"^abc[^xyz]+def$", (True, True)), # Complex valid regex with quantifiers |
| 141 | + (r"(|^)abc*?[^xyz]$", (False, True)), # Complex invalid regex with lazy quantifier |
| 142 | + (r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", (True, True)), |
| 143 | + |
| 144 | + # 7. The common regexes from zkemail |
| 145 | + (r">[^<>]+<.*", (True, True)), |
| 146 | + (r"(\r\n|^)to:[^\r\n]+\r\n", (True, True)), |
| 147 | + (r"(\r\n|^)subject:[^\r\n]+\r\n", (True, True)), |
| 148 | + #(r"[A-Za-z0-9!#$%&'*+=?\-\^_`{|}~.\/]+@[A-Za-z0-9.\-@]+", (True, True)), |
| 149 | + #(r"[A-Za-z0-9!#$%&'*+=?\-\^_`{|}~.\/@]+@[A-Za-z0-9.\-]+", (True, True)), |
| 150 | + (r"(\r\n|^)from:[^\r\n]+\r\n", (True, True)), |
| 151 | + (r"(\r\n|^)dkim-signature:([a-z]+=[^;]+; )+bh=[a-zA-Z0-9+/=]+;", (True, True)), |
| 152 | + (r"(\r\n|^)dkim-signature:([a-z]+=[^;]+; )+t=[0-9]+;", (True, True)), |
| 153 | + (r"(\r\n|^)message-id:<[A-Za-z0-9=@\.\+_-]+>\r\n", (True, True)), |
| 154 | + ] |
| 155 | + for regex, expected in test_cases: |
| 156 | + assert check_zkregex_rules_basic(regex) == expected, f"Expected {regex} to have correct zk-regex rules: {expected}" |
0 commit comments