copybook-core 0.4.3

Core COBOL copybook parser, schema, and validation primitives.
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Security Audit System
//!
//! Provides comprehensive security monitoring, access control auditing,
//! and threat detection for copybook-rs enterprise operations.

use std::collections::{HashMap, HashSet};

use serde::{Deserialize, Serialize};

use super::{AuditResult, generate_audit_id};

// Security audit functionality - implementation placeholder

/// Security audit system for monitoring and validation
pub struct SecurityAuditor;

impl SecurityAuditor {
    /// Creates a new `SecurityAuditor`.
    #[must_use]
    pub fn new() -> Self {
        Self
    }

    /// Audit an access event and validate for security violations
    pub fn audit_access_event(&self, event: &AccessEvent) -> AuditResult<SecurityValidation> {
        let mut violations = Vec::new();

        // Check for failed access attempts
        if matches!(event.result, AccessResult::Failed | AccessResult::Denied) {
            violations.push(SecurityViolation {
                violation_id: generate_audit_id(),
                severity: "medium".to_string(),
                description: format!(
                    "Access {:?} for user {} to {} {}",
                    event.result, event.user_id, event.resource_type, event.resource_id
                ),
            });
        }

        // Check for suspicious source IP patterns
        if let Some(source_ip) = &event.source_ip
            && (source_ip.starts_with("0.0.0.0") || source_ip == "127.0.0.1")
        {
            violations.push(SecurityViolation {
                violation_id: generate_audit_id(),
                severity: "low".to_string(),
                description: format!("Suspicious source IP detected: {}", source_ip),
            });
        }

        let is_compliant = violations.is_empty();

        Ok(SecurityValidation {
            is_compliant,
            violations,
        })
    }

    /// Check if security severity requires immediate attention
    #[must_use]
    pub fn check_sensitive_operation(&self, severity: &str) -> bool {
        matches!(severity, "critical" | "high")
    }

    /// Validate encryption configuration against security policy
    #[must_use]
    pub fn validate_encryption_config(&self, config: &EncryptionConfig) -> Vec<SecurityViolation> {
        let mut violations = Vec::new();

        // Check at-rest algorithm: must be AES-256 or higher
        let at_rest_ok = config.at_rest_algorithm.to_uppercase().contains("AES-256")
            || config.at_rest_algorithm.to_uppercase().contains("AES256");
        if !at_rest_ok {
            violations.push(SecurityViolation {
                violation_id: generate_audit_id(),
                severity: "high".to_string(),
                description: format!(
                    "At-rest encryption algorithm '{}' does not meet AES-256 minimum requirement",
                    config.at_rest_algorithm
                ),
            });
        }

        // Check in-transit protocol: must be TLS 1.2+
        let in_transit_upper = config.in_transit_protocol.to_uppercase();
        let tls_ok = in_transit_upper.contains("TLS 1.2")
            || in_transit_upper.contains("TLS1.2")
            || in_transit_upper.contains("TLS 1.3")
            || in_transit_upper.contains("TLS1.3")
            || in_transit_upper.contains("TLSV1.2")
            || in_transit_upper.contains("TLSV1.3");
        if !tls_ok {
            violations.push(SecurityViolation {
                violation_id: generate_audit_id(),
                severity: "high".to_string(),
                description: format!(
                    "In-transit protocol '{}' does not meet TLS 1.2+ minimum requirement",
                    config.in_transit_protocol
                ),
            });
        }

        // Check key rotation: must be <= 90 days
        if config.key_rotation_days > 90 {
            violations.push(SecurityViolation {
                violation_id: generate_audit_id(),
                severity: "medium".to_string(),
                description: format!(
                    "Key rotation period of {} days exceeds the maximum allowed 90 days",
                    config.key_rotation_days
                ),
            });
        }

        violations
    }
}

impl Default for SecurityAuditor {
    fn default() -> Self {
        Self::new()
    }
}

/// Encryption configuration for security validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptionConfig {
    /// Algorithm used for at-rest encryption (e.g., "AES-256-GCM")
    pub at_rest_algorithm: String,
    /// Protocol used for in-transit encryption (e.g., "TLS 1.3")
    pub in_transit_protocol: String,
    /// Maximum number of days before key rotation is required
    pub key_rotation_days: u32,
}

