From 51f8d405f8c86c17d8ea7fb13854aedd20c14b50 Mon Sep 17 00:00:00 2001 From: itiB Date: Wed, 6 Apr 2022 01:34:48 +0900 Subject: [PATCH] Add: test --- src/options/level_tuning.rs | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/options/level_tuning.rs b/src/options/level_tuning.rs index bd909923..f9e2ab66 100644 --- a/src/options/level_tuning.rs +++ b/src/options/level_tuning.rs @@ -101,3 +101,60 @@ impl LevelTuning { Result::Ok(()) } } + +mod tests { + use super::*; + + #[test] + fn rule_level_failed_to_open_file() -> Result<(), String> { + let level_tuning_config_path = "./none.txt"; + let res = LevelTuning::run(level_tuning_config_path); + let expected = Result::Err("Cannot open file. [file:./none.txt]".to_string()); + assert_eq!(res, expected); + Ok(()) + } + + #[test] + fn rule_level_id_error_file() -> Result<(), String> { + let level_tuning_config_path = "./test_files/config/level_tuning_error1.txt"; + let res = LevelTuning::run(level_tuning_config_path); + let expected = Result::Err("Failed to read level tuning file. 12345678-1234-1234-1234-12 is not correct id format, fix it.".to_string()); + assert_eq!(res, expected); + Ok(()) + } + + #[test] + fn rule_level_level_error_file() -> Result<(), String> { + let level_tuning_config_path = "./test_files/config/level_tuning_error2.txt"; + let res = LevelTuning::run(level_tuning_config_path); + let expected = Result::Err("level tuning file's level must in informational, low, medium, high, critical".to_string()); + assert_eq!(res, expected); + 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 + // "#; + + // 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 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(); + // } +}