dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
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
//! Unified validation engine for orchestrating both raw and owned validation.
//!
//! This module provides the core [`crate::metadata::validation::engine::ValidationEngine`] that coordinates validation across
//! both Stage 1 (raw) and Stage 2 (owned) validation. The engine supports parallel
//! execution, early termination, and comprehensive error collection while maintaining
//! a unified interface for all validation operations.
//!
//! # Architecture
//!
//! The validation engine operates in two distinct stages:
//! 1. **Raw Validation**: Validates raw assembly data using [`crate::metadata::validation::traits::RawValidator`] implementations
//! 2. **Owned Validation**: Validates resolved data structures using [`crate::metadata::validation::traits::OwnedValidator`] implementations
//!
//! # Key Components
//!
//! - [`crate::metadata::validation::ValidationEngine`] - Main validation orchestrator
//! - [`crate::metadata::validation::ValidationStatistics`] - Runtime validation statistics
//! - [`crate::metadata::validation::TwoStageValidationResult`] - Results from both validation stages
//! - [`crate::metadata::validation::ReferenceScanner`] - Shared reference scanning infrastructure
//!
//! # Usage Examples
//!
//! ```rust,no_run
//! use dotscope::metadata::validation::{ValidationEngine, ValidationConfig};
//! use dotscope::metadata::cilassemblyview::CilAssemblyView;
//! use std::path::Path;
//!
//! # let path = Path::new("assembly.dll");
//! let view = CilAssemblyView::from_path(&path)?;
//! let config = ValidationConfig::production();
//! let engine = ValidationEngine::new(&view, config)?;
//!
//! let result = engine.execute_two_stage_validation(&view, None, None)?;
//! if result.is_success() {
//!     println!("Validation passed");
//! }
//! # Ok::<(), dotscope::Error>(())
//! ```
//!
//! # Thread Safety
//!
//! All types in this module are [`Send`] and [`Sync`]. The validation engine uses parallel
//! execution internally for optimal validation speed.
//!
//! # Integration
//!
//! This module integrates with:
//! - [`crate::metadata::validation::validators`] - Collection of all validator implementations
//! - [`crate::metadata::validation::context`] - Validation context abstractions
//! - [`crate::metadata::validation::config`] - Configuration for validation behavior

use log::debug;

use crate::{
    cilassembly::AssemblyChanges,
    metadata::{
        cilassemblyview::CilAssemblyView,
        cilobject::CilObject,
        validation::{
            config::ValidationConfig,
            context::factory as context_factory,
            result::{TwoStageValidationResult, ValidationResult},
            scanner::{ReferenceScanner, ScannerStatistics},
            traits::{OwnedValidator, RawValidator},
            validators::{
                OwnedAccessibilityValidator, OwnedAssemblyValidator, OwnedAttributeValidator,
                OwnedCircularityValidator, OwnedDependencyValidator, OwnedFieldValidator,
                OwnedInheritanceValidator, OwnedMethodValidator, OwnedOwnershipValidator,
                OwnedSecurityValidator, OwnedSignatureValidator, OwnedTypeCircularityValidator,
                OwnedTypeConstraintValidator, OwnedTypeDefinitionValidator,
                OwnedTypeDependencyValidator, OwnedTypeOwnershipValidator,
                RawChangeIntegrityValidator, RawGenericConstraintValidator, RawHeapValidator,
                RawLayoutConstraintValidator, RawOperationValidator, RawSignatureValidator,
                RawTableValidator, RawTokenValidator,
            },
        },
    },
    Error, Result,
};
use rayon::{prelude::*, ThreadPool, ThreadPoolBuilder};
use std::{sync::OnceLock, time::Instant};

/// Static registry of raw validators.
///
/// Contains pre-built validator instances created once and reused for all validation operations.
/// Validators are ordered by priority (highest first) and initialized on first access.
static RAW_VALIDATORS: OnceLock<Vec<Box<dyn RawValidator>>> = OnceLock::new();

/// Static registry of owned validators.
///
/// Contains pre-built validator instances created once and reused for all validation operations.
/// Validators are ordered by priority (highest first) and initialized on first access.
static OWNED_VALIDATORS: OnceLock<Vec<Box<dyn OwnedValidator>>> = OnceLock::new();

