@@ -1208,3 +1208,83 @@ fn test_icmp_reply_size(#[case] medium: Medium) {
12081208 ) )
12091209 ) ;
12101210}
1211+
1212+ #[ rstest]
1213+ #[ case( Medium :: Ip ) ]
1214+ #[ cfg( feature = "medium-ip" ) ]
1215+ #[ case( Medium :: Ethernet ) ]
1216+ #[ cfg( feature = "medium-ethernet" ) ]
1217+ fn get_source_address ( #[ case] medium : Medium ) {
1218+ let ( mut iface, _, _) = setup ( medium) ;
1219+
1220+ const OWN_UNIQUE_LOCAL_ADDR1 : Ipv4Address = Ipv4Address :: new ( 172 , 18 , 1 , 2 ) ;
1221+ const OWN_UNIQUE_LOCAL_ADDR2 : Ipv4Address = Ipv4Address :: new ( 172 , 24 , 24 , 14 ) ;
1222+
1223+ // List of addresses of the interface:
1224+ // 172.18.1.2/24
1225+ // 172.24.24.14/24
1226+ iface. update_ip_addrs ( |addrs| {
1227+ addrs. clear ( ) ;
1228+
1229+ addrs
1230+ . push ( IpCidr :: Ipv4 ( Ipv4Cidr :: new ( OWN_UNIQUE_LOCAL_ADDR1 , 24 ) ) )
1231+ . unwrap ( ) ;
1232+ addrs
1233+ . push ( IpCidr :: Ipv4 ( Ipv4Cidr :: new ( OWN_UNIQUE_LOCAL_ADDR2 , 24 ) ) )
1234+ . unwrap ( ) ;
1235+ } ) ;
1236+
1237+ // List of addresses we test:
1238+ // 172.18.1.254 -> 172.18.1.2
1239+ // 172.24.24.12 -> 172.24.24.14
1240+ // 172.24.23.254 -> 172.18.1.2
1241+ const UNIQUE_LOCAL_ADDR1 : Ipv4Address = Ipv4Address :: new ( 172 , 18 , 1 , 254 ) ;
1242+ const UNIQUE_LOCAL_ADDR2 : Ipv4Address = Ipv4Address :: new ( 172 , 24 , 24 , 12 ) ;
1243+ const UNIQUE_LOCAL_ADDR3 : Ipv4Address = Ipv4Address :: new ( 172 , 24 , 23 , 254 ) ;
1244+
1245+ assert_eq ! (
1246+ iface. inner. get_source_address_ipv4( & UNIQUE_LOCAL_ADDR1 ) ,
1247+ Some ( OWN_UNIQUE_LOCAL_ADDR1 )
1248+ ) ;
1249+
1250+ assert_eq ! (
1251+ iface. inner. get_source_address_ipv4( & UNIQUE_LOCAL_ADDR2 ) ,
1252+ Some ( OWN_UNIQUE_LOCAL_ADDR2 )
1253+ ) ;
1254+ assert_eq ! (
1255+ iface. inner. get_source_address_ipv4( & UNIQUE_LOCAL_ADDR3 ) ,
1256+ Some ( OWN_UNIQUE_LOCAL_ADDR1 )
1257+ ) ;
1258+ }
1259+
1260+ #[ rstest]
1261+ #[ case( Medium :: Ip ) ]
1262+ #[ cfg( feature = "medium-ip" ) ]
1263+ #[ case( Medium :: Ethernet ) ]
1264+ #[ cfg( feature = "medium-ethernet" ) ]
1265+ fn get_source_address_empty_interface ( #[ case] medium : Medium ) {
1266+ let ( mut iface, _, _) = setup ( medium) ;
1267+
1268+ iface. update_ip_addrs ( |ips| ips. clear ( ) ) ;
1269+
1270+ // List of addresses we test:
1271+ // 172.18.1.254 -> None
1272+ // 172.24.24.12 -> None
1273+ // 172.24.23.254 -> None
1274+ const UNIQUE_LOCAL_ADDR1 : Ipv4Address = Ipv4Address :: new ( 172 , 18 , 1 , 254 ) ;
1275+ const UNIQUE_LOCAL_ADDR2 : Ipv4Address = Ipv4Address :: new ( 172 , 24 , 24 , 12 ) ;
1276+ const UNIQUE_LOCAL_ADDR3 : Ipv4Address = Ipv4Address :: new ( 172 , 24 , 23 , 254 ) ;
1277+
1278+ assert_eq ! (
1279+ iface. inner. get_source_address_ipv4( & UNIQUE_LOCAL_ADDR1 ) ,
1280+ None
1281+ ) ;
1282+ assert_eq ! (
1283+ iface. inner. get_source_address_ipv4( & UNIQUE_LOCAL_ADDR2 ) ,
1284+ None
1285+ ) ;
1286+ assert_eq ! (
1287+ iface. inner. get_source_address_ipv4( & UNIQUE_LOCAL_ADDR3 ) ,
1288+ None
1289+ ) ;
1290+ }
0 commit comments