Written geosite protobuf parser and tests 4 it

This commit is contained in:
namilsk 2026-03-18 21:21:21 +03:00
parent da8e70f2e3
commit 8887a775f5
No known key found for this signature in database
GPG key ID: 2B2F0A4D870B4F9F
10 changed files with 548 additions and 18 deletions

68
tests/v2ray_geosite.rs Normal file
View file

@ -0,0 +1,68 @@
use nsc::geoparsers::v2ray::parsing::GeoSiteService;
use nsc::geoparsers::v2ray::types::Domain;
use std::fs;
use std::path::PathBuf;
fn download_geosite() -> Result<PathBuf, Box<dyn std::error::Error>> {
let tmp_dir = std::env::temp_dir().join("seccontrol_test");
fs::create_dir_all(&tmp_dir)?;
let geosite_path = tmp_dir.join("geosite.dat");
if !geosite_path.exists() {
// Use v2fly domain-list-community which has standard protobuf format
let url = "https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat";
let response = ureq::get(url).call()?;
let mut file = fs::File::create(&geosite_path)?;
let mut reader = response.into_reader();
std::io::copy(&mut reader, &mut file)?;
}
Ok(geosite_path)
}
fn get_geosite_service() -> Result<GeoSiteService, Box<dyn std::error::Error>> {
let geosite_path = download_geosite()?;
let service = GeoSiteService::new(geosite_path.to_str().unwrap())?;
Ok(service)
}
#[test]
fn geosite_service_creation() {
let service = get_geosite_service();
assert!(service.is_ok(), "Failed to create GeoSiteService: {:?}", service.err());
}
#[test]
fn lookup_existing_domain() {
let service = get_geosite_service().expect("Failed to create service");
assert!(!service.is_empty(), "Service should have entries");
println!("Loaded {} GeoSite entries", service.len());
}
#[test]
fn lookup_nonexistent_domain() {
let service = get_geosite_service().expect("Failed to create service");
let domain = Domain {
r#type: nsc::geoparsers::v2ray::types::domain::Type::Full as i32,
value: "nfaklsfjlasfvjkcnjnasxcjsas-not-existing-domain.com".to_string(),
attribute: vec![],
};
let result = service.lookup(domain.value.as_str());
assert!(result.is_none(), "Should return none for not existing domain");
println!("{:?}", result);
}
#[test]
fn geosite_list_not_empty() {
let service = get_geosite_service().expect("Failed to create service");
assert!(
!service.is_empty(),
"GeoSiteList should not be empty"
);
println!("Loaded {} GeoSite entries", service.len());
}