/// Initialize the raw validators array with all validators in priority order.
fn init_raw_validators() -> Vec<Box<dyn RawValidator>> {
    vec![
        // Structure validators (priority 175-200)
        Box::new(RawTokenValidator::new()),     // priority 200
        Box::new(RawTableValidator::new()),     // priority 190
        Box::new(RawHeapValidator::new()),      // priority 180
        Box::new(RawSignatureValidator::new()), // priority 175
        // Constraint validators (priority 120-130)
        Box::new(RawGenericConstraintValidator::new()), // priority 130
        Box::new(RawLayoutConstraintValidator::new()),  // priority 120
        // Modification validators (priority 100-110)
        Box::new(RawOperationValidator::new()), // priority 110
        Box::new(RawChangeIntegrityValidator::new()), // priority 100
    ]
}

/// Initialize the owned validators array with all validators in priority order.
fn init_owned_validators() -> Vec<Box<dyn OwnedValidator>> {
    vec![
        // Type validators (priority 180-190)
        Box::new(OwnedTypeDefinitionValidator::new()), // priority 190
        Box::new(OwnedTypeConstraintValidator::new()), // priority 185
        Box::new(OwnedInheritanceValidator::new()),    // priority 180
        Box::new(OwnedTypeCircularityValidator::new()), // priority 175
        Box::new(OwnedTypeDependencyValidator::new()), // priority 170
        Box::new(OwnedTypeOwnershipValidator::new()),  // priority 165
        // Member validators (priority 150-160)
        Box::new(OwnedMethodValidator::new()), // priority 160
        Box::new(OwnedFieldValidator::new()),  // priority 155
        Box::new(OwnedAccessibilityValidator::new()), // priority 150
        // Metadata validators (priority 130-140)
        Box::new(OwnedSignatureValidator::new()), // priority 140
        Box::new(OwnedAttributeValidator::new()), // priority 130
        // Relationship validators (priority 125-135)
        Box::new(OwnedCircularityValidator::new()), // priority 135
        Box::new(OwnedDependencyValidator::new()),  // priority 130
        Box::new(OwnedOwnershipValidator::new()),   // priority 125
        // System validators (priority 110-120)
        Box::new(OwnedSecurityValidator::new()), // priority 120
        Box::new(OwnedAssemblyValidator::new()), // priority 110
    ]
}

/// Unified validation engine for coordinating all validation operations.
///
/// The [`crate::metadata::validation::engine::ValidationEngine`] serves as the central orchestrator for both raw (Stage 1)
/// and owned (Stage 2) validation. It manages parallel execution, error collection,
/// and provides a unified interface for all validation scenarios.
///
/// # Features
///
/// - **Parallel Execution**: Both stages use parallel processing
/// - **Early Termination**: Stage 1 failure prevents Stage 2 execution
/// - **Error Collection**: Comprehensive error reporting with detailed context
/// - **Flexible Configuration**: Supports various validation configurations
/// - **Statistics Tracking**: Tracks validation timing and statistics
///
/// # Examples
///
/// ```rust,ignore
/// use crate::metadata::validation::engine::ValidationEngine;
///
/// let engine = ValidationEngine::new(config)?;
///
/// // Two-stage validation
/// let result = engine.execute_two_stage_validation(
///     &assembly_view,
///     None, // No modifications
///     Some(&object_data),
///     &config,
/// )?;
/// ```
pub struct ValidationEngine {
    /// Validation configuration
    config: ValidationConfig,
    /// Shared reference scanner
    scanner: ReferenceScanner,
    /// Dedicated thread pool for this validation engine instance
    thread_pool: ThreadPool,
}

