eggress-pproxy-compat 1.0.2

pproxy-compatible CLI and URI translation layer for Eggress
Documentation
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
use std::fmt;
use std::path::{Path, PathBuf};

/// Maximum pattern length for compiled regexes (compile-time guard).
const MAX_PATTERN_LEN: usize = 4096;

/// Maximum number of rule entries per file.
const MAX_RULE_ENTRIES: usize = 10_000;

/// Explicit fancy_regex backtracking limit applied to the compatibility backend.
/// Matches the fancy_regex 0.14 default (1,000,000 steps) and makes the bound
/// stable and testable rather than relying on the opaque dependency default.
const FANCY_REGEX_BACKTRACK_LIMIT: usize = 1_000_000;

/// Regex backend used for pattern compilation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RegexBackend {
    /// Native Rust `regex` crate (fast, no look-around/backreferences).
    Fast,
    /// `fancy_regex` crate (Perl/Python-like features: look-around, backtracking).
    Fancy,
}

impl fmt::Display for RegexBackend {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Fast => f.write_str("fast"),
            Self::Fancy => f.write_str("fancy"),
        }
    }
}

/// A compiled regex that uses either the fast `regex` backend or the
/// `fancy_regex` backend for pproxy compatibility mode.
///
/// The `fancy_regex` backend supports Perl/Python-like constructs such as
/// look-around and backreferences, which are common in pproxy rule files.
/// The fast backend is used when fancy features are not needed.
#[derive(Clone)]
pub enum CompatRegex {
    Fast(regex::Regex),
    Fancy(fancy_regex::Regex),
}

impl fmt::Debug for CompatRegex {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Fast(r) => write!(f, "CompatRegex::Fast({})", r.as_str()),
            Self::Fancy(r) => write!(f, "CompatRegex::Fancy({})", r.as_str()),
        }
    }
}

impl CompatRegex {
    /// Try to compile a pattern using the fast `regex` backend first.
    /// Falls back to `fancy_regex` if the pattern contains unsupported
    /// constructs (look-around, backreferences, etc.).
    pub fn compile(pattern: &str) -> Result<Self, RegexCompileError> {
        if pattern.len() > MAX_PATTERN_LEN {
            return Err(RegexCompileError::PatternTooLong {
                len: pattern.len(),
                max: MAX_PATTERN_LEN,
            });
        }

        // Try fast regex first
        match regex::Regex::new(pattern) {
            Ok(r) => Ok(Self::Fast(r)),
            Err(_) => {
                // Fall back to fancy_regex for Perl/Python-like constructs
                match fancy_regex::RegexBuilder::new(pattern)
                    .backtrack_limit(FANCY_REGEX_BACKTRACK_LIMIT)
                    .build()
                {
                    Ok(r) => Ok(Self::Fancy(r)),
                    Err(e) => Err(RegexCompileError::CompileError {
                        pattern: pattern.to_string(),
                        message: e.to_string(),
                    }),
                }
            }
        }
    }

    /// Compile using only the fancy_regex backend (force compatibility mode).
    pub fn compile_fancy(pattern: &str) -> Result<Self, RegexCompileError> {
        if pattern.len() > MAX_PATTERN_LEN {
            return Err(RegexCompileError::PatternTooLong {
                len: pattern.len(),
                max: MAX_PATTERN_LEN,
            });
        }

        match fancy_regex::RegexBuilder::new(pattern)
            .backtrack_limit(FANCY_REGEX_BACKTRACK_LIMIT)
            .build()
        {
            Ok(r) => Ok(Self::Fancy(r)),
            Err(e) => Err(RegexCompileError::CompileError {
                pattern: pattern.to_string(),
                message: e.to_string(),
            }),
        }
    }

    /// Returns true if the given text matches this regex.
    pub fn is_match(&self, text: &str) -> Result<bool, RegexMatchError> {
        match self {
            Self::Fast(r) => Ok(r.is_match(text)),
            Self::Fancy(r) => r.is_match(text).map_err(|e| RegexMatchError {
                pattern: r.as_str().to_string(),
                message: e.to_string(),
            }),
        }
    }

