added tests for buggy function gc_blocklist
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Paul 2022-06-04 19:24:17 +02:00
parent 46d38fe318
commit 31688e9486

View File

@ -12,7 +12,8 @@ use std::collections::HashMap;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::path::Path; use std::path::Path;
const SERVER: &str = "ipbl.paulbsd.com"; const MASTERSERVER: &str = "ipbl.paulbsd.com";
const ZMQSUBSCRIPTION: &str = "ipbl";
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Context { pub struct Context {
@ -49,7 +50,7 @@ impl Context {
let debug = Context::argparse().is_present("debug"); let debug = Context::argparse().is_present("debug");
let server = Context::argparse() let server = Context::argparse()
.value_of("server") .value_of("server")
.unwrap_or(format!("https://{}", SERVER).as_str()) .unwrap_or(format!("https://{}", MASTERSERVER).as_str())
.to_string(); .to_string();
// Build context // Build context
@ -79,6 +80,10 @@ impl Context {
hashwd: HashMap::new(), hashwd: HashMap::new(),
}; };
ctx.discovery = ctx.discovery().await.unwrap(); ctx.discovery = ctx.discovery().await.unwrap();
#[cfg(test)]
return ctx;
print!("Loading config ... "); print!("Loading config ... ");
match ctx.load().await { match ctx.load().await {
Ok(_) => {} Ok(_) => {}
@ -100,7 +105,7 @@ impl Context {
.short('s') .short('s')
.long("server") .long("server")
.value_name("server") .value_name("server")
.default_value("https://ipbl.paulbsd.com") .default_value(format!("https://{MASTERSERVER}").as_str())
.help("Sets a http server") .help("Sets a http server")
.takes_value(true), .takes_value(true),
) )
@ -146,21 +151,20 @@ impl Context {
pub async fn gc_blocklist(&mut self) -> Vec<IpData> { pub async fn gc_blocklist(&mut self) -> Vec<IpData> {
let mut removed: Vec<IpData> = vec![]; let mut removed: Vec<IpData> = vec![];
let now: DateTime<Local> = Local::now().trunc_subsecs(0);
// nightly, future use // nightly, future use
//let drained: HashMap<String,IpData> = ctx.blocklist.drain_filter(|k,v| v.parse_date() < mindate) //let drained: HashMap<String,IpData> = ctx.blocklist.drain_filter(|k,v| v.parse_date() < mindate)
for (id, blocked) in self.blocklist.clone().iter() { for (id, blocked) in self.blocklist.clone().iter() {
for set in self.cfg.sets.clone() { for set in self.cfg.sets.clone() {
if blocked.src == set.t { if blocked.src == set.t {
let now: DateTime<Local> = Local::now().trunc_subsecs(0);
let mindate = now - Duration::minutes(set.blocktime); let mindate = now - Duration::minutes(set.blocktime);
if blocked.parse_date() < mindate { if blocked.parse_date() < mindate {
self.blocklist.remove(&id.to_string()).unwrap(); self.blocklist.remove(&id.to_string()).unwrap();
removed.push(blocked.clone()); removed.push(blocked.clone());
} }
break;
} }
break;
} }
} }
removed removed
@ -263,12 +267,12 @@ impl Config {
], ],
zmq: HashMap::from([("pubsub".to_string(),ZMQ{ zmq: HashMap::from([("pubsub".to_string(),ZMQ{
t: "pubsub".to_string(), t: "pubsub".to_string(),
hostname: SERVER.to_string(), hostname: MASTERSERVER.to_string(),
port: 9999, port: 9999,
subscription: "ipbl".to_string(), subscription: ZMQSUBSCRIPTION.to_string(),
}),("reqrep".to_string(),ZMQ { }),("reqrep".to_string(),ZMQ {
t: "reqrep".to_string(), t: "reqrep".to_string(),
hostname: SERVER.to_string(), hostname: MASTERSERVER.to_string(),
port: 9998, port: 9998,
subscription: String::new(), subscription: String::new(),
})]) })])
@ -383,10 +387,51 @@ impl PartialEq for Set {
} }
} }
impl Eq for Set {}
impl Hash for Set { impl Hash for Set {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
self.t.hash(state); self.t.hash(state);
} }
} }
#[cfg(test)]
mod test {
use super::*;
use crate::ip::*;
use Context;
#[tokio::test]
pub async fn test_gc_blocklist() {
let mut ctx = Context::new().await;
let now: DateTime<Local> = Local::now().trunc_subsecs(0);
ctx.blocklist = HashMap::new();
ctx.blocklist.insert(
"1.1.1.1".to_string(),
IpData {
ip: "1.1.1.1".to_string(),
hostname: "test1".to_string(),
date: (now - Duration::minutes(61)).to_rfc3339().to_string(),
src: "ssh".to_string(),
},
);
ctx.blocklist.insert(
"1.1.1.2".to_string(),
IpData {
ip: "1.1.1.2".to_string(),
hostname: "test2".to_string(),
date: (now - Duration::minutes(61)).to_rfc3339().to_string(),
src: "http".to_string(),
},
);
ctx.blocklist.insert(
"1.1.1.3".to_string(),
IpData {
ip: "1.1.1.3".to_string(),
hostname: "testgood".to_string(),
date: (now - Duration::minutes(59)).to_rfc3339().to_string(),
src: "http".to_string(),
},
);
let result = ctx.gc_blocklist().await;
assert_eq!(result.len(), 2);
}
}