impl ValidationEngine {
    /// Creates a new validation engine with the specified configuration.
    ///
    /// # Arguments
    ///
    /// * `view` - Assembly view for scanner initialization
    /// * `config` - Validation configuration
    ///
    /// # Returns
    ///
    /// Returns a configured validation engine ready for validation operations.
    ///
    /// # Errors
    ///
    /// Returns an error if the reference scanner cannot be initialized.
    pub fn new(view: &CilAssemblyView, config: ValidationConfig) -> Result<Self> {
        let scanner =
            ReferenceScanner::from_view(view).map_err(|e| Error::ValidationEngineInitFailed {
                message: format!("Failed to initialize reference scanner: {e}"),
            })?;

        let thread_count = std::thread::available_parallelism()
            .map(std::num::NonZero::get)
            .unwrap_or(4);
        let thread_pool = ThreadPoolBuilder::new()
            .thread_name(|i| format!("validation-engine-{i}"))
            .num_threads(thread_count)
            .build()
            .map_err(|e| Error::ValidationEngineInitFailed {
                message: format!("Failed to create thread pool: {e}"),
            })?;

        debug!(
            "Validation starting: {} raw + {} owned validators, lenient={}",
            init_raw_validators().len(),
            init_owned_validators().len(),
            config.lenient
        );

        Ok(Self {
            config,
            scanner,
            thread_pool,
        })
    }

    /// Executes validation in two stages: Raw → Owned.
    ///
    /// Stage 1 must pass completely before Stage 2 runs. This method supports
    /// both loading validation (no changes) and modification validation (with changes).
    ///
    /// # Arguments
    ///
    /// * `view` - Assembly view containing raw metadata
    /// * `changes` - Optional assembly changes for modification validation
    /// * `object` - Optional CilObject for Stage 2 validation
    /// * `config` - Validation configuration
    ///
    /// # Returns
    ///
    /// Returns a comprehensive result containing outcomes from both stages.
    ///
    /// # Errors
    ///
    /// Returns an error if Stage 1 fails (preventing Stage 2) or if Stage 2 fails.
    pub fn execute_two_stage_validation(
        &self,
        view: &CilAssemblyView,
        changes: Option<&AssemblyChanges>,
        object: Option<&CilObject>,
    ) -> Result<TwoStageValidationResult> {
        let mut result = TwoStageValidationResult::new();

        // Stage 1: Raw validation (ALWAYS runs first if enabled)
        if self.config.should_validate_raw() {
            let stage1_result = self.execute_stage1_validation(view, changes)?;
            let stage1_success = stage1_result.is_success();
            result.set_stage1_result(stage1_result);

            // CRITICAL: Early termination if Stage 1 fails
            if !stage1_success {
                return Ok(result); // Return with only Stage 1 result
            }
        }

        // Stage 2: Owned validation (ONLY runs if Stage 1 passed and object is available)
        if let Some(obj) = object {
            if self.config.should_validate_owned() {
                let stage2_result = self.execute_stage2_validation(obj)?;
                result.set_stage2_result(stage2_result);
            }
        }

        Ok(result)
    }

    /// Executes Stage 1 (raw) validation with parallel processing.
    ///
    /// This method coordinates raw validators using parallel execution while
    /// maintaining fail-fast behavior for early error detection.
    ///
    /// # Arguments
    ///
    /// * `view` - Assembly view to validate
    /// * `changes` - Optional changes for modification validation
    ///
    /// # Returns
    ///
    /// Returns validation results from all raw validators.
    ///
    /// # Errors
    ///
    /// Returns an error if raw validation fails or validator execution encounters issues.
    pub fn execute_stage1_validation(
        &self,
        view: &CilAssemblyView,
        changes: Option<&AssemblyChanges>,
    ) -> Result<ValidationResult> {
        let validators = Self::get_raw_validators();
        self.validate_raw_stage(view, changes, validators)
    }

    /// Executes Stage 2 (owned) validation with parallel processing.
    ///
    /// This method coordinates owned validators using parallel execution with
    /// comprehensive error collection.
    ///
    /// # Arguments
    ///
    /// * `object` - CilObject to validate
    ///
    /// # Returns
    ///
    /// Returns validation results from all owned validators.
    ///
    /// # Errors
    ///
    /// Returns an error if owned validation fails or validator execution encounters issues.
    pub fn execute_stage2_validation(&self, object: &CilObject) -> Result<ValidationResult> {
        let validators = Self::get_owned_validators();
        self.validate_owned_stage(object, validators)
    }

