diff --git a/src/tests.rs b/src/tests.rs index f437169..e298900 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,6 +1,10 @@ #![allow(non_snake_case)] -use crate::{bridge_verification_info::BridgeVerificationInfo, *}; +use crate::{ + analysis::{blocked_in, Analyzer}, + bridge_verification_info::BridgeVerificationInfo, + *, +}; use lox_library::{ bridge_table::{self, BridgeLine, BridgeTable}, cred::Lox, @@ -171,7 +175,7 @@ async fn test_extra_infos() { bincode::deserialize(&db.get("bridges").unwrap().unwrap()).unwrap(); assert!(bridges.contains(&bridge_to_test)); assert!(db.contains_key(bridge_to_test).unwrap()); - let bridge_info: BridgeInfo = + let _bridge_info: BridgeInfo = bincode::deserialize(&db.get(bridge_to_test).unwrap().unwrap()).unwrap(); } @@ -773,3 +777,194 @@ fn test_positive_reports() { let positive_reports = prs_to_process.get(&map_key_2).unwrap(); assert_eq!(positive_reports.len(), 1); } + +#[test] +fn test_analysis() { + // Test stage 1 analysis + { + let mut date = get_date(); + + // New bridge info + let mut bridge_info = BridgeInfo::new([0; 20], &String::default()); + + bridge_info + .info_by_country + .insert("ru".to_string(), BridgeCountryInfo::new()); + let analyzer = analysis::NormalAnalyzer::new(5, 0.25); + let confidence = 0.95; + + let mut blocking_countries = HashSet::::new(); + + // No data today + assert_eq!( + blocked_in(&analyzer, &bridge_info, confidence, date), + blocking_countries + ); + + // 1 connection, 0 negative reports + date += 1; + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::BridgeIps, + date, + 8, + ); + assert_eq!( + blocked_in(&analyzer, &bridge_info, confidence, date), + blocking_countries + ); + + // 0 connections, 0 negative reports + date += 1; + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::BridgeIps, + date, + 0, + ); + assert_eq!( + blocked_in(&analyzer, &bridge_info, confidence, date), + blocking_countries + ); + + // 0 connections, 1 negative report + // (exceeds scaled threshold) + date += 1; + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::NegativeReports, + date, + 1, + ); + blocking_countries.insert("ru".to_string()); + assert_eq!( + blocked_in(&analyzer, &bridge_info, confidence, date), + blocking_countries + ); + } + + { + let mut date = get_date(); + + // New bridge info + let mut bridge_info = BridgeInfo::new([0; 20], &String::default()); + + bridge_info + .info_by_country + .insert("ru".to_string(), BridgeCountryInfo::new()); + let analyzer = analysis::NormalAnalyzer::new(5, 0.25); + let confidence = 0.95; + + let mut blocking_countries = HashSet::::new(); + + // No data today + assert_eq!( + blocked_in(&analyzer, &bridge_info, confidence, date), + blocking_countries + ); + + // 1 connection, 1 negative report + date += 1; + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::BridgeIps, + date, + 8, + ); + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::NegativeReports, + date, + 1, + ); + assert_eq!( + blocked_in(&analyzer, &bridge_info, confidence, date), + blocking_countries + ); + + // 8 connections, 2 negative reports + date += 1; + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::BridgeIps, + date, + 8, + ); + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::NegativeReports, + date, + 2, + ); + assert_eq!( + blocked_in(&analyzer, &bridge_info, confidence, date), + blocking_countries + ); + + // 8 connections, 3 negative reports + // (exceeds scaled threshold) + date += 1; + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::BridgeIps, + date, + 8, + ); + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::NegativeReports, + date, + 3, + ); + blocking_countries.insert("ru".to_string()); + assert_eq!( + blocked_in(&analyzer, &bridge_info, confidence, date), + blocking_countries + ); + } + + { + let mut date = get_date(); + + // New bridge info + let mut bridge_info = BridgeInfo::new([0; 20], &String::default()); + + bridge_info + .info_by_country + .insert("ru".to_string(), BridgeCountryInfo::new()); + let analyzer = analysis::NormalAnalyzer::new(5, 0.25); + let confidence = 0.95; + + let mut blocking_countries = HashSet::::new(); + + // 24 connections, 5 negative reports + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::BridgeIps, + date, + 24, + ); + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::NegativeReports, + date, + 5, + ); + assert_eq!( + blocked_in(&analyzer, &bridge_info, confidence, date), + blocking_countries + ); + + // 24 connections, 6 negative reports + // (exceeds max threshold) + date += 1; + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::BridgeIps, + date, + 24, + ); + bridge_info.info_by_country.get_mut("ru").unwrap().add_info( + BridgeInfoType::NegativeReports, + date, + 6, + ); + blocking_countries.insert("ru".to_string()); + assert_eq!( + blocked_in(&analyzer, &bridge_info, confidence, date), + blocking_countries + ); + } + + // TODO: Test stage 2 analysis + + // TODO: Test stage 3 analysis +}