use super::*;
#[test]
fn test_external_ip_mapper_validate_ip_string() -> Result<()> {
let ip = validate_ip_string("1.2.3.4")?;
assert!(ip.is_ipv4(), "should be true");
assert_eq!("1.2.3.4", ip.to_string(), "should be true");
let ip = validate_ip_string("2601:4567::5678")?;
assert!(!ip.is_ipv4(), "should be false");
assert_eq!("2601:4567::5678", ip.to_string(), "should be true");
let result = validate_ip_string("bad.6.6.6");
assert!(result.is_err(), "should fail");
Ok(())
}
#[test]
fn test_external_ip_mapper_new_external_ip_mapper() -> Result<()> {
let m = ExternalIpMapper::new(CandidateType::Unspecified, &[])?;
assert!(m.is_none(), "should be none");
let m = ExternalIpMapper::new(CandidateType::Unspecified, &["1.2.3.4".to_owned()])?.unwrap();
assert_eq!(m.candidate_type, CandidateType::Host, "should match");
assert!(m.ipv4_mapping.ip_sole.is_some());
assert!(m.ipv6_mapping.ip_sole.is_none());
assert_eq!(m.ipv4_mapping.ip_map.len(), 0, "should match");
assert_eq!(m.ipv6_mapping.ip_map.len(), 0, "should match");
let m =
ExternalIpMapper::new(CandidateType::ServerReflexive, &["1.2.3.4".to_owned()])?.unwrap();
assert_eq!(
CandidateType::ServerReflexive,
m.candidate_type,
"should match"
);
assert!(m.ipv4_mapping.ip_sole.is_some());
assert!(m.ipv6_mapping.ip_sole.is_none());
assert_eq!(m.ipv4_mapping.ip_map.len(), 0, "should match");
assert_eq!(m.ipv6_mapping.ip_map.len(), 0, "should match");
let m = ExternalIpMapper::new(CandidateType::Unspecified, &["2601:4567::5678".to_owned()])?
.unwrap();
assert_eq!(m.candidate_type, CandidateType::Host, "should match");
assert!(m.ipv4_mapping.ip_sole.is_none());
assert!(m.ipv6_mapping.ip_sole.is_some());
assert_eq!(m.ipv4_mapping.ip_map.len(), 0, "should match");
assert_eq!(m.ipv6_mapping.ip_map.len(), 0, "should match");
let m = ExternalIpMapper::new(
CandidateType::Unspecified,
&["1.2.3.4".to_owned(), "2601:4567::5678".to_owned()],
)?
.unwrap();
assert_eq!(m.candidate_type, CandidateType::Host, "should match");
assert!(m.ipv4_mapping.ip_sole.is_some());
assert!(m.ipv6_mapping.ip_sole.is_some());
assert_eq!(m.ipv4_mapping.ip_map.len(), 0, "should match");
assert_eq!(m.ipv6_mapping.ip_map.len(), 0, "should match");
let result = ExternalIpMapper::new(CandidateType::PeerReflexive, &["1.2.3.4".to_owned()]);
assert!(result.is_err(), "should fail");
let result = ExternalIpMapper::new(CandidateType::PeerReflexive, &["1.2.3.4".to_owned()]);
assert!(result.is_err(), "should fail");
let result = ExternalIpMapper::new(
CandidateType::ServerReflexive,
&["1.2.3.4".to_owned(), "5.6.7.8".to_owned()],
);
assert!(result.is_err(), "should fail");
let result = ExternalIpMapper::new(
CandidateType::ServerReflexive,
&["2201::1".to_owned(), "2201::0002".to_owned()],
);
assert!(result.is_err(), "should fail");
let result = ExternalIpMapper::new(CandidateType::ServerReflexive, &["bad.2.3.4".to_owned()]);
assert!(result.is_err(), "should fail");
let result = ExternalIpMapper::new(
CandidateType::ServerReflexive,
&["1.2.3.4/10.0.0.bad".to_owned()],
);
assert!(result.is_err(), "should fail");
Ok(())
}
#[test]
fn test_external_ip_mapper_new_external_ip_mapper_with_explicit_local_ip() -> Result<()> {
let m = ExternalIpMapper::new(CandidateType::Unspecified, &["1.2.3.4/10.0.0.1".to_owned()])?
.unwrap();
assert_eq!(m.candidate_type, CandidateType::Host, "should match");
assert!(m.ipv4_mapping.ip_sole.is_none());
assert!(m.ipv6_mapping.ip_sole.is_none());
assert_eq!(m.ipv4_mapping.ip_map.len(), 1, "should match");
assert_eq!(m.ipv6_mapping.ip_map.len(), 0, "should match");
let result = ExternalIpMapper::new(
CandidateType::Unspecified,
&["1.2.3.4/10.0.0.1".to_owned(), "1.2.3.5/10.0.0.1".to_owned()],
);
assert!(result.is_err(), "should fail");
let result = ExternalIpMapper::new(
CandidateType::Unspecified,
&[
"2200::1/fe80::1".to_owned(),
"2200::0002/fe80::1".to_owned(),
],
);
assert!(result.is_err(), "should fail");
let result =
ExternalIpMapper::new(CandidateType::Unspecified, &["2200::1/10.0.0.1".to_owned()]);
assert!(result.is_err(), "should fail");
let result = ExternalIpMapper::new(CandidateType::Unspecified, &["1.2.3.4/fe80::1".to_owned()]);
assert!(result.is_err(), "should fail");
let result = ExternalIpMapper::new(
CandidateType::Unspecified,
&["1.2.3.4/192.168.0.2/10.0.0.1".to_owned()],
);
assert!(result.is_err(), "should fail");
Ok(())
}
#[test]
fn test_external_ip_mapper_new_external_ip_mapper_with_implicit_local_ip() -> Result<()> {
let result = ExternalIpMapper::new(
CandidateType::Unspecified,
&["1.2.3.4".to_owned(), "1.2.3.5/10.0.0.1".to_owned()],
);
assert!(result.is_err(), "should fail");
let result = ExternalIpMapper::new(
CandidateType::Unspecified,
&["1.2.3.5/10.0.0.1".to_owned(), "1.2.3.4".to_owned()],
);
assert!(result.is_err(), "should fail");
Ok(())
}
#[test]
fn test_external_ip_mapper_find_external_ip_without_explicit_local_ip() -> Result<()> {
let m = ExternalIpMapper::new(
CandidateType::Unspecified,
&["1.2.3.4".to_owned(), "2200::1".to_owned()],
)?
.unwrap();
assert!(m.ipv4_mapping.ip_sole.is_some());
assert!(m.ipv6_mapping.ip_sole.is_some());
let ext_ip = m.find_external_ip("10.0.0.1")?;
assert_eq!(ext_ip.to_string(), "1.2.3.4", "should match");
let ext_ip = m.find_external_ip("fe80::0001")?; assert_eq!(ext_ip.to_string(), "2200::1", "should match");
let result = m.find_external_ip("really.bad");
assert!(result.is_err(), "should fail");
Ok(())
}
#[test]
fn test_external_ip_mapper_find_external_ip_with_explicit_local_ip() -> Result<()> {
let m = ExternalIpMapper::new(
CandidateType::Unspecified,
&[
"1.2.3.4/10.0.0.1".to_owned(),
"1.2.3.5/10.0.0.2".to_owned(),
"2200::1/fe80::1".to_owned(),
"2200::2/fe80::2".to_owned(),
],
)?
.unwrap();
let ext_ip = m.find_external_ip("10.0.0.1")?;
assert_eq!(ext_ip.to_string(), "1.2.3.4", "should match");
let ext_ip = m.find_external_ip("10.0.0.2")?;
assert_eq!(ext_ip.to_string(), "1.2.3.5", "should match");
let result = m.find_external_ip("10.0.0.3");
assert!(result.is_err(), "should fail");
let ext_ip = m.find_external_ip("fe80::0001")?; assert_eq!(ext_ip.to_string(), "2200::1", "should match");
let ext_ip = m.find_external_ip("fe80::0002")?; assert_eq!(ext_ip.to_string(), "2200::2", "should match");
let result = m.find_external_ip("fe80::3");
assert!(result.is_err(), "should fail");
let result = m.find_external_ip("really.bad");
assert!(result.is_err(), "should fail");
Ok(())
}
#[test]
fn test_external_ip_mapper_find_external_ip_with_empty_map() -> Result<()> {
let m = ExternalIpMapper::new(CandidateType::Unspecified, &["1.2.3.4".to_owned()])?.unwrap();
let result = m.find_external_ip("fe80::1");
assert!(result.is_err(), "should fail");
let m = ExternalIpMapper::new(CandidateType::Unspecified, &["2200::1".to_owned()])?.unwrap();
let result = m.find_external_ip("10.0.0.1");
assert!(result.is_err(), "should fail");
Ok(())
}