/// Access control events for audit trail
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessEvent {
    /// Identifier of the user who initiated the access.
    pub user_id: String,
    /// Type of resource being accessed (e.g., "database", "file").
    pub resource_type: String,
    /// Identifier of the specific resource being accessed.
    pub resource_id: String,
    /// Kind of access performed (e.g., "read", "write").
    pub access_type: String,
    /// Source IP address of the request, if available.
    pub source_ip: Option<String>,
    /// User-Agent string from the request, if available.
    pub user_agent: Option<String>,
    /// Outcome of the access attempt.
    pub result: AccessResult,
    /// Optional RFC3339 timestamp for time-based anomaly detection
    pub timestamp: Option<String>,
}

/// Access attempt results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AccessResult {
    /// Access was granted successfully.
    Success,
    /// Access was explicitly denied by policy.
    Denied,
    /// Access failed due to an error or invalid credentials.
    Failed,
}

/// Security validation results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityValidation {
    /// Whether the validated event is compliant with security policy.
    pub is_compliant: bool,
    /// List of security violations detected during validation.
    pub violations: Vec<SecurityViolation>,
}

/// Security violations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityViolation {
    /// Unique identifier for this violation instance.
    pub violation_id: String,
    /// Severity level (e.g., "low", "medium", "high", "critical").
    pub severity: String,
    /// Human-readable description of the violation.
    pub description: String,
}

/// Access auditing system
pub struct AccessAuditor;

impl AccessAuditor {
    /// Creates a new `AccessAuditor`.
    #[must_use]
    pub fn new() -> Self {
        Self
    }

    /// Create an access record with default values
    #[must_use]
    pub fn create_access_record(
        &self,
        user_id: &str,
        resource_type: &str,
        resource_id: &str,
        access_type: &str,
    ) -> AccessEvent {
        AccessEvent {
            user_id: user_id.to_string(),
            resource_type: resource_type.to_string(),
            resource_id: resource_id.to_string(),
            access_type: access_type.to_string(),
            source_ip: None,
            user_agent: None,
            result: AccessResult::Success,
            timestamp: None,
        }
    }

    /// Audit access patterns and detect suspicious behavior
    #[must_use]
    pub fn audit_access_pattern(&self, events: &[AccessEvent]) -> Vec<SecurityViolation> {
        let mut violations = Vec::new();

        // Count failures by user
        let mut user_failures: HashMap<String, usize> = HashMap::new();

        for event in events {
            if matches!(event.result, AccessResult::Failed | AccessResult::Denied) {
                *user_failures.entry(event.user_id.clone()).or_insert(0) += 1;
            }
        }

        // Generate violations for users with multiple failures
        for (user_id, failure_count) in user_failures {
            if failure_count >= 3 {
                violations.push(SecurityViolation {
                    violation_id: generate_audit_id(),
                    severity: if failure_count >= 5 {
                        "high".to_string()
                    } else {
                        "medium".to_string()
                    },
                    description: format!(
                        "User {} has {} failed access attempts - possible brute force attack",
                        user_id, failure_count
                    ),
                });
            }
        }

        violations
    }

    /// Detect anomalies in a time window using frequency and pattern analysis.
    ///
    /// Checks for:
    /// - Rapid-fire access: >10 events from same user in window
    /// - Multi-resource scanning: same user accessing >5 distinct resources
    ///
    /// The `window_secs` parameter is reserved for future time-filtering when
    /// events carry timestamps; currently all provided events are analysed.
    #[must_use]
    pub fn detect_anomalies(
        &self,
        events: &[AccessEvent],
        _window_secs: u64,
    ) -> Vec<SecurityViolation> {
        let mut violations = Vec::new();

        // Aggregate per-user counts and distinct resources
        let mut user_event_count: HashMap<&str, usize> = HashMap::new();
        let mut user_resources: HashMap<&str, HashSet<&str>> = HashMap::new();

        for event in events {
            *user_event_count.entry(event.user_id.as_str()).or_insert(0) += 1;

            user_resources
                .entry(event.user_id.as_str())
                .or_default()
                .insert(event.resource_id.as_str());
        }

        // Rapid-fire access: >10 events per user
        for (user_id, count) in &user_event_count {
            if *count > 10 {
                violations.push(SecurityViolation {
                    violation_id: generate_audit_id(),
                    severity: "high".to_string(),
                    description: format!(
                        "Anomaly: user {} generated {} events in the monitoring window (threshold: 10)",
                        user_id, count
                    ),
                });
            }
        }

        // Multi-resource scanning: >5 distinct resources per user
        for (user_id, resources) in &user_resources {
            if resources.len() > 5 {
                violations.push(SecurityViolation {
                    violation_id: generate_audit_id(),
                    severity: "high".to_string(),
                    description: format!(
                        "Anomaly: user {} accessed {} distinct resources in the monitoring window (threshold: 5)",
                        user_id,
                        resources.len()
                    ),
                });
            }
        }

        violations
    }
}