    /// Validates raw metadata using parallel execution with fail-fast behavior.
    ///
    /// # Arguments
    ///
    /// * `view` - Assembly view to validate
    /// * `changes` - Optional changes for modification validation
    /// * `validators` - Collection of raw validators to execute
    ///
    /// # Returns
    ///
    /// Returns aggregated validation results.
    ///
    /// # Errors
    ///
    /// Returns an error if validation context creation fails or validator execution encounters issues.
    pub fn validate_raw_stage(
        &self,
        view: &CilAssemblyView,
        changes: Option<&AssemblyChanges>,
        validators: &Vec<Box<dyn RawValidator>>,
    ) -> Result<ValidationResult> {
        let start_time = Instant::now();

        // Create validation context
        let context = if let Some(changes) = changes {
            context_factory::raw_modification_context(
                view,
                changes,
                &self.scanner,
                &self.config,
                &self.thread_pool,
            )
        } else {
            context_factory::raw_loading_context(
                view,
                &self.scanner,
                &self.config,
                &self.thread_pool,
            )
        };

        let active_validators: Vec<_> = validators
            .iter()
            .filter(|v| v.should_run(&context))
            .collect();

        if active_validators.is_empty() {
            return Ok(ValidationResult::success());
        }

        // Execute validators in parallel using this instance's dedicated thread pool
        let results: Vec<(&str, Result<()>)> = self.thread_pool.install(|| {
            active_validators
                .par_iter()
                .map(|validator| {
                    let validator_result =
                        validator
                            .validate_raw(&context)
                            .map_err(|e| Error::ValidationRawFailed {
                                validator: validator.name().to_string(),
                                message: e.to_string(),
                            });
                    (validator.name(), validator_result)
                })
                .collect()
        });

        let duration = start_time.elapsed();

        // Convert to named results for better error reporting
        let named_results: Vec<(&str, Result<()>)> = results.into_iter().collect();
        let error_count = named_results.iter().filter(|(_, r)| r.is_err()).count();
        debug!(
            "Raw validation completed in {}ms: {} errors",
            duration.as_millis(),
            error_count
        );

        let diagnostics = if self.config.lenient {
            Some(view.diagnostics())
        } else {
            None
        };

        Ok(ValidationResult::from_named_results(
            named_results,
            duration,
            diagnostics,
        ))
    }

    /// Validates owned metadata using parallel execution with error collection.
    ///
    /// # Arguments
    ///
    /// * `object` - CilObject to validate
    /// * `validators` - Collection of owned validators to execute
    ///
    /// # Returns
    ///
    /// Returns aggregated validation results.
    ///
    /// # Errors
    ///
    /// Returns an error if validation context creation fails or validator execution encounters issues.
    pub fn validate_owned_stage(
        &self,
        object: &CilObject,
        validators: &Vec<Box<dyn OwnedValidator>>,
    ) -> Result<ValidationResult> {
        let start_time = Instant::now();

        // Create validation context
        let context =
            context_factory::owned_context(object, &self.scanner, &self.config, &self.thread_pool);

        let active_validators: Vec<_> = validators
            .iter()
            .filter(|v| v.should_run(&context))
            .collect();

        if active_validators.is_empty() {
            return Ok(ValidationResult::success());
        }

        // Execute validators in parallel using this instance's dedicated thread pool (collect all errors)
        let results: Vec<(&str, Result<()>)> = self.thread_pool.install(|| {
            active_validators
                .par_iter()
                .map(|validator| {
                    let validator_result = validator.validate_owned(&context).map_err(|e| {
                        Error::ValidationOwnedFailed {
                            validator: validator.name().to_string(),
                            message: e.to_string(),
                        }
                    });
                    (validator.name(), validator_result)
                })
                .collect()
        });

        let duration = start_time.elapsed();

        // Convert to named results for comprehensive error collection
        let named_results: Vec<(&str, Result<()>)> = results.into_iter().collect();
        let error_count = named_results.iter().filter(|(_, r)| r.is_err()).count();
        debug!(
            "Owned validation completed in {}ms: {} errors",
            duration.as_millis(),
            error_count
        );

        let diagnostics = if self.config.lenient {
            Some(object.diagnostics())
        } else {
            None
        };

        Ok(ValidationResult::from_named_results(
            named_results,
            duration,
            diagnostics,
        ))
    }

