Skip to content

Commit

Permalink
Solved unit tests errors and clippy report
Browse files Browse the repository at this point in the history
  • Loading branch information
okynos committed Apr 25, 2024
1 parent fe238f1 commit 44446aa
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/appconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ mod tests {
assert!(!cfg.match_allowed(2, "file.swp", cfg.monitor.clone()));
assert!(cfg.match_allowed(2, "file.txt", cfg.monitor.clone()));

let config_audit = AppConfig::new(&utils::get_os(), Some("test/unit/config/linux/audit_allowed.yml"));
let cfg_audit = AppConfig::new(&utils::get_os(), Some("test/unit/config/linux/audit_allowed.yml"));
assert!(!cfg_audit.match_allowed(0, "file.swp", cfg_audit.audit.clone()));
assert!(cfg_audit.match_allowed(0, "file.txt", cfg_audit.audit.clone()));
}
Expand Down
1 change: 0 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ pub async fn push_template(cfg: AppConfig){
#[cfg(test)]
mod tests {
use super::*;
use crate::appconfig;
use crate::utils;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn init() -> (AppConfig, Ruleset) {

println!("[INFO] Achiefs File Integrity Monitoring software starting!");
println!("[INFO] Reading config...");
let cfg = AppConfig::new(&utils::get_os(), None);
let cfg = AppConfig::new(utils::get_os(), None);

// Create folders to store logs based on config.yml
fs::create_dir_all(
Expand All @@ -36,7 +36,7 @@ pub fn init() -> (AppConfig, Ruleset) {
println!("[INFO] Log file: '{}'", cfg.clone().log_file);
println!("[INFO] Log level: '{}'", cfg.clone().log_level);

let ruleset = Ruleset::new(&utils::get_os(), None);
let ruleset = Ruleset::new(utils::get_os(), None);

log_panics::init();
(cfg, ruleset)
Expand Down
1 change: 0 additions & 1 deletion src/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ mod tests {
use super::*;
use std::path::PathBuf;
use crate::monitorevent::MonitorEvent;
use crate::appconfig::*;
use notify::event::*;

// ------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion src/logreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ pub fn parse_audit_log(log: String) -> HashMap<String, String> {
#[cfg(test)]
mod tests {
use super::*;
use crate::appconfig::*;

#[test]
fn test_read_log() {
Expand Down
4 changes: 2 additions & 2 deletions src/monitorevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ mod tests {
use crate::monitorevent::MonitorEvent;
use crate::event::*;
use crate::utils;
use crate::ruleset;
use std::path::PathBuf;
use tokio_test::block_on;
use std::fs;
Expand Down Expand Up @@ -442,8 +441,9 @@ mod tests {
fn test_process() {
let event = create_test_event();
let cfg = AppConfig::new(&utils::get_os(), None);
let ruleset = Ruleset::new(&utils::get_os(), None);

block_on(event.process(cfg));
block_on(event.process(cfg, ruleset));
//block_on(event.process(appconfig::NETWORK_MODE, String::from("test"), cfg.clone()));
//block_on(event.process(appconfig::FILE_MODE, String::from("test2"), cfg.clone()));
//block_on(event.process(appconfig::BOTH_MODE, String::from("test3"), cfg.clone()));
Expand Down
1 change: 0 additions & 1 deletion src/rotator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ fn rotate_file(filepath: &str, iteration: u32, lock: Mutex<bool>){
};

*lock.lock().unwrap() = false;
drop(lock);
info!("File {} rotated.", filepath);
info!("Compressing rotated file {}", file_rotated);
#[cfg(windows)]
Expand Down
2 changes: 1 addition & 1 deletion src/ruleevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Event for RuleEvent {

fn clone(&self) -> Self {
RuleEvent {
id: self.id.clone(),
id: self.id,
rule: self.rule.clone(),
timestamp: self.timestamp.clone(),
hostname: self.hostname.clone(),
Expand Down
22 changes: 17 additions & 5 deletions src/ruleset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Ruleset {

// Manage null value on rules
let mut rules = HashMap::new();
if yaml.len() > 0 {
if !yaml.is_empty() {
let vec_of_rules = match yaml[0]["rules"].as_vec() {
Some(value) => value.to_vec(),
None => {
Expand Down Expand Up @@ -95,13 +95,19 @@ impl Ruleset {
// ------------------------------------------------------------------------

pub async fn match_rule(&self, cfg: AppConfig, filepath: PathBuf) -> (bool, usize) {
let path = filepath.parent().unwrap().to_str().unwrap();
let path = match filepath.parent() {
Some(p) => p.to_str().unwrap(),
None => {
error!("(match_rule): Cannot retrieve event parent path.");
""
}
};
let find = self.rules.iter().find(|map| {
map.1.contains_key("path") && utils::match_path(map.1.get("path").unwrap(), path)
});

let (id, rule) = match find {
Some(element) => (element.0.clone(), element.1.get("rule").unwrap().clone()),
Some(element) => (*element.0, element.1.get("rule").unwrap().clone()),
None => {
debug!("No rule matched");
(usize::MAX, String::from(""))
Expand All @@ -116,7 +122,13 @@ impl Ruleset {
},
};

let filename = filepath.file_name().unwrap().to_str().unwrap();
let filename = match filepath.file_name() {
Some(f) => f.to_str().unwrap(),
None => {
error!("(match_rule): Cannot retrieve event filename.");
""
}
};
if id != usize::MAX {
if expression.is_match(filename){
debug!("Rule with ID: '{}', match event path: '{}'.", id, path);
Expand Down Expand Up @@ -156,7 +168,7 @@ pub fn read_ruleset(path: String) -> Vec<Yaml> {
let mut contents: String = String::new();

file.read_to_string(&mut contents)
.expect(&format!("(read_ruleset): Unable to read contents of file '{}'", path));
.unwrap_or_else(|_| panic!("(read_ruleset): Unable to read contents of file '{}'", path));
YamlLoader::load_from_str(&contents).unwrap()
}

Expand Down

0 comments on commit 44446aa

Please sign in to comment.