impl Default for AccessAuditor {
    fn default() -> Self {
        Self::new()
    }
}

/// Map a severity string to a numeric value for SIEM formats.
fn severity_to_num(severity: &str) -> u8 {
    match severity {
        "critical" => 10,
        "high" => 7,
        "medium" => 5,
        "low" => 3,
        _ => 1,
    }
}

/// Security monitoring system
pub struct SecurityMonitor;

impl SecurityMonitor {
    /// Creates a new `SecurityMonitor`.
    #[must_use]
    pub fn new() -> Self {
        Self
    }

    /// Detect threats based on failure count thresholds
    #[must_use]
    pub fn detect_threats(&self, failed_count: usize) -> Vec<SecurityViolation> {
        let mut violations = Vec::new();

        // Threshold-based threat detection
        if failed_count >= 10 {
            violations.push(SecurityViolation {
                violation_id: generate_audit_id(),
                severity: "critical".to_string(),
                description: format!(
                    "Critical: {} failed operations detected - possible security breach or system compromise",
                    failed_count
                ),
            });
        } else if failed_count >= 5 {
            violations.push(SecurityViolation {
                violation_id: generate_audit_id(),
                severity: "high".to_string(),
                description: format!(
                    "High: {} failed operations detected - potential security incident",
                    failed_count
                ),
            });
        } else if failed_count >= 3 {
            violations.push(SecurityViolation {
                violation_id: generate_audit_id(),
                severity: "medium".to_string(),
                description: format!(
                    "Medium: {} failed operations detected - monitoring required",
                    failed_count
                ),
            });
        }

        violations
    }

    /// Generate a security alert string from a violation
    #[must_use]
    pub fn generate_security_alert(&self, violation: &SecurityViolation) -> String {
        format!(
            "[SECURITY ALERT] {} | Severity: {} | {}",
            violation.violation_id, violation.severity, violation.description
        )
    }

    /// Format a violation as ArcSight CEF (Common Event Format).
    ///
    /// Format: `CEF:0|Vendor|Product|Version|SignatureID|Name|Severity|Extensions`
    #[must_use]
    pub fn format_cef(&self, violation: &SecurityViolation) -> String {
        let severity_num = severity_to_num(&violation.severity);
        format!(
            "CEF:0|copybook-rs|SecurityMonitor|1.0|{}|{}|{}|",
            violation.violation_id, violation.description, severity_num
        )
    }

    /// Format a violation as IBM QRadar LEEF (Log Event Extended Format).
    ///
    /// Format: `LEEF:2.0|Vendor|Product|Version|EventID|key=value\t...`
    #[must_use]
    pub fn format_leef(&self, violation: &SecurityViolation) -> String {
        format!(
            "LEEF:2.0|copybook-rs|SecurityMonitor|1.0|{}|severity={}\tdescription={}",
            violation.violation_id, violation.severity, violation.description
        )
    }

    /// Format a violation as a Syslog RFC 5424 message.
    ///
    /// Format: `<Priority>VERSION TIMESTAMP HOSTNAME APP-NAME PROCID MSGID STRUCTURED-DATA MSG`
    #[must_use]
    pub fn format_syslog(&self, violation: &SecurityViolation) -> String {
        // RFC 5424 priority = (facility * 8) + severity_level
        // Using facility 16 (local0) and severity 4 (warning) as a reasonable default.
        let priority = 16 * 8 + 4; // 132
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        format!(
            "<{}>1 {} copybook-rs SecurityMonitor - {} - {}",
            priority, timestamp, violation.violation_id, violation.description
        )
    }
}

