From e26f4bd1c7729a4a1bbfe08c5dc16df9619ad6cd Mon Sep 17 00:00:00 2001 From: containerscrew Date: Thu, 5 Dec 2024 00:01:31 +0100 Subject: [PATCH] Refactor tests --- nflux/src/config.rs | 81 ++++++++++++++++++++++++++++++++++++- nflux/src/lib.rs | 12 ------ nflux/src/main.rs | 15 ++----- nflux/tests/config_tests.rs | 77 ----------------------------------- nflux/tests/first.rs | 4 -- 5 files changed, 84 insertions(+), 105 deletions(-) delete mode 100644 nflux/src/lib.rs delete mode 100644 nflux/tests/config_tests.rs delete mode 100644 nflux/tests/first.rs diff --git a/nflux/src/config.rs b/nflux/src/config.rs index ea22801..94615b6 100644 --- a/nflux/src/config.rs +++ b/nflux/src/config.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use std::env; use std::fs; -/// Enum for `action` +// Enum for `action` #[derive(Debug, Deserialize, PartialEq)] #[serde(rename_all = "lowercase")] pub enum Action { @@ -85,3 +85,82 @@ impl Nflux { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + use tempfile::TempDir; + + fn setup_temp_config(content: &str) -> TempDir { + let temp_dir = tempfile::tempdir().unwrap(); + let config_path = temp_dir.path().join("nflux.toml"); + fs::write(&config_path, content).unwrap(); + + std::env::set_var("NFLUX_CONFIG_FILE_PATH", config_path.to_str().unwrap()); + + temp_dir + } + + #[test] + fn test_load_valid_config() { + let config_content = r#" + [nflux] + interface_names = ["eth0", "wlan0"] + + [logging] + log_level = "debug" + log_type = "json" + + [ip_rules] + "192.168.0.1" = { priority = 1, action = "allow", ports = [22], protocol = "tcp", log = true, description = "SSH rule" } + "#; + + let _temp_dir = setup_temp_config(config_content); + + let config = Nflux::load_config().unwrap(); + + // Assertions + assert_eq!(config.nflux.interface_names, vec!["eth0", "wlan0"]); + assert_eq!(config.logging.log_level, "debug"); + assert_eq!(config.logging.log_type, "json"); + + let rule = config.ip_rules.get("192.168.0.1").unwrap(); + assert_eq!(rule.priority, 1); + assert_eq!(rule.action, Action::Allow); + assert_eq!(rule.ports, vec![22]); + assert_eq!(rule.protocol, Protocol::Tcp); + assert_eq!(rule.log, true); + assert_eq!(rule.description, "SSH rule"); + } + + #[test] + fn test_load_missing_config_file() { + std::env::set_var("NFLUX_CONFIG_FILE_PATH", "/nonexistent/path/nflux.toml"); + + let result = Nflux::load_config(); + + // Assert that loading fails + assert!(result.is_err()); + assert!(result + .unwrap_err() + .to_string() + .contains("Failed to read configuration file")); + } + + // #[test] + // fn test_load_invalid_config_format() { + // let invalid_config_content = "invalid: [toml"; + + // setup_temp_config(invalid_config_content); + + // let result = Nflux::load_config(); + + // // Assert that loading fails due to parse error + // assert!(result.is_err()); + // assert!(result + // .unwrap_err() + // .to_string() + // .contains("Failed to parse configuration file")); + // } +} diff --git a/nflux/src/lib.rs b/nflux/src/lib.rs deleted file mode 100644 index dc87fcc..0000000 --- a/nflux/src/lib.rs +++ /dev/null @@ -1,12 +0,0 @@ -mod config; -mod core; -mod logger; -mod utils; - -// Dependencies -pub use config::{NfluxConfig, Rules, Nflux, Action, Protocol}; -pub use core::set_mem_limit; -pub use utils::{is_root_user, wait_for_shutdown}; - -/// RXH version. -pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/nflux/src/main.rs b/nflux/src/main.rs index 9a18efe..9daf6c1 100644 --- a/nflux/src/main.rs +++ b/nflux/src/main.rs @@ -7,21 +7,21 @@ mod utils; use anyhow::Context; use aya::maps::lpm_trie::Key; use aya::maps::perf::{AsyncPerfEventArrayBuffer, PerfBufferError}; -use aya::maps::{AsyncPerfEventArray, LpmTrie, Map, MapData}; +use aya::maps::{AsyncPerfEventArray, LpmTrie, MapData}; use aya::programs::{Xdp, XdpFlags}; use aya::util::online_cpus; use aya::{include_bytes_aligned, Ebpf}; use bytes::BytesMut; use config::{Action, Nflux, Protocol, Rules}; use logger::setup_logger; -use nflux_common::{convert_protocol, ConnectionEvent, IpRule, LpmKeyIpv4, LpmKeyIpv6}; +use nflux_common::{convert_protocol, ConnectionEvent, IpRule, LpmKeyIpv4}; use utils::{is_root_user, wait_for_shutdown}; use core::set_mem_limit; use std::collections::HashMap; use std::net::Ipv4Addr; use std::ptr; use tokio::task; -use tracing::{error, info, warn}; +use tracing::{error, info}; #[tokio::main] async fn main() -> anyhow::Result<()> { @@ -161,25 +161,18 @@ fn prepare_ip_rule(rule: &Rules) -> anyhow::Result { action: match rule.action { Action::Allow => 1, Action::Deny => 0, - _ => { - warn!("Unsupported action: {:?}", rule.action); - return Err(anyhow::anyhow!("Unsupported action")); - } }, ports, protocol: match rule.protocol { Protocol::Tcp => 6, Protocol::Udp => 17, Protocol::Icmp => 1, - _ => { - warn!("Unsupported protocol: {:?}", rule.protocol); - return Err(anyhow::anyhow!("Unsupported protocol")); - } }, priority: rule.priority, }) } + // fn populate_ipv6_rules(bpf: &mut Ebpf, ip_rules: &HashMap) -> anyhow::Result<()> { // let mut ipv6_map: LpmTrie<&mut MapData, LpmKeyIpv6, IpRule> = LpmTrie::try_from( // bpf.map_mut("IPV6_RULES").context("Failed to find IPV4_RULES map")?, diff --git a/nflux/tests/config_tests.rs b/nflux/tests/config_tests.rs deleted file mode 100644 index afbc5eb..0000000 --- a/nflux/tests/config_tests.rs +++ /dev/null @@ -1,77 +0,0 @@ -use std::fs; -use nflux::{Action, Nflux, Protocol}; -use tempfile::TempDir; - -fn setup_temp_config(content: &str) -> TempDir { - let temp_dir = tempfile::tempdir().unwrap(); - let config_path = temp_dir.path().join("nflux.toml"); - fs::write(&config_path, content).unwrap(); - - std::env::set_var("NFLUX_CONFIG_FILE_PATH", config_path.to_str().unwrap()); - - temp_dir -} - - -#[test] -fn test_load_valid_config() { - let config_content = r#" - [nflux] - interface_names = ["eth0", "wlan0"] - - [logging] - log_level = "debug" - log_type = "json" - - [ip_rules] - "192.168.0.1" = { priority = 1, action = "allow", ports = [22], protocol = "tcp", log = true, description = "SSH rule" } - "#; - - let _temp_dir = setup_temp_config(config_content); - - let config = Nflux::load_config().unwrap(); - - // Assertions - assert_eq!(config.nflux.interface_names, vec!["eth0", "wlan0"]); - assert_eq!(config.logging.log_level, "debug"); - assert_eq!(config.logging.log_type, "json"); - - let rule = config.ip_rules.get("192.168.0.1").unwrap(); - assert_eq!(rule.priority, 1); - assert_eq!(rule.action, Action::Allow); - assert_eq!(rule.ports, vec![22]); - assert_eq!(rule.protocol, Protocol::Tcp); - assert_eq!(rule.log, true); - assert_eq!(rule.description, "SSH rule"); -} - - -// #[test] -// fn test_load_missing_config_file() { -// std::env::set_var("NFLUX_CONFIG_FILE_PATH", "/nonexistent/path/nflux.toml"); - -// let result = Nflux::load_config(); - -// // Assert that loading fails -// assert!(result.is_err()); -// assert!(result -// .unwrap_err() -// .to_string() -// .contains("Failed to read configuration file")); -// } - -// #[test] -// fn test_load_invalid_config_format() { -// let invalid_config_content = "invalid: [toml"; - -// setup_temp_config(invalid_config_content); - -// let result = Nflux::load_config(); - -// // Assert that loading fails due to parse error -// assert!(result.is_err()); -// assert!(result -// .unwrap_err() -// .to_string() -// .contains("Failed to parse configuration file")); -// } diff --git a/nflux/tests/first.rs b/nflux/tests/first.rs deleted file mode 100644 index 2837a57..0000000 --- a/nflux/tests/first.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[test] -fn it_works() { - assert_eq!(2 + 2, 4); -}