From 5f8a5f64a076728b7fc5eef1083f64e2bf22700f Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 29 Feb 2024 22:33:56 +0800 Subject: [PATCH] add test case --- .gitignore | 2 +- config.yml | 20 -------------------- src/config/config.rs | 12 ++++++------ src/config/mod.rs | 27 +++++++++++++++++++++++++++ src/main.rs | 2 ++ 5 files changed, 36 insertions(+), 27 deletions(-) delete mode 100644 config.yml diff --git a/.gitignore b/.gitignore index 6985cf1..7714bf1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ # will have compiled files and executables debug/ target/ - +.idea/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html Cargo.lock diff --git a/config.yml b/config.yml deleted file mode 100644 index 9ed7b5d..0000000 --- a/config.yml +++ /dev/null @@ -1,20 +0,0 @@ -interface: "enp3s0" -rules: - - name: "Enable SSH" - timeout: 5 - command: "/usr/sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT" - sequence: - - 15523 - - 17767 - - 32768 - - 28977 - - 51234 - - name: "Disable SSH" - timeout: 5 - command: "/usr/sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT" - sequence: - - 51234 - - 28977 - - 32768 - - 17767 - - 15523 \ No newline at end of file diff --git a/src/config/config.rs b/src/config/config.rs index 53bd5de..9169ab3 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -4,14 +4,14 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct Rule { - name: String, - sequence: Vec, - timeout: i32, - command: String, + pub name: String, + pub sequence: Vec, + pub timeout: i32, + pub command: String, } #[derive(Debug, Serialize, Deserialize)] pub struct Config { - interface: String, - rules: Vec, + pub interface: String, + pub rules: Vec, } diff --git a/src/config/mod.rs b/src/config/mod.rs index 1bf79df..47ab4f8 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1 +1,28 @@ mod config; + +use config::Config; +use std::fs::File; +use std::io::Read; + +fn load_config() -> Result> { + let mut file = File::open("config.yaml")?; + let mut content = String::new(); + + file.read_to_string(&mut content)?; + let config: Config = serde_yaml::from_str(&content)?; + + Ok(config) +} + +// test case for load_config +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_load_config() { + let config = load_config().unwrap(); + assert_eq!(config.interface, "enp3s0"); + assert_eq!(config.rules.len(), 2); + } +} diff --git a/src/main.rs b/src/main.rs index 4ea5ee2..d755f5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ mod server; +mod config; + use server::Server; fn main() {