tldr-cli 0.1.2

CLI binary for TLDR code analysis tool
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! Input validation and path safety utilities for Pattern Analysis commands.
//!
//! Provides security-focused validation functions to mitigate:
//! - **T01 - Path Traversal**: BLOCKED_PREFIXES for system directories
//! - **T02 - Project Root Enforcement**: validate_file_path_in_project()
//! - **T03 - Integer Overflow**: Checked arithmetic for depth calculations
//! - **T08 - Memory Exhaustion**: Resource limit constants
//!
//! All file paths are canonicalized and checked against project boundaries.
//! Resource limits are enforced to prevent denial-of-service conditions.

use std::fs;
use std::path::{Path, PathBuf};

use super::error::{PatternsError, PatternsResult};

// =============================================================================
// Resource Limits (TIGER-08 Mitigations)
// =============================================================================

/// Maximum file size for analysis (10 MB).
/// Files larger than this will be rejected.
pub const MAX_FILE_SIZE: u64 = 10 * 1024 * 1024;

/// Warning threshold for file size (1 MB).
/// Files larger than this emit a warning but are still processed.
pub const WARN_FILE_SIZE: u64 = 1024 * 1024;

/// Maximum files to scan in directory analysis.
pub const MAX_DIRECTORY_FILES: u32 = 1000;

/// Maximum AST traversal depth.
/// Prevents stack overflow from deeply nested source code.
pub const MAX_AST_DEPTH: usize = 100;

/// Maximum recursion depth for analysis algorithms.
/// Used for CFG path enumeration, temporal mining, etc.
pub const MAX_ANALYSIS_DEPTH: usize = 500;

/// Maximum function name length.
pub const MAX_FUNCTION_NAME_LEN: usize = 256;

/// Maximum constraints to report per file.
pub const MAX_CONSTRAINTS_PER_FILE: usize = 500;

/// Maximum methods per class for cohesion analysis.
pub const MAX_METHODS_PER_CLASS: usize = 200;

/// Maximum fields per class for cohesion analysis.
pub const MAX_FIELDS_PER_CLASS: usize = 100;

/// Maximum classes per file.
pub const MAX_CLASSES_PER_FILE: usize = 500;

/// Maximum CFG paths to enumerate (TIGER-04).
/// Prevents unbounded path enumeration in resources command.
pub const MAX_PATHS: usize = 1000;

/// Maximum trigrams to collect (TIGER-05).
/// Prevents memory exhaustion in temporal mining.
pub const MAX_TRIGRAMS: usize = 10000;

/// Maximum class complexity (methods * fields) for analysis.
pub const MAX_CLASS_COMPLEXITY: usize = 500;

// =============================================================================
// Blocked System Directories (TIGER-01)
// =============================================================================

/// System directories that should never be analyzed (security measure).
/// Note: We specifically target sensitive system directories.
const BLOCKED_PREFIXES: &[&str] = &[
    "/etc/",
    "/etc/passwd",
    "/etc/shadow",
    "/root/",
    "/sys/",
    "/proc/",
    "/dev/",
    "/var/run/",
    "/var/log/",
    "/private/etc/",  // macOS system config
    "C:\\Windows\\",  // Windows
    "C:\\System32\\", // Windows
];

// =============================================================================
// Path Validation (TIGER-01, TIGER-02)
// =============================================================================

