use std::collections::{BTreeMap, BTreeSet};
use std::net::{IpAddr, Ipv4Addr};
use coip_builder::{
BuildOptions, CoipBuilder, ImportFormat, ImportOptions, PruneConfig, import_reader,
};
use coip_format::Value;
use coip_reader::Reader;
#[test]
fn test_overlapping_lpm_cidrs_and_explicit_v4_v6_holes() {
let mut b = CoipBuilder::new(BuildOptions::default());
let mut r_world = BTreeMap::new();
r_world.insert("tier".into(), Value::Utf8("world".into()));
let mut r_ten = BTreeMap::new();
r_ten.insert("tier".into(), Value::Utf8("ten".into()));
let mut r_host = BTreeMap::new();
r_host.insert("tier".into(), Value::Utf8("host_v2".into()));
let mut r_host_old = BTreeMap::new();
r_host_old.insert("tier".into(), Value::Utf8("host_v1".into()));
b.insert_ipv4_cidr(Ipv4Addr::new(10, 1, 1, 1), 32, r_host_old)
.unwrap();
b.insert_ipv4_cidr(Ipv4Addr::new(0, 0, 0, 0), 0, r_world.clone())
.unwrap();
b.insert_ipv4_cidr(Ipv4Addr::new(10, 0, 0, 0), 8, r_ten.clone())
.unwrap();
b.delete_ipv4_cidr(Ipv4Addr::new(10, 1, 0, 0), 16).unwrap();
b.insert_ipv4_cidr(Ipv4Addr::new(10, 1, 1, 1), 32, r_host.clone())
.unwrap();
b.insert_ipv6_cidr("2001:db8::".parse().unwrap(), 32, r_world.clone())
.unwrap();
b.insert_ipv6_cidr("2001:db8:1::".parse().unwrap(), 48, r_ten.clone())
.unwrap();
b.insert_ipv6_cidr("2001:db8::99".parse().unwrap(), 128, r_host.clone())
.unwrap();
b.delete_ipv6_cidr("2001:db8::dead".parse().unwrap(), 128)
.unwrap();
let (bytes, _stats) = b.build().unwrap();
let r = Reader::from_vec(bytes).unwrap();
r.verify_checksums().unwrap();
assert_eq!(
r.lookup_value(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)))
.unwrap(),
Some(Value::Map(r_world.clone()))
);
assert_eq!(
r.lookup_value(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 5)))
.unwrap(),
Some(Value::Map(r_ten.clone()))
);
assert_eq!(
r.lookup_value(IpAddr::V4(Ipv4Addr::new(10, 1, 0, 5)))
.unwrap(),
None );
assert_eq!(
r.lookup_value(IpAddr::V4(Ipv4Addr::new(10, 1, 1, 1)))
.unwrap(),
Some(Value::Map(r_host.clone())) );
assert_eq!(
r.lookup_value(IpAddr::V4(Ipv4Addr::new(10, 2, 0, 1)))
.unwrap(),
Some(Value::Map(r_ten.clone()))
);
assert_eq!(
r.lookup_value(IpAddr::V6("2001:db8::1".parse().unwrap()))
.unwrap(),
Some(Value::Map(r_world))
);
assert_eq!(
r.lookup_value(IpAddr::V6("2001:db8:1::1".parse().unwrap()))
.unwrap(),
Some(Value::Map(r_ten))
);
assert_eq!(
r.lookup_value(IpAddr::V6("2001:db8::99".parse().unwrap()))
.unwrap(),
Some(Value::Map(r_host))
);
assert_eq!(
r.lookup_value(IpAddr::V6("2001:db8::dead".parse().unwrap()))
.unwrap(),
None );
}
#[test]
fn test_build_time_pruning_collapses_records_and_transitions() {
let mut opts = BuildOptions::compact();
let mut langs = BTreeSet::new();
langs.insert("en".into());
langs.insert("zh-CN".into());
opts.prune = Some(PruneConfig {
keep_fields: Some(vec!["country.iso_code".into(), "country.names".into()]),
languages: Some(langs),
});
let mut b = CoipBuilder::new(opts);
b.set_metadata_field("languages", serde_json::json!(["de", "en", "fr", "zh-CN"]));
for octet in [0u8, 1u8] {
let mut m = BTreeMap::new();
let mut country = BTreeMap::new();
country.insert("iso_code".into(), Value::Utf8("US".into()));
country.insert("geoname_id".into(), Value::U32(6252001));
let mut names = BTreeMap::new();
names.insert("en".into(), Value::Utf8("United States".into()));
names.insert("zh-CN".into(), Value::Utf8("美国".into()));
names.insert("fr".into(), Value::Utf8(format!("Etats-Unis-{octet}")));
names.insert("de".into(), Value::Utf8("USA".into()));
country.insert("names".into(), Value::Map(names));
m.insert("country".into(), Value::Map(country));
let mut postal = BTreeMap::new();
postal.insert("code".into(), Value::Utf8(format!("9404{octet}")));
m.insert("postal".into(), Value::Map(postal));
b.insert_ipv4_cidr(Ipv4Addr::new(8, 8, octet, 0), 24, m)
.unwrap();
}
let (bytes, stats) = b.build().unwrap();
assert_eq!(stats.record_count, 1);
assert_eq!(stats.v4_transitions, 2);
let r = Reader::from_vec(bytes).unwrap();
assert_eq!(
r.metadata().get("languages"),
Some(&serde_json::json!(["en", "zh-CN"]))
);
let val = r
.lookup_value(IpAddr::V4(Ipv4Addr::new(8, 8, 1, 42)))
.unwrap()
.unwrap();
let root = val.as_map().unwrap();
assert!(!root.contains_key("postal"));
let c = root["country"].as_map().unwrap();
assert_eq!(c.get("iso_code"), Some(&Value::Utf8("US".into())));
assert!(!c.contains_key("geoname_id"));
let n = c["names"].as_map().unwrap();
assert_eq!(n.len(), 2);
assert_eq!(n.get("en"), Some(&Value::Utf8("United States".into())));
assert_eq!(n.get("zh-CN"), Some(&Value::Utf8("美国".into())));
}
#[test]
fn test_jsonl_and_csv_import_and_range_scanning() {
let jsonl = r#"
{"cidr": "192.0.2.0/24", "asn": 64500, "org": "Example Net", "tags": ["isp", "v4"]}
{"cidr": "192.0.2.10", "asn": 64501, "org": "Special Host", "tags": ["vip"]}
{"cidr": "2001:db8:abcd::/48", "asn": 64502, "org": "V6 Corp"}
"#;
let (jsonl_coi, _) = import_reader(
jsonl.as_bytes(),
&ImportOptions {
format: ImportFormat::Jsonl,
database_type: Some("Custom-ASN".into()),
..ImportOptions::default()
},
)
.unwrap();
let jr = Reader::from_vec(jsonl_coi).unwrap();
jr.verify_checksums().unwrap();
assert_eq!(jr.ipv4_ranges().unwrap().len(), 3); assert_eq!(jr.ipv6_ranges().unwrap().len(), 1);
let v_host = jr
.lookup_value(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 10)))
.unwrap()
.unwrap();
assert_eq!(
v_host.as_map().unwrap().get("asn"),
Some(&Value::I64(64501))
);
let v_other = jr
.lookup_value(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 11)))
.unwrap()
.unwrap();
assert_eq!(
v_other.as_map().unwrap().get("asn"),
Some(&Value::I64(64500))
);
let csv_data = "\
start_ip,end_ip,country.iso_code,postal.code,asn,is_proxy,location.latitude
10.0.0.1,10.0.0.10,US,01001,15169,false,42.0654
10.0.0.11,10.0.0.20,US,02108,13335,true,42.3584
";
let (csv_coi, _) = import_reader(
csv_data.as_bytes(),
&ImportOptions {
format: ImportFormat::Csv,
..ImportOptions::default()
},
)
.unwrap();
let cr = Reader::from_vec(csv_coi).unwrap();
cr.verify_checksums().unwrap();
assert!(cr.header().has_ipv4());
assert!(!cr.header().has_ipv6()); let rec = cr
.lookup_value(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 5)))
.unwrap()
.unwrap();
let rm = rec.as_map().unwrap();
assert_eq!(
rm["postal"].as_map().unwrap().get("code"),
Some(&Value::Utf8("01001".into())) );
assert_eq!(rm.get("asn"), Some(&Value::U32(15169)));
assert_eq!(rm.get("is_proxy"), Some(&Value::Bool(false)));
assert_eq!(
rm["location"].as_map().unwrap().get("latitude"),
Some(&Value::F64(42.0654))
);
assert_eq!(
cr.lookup_value(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 21)))
.unwrap(),
None
);
}