From dab91e5e61d537901d030b05043a763ce1f6beb6 Mon Sep 17 00:00:00 2001 From: DustInDark Date: Wed, 6 Apr 2022 22:34:32 +0900 Subject: [PATCH] fixed level tuning test and added test files #390 --- src/options/level_tuning.rs | 65 +++++++++++++++-------- test_files/config/level_tuning_error1.txt | 2 + test_files/config/level_tuning_error2.txt | 2 + 3 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 test_files/config/level_tuning_error1.txt create mode 100644 test_files/config/level_tuning_error2.txt diff --git a/src/options/level_tuning.rs b/src/options/level_tuning.rs index 6619e09e..2f82c8ce 100644 --- a/src/options/level_tuning.rs +++ b/src/options/level_tuning.rs @@ -104,6 +104,10 @@ impl LevelTuning { #[cfg(test)] mod tests { + + use crate::{filter::RuleExclude, yaml}; + use hashbrown::HashSet; + use super::*; #[test] @@ -136,28 +140,47 @@ mod tests { Ok(()) } - // TODO: make test option for read ./test_files/rules/ dir. - // #[test] - // fn test_detect_mutiple_regex_and() { - // let level_tuning_config_path = "./test_files/config/level_tuning.txt"; - // let rule_str = r#" - // id: 12345678-1234-1234-1234-123456789012 - // level: informational - // "#; + #[test] + fn test_detect_mutiple_regex_and() { + let level_tuning_config_path = "./test_files/config/level_tuning.txt"; + let rule_str = r#" + id: 12345678-1234-1234-1234-123456789012 + level: informational + "#; - // let expected_rule = r#" - // level: high - // "#; + let expected_rule = r#" + level: high + "#; - // let path = "test_files/rules/level_tuning_sample.yml"; - // let mut file = File::create(path).unwrap(); - // let buf = rule_str.as_bytes(); - // file.write_all(buf).unwrap(); - // file.flush().unwrap(); + let path = "test_files/rules/level_tuning_sample.yml"; + let mut file = File::create(path).unwrap(); + let buf = rule_str.as_bytes(); + file.write_all(buf).unwrap(); + file.flush().unwrap(); - // let res = LevelTuning::run(level_tuning_config_path); - // assert_eq!(res, Ok(())); - // assert_eq!(fs::read_to_string(path).unwrap(), expected_rule); - // fs::remove_file(path).unwrap(); - // } + let mut parser = yaml::ParseYaml::new(); + parser + .read_dir( + "test_files/rules", + "informational", + &RuleExclude { + no_use_rule: HashSet::new(), + }, + ) + .ok(); + + for (_filepath, yaml) in parser.files { + if yaml["id"].as_str().unwrap_or(&String::default()) + == "12345678-1234-1234-1234-123456789012" + { + println!("{}", _filepath); + assert_eq!("high", yaml["level"].as_str().unwrap()); + } + } + + let res = LevelTuning::run(level_tuning_config_path); + assert_eq!(res, Ok(())); + assert_eq!(fs::read_to_string(path).unwrap(), expected_rule); + fs::remove_file(path).unwrap(); + } } diff --git a/test_files/config/level_tuning_error1.txt b/test_files/config/level_tuning_error1.txt new file mode 100644 index 00000000..c91f760d --- /dev/null +++ b/test_files/config/level_tuning_error1.txt @@ -0,0 +1,2 @@ +id,next_level +12345678-1234-1234-1234-12,informational # sample leveltunig line diff --git a/test_files/config/level_tuning_error2.txt b/test_files/config/level_tuning_error2.txt new file mode 100644 index 00000000..33788388 --- /dev/null +++ b/test_files/config/level_tuning_error2.txt @@ -0,0 +1,2 @@ +id,next_level +00000000-0000-0000-0000-000000000000,no_exist_level # sample leveltunig line