    /// Returns the backend used for this compiled regex.
    pub fn backend(&self) -> RegexBackend {
        match self {
            Self::Fast(_) => RegexBackend::Fast,
            Self::Fancy(_) => RegexBackend::Fancy,
        }
    }

    /// Returns the original pattern string.
    pub fn as_str(&self) -> &str {
        match self {
            Self::Fast(r) => r.as_str(),
            Self::Fancy(r) => r.as_str(),
        }
    }

    /// Returns true if this regex was compiled with the fancy backend.
    pub fn is_fancy(&self) -> bool {
        matches!(self, Self::Fancy(_))
    }
}

/// Error during regex compilation.
#[derive(Debug, Clone)]
pub enum RegexCompileError {
    /// Pattern exceeds the maximum allowed length.
    PatternTooLong { len: usize, max: usize },
    /// The pattern could not be compiled by either backend.
    CompileError { pattern: String, message: String },
}

impl fmt::Display for RegexCompileError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::PatternTooLong { len, max } => {
                write!(f, "pattern too long: {} bytes (max {})", len, max)
            }
            Self::CompileError { pattern, message } => {
                write!(f, "failed to compile regex '{}': {}", pattern, message)
            }
        }
    }
}

impl std::error::Error for RegexCompileError {}

/// Error during regex matching.
#[derive(Debug, Clone)]
pub struct RegexMatchError {
    pub pattern: String,
    pub message: String,
}

impl fmt::Display for RegexMatchError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "regex match failed for '{}': {}",
            self.pattern, self.message
        )
    }
}

impl std::error::Error for RegexMatchError {}

/// A diagnostic produced during rulefile loading or regex compilation.
#[derive(Debug, Clone)]
pub struct RuleDiagnostic {
    /// Line number in the rule file (1-indexed).
    pub line_number: Option<usize>,
    /// Severity level.
    pub severity: RuleSeverity,
    /// Human-readable message.
    pub message: String,
}

/// Severity of a rule diagnostic.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RuleSeverity {
    /// Informational note (e.g., fancy_regex backend used).
    Info,
    /// Warning about partial compatibility or degraded behavior.
    Warning,
    /// Error that prevented a rule from being loaded.
    Error,
}

impl fmt::Display for RuleSeverity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Info => f.write_str("info"),
            Self::Warning => f.write_str("warning"),
            Self::Error => f.write_str("error"),
        }
    }
}

/// A single parsed entry from a pproxy rule file.
#[derive(Debug, Clone)]
pub struct PproxyRuleEntry {
    /// Line number in the file (1-indexed).
    pub line_number: usize,
    /// Raw pattern string from the file.
    pub raw: String,
    /// Compiled regex (fast or fancy depending on pattern).
    pub regex: CompatRegex,
    /// Whether this rule uses the fancy_regex backend.
    pub uses_fancy: bool,
}

/// A loaded pproxy rule file with parsed entries and diagnostics.
#[derive(Debug)]
pub struct PproxyRuleFile {
    /// Path to the rule file.
    pub path: PathBuf,
    /// Parsed and compiled rule entries.
    pub entries: Vec<PproxyRuleEntry>,
    /// Diagnostics produced during loading.
    pub diagnostics: Vec<RuleDiagnostic>,
}