/// Validate and canonicalize a file path.
///
/// This function:
/// 1. Checks that the path exists
/// 2. Canonicalizes the path (resolves symlinks, `.`, `..`)
/// 3. Rejects system directories
/// 4. Validates UTF-8 encoding
///
/// # Arguments
///
/// * `path` - The path to validate
///
/// # Returns
///
/// The canonicalized path if valid, or an error.
///
/// # Errors
///
/// - `PatternsError::FileNotFound` if the file doesn't exist
/// - `PatternsError::PathTraversal` if path is a system dir or has invalid encoding
///
/// # Example
///
/// ```ignore
/// let valid = validate_file_path(Path::new("src/main.py"))?;
/// assert!(valid.is_absolute());
/// ```
pub fn validate_file_path(path: &Path) -> PatternsResult<PathBuf> {
    // Check file exists
    if !path.exists() {
        return Err(PatternsError::FileNotFound {
            path: path.to_path_buf(),
        });
    }

    // Canonicalize the path (resolves symlinks, .., .)
    let canonical = fs::canonicalize(path).map_err(|_| PatternsError::FileNotFound {
        path: path.to_path_buf(),
    })?;

    // Check for system directories
    let canonical_str = canonical.to_string_lossy();
    for blocked in BLOCKED_PREFIXES {
        // Check with trailing slash for directories, or exact match for files
        if canonical_str.starts_with(blocked) || canonical_str == blocked.trim_end_matches('/') {
            return Err(PatternsError::PathTraversal {
                path: path.to_path_buf(),
            });
        }
    }

    // Validate UTF-8 (path.to_str() returns None if not valid UTF-8)
    if canonical.to_str().is_none() {
        return Err(PatternsError::PathTraversal {
            path: path.to_path_buf(),
        });
    }

    Ok(canonical)
}

/// Validate a file path ensuring it stays within a project root.
///
/// This is stricter than `validate_file_path` - it ensures the resolved
/// path is a descendant of the project root directory.
///
/// # Arguments
///
/// * `path` - The path to validate
/// * `project_root` - The project root directory to stay within
///
/// # Returns
///
/// The canonicalized path if valid and within project root.
///
/// # Errors
///
/// - `PatternsError::FileNotFound` if the file doesn't exist
/// - `PatternsError::PathTraversal` if path escapes project root
pub fn validate_file_path_in_project(path: &Path, project_root: &Path) -> PatternsResult<PathBuf> {
    // First do basic validation
    let canonical = validate_file_path(path)?;

    // Canonicalize project root too
    let canonical_root =
        fs::canonicalize(project_root).map_err(|_| PatternsError::FileNotFound {
            path: project_root.to_path_buf(),
        })?;

    // Check that canonical path starts with canonical root
    if !canonical.starts_with(&canonical_root) {
        return Err(PatternsError::PathTraversal {
            path: path.to_path_buf(),
        });
    }

    Ok(canonical)
}

/// Validate and canonicalize a directory path.
///
/// # Arguments
///
/// * `path` - The path to validate
///
/// # Returns
///
/// The canonicalized path if valid and is a directory.
///
/// # Errors
///
/// - `PatternsError::FileNotFound` if the directory doesn't exist
/// - `PatternsError::NotADirectory` if the path is not a directory
pub fn validate_directory_path(path: &Path) -> PatternsResult<PathBuf> {
    let canonical = validate_file_path(path)?;

    if !canonical.is_dir() {
        return Err(PatternsError::NotADirectory {
            path: path.to_path_buf(),
        });
    }

    Ok(canonical)
}

/// Check if a path contains path traversal patterns.
///
/// This is a quick check for suspicious patterns before canonicalization.
/// Returns true if the path looks suspicious.
///
/// # Arguments
///
/// * `path` - The path to check
///
/// # Returns
///
/// `true` if the path contains traversal patterns (`..\` or null bytes)
pub fn is_path_traversal_attempt(path: &Path) -> bool {
    let path_str = path.to_string_lossy();

    // Check for explicit traversal patterns
    if path_str.contains("..") {
        return true;
    }

    // Check for null bytes (could be used to truncate paths)
    if path_str.contains('\0') {
        return true;
    }

    false
}

// =============================================================================
// File Size Validation (TIGER-08)
// =============================================================================

/// Validate file size against limits.
///
/// # Arguments
///
/// * `path` - The path to the file
///
/// # Returns
///
/// The file size in bytes if within limits.
///
/// # Errors
///
/// - `PatternsError::FileNotFound` if file doesn't exist
/// - `PatternsError::FileTooLarge` if file exceeds MAX_FILE_SIZE
pub fn validate_file_size(path: &Path) -> PatternsResult<u64> {
    let canonical = validate_file_path(path)?;

    let metadata = fs::metadata(&canonical)?;
    let size = metadata.len();

    if size > MAX_FILE_SIZE {
        return Err(PatternsError::FileTooLarge {
            path: path.to_path_buf(),
            bytes: size,
            max_bytes: MAX_FILE_SIZE,
        });
    }

    Ok(size)
}

