Skip to content

Commit 92c3206

Browse files
author
Max Larsson
committed
Add test cases for both IPv4 and IPv6
1 parent 177ee51 commit 92c3206

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/iface/interface/tests/ipv4.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,3 +1102,83 @@ fn test_icmp_reply_size(#[case] medium: Medium) {
11021102
))
11031103
);
11041104
}
1105+
1106+
#[rstest]
1107+
#[case(Medium::Ip)]
1108+
#[cfg(feature = "medium-ip")]
1109+
#[case(Medium::Ethernet)]
1110+
#[cfg(feature = "medium-ethernet")]
1111+
fn get_source_address(#[case] medium: Medium) {
1112+
let (mut iface, _, _) = setup(medium);
1113+
1114+
const OWN_UNIQUE_LOCAL_ADDR1: Ipv4Address = Ipv4Address::new(172, 18, 1, 2);
1115+
const OWN_UNIQUE_LOCAL_ADDR2: Ipv4Address = Ipv4Address::new(172, 24, 24, 14);
1116+
1117+
// List of addresses of the interface:
1118+
// 172.18.1.2/24
1119+
// 172.24.24.14/24
1120+
iface.update_ip_addrs(|addrs| {
1121+
addrs.clear();
1122+
1123+
addrs
1124+
.push(IpCidr::Ipv4(Ipv4Cidr::new(OWN_UNIQUE_LOCAL_ADDR1, 24)))
1125+
.unwrap();
1126+
addrs
1127+
.push(IpCidr::Ipv4(Ipv4Cidr::new(OWN_UNIQUE_LOCAL_ADDR2, 24)))
1128+
.unwrap();
1129+
});
1130+
1131+
// List of addresses we test:
1132+
// 172.18.1.255 -> 172.18.1.2
1133+
// 172.24.24.12 -> 172.24.24.14
1134+
// 172.24.23.255 -> 172.18.1.2
1135+
const UNIQUE_LOCAL_ADDR1: Ipv4Address = Ipv4Address::new(172, 18, 1, 255);
1136+
const UNIQUE_LOCAL_ADDR2: Ipv4Address = Ipv4Address::new(172, 24, 24, 12);
1137+
const UNIQUE_LOCAL_ADDR3: Ipv4Address = Ipv4Address::new(172, 24, 23, 255);
1138+
1139+
assert_eq!(
1140+
iface.inner.get_source_address_ipv4(&UNIQUE_LOCAL_ADDR1),
1141+
Some(OWN_UNIQUE_LOCAL_ADDR1)
1142+
);
1143+
1144+
assert_eq!(
1145+
iface.inner.get_source_address_ipv4(&UNIQUE_LOCAL_ADDR2),
1146+
Some(OWN_UNIQUE_LOCAL_ADDR2)
1147+
);
1148+
assert_eq!(
1149+
iface.inner.get_source_address_ipv4(&UNIQUE_LOCAL_ADDR3),
1150+
Some(OWN_UNIQUE_LOCAL_ADDR1)
1151+
);
1152+
}
1153+
1154+
#[rstest]
1155+
#[case(Medium::Ip)]
1156+
#[cfg(feature = "medium-ip")]
1157+
#[case(Medium::Ethernet)]
1158+
#[cfg(feature = "medium-ethernet")]
1159+
fn get_source_address_empty_interface(#[case] medium: Medium) {
1160+
let (mut iface, _, _) = setup(medium);
1161+
1162+
iface.update_ip_addrs(|ips| ips.clear());
1163+
1164+
// List of addresses we test:
1165+
// 172.18.1.255 -> None
1166+
// 172.24.24.12 -> None
1167+
// 172.24.23.255 -> None
1168+
const UNIQUE_LOCAL_ADDR1: Ipv4Address = Ipv4Address::new(172, 18, 1, 255);
1169+
const UNIQUE_LOCAL_ADDR2: Ipv4Address = Ipv4Address::new(172, 24, 24, 12);
1170+
const UNIQUE_LOCAL_ADDR3: Ipv4Address = Ipv4Address::new(172, 24, 23, 255);
1171+
1172+
assert_eq!(
1173+
iface.inner.get_source_address_ipv4(&UNIQUE_LOCAL_ADDR1),
1174+
None
1175+
);
1176+
assert_eq!(
1177+
iface.inner.get_source_address_ipv4(&UNIQUE_LOCAL_ADDR2),
1178+
None
1179+
);
1180+
assert_eq!(
1181+
iface.inner.get_source_address_ipv4(&UNIQUE_LOCAL_ADDR3),
1182+
None
1183+
);
1184+
}

src/iface/interface/tests/ipv6.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,13 +989,15 @@ fn get_source_address() {
989989
// fd00::201:1:1:1:1 -> fd00::201:1:1:1:2
990990
// fd01::201:1:1:1:1 -> fd01::201:1:1:1:2
991991
// fd02::201:1:1:1:1 -> fd00::201:1:1:1:2 (because first added in the list)
992+
// fd01::201:1:1:1:3 -> fd01::201:1:1:1:2 (because in same subnet)
992993
// ff02::1 -> fe80::1 (same scope)
993994
// 2001:db8:3::2 -> 2001:db8:3::1
994995
// 2001:db9:3::2 -> 2001:db8:3::1
995996
const LINK_LOCAL_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 42);
996997
const UNIQUE_LOCAL_ADDR1: Ipv6Address = Ipv6Address::new(0xfd00, 0, 0, 201, 1, 1, 1, 1);
997998
const UNIQUE_LOCAL_ADDR2: Ipv6Address = Ipv6Address::new(0xfd01, 0, 0, 201, 1, 1, 1, 1);
998999
const UNIQUE_LOCAL_ADDR3: Ipv6Address = Ipv6Address::new(0xfd02, 0, 0, 201, 1, 1, 1, 1);
1000+
const UNIQUE_LOCAL_ADDR4: Ipv6Address = Ipv6Address::new(0xfd01, 0, 0, 201, 1, 1, 1, 3);
9991001
const GLOBAL_UNICAST_ADDR1: Ipv6Address =
10001002
Ipv6Address::new(0x2001, 0x0db8, 0x0003, 0, 0, 0, 0, 2);
10011003
const GLOBAL_UNICAST_ADDR2: Ipv6Address =
@@ -1022,6 +1024,10 @@ fn get_source_address() {
10221024
iface.inner.get_source_address_ipv6(&UNIQUE_LOCAL_ADDR3),
10231025
OWN_UNIQUE_LOCAL_ADDR1
10241026
);
1027+
assert_eq!(
1028+
iface.inner.get_source_address_ipv6(&UNIQUE_LOCAL_ADDR4),
1029+
OWN_UNIQUE_LOCAL_ADDR2
1030+
);
10251031
assert_eq!(
10261032
iface
10271033
.inner

0 commit comments

Comments
 (0)