    /// Gets direct access to ALL raw validators from static registry.
    ///
    /// Configuration controls execution through each validator's should_run() method.
    /// This ensures consistent validator registration and makes the system more predictable.
    /// Validators are initialized once and reused for all validation operations.
    fn get_raw_validators() -> &'static Vec<Box<dyn RawValidator>> {
        RAW_VALIDATORS.get_or_init(init_raw_validators)
    }

    /// Gets direct access to ALL owned validators from static registry.
    ///
    /// Configuration controls execution through each validator's should_run() method.
    /// This ensures consistent validator registration and makes the system more predictable.
    /// Validators are initialized once and reused for all validation operations.
    fn get_owned_validators() -> &'static Vec<Box<dyn OwnedValidator>> {
        OWNED_VALIDATORS.get_or_init(init_owned_validators)
    }

    /// Returns the validation configuration.
    #[must_use]
    pub fn config(&self) -> &ValidationConfig {
        &self.config
    }

    /// Returns the reference scanner.
    #[must_use]
    pub fn scanner(&self) -> &ReferenceScanner {
        &self.scanner
    }

    /// Returns engine statistics and performance information.
    #[must_use]
    pub fn statistics(&self) -> EngineStatistics {
        EngineStatistics {
            scanner_stats: self.scanner.statistics(),
            raw_validator_count: Self::get_raw_validators().len(),
            owned_validator_count: Self::get_owned_validators().len(),
        }
    }
}

/// Statistics about the validation engine.
#[derive(Debug, Clone)]
pub struct EngineStatistics {
    /// Reference scanner statistics
    pub scanner_stats: ScannerStatistics,
    /// Number of raw validators available
    pub raw_validator_count: usize,
    /// Number of owned validators available
    pub owned_validator_count: usize,
}

impl std::fmt::Display for EngineStatistics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Engine Statistics: {} raw validators, {} owned validators, {}",
            self.raw_validator_count, self.owned_validator_count, self.scanner_stats
        )
    }
}

/// Factory functions for creating validation engines with common configurations.
pub mod factory {
    use super::{CilAssemblyView, Result, ValidationConfig, ValidationEngine};

    /// Creates a validation engine with minimal configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if engine initialization fails.
    pub fn minimal_engine(view: &CilAssemblyView) -> Result<ValidationEngine> {
        ValidationEngine::new(view, ValidationConfig::minimal())
    }

    /// Creates a validation engine with production configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if engine initialization fails.
    pub fn production_engine(view: &CilAssemblyView) -> Result<ValidationEngine> {
        ValidationEngine::new(view, ValidationConfig::production())
    }

    /// Creates a validation engine with comprehensive configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the validation engine cannot be initialized with the comprehensive configuration.
    pub fn comprehensive_engine(view: &CilAssemblyView) -> Result<ValidationEngine> {
        ValidationEngine::new(view, ValidationConfig::comprehensive())
    }

    /// Creates a validation engine with strict configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the validation engine cannot be initialized with the strict configuration.
    pub fn strict_engine(view: &CilAssemblyView) -> Result<ValidationEngine> {
        ValidationEngine::new(view, ValidationConfig::strict())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        cilassembly::AssemblyChanges,
        metadata::{
            cilassemblyview::CilAssemblyView,
            cilobject::CilObject,
            validation::{
                config::ValidationConfig, context::RawValidationContext, traits::RawValidator,
            },
        },
    };
    use std::path::PathBuf;

    // Test validator for validation
    struct TestRawValidator {
        should_fail: bool,
    }

    impl RawValidator for TestRawValidator {
        fn validate_raw(&self, _context: &RawValidationContext) -> Result<()> {
            if self.should_fail {
                Err(Error::NotSupported)
            } else {
                Ok(())
            }
        }