/// Safely read a file with size limits and UTF-8 validation.
///
/// This function:
/// 1. Validates the file path
/// 2. Checks file size against limits
/// 3. Reads the file content
/// 4. Validates UTF-8 encoding
///
/// # Arguments
///
/// * `path` - The path to the file to read
///
/// # Returns
///
/// The file contents as a String if successful.
///
/// # Errors
///
/// - `PatternsError::FileNotFound` if file doesn't exist
/// - `PatternsError::FileTooLarge` if file exceeds MAX_FILE_SIZE
/// - `PatternsError::ParseError` if file is not valid UTF-8
/// - `PatternsError::Io` for other IO errors
pub fn read_file_safe(path: &Path) -> PatternsResult<String> {
    // Validate path and size
    let canonical = validate_file_path(path)?;

    let metadata = fs::metadata(&canonical)?;
    let size = metadata.len();

    if size > MAX_FILE_SIZE {
        return Err(PatternsError::FileTooLarge {
            path: path.to_path_buf(),
            bytes: size,
            max_bytes: MAX_FILE_SIZE,
        });
    }

    // Read the file
    let content = fs::read(&canonical)?;

    // Validate UTF-8
    String::from_utf8(content).map_err(|_| PatternsError::ParseError {
        file: path.to_path_buf(),
        message: "file is not valid UTF-8".to_string(),
    })
}

// =============================================================================
// Depth Checking (TIGER-03)
// =============================================================================

/// Check if AST depth limit has been exceeded.
///
/// Uses checked comparison to avoid any overflow issues.
///
/// # Arguments
///
/// * `current_depth` - The current traversal depth
///
/// # Returns
///
/// `Ok(())` if within limits, error otherwise.
///
/// # Errors
///
/// - `PatternsError::DepthLimitExceeded` if depth >= MAX_AST_DEPTH
pub fn check_ast_depth(current_depth: usize) -> PatternsResult<()> {
    if current_depth >= MAX_AST_DEPTH {
        Err(PatternsError::DepthLimitExceeded {
            depth: current_depth.min(u32::MAX as usize) as u32,
            max_depth: MAX_AST_DEPTH as u32,
        })
    } else {
        Ok(())
    }
}

/// Check if analysis depth limit has been exceeded.
///
/// Uses saturating arithmetic to prevent overflow.
///
/// # Arguments
///
/// * `current_depth` - The current analysis depth
///
/// # Returns
///
/// `Ok(())` if within limits, error otherwise.
///
/// # Errors
///
/// - `PatternsError::DepthLimitExceeded` if depth >= MAX_ANALYSIS_DEPTH
pub fn check_analysis_depth(current_depth: usize) -> PatternsResult<()> {
    if current_depth >= MAX_ANALYSIS_DEPTH {
        Err(PatternsError::DepthLimitExceeded {
            depth: current_depth.min(u32::MAX as usize) as u32,
            max_depth: MAX_ANALYSIS_DEPTH as u32,
        })
    } else {
        Ok(())
    }
}

/// Check if directory file count limit has been exceeded.
///
/// # Arguments
///
/// * `count` - The current file count
///
/// # Returns
///
/// `Ok(())` if within limits, error otherwise.
///
/// # Errors
///
/// - `PatternsError::TooManyFiles` if count > MAX_DIRECTORY_FILES
pub fn check_directory_file_count(count: usize) -> PatternsResult<()> {
    if count > MAX_DIRECTORY_FILES as usize {
        Err(PatternsError::TooManyFiles {
            count: count.min(u32::MAX as usize) as u32,
            max_files: MAX_DIRECTORY_FILES,
        })
    } else {
        Ok(())
    }
}

// =============================================================================
// Function Name Validation
// =============================================================================