impl Default for SecurityMonitor {
    fn default() -> Self {
        Self::new()
    }
}

/// Real-time event monitor that tracks rolling access events and emits alerts
/// when a configurable threshold is breached.
pub struct EventMonitor {
    /// Number of events that must accumulate before an alert is emitted
    pub alert_threshold: usize,
    /// Events seen since last reset
    events: Vec<AccessEvent>,
    /// Alerts that have been emitted and are awaiting consumption
    alerts: Vec<SecurityViolation>,
}

impl EventMonitor {
    /// Create a new `EventMonitor` with the given alert threshold.
    #[must_use]
    pub fn new(alert_threshold: usize) -> Self {
        Self {
            alert_threshold,
            events: Vec::new(),
            alerts: Vec::new(),
        }
    }

    /// Process a single event. Returns `Some(SecurityViolation)` if the alert
    /// threshold has just been crossed, and adds the violation to
    /// `pending_alerts`. Returns `None` otherwise.
    pub fn process_event(&mut self, event: &AccessEvent) -> Option<SecurityViolation> {
        self.events.push(event.clone());

        if self.events.len() == self.alert_threshold + 1 {
            let violation = SecurityViolation {
                violation_id: generate_audit_id(),
                severity: "high".to_string(),
                description: format!(
                    "EventMonitor: alert threshold of {} events breached ({} events received)",
                    self.alert_threshold,
                    self.events.len()
                ),
            };
            self.alerts.push(violation.clone());
            return Some(violation);
        }

        None
    }

