santh-sigma 0.1.4

Sigma rule compiler for security research
Documentation
use sigma::SigmaCompiler;
use std::sync::{Arc, Barrier};
use std::thread;

#[test]
fn test_depth_concurrent_compile_32_threads() {
    let yaml = r#"
title: Depth Concurrent Compile Test
logsource:
  product: windows
detection:
  selection:
    CommandLine|contains: powershell.exe
  condition: selection
level: high
"#;

    let num_threads = 32;
    let barrier = Arc::new(Barrier::new(num_threads));
    let mut handles = vec![];

    let yaml_str = Arc::new(yaml.to_string());

    for i in 0..num_threads {
        let b = Arc::clone(&barrier);
        let y = Arc::clone(&yaml_str);

        handles.push(thread::spawn(move || {
            b.wait();

            // Perform multiple compilations in each thread
            for _ in 0..50 {
                let template =
                    SigmaCompiler::compile_str(&y).expect("Should compile without panic or error");
                assert_eq!(
                    template.id, "depth-concurrent-compile-test",
                    "Thread {} failed ID check",
                    i
                );
                assert_eq!(
                    template.requests[0].matchers.len(),
                    1,
                    "Thread {} failed matcher len",
                    i
                );
                assert_eq!(
                    template.requests[0].matchers[0].values[0], "powershell.exe",
                    "Thread {} failed value check",
                    i
                );
            }
        }));
    }

    for handle in handles {
        handle
            .join()
            .expect("A thread panicked during concurrent compilation stress test");
    }
}

#[test]
fn test_depth_concurrent_different_rules_32_threads() {
    let num_threads = 32;
    let barrier = Arc::new(Barrier::new(num_threads));
    let mut handles = vec![];

    for i in 0..num_threads {
        let b = Arc::clone(&barrier);
        let yaml = format!(
            r#"
title: Concurrent Depth Diff {}
logsource:
  product: linux
detection:
  selection:
    EventID: {}
  condition: selection
level: medium
"#,
            i, i
        );

        handles.push(thread::spawn(move || {
            b.wait();

            for _ in 0..20 {
                let template = SigmaCompiler::compile_str(&yaml)
                    .unwrap_or_else(|e| panic!("Failed to compile in thread {}: {}", i, e));
                assert_eq!(
                    template.id,
                    format!("concurrent-depth-diff-{}", i),
                    "Thread {} failed ID check",
                    i
                );
                assert!(
                    template.requests[0].matchers[0].values[0].contains(&format!("{}", i)),
                    "Thread {} failed value check: expected {} in {}",
                    i,
                    i,
                    template.requests[0].matchers[0].values[0]
                );
            }
        }));
    }

    for handle in handles {
        handle
            .join()
            .expect("A thread panicked during diverse concurrent compilation");
    }
}