/// Validate a function name for safety.
///
/// Ensures the name:
/// - Is not empty
/// - Contains only valid identifier characters
/// - Doesn't exceed maximum length
/// - Doesn't contain suspicious characters
///
/// # Arguments
///
/// * `name` - The function name to validate
///
/// # Returns
///
/// `Ok(())` if valid, error otherwise.
///
/// # Errors
///
/// - `PatternsError::InvalidParameter` for invalid names
pub fn validate_function_name(name: &str) -> PatternsResult<()> {
    // Check empty
    if name.is_empty() {
        return Err(PatternsError::InvalidParameter {
            message: "function name cannot be empty".to_string(),
        });
    }

    // Check length
    if name.len() > MAX_FUNCTION_NAME_LEN {
        return Err(PatternsError::InvalidParameter {
            message: format!(
                "function name too long ({} chars, max {})",
                name.len(),
                MAX_FUNCTION_NAME_LEN
            ),
        });
    }

    // Check for suspicious characters that could be used for injection
    // Valid identifiers: letters, digits, underscore (and some languages allow $)
    let suspicious_chars = [
        ';', '(', ')', '{', '}', '[', ']', '`', '"', '\'', '\\', '/', '\0',
    ];
    for c in name.chars() {
        if suspicious_chars.contains(&c) {
            return Err(PatternsError::InvalidParameter {
                message: format!("function name contains invalid character: '{}'", c),
            });
        }
    }

    // First character should be letter or underscore (standard identifier rules)
    if let Some(first) = name.chars().next() {
        if !first.is_alphabetic() && first != '_' {
            return Err(PatternsError::InvalidParameter {
                message: "function name must start with letter or underscore".to_string(),
            });
        }
    }

    Ok(())
}

// =============================================================================
// Checked Arithmetic Utilities (TIGER-03)
// =============================================================================

/// Safely increment a depth counter with overflow protection.
///
/// Returns the incremented value or saturates at usize::MAX.
///
/// # Arguments
///
/// * `depth` - The current depth value
///
/// # Returns
///
/// The incremented depth (or usize::MAX if overflow would occur)
#[inline]
pub fn saturating_depth_increment(depth: usize) -> usize {
    depth.saturating_add(1)
}

/// Safely add to a counter with overflow protection.
///
/// Returns the sum or saturates at the type maximum.
///
/// # Arguments
///
/// * `count` - The current count
/// * `add` - The amount to add
///
/// # Returns
///
/// The sum (or type max if overflow would occur)
#[inline]
pub fn saturating_count_add(count: u32, add: u32) -> u32 {
    count.saturating_add(add)
}

/// Check if a value is within a limit using checked arithmetic.
///
/// # Arguments
///
/// * `value` - The value to check
/// * `limit` - The maximum allowed value
///
/// # Returns
///
/// `true` if value < limit
#[inline]
pub fn within_limit(value: usize, limit: usize) -> bool {
    value < limit
}

// =============================================================================
// Warning Utilities
// =============================================================================

/// Check if a file size is large enough to warrant a warning.
///
/// # Arguments
///
/// * `size` - The file size in bytes
///
/// # Returns
///
/// `true` if size > WARN_FILE_SIZE
#[inline]
pub fn should_warn_file_size(size: u64) -> bool {
    size > WARN_FILE_SIZE
}

/// Format a warning message for a large file.
///
/// # Arguments
///
/// * `path` - The file path
/// * `size` - The file size in bytes
///
/// # Returns
///
/// A formatted warning string
pub fn format_large_file_warning(path: &Path, size: u64) -> String {
    format!(
        "Warning: {} is large ({:.1} MB), analysis may be slow",
        path.display(),
        size as f64 / 1024.0 / 1024.0
    )
}

// =============================================================================
// Near-Limit Warning Utilities
// =============================================================================

/// Check if a count is approaching a limit (>80%).
///
/// # Arguments
///
/// * `count` - The current count
/// * `limit` - The maximum limit
///
/// # Returns
///
/// `true` if count > 80% of limit
#[inline]
pub fn approaching_limit(count: usize, limit: usize) -> bool {
    // Use checked arithmetic to avoid overflow
    let threshold = limit.saturating_mul(80) / 100;
    count > threshold
}