impl PproxyRuleFile {
    /// Load and parse a pproxy-style rule file.
    ///
    /// Rule file format:
    /// - Lines starting with `#` are comments (ignored).
    /// - Empty lines are ignored.
    /// - Every other line is a regular-expression alternative.
    ///
    /// This matches pproxy 2.7.9's `compile_rule`: it joins non-comment,
    /// non-empty lines into one expression. The `pattern -> action` syntax is
    /// accepted as an extension for older Eggress files, but is not a pproxy
    /// rule-file format and therefore remains a diagnostic rather than a
    /// routing action.
    pub fn load(path: &Path) -> Result<Self, RegexCompileError> {
        let content =
            std::fs::read_to_string(path).map_err(|e| RegexCompileError::CompileError {
                pattern: String::new(),
                message: format!("failed to read '{}': {}", path.display(), e),
            })?;

        let mut entries = Vec::new();
        let mut diagnostics = Vec::new();

        for (line_num, line) in content.lines().enumerate() {
            let line = line.trim();
            let line_number = line_num + 1;

            // Skip comments and blank lines
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            if entries.len() >= MAX_RULE_ENTRIES {
                diagnostics.push(RuleDiagnostic {
                    line_number: Some(line_number),
                    severity: RuleSeverity::Error,
                    message: format!(
                        "rule file exceeds maximum of {} entries; remaining lines ignored",
                        MAX_RULE_ENTRIES
                    ),
                });
                break;
            }

            let pattern = if let Some((pattern, action)) = line.split_once("->") {
                diagnostics.push(RuleDiagnostic {
                    line_number: Some(line_number),
                    severity: RuleSeverity::Warning,
                    message: format!(
                        "line {}: action suffix '{}' is not part of pproxy's regex-line format; using pattern only",
                        line_number,
                        action.trim()
                    ),
                });
                pattern.trim().to_string()
            } else {
                line.to_string()
            };

            match CompatRegex::compile(&pattern) {
                Ok(regex) => {
                    let uses_fancy = regex.is_fancy();
                    if uses_fancy {
                        diagnostics.push(RuleDiagnostic {
                            line_number: Some(line_number),
                            severity: RuleSeverity::Info,
                            message: format!(
                                "pattern '{}' compiled with fancy_regex backend (Python-like features enabled)",
                                pattern
                            ),
                        });
                    }
                    entries.push(PproxyRuleEntry {
                        line_number,
                        raw: pattern,
                        regex,
                        uses_fancy,
                    });
                }
                Err(e) => {
                    diagnostics.push(RuleDiagnostic {
                        line_number: Some(line_number),
                        severity: RuleSeverity::Error,
                        message: format!(
                            "line {}: failed to compile regex '{}': {}",
                            line_number, pattern, e
                        ),
                    });
                }
            }
        }

        Ok(PproxyRuleFile {
            path: path.to_path_buf(),
            entries,
            diagnostics,
        })
    }

    /// Match a hostname against all rules in the file.
    ///
    /// Returns `true` if any rule matches the hostname (first-match-wins semantics).
    pub fn matches_host(&self, hostname: &str) -> Result<bool, RegexMatchError> {
        for entry in &self.entries {
            if entry.regex.is_match(hostname)? {
                return Ok(true);
            }
        }
        Ok(false)
    }

    /// Return only the error-level diagnostics.
    pub fn errors(&self) -> Vec<&RuleDiagnostic> {
        self.diagnostics
            .iter()
            .filter(|d| d.severity == RuleSeverity::Error)
            .collect()
    }

    /// Return true if there are any error-level diagnostics.
    pub fn has_errors(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|d| d.severity == RuleSeverity::Error)
    }
}

