Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
containerscrew committed Dec 4, 2024
1 parent 1549e3e commit e26f4bd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 105 deletions.
81 changes: 80 additions & 1 deletion nflux/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"));
// }
}
12 changes: 0 additions & 12 deletions nflux/src/lib.rs

This file was deleted.

15 changes: 4 additions & 11 deletions nflux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -161,25 +161,18 @@ fn prepare_ip_rule(rule: &Rules) -> anyhow::Result<IpRule> {
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<String, Rules>) -> 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")?,
Expand Down
77 changes: 0 additions & 77 deletions nflux/tests/config_tests.rs

This file was deleted.

4 changes: 0 additions & 4 deletions nflux/tests/first.rs

This file was deleted.

0 comments on commit e26f4bd

Please sign in to comment.