        fn name(&self) -> &'static str {
            "TestRawValidator"
        }
    }

    #[test]
    fn test_validation_engine_creation() {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WindowsBase.dll");
        if let Ok(view) = CilAssemblyView::from_path(&path) {
            let config = ValidationConfig::minimal();
            let engine = ValidationEngine::new(&view, config);
            assert!(engine.is_ok(), "Engine creation should succeed");

            let engine = engine.unwrap();
            let stats = engine.statistics();
            assert!(stats.scanner_stats.total_tokens > 0);
        }
    }

    #[test]
    fn test_two_stage_validation_early_termination() {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WindowsBase.dll");
        if let Ok(view) = CilAssemblyView::from_path(&path) {
            let mut config = ValidationConfig::comprehensive();
            config.enable_raw_validation = true;
            config.enable_owned_validation = true;

            if let Ok(engine) = ValidationEngine::new(&view, config) {
                // Test with no object data - should only run Stage 1
                let result = engine.execute_two_stage_validation(&view, None, None);
                assert!(result.is_ok());

                let result = result.unwrap();
                assert!(result.stage1_result().is_some());
                assert!(result.stage2_result().is_none());
            }
        }
    }

    #[test]
    fn test_raw_validation_with_changes() {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WindowsBase.dll");
        if let Ok(view) = CilAssemblyView::from_path(&path) {
            let config = ValidationConfig::minimal();
            if let Ok(engine) = ValidationEngine::new(&view, config) {
                let changes = AssemblyChanges::empty();

                // Test modification validation
                let result = engine.execute_stage1_validation(&view, Some(&changes));
                assert!(result.is_ok());

                // Test loading validation
                let result = engine.execute_stage1_validation(&view, None);
                assert!(result.is_ok());
            }
        }
    }

    #[test]
    fn test_factory_functions() {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WindowsBase.dll");
        if let Ok(view) = CilAssemblyView::from_path(&path) {
            assert!(factory::minimal_engine(&view).is_ok());
            assert!(factory::production_engine(&view).is_ok());
            assert!(factory::comprehensive_engine(&view).is_ok());
            assert!(factory::strict_engine(&view).is_ok());
        }
    }

    #[test]
    fn test_engine_statistics() {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WindowsBase.dll");
        if let Ok(view) = CilAssemblyView::from_path(&path) {
            if let Ok(engine) = ValidationEngine::new(&view, ValidationConfig::minimal()) {
                let stats = engine.statistics();
                let stats_string = stats.to_string();

                assert!(stats_string.contains("validators"));
                assert!(stats_string.contains("tokens"));
            }
        }
    }

    /// Test that all validators are properly registered and the engine can create them
    #[test]
    fn test_all_validators_registered() {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WindowsBase.dll");
        let view = CilAssemblyView::from_path(&path).expect("Failed to load test assembly");

        // Create validation engine with comprehensive config to ensure all validators would run
        let config = ValidationConfig::comprehensive();
        let engine =
            ValidationEngine::new(&view, config).expect("Failed to create validation engine");

        // Test engine statistics - this will call the validator creation methods
        let stats = engine.statistics();

        // Verify we have the expected number of validators
        // As of current implementation: 7 raw + 15 owned = 22 total
        assert!(
            stats.raw_validator_count >= 7,
            "Expected at least 7 raw validators, got {}",
            stats.raw_validator_count
        );
        assert!(
            stats.owned_validator_count >= 15,
            "Expected at least 15 owned validators, got {}",
            stats.owned_validator_count
        );
    }

    /// Test that raw validator creation doesn't panic and validators have unique names
    #[test]
    fn test_raw_validators_creation_and_uniqueness() {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WindowsBase.dll");
        let view = CilAssemblyView::from_path(&path).expect("Failed to load test assembly");

        let config = ValidationConfig::comprehensive();
        let engine =
            ValidationEngine::new(&view, config).expect("Failed to create validation engine");

        // This will internally call create_raw_validators()
        let result = engine.execute_stage1_validation(&view, None);

        // Should not panic during validator creation or execution
        // The important thing is that we didn't panic during validator creation
        assert!(
            result.is_ok() || result.is_err(),
            "Validation should complete without panicking"
        );

        // Test that we can access the raw validators internally
        // Note: This is testing that all raw validators can be instantiated
    }

    /// Test that owned validator creation doesn't panic and validators have unique names
    #[test]
    fn test_owned_validators_creation_and_uniqueness() {
        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
        let path = PathBuf::from(&manifest_dir).join("tests/samples/mono_4.8/mscorlib.dll");

        let object = CilObject::from_path(&path).expect("Failed to load mscorlib.dll");
        let view = CilAssemblyView::from_path(&path).expect("Failed to load test assembly");
        let config = ValidationConfig::comprehensive();
        let engine =
            ValidationEngine::new(&view, config).expect("Failed to create validation engine");

        // This will internally call create_owned_validators()
        let validation_result = engine.execute_stage2_validation(&object);

        // Should not panic during validator creation or execution
        // The important thing is that we didn't panic during validator creation
        assert!(
            validation_result.is_ok() || validation_result.is_err(),
            "Validation should complete without panicking"
        );
    }

    /// Test two-stage validation with all validators
    #[test]
    fn test_complete_two_stage_validation() {
        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
        let path = PathBuf::from(&manifest_dir).join("tests/samples/mono_4.8/mscorlib.dll");
        let view = CilAssemblyView::from_path(&path).expect("Failed to load test assembly");
        let object = CilObject::from_path(&path).expect("Failed to load mscorlib.dll");

        // Test with different validation configurations
        let configs = vec![
            ("minimal", ValidationConfig::minimal()),
            ("production", ValidationConfig::production()),
            ("comprehensive", ValidationConfig::comprehensive()),
        ];

        // Run all configurations in parallel for better performance
        configs.into_par_iter().for_each(|(name, config)| {
            let engine =
                ValidationEngine::new(&view, config).expect("Failed to create validation engine");

            // Test two-stage validation directly through the engine
            let result = engine.execute_two_stage_validation(&view, None, Some(&object));
            assert!(
                result.is_ok(),
                "Two-stage validation should complete for {name} config"
            );

            if let Ok(two_stage_result) = result {
                // Verify that the result structure is valid
                assert!(
                    two_stage_result.stage1_result().is_some()
                        || two_stage_result.stage2_result().is_some(),
                    "At least one validation stage should have run for {name} config"
                );
            }
        });
    }

    /// Test validation engine factory methods work with all validators
    #[test]
    fn test_validation_engine_factories() {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WindowsBase.dll");
        let view = CilAssemblyView::from_path(&path).expect("Failed to load test assembly");

        // Test all factory methods
        let engines = vec![
            ("minimal", factory::minimal_engine(&view)),
            ("production", factory::production_engine(&view)),
            ("comprehensive", factory::comprehensive_engine(&view)),
            ("strict", factory::strict_engine(&view)),
        ];

        for (name, engine_result) in engines {
            assert!(
                engine_result.is_ok(),
                "Failed to create {name} engine: {:?}",
                engine_result.err()
            );

            if let Ok(engine) = engine_result {
                let stats = engine.statistics();
                assert!(
                    stats.raw_validator_count > 0,
                    "{name} engine should have raw validators"
                );
                assert!(
                    stats.owned_validator_count > 0,
                    "{name} engine should have owned validators"
                );
            }
        }
    }

    /// Test that validator names are unique for debugging purposes
    #[test]
    fn test_validator_name_uniqueness() {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WindowsBase.dll");
        let view = CilAssemblyView::from_path(&path).expect("Failed to load test assembly");

        let config = ValidationConfig::comprehensive();
        let engine =
            ValidationEngine::new(&view, config).expect("Failed to create validation engine");

        // We can't directly access validator names without executing, but we can verify
        // that the engine can be created and validators are accessible
        let stats = engine.statistics();

        // Verify total validator count makes sense
        let total_validators = stats.raw_validator_count + stats.owned_validator_count;
        assert!(
            total_validators >= 22,
            "Expected at least 22 total validators, got {total_validators}"
        );
        assert_eq!(
            total_validators,
            stats.raw_validator_count + stats.owned_validator_count,
            "Total validator count should equal sum of raw and owned validators"
        );
    }
}