    /// Return pending alerts that have not yet been consumed.
    #[must_use]
    pub fn pending_alerts(&self) -> &[SecurityViolation] {
        &self.alerts
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;

    // -------------------------------------------------------------------------
    // Helpers
    // -------------------------------------------------------------------------

    fn make_event(user_id: &str, resource_id: &str, result: AccessResult) -> AccessEvent {
        AccessEvent {
            user_id: user_id.to_string(),
            resource_type: "database".to_string(),
            resource_id: resource_id.to_string(),
            access_type: "read".to_string(),
            source_ip: None,
            user_agent: None,
            result,
            timestamp: None,
        }
    }

    // -------------------------------------------------------------------------
    // SecurityAuditor – existing tests (ported, timestamp field added)
    // -------------------------------------------------------------------------

    #[test]
    fn test_security_auditor_audit_access_event_success() {
        let auditor = SecurityAuditor::new();
        let event = AccessEvent {
            user_id: "user123".to_string(),
            resource_type: "database".to_string(),
            resource_id: "prod-db".to_string(),
            access_type: "read".to_string(),
            source_ip: Some("192.168.1.1".to_string()),
            user_agent: Some("Mozilla/5.0".to_string()),
            result: AccessResult::Success,
            timestamp: None,
        };

        let validation = auditor
            .audit_access_event(&event)
            .expect("Audit should succeed");

        assert!(validation.is_compliant);
        assert!(validation.violations.is_empty());
    }

    #[test]
    fn test_security_auditor_audit_access_event_failed() {
        let auditor = SecurityAuditor::new();
        let event = AccessEvent {
            user_id: "user123".to_string(),
            resource_type: "database".to_string(),
            resource_id: "prod-db".to_string(),
            access_type: "write".to_string(),
            source_ip: Some("192.168.1.1".to_string()),
            user_agent: None,
            result: AccessResult::Failed,
            timestamp: None,
        };

        let validation = auditor
            .audit_access_event(&event)
            .expect("Audit should succeed");

        assert!(!validation.is_compliant);
        assert_eq!(validation.violations.len(), 1);
        assert_eq!(validation.violations[0].severity, "medium");
    }

    #[test]
    fn test_security_auditor_audit_access_event_denied() {
        let auditor = SecurityAuditor::new();
        let event = AccessEvent {
            user_id: "user456".to_string(),
            resource_type: "file".to_string(),
            resource_id: "secret.txt".to_string(),
            access_type: "read".to_string(),
            source_ip: None,
            user_agent: None,
            result: AccessResult::Denied,
            timestamp: None,
        };

        let validation = auditor
            .audit_access_event(&event)
            .expect("Audit should succeed");

        assert!(!validation.is_compliant);
        assert_eq!(validation.violations.len(), 1);
        assert!(
            validation.violations[0]
                .description
                .contains("Access Denied")
        );
    }

    #[test]
    fn test_security_auditor_suspicious_ip() {
        let auditor = SecurityAuditor::new();
        let event = AccessEvent {
            user_id: "user123".to_string(),
            resource_type: "database".to_string(),
            resource_id: "prod-db".to_string(),
            access_type: "read".to_string(),
            source_ip: Some("0.0.0.0".to_string()),
            user_agent: None,
            result: AccessResult::Success,
            timestamp: None,
        };

        let validation = auditor
            .audit_access_event(&event)
            .expect("Audit should succeed");

        assert!(!validation.is_compliant);
        assert_eq!(validation.violations.len(), 1);
        assert_eq!(validation.violations[0].severity, "low");
        assert!(
            validation.violations[0]
                .description
                .contains("Suspicious source IP")
        );
    }

    #[test]
    fn test_security_auditor_check_sensitive_operation() {
        let auditor = SecurityAuditor::new();

        assert!(auditor.check_sensitive_operation("critical"));
        assert!(auditor.check_sensitive_operation("high"));
        assert!(!auditor.check_sensitive_operation("medium"));
        assert!(!auditor.check_sensitive_operation("low"));
    }

    // -------------------------------------------------------------------------
    // EncryptionConfig / validate_encryption_config
    // -------------------------------------------------------------------------

    #[test]
    fn test_validate_encryption_config_compliant() {
        let auditor = SecurityAuditor::new();
        let config = EncryptionConfig {
            at_rest_algorithm: "AES-256-GCM".to_string(),
            in_transit_protocol: "TLS 1.3".to_string(),
            key_rotation_days: 30,
        };
        let violations = auditor.validate_encryption_config(&config);
        assert!(
            violations.is_empty(),
            "Expected no violations, got: {:?}",
            violations
        );
    }

    #[test]
    fn test_validate_encryption_config_weak_at_rest() {
        let auditor = SecurityAuditor::new();
        let config = EncryptionConfig {
            at_rest_algorithm: "AES-128-CBC".to_string(),
            in_transit_protocol: "TLS 1.2".to_string(),
            key_rotation_days: 60,
        };
        let violations = auditor.validate_encryption_config(&config);
        assert_eq!(violations.len(), 1);
        assert!(
            violations[0]
                .description
                .contains("at-rest encryption algorithm")
                || violations[0].description.contains("At-rest")
        );
        assert_eq!(violations[0].severity, "high");
    }

    #[test]
    fn test_validate_encryption_config_weak_in_transit() {
        let auditor = SecurityAuditor::new();
        let config = EncryptionConfig {
            at_rest_algorithm: "AES-256-GCM".to_string(),
            in_transit_protocol: "TLS 1.0".to_string(),
            key_rotation_days: 30,
        };
        let violations = auditor.validate_encryption_config(&config);
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].severity, "high");
        assert!(
            violations[0].description.contains("TLS 1.2+")
                || violations[0].description.contains("in-transit")
                || violations[0].description.contains("In-transit")
        );
    }

    #[test]
    fn test_validate_encryption_config_key_rotation_too_long() {
        let auditor = SecurityAuditor::new();
        let config = EncryptionConfig {
            at_rest_algorithm: "AES-256-GCM".to_string(),
            in_transit_protocol: "TLS 1.2".to_string(),
            key_rotation_days: 120,
        };
        let violations = auditor.validate_encryption_config(&config);
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].severity, "medium");
        assert!(
            violations[0].description.contains("90 days")
                || violations[0].description.contains("key rotation")
        );
    }

    #[test]
    fn test_validate_encryption_config_multiple_violations() {
        let auditor = SecurityAuditor::new();
        let config = EncryptionConfig {
            at_rest_algorithm: "DES".to_string(),
            in_transit_protocol: "SSL 3.0".to_string(),
            key_rotation_days: 365,
        };
        let violations = auditor.validate_encryption_config(&config);
        assert_eq!(
            violations.len(),
            3,
            "Expected 3 violations, got: {:?}",
            violations
        );
    }

    #[test]
    fn test_validate_encryption_config_exactly_90_days() {
        let auditor = SecurityAuditor::new();
        let config = EncryptionConfig {
            at_rest_algorithm: "AES-256-GCM".to_string(),
            in_transit_protocol: "TLSv1.2".to_string(),
            key_rotation_days: 90,
        };
        let violations = auditor.validate_encryption_config(&config);
        assert!(
            violations.is_empty(),
            "90-day rotation should be compliant: {:?}",
            violations
        );
    }

    // -------------------------------------------------------------------------
    // AccessAuditor – existing tests (ported with timestamp field)
    // -------------------------------------------------------------------------

    #[test]
    fn test_access_auditor_create_access_record() {
        let auditor = AccessAuditor::new();
        let event = auditor.create_access_record("user123", "database", "prod-db", "read");

        assert_eq!(event.user_id, "user123");
        assert_eq!(event.resource_type, "database");
        assert_eq!(event.resource_id, "prod-db");
        assert_eq!(event.access_type, "read");
        assert!(event.source_ip.is_none());
        assert!(event.user_agent.is_none());
        assert!(event.timestamp.is_none());
        assert!(matches!(event.result, AccessResult::Success));
    }

    #[test]
    fn test_access_auditor_audit_access_pattern_no_violations() {
        let auditor = AccessAuditor::new();
        let events = vec![
            make_event("user123", "prod-db", AccessResult::Success),
            make_event("user456", "data.txt", AccessResult::Success),
        ];

        let violations = auditor.audit_access_pattern(&events);
        assert!(violations.is_empty());
    }

    #[test]
    fn test_access_auditor_audit_access_pattern_medium_threat() {
        let auditor = AccessAuditor::new();
        let events = vec![
            make_event("user123", "prod-db", AccessResult::Failed),
            make_event("user123", "prod-db", AccessResult::Failed),
            make_event("user123", "prod-db", AccessResult::Denied),
        ];

        let violations = auditor.audit_access_pattern(&events);
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].severity, "medium");
        assert!(
            violations[0]
                .description
                .contains("3 failed access attempts")
        );
    }

    #[test]
    fn test_access_auditor_audit_access_pattern_high_threat() {
        let auditor = AccessAuditor::new();
        let events: Vec<_> = (0..5)
            .map(|_| make_event("attacker", "prod-db", AccessResult::Failed))
            .collect();

        let violations = auditor.audit_access_pattern(&events);
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].severity, "high");
        assert!(
            violations[0]
                .description
                .contains("5 failed access attempts")
        );
    }

    // -------------------------------------------------------------------------
    // AccessAuditor – detect_anomalies
    // -------------------------------------------------------------------------

    #[test]
    fn test_detect_anomalies_no_violations() {
        let auditor = AccessAuditor::new();
        // 5 events, single user, single resource — well under thresholds
        let events: Vec<_> = (0..5)
            .map(|_| make_event("alice", "db-1", AccessResult::Success))
            .collect();
        let violations = auditor.detect_anomalies(&events, 60);
        assert!(violations.is_empty());
    }

    #[test]
    fn test_detect_anomalies_rapid_fire() {
        let auditor = AccessAuditor::new();
        // 11 events from same user — exceeds the 10-event threshold
        let events: Vec<_> = (0..11)
            .map(|_| make_event("rapid-user", "db-1", AccessResult::Success))
            .collect();
        let violations = auditor.detect_anomalies(&events, 60);
        // At minimum one rapid-fire violation
        assert!(!violations.is_empty());
        assert!(
            violations
                .iter()
                .any(|v| v.description.contains("rapid-user"))
        );
    }

    #[test]
    fn test_detect_anomalies_multi_resource_scanning() {
        let auditor = AccessAuditor::new();
        // 6 distinct resources — exceeds the 5-resource threshold
        let events: Vec<_> = (0..6)
            .map(|i| make_event("scanner", &format!("resource-{}", i), AccessResult::Success))
            .collect();
        let violations = auditor.detect_anomalies(&events, 60);
        assert!(!violations.is_empty());
        assert!(violations.iter().any(|v| v.description.contains("scanner")));
    }

    #[test]
    fn test_detect_anomalies_both_triggers() {
        let auditor = AccessAuditor::new();
        // 11 events across 6 resources — triggers both anomaly types
        let events: Vec<_> = (0..11)
            .map(|i| make_event("attacker", &format!("res-{}", i % 6), AccessResult::Success))
            .collect();
        let violations = auditor.detect_anomalies(&events, 300);
        assert!(
            violations.len() >= 2,
            "Expected at least 2 violations, got: {}",
            violations.len()
        );
    }

    #[test]
    fn test_detect_anomalies_multi_user_no_single_exceeds_threshold() {
        let auditor = AccessAuditor::new();
        // 5 users × 2 events each = 10 events total, but no single user exceeds threshold
        let events: Vec<_> = (0..5)
            .flat_map(|u| {
                (0..2)
                    .map(move |_| make_event(&format!("user-{}", u), "db-1", AccessResult::Success))
            })
            .collect();
        let violations = auditor.detect_anomalies(&events, 60);
        assert!(violations.is_empty());
    }

    // -------------------------------------------------------------------------
    // SecurityMonitor – existing tests (unchanged)
    // -------------------------------------------------------------------------

    #[test]
    fn test_security_monitor_detect_threats_critical() {
        let monitor = SecurityMonitor::new();
        let violations = monitor.detect_threats(10);

        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].severity, "critical");
        assert!(violations[0].description.contains("10 failed operations"));
    }

    #[test]
    fn test_security_monitor_detect_threats_high() {
        let monitor = SecurityMonitor::new();
        let violations = monitor.detect_threats(7);

        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].severity, "high");
        assert!(violations[0].description.contains("7 failed operations"));
    }

    #[test]
    fn test_security_monitor_detect_threats_medium() {
        let monitor = SecurityMonitor::new();
        let violations = monitor.detect_threats(3);

        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].severity, "medium");
        assert!(violations[0].description.contains("3 failed operations"));
    }

    #[test]
    fn test_security_monitor_detect_threats_no_violations() {
        let monitor = SecurityMonitor::new();
        let violations = monitor.detect_threats(2);

        assert!(violations.is_empty());
    }

    #[test]
    fn test_security_monitor_generate_security_alert() {
        let monitor = SecurityMonitor::new();
        let violation = SecurityViolation {
            violation_id: "test-violation-123".to_string(),
            severity: "high".to_string(),
            description: "Test security violation".to_string(),
        };

        let alert = monitor.generate_security_alert(&violation);

        assert!(alert.contains("[SECURITY ALERT]"));
        assert!(alert.contains("test-violation-123"));
        assert!(alert.contains("Severity: high"));
        assert!(alert.contains("Test security violation"));
    }

    // -------------------------------------------------------------------------
    // SecurityMonitor – SIEM format methods
    // -------------------------------------------------------------------------

    fn sample_violation() -> SecurityViolation {
        SecurityViolation {
            violation_id: "viol-001".to_string(),
            severity: "high".to_string(),
            description: "Suspicious access pattern detected".to_string(),
        }
    }

    #[test]
    fn test_format_cef_structure() {
        let monitor = SecurityMonitor::new();
        let v = sample_violation();
        let cef = monitor.format_cef(&v);

        assert!(
            cef.starts_with("CEF:0|copybook-rs|SecurityMonitor|1.0|"),
            "CEF header missing: {}",
            cef
        );
        assert!(cef.contains("viol-001"), "violation_id missing: {}", cef);
        assert!(
            cef.contains("Suspicious access pattern detected"),
            "description missing: {}",
            cef
        );
        // severity_num for "high" is 7
        assert!(cef.contains('7'), "severity number missing: {}", cef);
    }

    #[test]
    fn test_format_cef_severity_mapping() {
        let monitor = SecurityMonitor::new();
        for (sev, expected_num) in [
            ("critical", "10"),
            ("high", "7"),
            ("medium", "5"),
            ("low", "3"),
        ] {
            let v = SecurityViolation {
                violation_id: "x".to_string(),
                severity: sev.to_string(),
                description: "d".to_string(),
            };
            let cef = monitor.format_cef(&v);
            assert!(
                cef.contains(expected_num),
                "severity {} should map to {}: {}",
                sev,
                expected_num,
                cef
            );
        }
    }

    #[test]
    fn test_format_leef_structure() {
        let monitor = SecurityMonitor::new();
        let v = sample_violation();
        let leef = monitor.format_leef(&v);

        assert!(
            leef.starts_with("LEEF:2.0|copybook-rs|SecurityMonitor|1.0|"),
            "LEEF header missing: {}",
            leef
        );
        assert!(leef.contains("viol-001"), "violation_id missing: {}", leef);
        assert!(
            leef.contains("severity=high"),
            "severity field missing: {}",
            leef
        );
        assert!(
            leef.contains("description=Suspicious access pattern detected"),
            "description field missing: {}",
            leef
        );
        // LEEF key-value pairs are tab-separated
        assert!(
            leef.contains('\t'),
            "LEEF should use tab separator: {}",
            leef
        );
    }

    #[test]
    fn test_format_syslog_structure() {
        let monitor = SecurityMonitor::new();
        let v = sample_violation();
        let syslog = monitor.format_syslog(&v);

        // Must start with <priority>version
        assert!(
            syslog.starts_with('<'),
            "Syslog should start with <priority>: {}",
            syslog
        );
        assert!(
            syslog.contains("copybook-rs"),
            "hostname/app missing: {}",
            syslog
        );
        assert!(
            syslog.contains("SecurityMonitor"),
            "app-name missing: {}",
            syslog
        );
        assert!(
            syslog.contains("viol-001"),
            "violation_id missing: {}",
            syslog
        );
        assert!(
            syslog.contains("Suspicious access pattern detected"),
            "description missing: {}",
            syslog
        );
    }

    #[test]
    fn test_format_syslog_priority() {
        let monitor = SecurityMonitor::new();
        let v = sample_violation();
        let syslog = monitor.format_syslog(&v);
        // Priority <132> = facility 16 * 8 + severity 4
        assert!(syslog.starts_with("<132>"), "Wrong priority: {}", syslog);
    }

    // -------------------------------------------------------------------------
    // EventMonitor
    // -------------------------------------------------------------------------

    #[test]
    fn test_event_monitor_no_alert_below_threshold() {
        let mut monitor = EventMonitor::new(5);
        // Send exactly 5 events — threshold not crossed
        for i in 0..5 {
            let event = make_event("alice", &format!("res-{}", i), AccessResult::Success);
            let alert = monitor.process_event(&event);
            assert!(
                alert.is_none(),
                "Should not alert before threshold: i={}",
                i
            );
        }
        assert!(monitor.pending_alerts().is_empty());
    }

    #[test]
    fn test_event_monitor_alert_at_threshold_plus_one() {
        let mut monitor = EventMonitor::new(5);
        // Events 0..5 (five events) — no alert
        for i in 0..5 {
            let event = make_event("alice", &format!("res-{}", i), AccessResult::Success);
            monitor.process_event(&event);
        }
        // 6th event crosses the threshold
        let event = make_event("alice", "res-5", AccessResult::Success);
        let alert = monitor.process_event(&event);
        assert!(alert.is_some(), "Should alert when threshold is crossed");
        assert_eq!(monitor.pending_alerts().len(), 1);
    }

    #[test]
    fn test_event_monitor_alert_contains_threshold_info() {
        let mut monitor = EventMonitor::new(5);
        for i in 0..=5 {
            let event = make_event("alice", &format!("res-{}", i), AccessResult::Success);
            monitor.process_event(&event);
        }
        let alerts = monitor.pending_alerts();
        assert_eq!(alerts.len(), 1);
        assert!(
            alerts[0].description.contains('5'),
            "Description should mention threshold: {}",
            alerts[0].description
        );
        assert_eq!(alerts[0].severity, "high");
    }

    #[test]
    fn test_event_monitor_only_one_alert_per_threshold_crossing() {
        let mut monitor = EventMonitor::new(3);
        // Cross threshold at event 4 (index 3)
        for i in 0..10 {
            let event = make_event("alice", &format!("res-{}", i), AccessResult::Success);
            monitor.process_event(&event);
        }
        // Only one alert should have been emitted (the crossing)
        assert_eq!(monitor.pending_alerts().len(), 1);
    }

    #[test]
    fn test_event_monitor_default_threshold() {
        // Default threshold documented as 5
        let monitor = EventMonitor::new(5);
        assert_eq!(monitor.alert_threshold, 5);
    }

    #[test]
    fn test_event_monitor_pending_alerts_initially_empty() {
        let monitor = EventMonitor::new(5);
        assert!(monitor.pending_alerts().is_empty());
    }
}