/// Log a warning if approaching a limit.
///
/// # Arguments
///
/// * `count` - The current count
/// * `limit` - The maximum limit
/// * `resource_name` - Name of the resource for the warning message
pub fn warn_if_approaching_limit(count: usize, limit: usize, resource_name: &str) {
    if approaching_limit(count, limit) {
        eprintln!(
            "Warning: {} count ({}) approaching limit ({})",
            resource_name, count, limit
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::io::Write;
    use std::path::Path;
    use tempfile::{tempdir, NamedTempFile};

    // =========================================================================
    // Resource Limits Constants Tests (TIGER-08)
    // =========================================================================

    #[test]
    fn test_resource_limits_constants() {
        // Verify TIGER mitigation constants have sensible values
        assert_eq!(MAX_FILE_SIZE, 10 * 1024 * 1024); // 10 MB
        assert_eq!(MAX_DIRECTORY_FILES, 1000);
        assert_eq!(MAX_AST_DEPTH, 100); // TIGER-08
        assert_eq!(MAX_ANALYSIS_DEPTH, 500);
        assert_eq!(MAX_PATHS, 1000); // TIGER-04
        assert_eq!(MAX_TRIGRAMS, 10000); // TIGER-05
        assert_eq!(MAX_CLASS_COMPLEXITY, 500);
    }

    // =========================================================================
    // Path Validation Tests (TIGER-01, TIGER-02)
    // =========================================================================

    #[test]
    fn test_validate_file_path_normal() {
        let file = NamedTempFile::new().unwrap();
        let path = file.path();

        let result = validate_file_path(path);
        assert!(result.is_ok());

        let canonical = result.unwrap();
        assert!(canonical.is_absolute());
    }

    #[test]
    fn test_validate_file_path_not_exists() {
        let result = validate_file_path(Path::new("/nonexistent/file.py"));
        assert!(result.is_err());

        match result.unwrap_err() {
            PatternsError::FileNotFound { path } => {
                assert!(path.to_string_lossy().contains("nonexistent"));
            }
            e => panic!("Expected FileNotFound error, got {:?}", e),
        }
    }

    #[test]
    fn test_validate_file_path_traversal_blocked_dotdot() {
        // Check that path with .. pattern is detected
        let suspicious = Path::new("../etc/passwd");
        assert!(is_path_traversal_attempt(suspicious));
    }

    #[test]
    fn test_validate_file_path_traversal_blocked_null() {
        let suspicious = Path::new("file\0.txt");
        assert!(is_path_traversal_attempt(suspicious));
    }

    #[test]
    fn test_validate_file_path_in_project_valid() {
        let project = tempdir().unwrap();
        let file_path = project.path().join("src/main.py");
        fs::create_dir_all(project.path().join("src")).unwrap();
        fs::write(&file_path, "# test").unwrap();

        let result = validate_file_path_in_project(&file_path, project.path());
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_file_path_outside_project() {
        let project = tempdir().unwrap();
        let outside = tempdir().unwrap();
        let outside_file = outside.path().join("secret.txt");
        fs::write(&outside_file, "secret").unwrap();

        let result = validate_file_path_in_project(&outside_file, project.path());
        assert!(result.is_err());

        match result.unwrap_err() {
            PatternsError::PathTraversal { .. } => {}
            e => panic!("Expected PathTraversal error, got {:?}", e),
        }
    }

    #[test]
    fn test_validate_path_blocked_system_dirs() {
        // Test that system directories are blocked
        let blocked = [
            "/etc/passwd",
            "/root/.bashrc",
            "/sys/kernel/config",
            "/proc/self/status",
        ];

        for path_str in blocked {
            let path = Path::new(path_str);
            // If the file exists on this system, it should be rejected
            if path.exists() {
                let result = validate_file_path(path);
                assert!(result.is_err(), "Should reject system path: {}", path_str);
            }
        }
    }

    #[test]
    fn test_validate_directory_path_exists() {
        let dir = tempdir().unwrap();
        let result = validate_directory_path(dir.path());
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_directory_path_is_file() {
        let file = NamedTempFile::new().unwrap();
        let result = validate_directory_path(file.path());
        assert!(result.is_err());
    }

    // =========================================================================
    // File Size Validation Tests (TIGER-08)
    // =========================================================================

    #[test]
    fn test_validate_file_size_ok() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "small content").unwrap();

        let result = validate_file_size(file.path());
        assert!(result.is_ok());
        assert!(result.unwrap() < MAX_FILE_SIZE);
    }

    #[test]
    fn test_validate_file_size_not_exists() {
        let result = validate_file_size(Path::new("/nonexistent"));
        assert!(result.is_err());
    }

    #[test]
    fn test_read_file_safe_success() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "def hello():\n    print('hello')").unwrap();

        let content = read_file_safe(file.path()).unwrap();
        assert!(content.contains("def hello"));
        assert!(content.contains("print"));
    }

    #[test]
    fn test_read_file_safe_not_utf8() {
        let temp = tempdir().unwrap();
        let binary_file = temp.path().join("binary.bin");

        // Write invalid UTF-8 bytes
        let invalid_utf8 = vec![0xFF, 0xFE, 0x00, 0x01];
        fs::write(&binary_file, invalid_utf8).unwrap();

        let result = read_file_safe(&binary_file);
        assert!(result.is_err());

        match result.unwrap_err() {
            PatternsError::ParseError { message, .. } => {
                assert!(message.contains("UTF-8"));
            }
            e => panic!("Expected ParseError, got {:?}", e),
        }
    }

    // =========================================================================
    // Depth Checking Tests (TIGER-03)
    // =========================================================================

    #[test]
    fn test_check_ast_depth_ok() {
        assert!(check_ast_depth(0).is_ok());
        assert!(check_ast_depth(50).is_ok());
        assert!(check_ast_depth(MAX_AST_DEPTH - 1).is_ok());
    }

    #[test]
    fn test_check_ast_depth_exceeded() {
        let result = check_ast_depth(MAX_AST_DEPTH);
        assert!(result.is_err());

        match result.unwrap_err() {
            PatternsError::DepthLimitExceeded { depth, max_depth } => {
                assert_eq!(depth, MAX_AST_DEPTH as u32);
                assert_eq!(max_depth, MAX_AST_DEPTH as u32);
            }
            e => panic!("Expected DepthLimitExceeded error, got {:?}", e),
        }
    }

    #[test]
    fn test_check_analysis_depth_ok() {
        assert!(check_analysis_depth(0).is_ok());
        assert!(check_analysis_depth(MAX_ANALYSIS_DEPTH - 1).is_ok());
    }

    #[test]
    fn test_check_analysis_depth_exceeded() {
        let result = check_analysis_depth(MAX_ANALYSIS_DEPTH);
        assert!(result.is_err());

        match result.unwrap_err() {
            PatternsError::DepthLimitExceeded { .. } => {}
            e => panic!("Expected DepthLimitExceeded error, got {:?}", e),
        }
    }

    #[test]
    fn test_check_directory_file_count_ok() {
        assert!(check_directory_file_count(0).is_ok());
        assert!(check_directory_file_count(500).is_ok());
        assert!(check_directory_file_count(MAX_DIRECTORY_FILES as usize - 1).is_ok());
    }

    #[test]
    fn test_check_directory_file_count_exceeded() {
        let result = check_directory_file_count(MAX_DIRECTORY_FILES as usize + 1);
        assert!(result.is_err());

        match result.unwrap_err() {
            PatternsError::TooManyFiles { .. } => {}
            e => panic!("Expected TooManyFiles error, got {:?}", e),
        }
    }

    // =========================================================================
    // Function Name Validation Tests
    // =========================================================================

    #[test]
    fn test_validate_function_name_valid() {
        assert!(validate_function_name("my_function").is_ok());
        assert!(validate_function_name("_private").is_ok());
        assert!(validate_function_name("CamelCase").is_ok());
        assert!(validate_function_name("func123").is_ok());
        assert!(validate_function_name("__dunder__").is_ok());
    }

    #[test]
    fn test_validate_function_name_empty() {
        let result = validate_function_name("");
        assert!(result.is_err());

        match result.unwrap_err() {
            PatternsError::InvalidParameter { message } => {
                assert!(message.contains("empty"));
            }
            e => panic!("Expected InvalidParameter error, got {:?}", e),
        }
    }

    #[test]
    fn test_validate_function_name_too_long() {
        let long_name = "a".repeat(MAX_FUNCTION_NAME_LEN + 1);
        let result = validate_function_name(&long_name);
        assert!(result.is_err());

        match result.unwrap_err() {
            PatternsError::InvalidParameter { message } => {
                assert!(message.contains("too long"));
            }
            e => panic!("Expected InvalidParameter error, got {:?}", e),
        }
    }

    #[test]
    fn test_validate_function_name_invalid_chars() {
        let invalid_names = [
            "func;drop",  // semicolon
            "func()",     // parentheses
            "func{}",     // braces
            "func`cmd`",  // backticks
            "func\"name", // quotes
            "func\\name", // backslash
            "func/name",  // forward slash
        ];

        for name in invalid_names {
            let result = validate_function_name(name);
            assert!(result.is_err(), "Should reject: {}", name);
        }
    }

    #[test]
    fn test_validate_function_name_invalid_start() {
        let result = validate_function_name("123func");
        assert!(result.is_err());

        match result.unwrap_err() {
            PatternsError::InvalidParameter { message } => {
                assert!(message.contains("start with"));
            }
            e => panic!("Expected InvalidParameter error, got {:?}", e),
        }
    }

    // =========================================================================
    // Path Traversal Pattern Detection Tests
    // =========================================================================

    #[test]
    fn test_is_path_traversal_attempt_dotdot() {
        assert!(is_path_traversal_attempt(Path::new("../etc/passwd")));
        assert!(is_path_traversal_attempt(Path::new("foo/../bar")));
        assert!(is_path_traversal_attempt(Path::new(
            "..\\Windows\\System32"
        )));
    }

    #[test]
    fn test_is_path_traversal_attempt_normal() {
        assert!(!is_path_traversal_attempt(Path::new("src/main.rs")));
        assert!(!is_path_traversal_attempt(Path::new("/home/user/project")));
        assert!(!is_path_traversal_attempt(Path::new(".")));
    }

    // =========================================================================
    // Checked Arithmetic Tests (TIGER-03)
    // =========================================================================

    #[test]
    fn test_checked_depth_increment() {
        // Test that we use checked arithmetic - verify the function handles edge cases
        let current = usize::MAX - 1;
        // This should not panic due to overflow
        let result = check_analysis_depth(current);
        assert!(result.is_err()); // Should exceed limit, not overflow
    }

    #[test]
    fn test_saturating_depth_increment() {
        assert_eq!(saturating_depth_increment(0), 1);
        assert_eq!(saturating_depth_increment(100), 101);
        assert_eq!(saturating_depth_increment(usize::MAX), usize::MAX);
    }

    #[test]
    fn test_saturating_count_add() {
        assert_eq!(saturating_count_add(0, 1), 1);
        assert_eq!(saturating_count_add(100, 50), 150);
        assert_eq!(saturating_count_add(u32::MAX, 1), u32::MAX);
    }

    #[test]
    fn test_within_limit() {
        assert!(within_limit(0, 100));
        assert!(within_limit(99, 100));
        assert!(!within_limit(100, 100));
        assert!(!within_limit(101, 100));
    }

    #[test]
    fn test_approaching_limit() {
        // 80% of 100 = 80
        assert!(!approaching_limit(79, 100));
        assert!(!approaching_limit(80, 100));
        assert!(approaching_limit(81, 100));
        assert!(approaching_limit(100, 100));
    }

    #[test]
    fn test_should_warn_file_size() {
        assert!(!should_warn_file_size(0));
        assert!(!should_warn_file_size(WARN_FILE_SIZE));
        assert!(should_warn_file_size(WARN_FILE_SIZE + 1));
    }

    #[test]
    fn test_format_large_file_warning() {
        let warning = format_large_file_warning(Path::new("/test/file.py"), 2 * 1024 * 1024);
        assert!(warning.contains("file.py"));
        assert!(warning.contains("2.0 MB"));
        assert!(warning.contains("Warning"));
    }
}