/// Compile a single block regex pattern (from `-b` flag).
///
/// Validates pattern length and compile-time correctness.
pub fn compile_block_pattern(pattern: &str) -> Result<CompatRegex, RegexCompileError> {
    CompatRegex::compile(pattern)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn compile_simple_pattern() {
        let re = CompatRegex::compile(".*\\.example\\.com").unwrap();
        assert!(re.is_match("www.example.com").unwrap());
        assert!(!re.is_match("example.org").unwrap());
        assert_eq!(re.backend(), RegexBackend::Fast);
        assert!(!re.is_fancy());
    }

    #[test]
    fn compile_lookahead_pattern() {
        // Lookahead is not supported by regex crate, should fall back to fancy_regex
        let re = CompatRegex::compile("(?=foo)foo").unwrap();
        assert!(re.is_match("foo").unwrap());
        assert!(!re.is_match("bar").unwrap());
        assert_eq!(re.backend(), RegexBackend::Fancy);
        assert!(re.is_fancy());
    }

    #[test]
    fn compile_lookbehind_pattern() {
        let re = CompatRegex::compile("(?<=foo)bar").unwrap();
        assert!(re.is_match("foobar").unwrap());
        assert!(!re.is_match("bazbar").unwrap());
        assert_eq!(re.backend(), RegexBackend::Fancy);
    }

    #[test]
    fn compile_backreference_pattern() {
        // Backreferences are not supported by regex crate
        let re = CompatRegex::compile(r"(.)\1").unwrap();
        assert!(re.is_match("aa").unwrap());
        assert!(!re.is_match("ab").unwrap());
        assert_eq!(re.backend(), RegexBackend::Fancy);
    }

    #[test]
    fn compile_invalid_pattern() {
        let err = CompatRegex::compile("[invalid").unwrap_err();
        match err {
            RegexCompileError::CompileError { pattern, .. } => {
                assert!(pattern.contains("[invalid"));
            }
            _ => panic!("expected CompileError"),
        }
    }

    #[test]
    fn compile_pattern_too_long() {
        let pattern = "a".repeat(MAX_PATTERN_LEN + 1);
        let err = CompatRegex::compile(&pattern).unwrap_err();
        match err {
            RegexCompileError::PatternTooLong { len, max } => {
                assert_eq!(len, MAX_PATTERN_LEN + 1);
                assert_eq!(max, MAX_PATTERN_LEN);
            }
            _ => panic!("expected PatternTooLong"),
        }
    }

    #[test]
    fn compile_pattern_at_length_boundary() {
        let pattern = "a".repeat(MAX_PATTERN_LEN);
        let re = CompatRegex::compile(&pattern).unwrap();
        assert_eq!(re.as_str().len(), MAX_PATTERN_LEN);
    }

    #[test]
    fn fancy_regex_backtrack_limit_exhaustion() {
        // Pattern known to cause catastrophic backtracking with repeated alternation
        // and a lookahead that forces the VM to try all combinations.
        // Using a deliberately low limit through compile_fancy to prove limit enforcement.
        use fancy_regex::RegexBuilder;
        let low_limit = 100;
        let re = RegexBuilder::new("(?i)(a|b|ab)*(?=c)")
            .backtrack_limit(low_limit)
            .build()
            .unwrap();
        // This input forces the backtracking engine to explore many paths
        let result = re.is_match("abababababababababababababababababababababababababababab");
        assert!(result.is_err(), "should fail with BacktrackLimitExceeded");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("backtrack")
                || err_msg.contains("limit")
                || err_msg.contains("Runtime"),
            "error should be backtrack-limit related: {err_msg}"
        );
    }

    #[test]
    fn fancy_regex_explicit_limit_matches_default() {
        // Verify the constant matches the fancy_regex 0.14 default.
        use fancy_regex::RegexBuilder;
        let default_re = RegexBuilder::new("(?=.*(\\d)\\1)").build().unwrap();
        let explicit_re = RegexBuilder::new("(?=.*(\\d)\\1)")
            .backtrack_limit(FANCY_REGEX_BACKTRACK_LIMIT)
            .build()
            .unwrap();
        // Both should produce the same match result on normal input
        let input = "a11b";
        assert_eq!(
            default_re.is_match(input).unwrap(),
            explicit_re.is_match(input).unwrap(),
            "explicit limit should match default behavior"
        );
    }

    #[test]
    fn fancy_regex_backtrack_limit_is_configured() {
        // Verify the constant is set to the expected value
        assert_eq!(
            FANCY_REGEX_BACKTRACK_LIMIT, 1_000_000,
            "FANCY_REGEX_BACKTRACK_LIMIT should be 1,000,000"
        );
    }

    #[test]
    fn compile_fancy_forces_fancy_backend() {
        // Simple pattern that would normally use fast backend
        let re = CompatRegex::compile_fancy(".*\\.com").unwrap();
        assert_eq!(re.backend(), RegexBackend::Fancy);
        assert!(re.is_fancy());
        assert!(re.is_match("example.com").unwrap());
    }

    #[test]
    fn compile_fancy_invalid_pattern() {
        let err = CompatRegex::compile_fancy("[invalid").unwrap_err();
        match err {
            RegexCompileError::CompileError { .. } => {}
            _ => panic!("expected CompileError"),
        }
    }

    #[test]
    fn rulefile_load_simple() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "# comment line").unwrap();
        writeln!(f).unwrap();
        writeln!(f, ".*\\.example\\.com -> reject").unwrap();
        writeln!(f, "ads\\.com -> block").unwrap();

        let file = PproxyRuleFile::load(f.path()).unwrap();
        assert_eq!(file.entries.len(), 2);
        assert_eq!(file.entries[0].raw, ".*\\.example\\.com");
        assert_eq!(file.entries[1].raw, "ads\\.com");
        assert!(file.errors().is_empty());
    }

    #[test]
    fn rulefile_load_with_lookahead() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "(?=foo)foo -> reject").unwrap();

        let file = PproxyRuleFile::load(f.path()).unwrap();
        assert_eq!(file.entries.len(), 1);
        assert!(file.entries[0].uses_fancy);
        // Should have an info diagnostic about fancy backend
        assert!(file
            .diagnostics
            .iter()
            .any(|d| d.severity == RuleSeverity::Info));
    }

    #[test]
    fn rulefile_load_invalid_regex() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "[invalid -> reject").unwrap();

        let file = PproxyRuleFile::load(f.path()).unwrap();
        assert!(file.entries.is_empty());
        assert!(file.has_errors());
    }

    #[test]
    fn rulefile_load_partial_action() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, ".*\\.com -> allow").unwrap();

        let file = PproxyRuleFile::load(f.path()).unwrap();
        assert_eq!(file.entries.len(), 1);
        assert!(file
            .diagnostics
            .iter()
            .any(|d| d.severity == RuleSeverity::Warning));
    }

    #[test]
    fn rulefile_load_unrecognized_format() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "just a plain line").unwrap();

        let file = PproxyRuleFile::load(f.path()).unwrap();
        assert_eq!(file.entries.len(), 1);
        assert!(file.diagnostics.is_empty());
    }

    #[test]
    fn rulefile_matches_host() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, ".*\\.blocked\\.com -> reject").unwrap();
        writeln!(f, "ads\\..* -> block").unwrap();

        let file = PproxyRuleFile::load(f.path()).unwrap();
        assert!(file.matches_host("www.blocked.com").unwrap());
        assert!(file.matches_host("ads.example.com").unwrap());
        assert!(!file.matches_host("safe.example.com").unwrap());
    }

    #[test]
    fn rulefile_matches_first_wins() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, ".* -> reject").unwrap();
        writeln!(f, "safe\\.com -> block").unwrap();

        let file = PproxyRuleFile::load(f.path()).unwrap();
        // First rule matches everything
        assert!(file.matches_host("safe.com").unwrap());
    }

    #[test]
    fn compile_block_pattern_simple() {
        let re = compile_block_pattern(".*\\.ads\\.com").unwrap();
        assert!(re.is_match("banner.ads.com").unwrap());
        assert!(!re.is_match("clean.com").unwrap());
    }

    #[test]
    fn rulefile_empty_file() {
        let f = NamedTempFile::new().unwrap();
        let file = PproxyRuleFile::load(f.path()).unwrap();
        assert!(file.entries.is_empty());
        assert!(!file.has_errors());
    }

    #[test]
    fn regex_display_debug() {
        let re = CompatRegex::compile("test").unwrap();
        let debug = format!("{:?}", re);
        assert!(debug.contains("CompatRegex::Fast"));
        let display = format!("{}", re.backend());
        assert_eq!(display, "fast");
    }

    #[test]
    fn rule_diagnostic_display() {
        let diag = RuleDiagnostic {
            line_number: Some(5),
            severity: RuleSeverity::Error,
            message: "bad pattern".to_string(),
        };
        assert_eq!(diag.severity.to_string(), "error");
        assert_eq!(diag.line_number, Some(5));
        assert_eq!(diag.message, "bad pattern");
    }

    #[test]
    fn regex_compile_error_display() {
        let err = RegexCompileError::PatternTooLong {
            len: 5000,
            max: 4096,
        };
        let s = err.to_string();
        assert!(s.contains("5000"));
        assert!(s.contains("4096"));

        let err = RegexCompileError::CompileError {
            pattern: "bad".to_string(),
            message: "syntax error".to_string(),
        };
        let s = err.to_string();
        assert!(s.contains("bad"));
        assert!(s.contains("syntax error"));
    }

    #[test]
    fn fancy_regex_python_conditional() {
        // Python re supports conditionals (?(id/name)yes-pattern|no-pattern)
        // fancy_regex also supports this construct
        let re = CompatRegex::compile("(?(foo)yes|no)").unwrap();
        assert_eq!(re.backend(), RegexBackend::Fancy);
        // The conditional checks if capture group "foo" matched
        // Without a preceding match, it takes the "no" branch
        assert!(re.is_match("no").unwrap());
    }

    #[test]
    fn fancy_regex_atomic_group() {
        // Python 3.11+ supports atomic groups (?>...)
        // fancy_regex also supports this construct
        let re = CompatRegex::compile("(?>foo)").unwrap();
        assert!(re.is_match("foo").unwrap());
        // Atomic group matches "foo" at start of "foobar" (not anchored to end)
    }

    #[test]
    fn regex_unicode_category() {
        // Python re supports \p{Letter} Unicode categories
        // The fast regex crate also supports this
        let re = CompatRegex::compile("\\p{Letter}").unwrap();
        assert!(re.is_match("a").unwrap());
        assert!(re.is_match("Z").unwrap());
        assert!(!re.is_match("1").unwrap());
        assert_eq!(re.backend(), RegexBackend::Fast);
    }

    #[test]
    fn fancy_regex_backreference_in_lookahead() {
        // Backreference inside a lookahead — compiles and runs in fancy_regex
        let re = CompatRegex::compile(r"(?=.*(\d)\1)").unwrap();
        // Matches strings containing a repeated digit (e.g., "11", "22")
        assert!(re.is_match("a11b").unwrap());
        assert!(!re.is_match("abc").unwrap());
    }

    #[test]
    fn fancy_regex_backreference_matches_correctly() {
        // Verify that simple backreferences work as expected
        let re = CompatRegex::compile(r"(\w+)\s+\1").unwrap();
        assert!(re.is_match("the the").unwrap());
        assert!(!re.is_match("the that").unwrap());
        assert_eq!(re.backend(), RegexBackend::Fancy);
    }

    #[test]
    fn fancy_regex_lookahead_lookbehind_combined() {
        // Combined lookahead and lookbehind
        let re = CompatRegex::compile(r"(?<=@)\w+(?=\.com)").unwrap();
        assert!(re.is_match("user@example.com").unwrap());
        assert!(!re.is_match("user@example.org").unwrap());
        assert_eq!(re.backend(), RegexBackend::Fancy);
    }

    #[test]
    fn rulefile_max_entries_enforced() {
        let mut f = NamedTempFile::new().unwrap();
        // Write MAX_RULE_ENTRIES valid patterns plus one extra
        for i in 0..=MAX_RULE_ENTRIES {
            writeln!(f, "pattern_{i}").unwrap();
        }
        f.flush().unwrap();

        let file = PproxyRuleFile::load(f.path()).unwrap();
        // Should have loaded exactly MAX_RULE_ENTRIES entries (the extra one is dropped)
        assert_eq!(file.entries.len(), MAX_RULE_ENTRIES);
        // Should have an error diagnostic about exceeding the limit
        assert!(file.has_errors());
        let err_diag = file
            .diagnostics
            .iter()
            .find(|d| d.severity == RuleSeverity::Error)
            .expect("should have an error diagnostic");
        assert!(
            err_diag.message.contains("exceeds maximum"),
            "diagnostic should mention exceeding max: {}",
            err_diag.message
        );
    }
}