Skip to content

Commit

Permalink
Fix and add ipv6 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
izissise committed Oct 4, 2023
1 parent 1c80ba7 commit 34b997a
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 56 deletions.
83 changes: 58 additions & 25 deletions tests/addr_parse.rs
Original file line number Diff line number Diff line change
@@ -1,85 +1,118 @@
use mdns_sd::AsIpv4Addrs;
use mdns_sd::AsIpAddrs;
use std::collections::HashSet;
use std::net::Ipv4Addr;
use std::net::{Ipv4Addr, Ipv6Addr, IpAddr};

#[test]
fn test_addr_str() {
assert_eq!(
"127.0.0.1".as_ipv4_addrs(),
"127.0.0.1".as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());

set
})
);

let addr = "127.0.0.1".to_string();
assert_eq!(
addr.as_ipv4_addrs(),
addr.as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());

set
})
);

// verify that `&String` also works.
assert_eq!(
(&addr).as_ipv4_addrs(),
(&addr).as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());

set
})
);

assert_eq!(
"127.0.0.1,127.0.0.2".as_ipv4_addrs(),
"127.0.0.1,127.0.0.2".as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 2));
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());
set.insert(Ipv4Addr::new(127, 0, 0, 2).into());

set
})
);

let addr = "2001:db8::1".to_string();
assert_eq!(
addr.as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1).into());

set
})
);

assert_eq!(
"2001:db8::1,2001:db8::2".as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1).into());
set.insert(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 2).into());

set
})
);

// verify that an empty string parsed into an empty set.
assert_eq!("".as_ipv4_addrs(), Ok(HashSet::new()));
assert_eq!("".as_ip_addrs(), Ok(HashSet::new()));
}

#[test]
fn test_addr_slice() {
assert_eq!(
(&["127.0.0.1"][..]).as_ipv4_addrs(),
(&["127.0.0.1"][..]).as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());

set
})
);

assert_eq!(
(&["127.0.0.1", "127.0.0.2"][..]).as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());
set.insert(Ipv4Addr::new(127, 0, 0, 2).into());

set
})
);

assert_eq!(
(&["127.0.0.1", "127.0.0.2"][..]).as_ipv4_addrs(),
(&vec!["127.0.0.1", "127.0.0.2"][..]).as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 2));
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());
set.insert(Ipv4Addr::new(127, 0, 0, 2).into());

set
})
);

assert_eq!(
(&vec!["127.0.0.1", "127.0.0.2"][..]).as_ipv4_addrs(),
(&vec!["2001:db8::1", "2001:db8::2"][..]).as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 2));
set.insert(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1).into());
set.insert(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 2).into());

set
})
Expand All @@ -88,23 +121,23 @@ fn test_addr_slice() {

#[test]
fn test_addr_ip() {
let ip = Ipv4Addr::new(127, 0, 0, 1);
let ip: IpAddr = Ipv4Addr::new(127, 0, 0, 1).into();

assert_eq!(
ip.as_ipv4_addrs(),
ip.as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());

set
})
);

assert_eq!(
(&ip).as_ipv4_addrs(),
(&ip).as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());

set
})
Expand Down
Loading

0 comments on commit 34b997a

Please sign in to comment.