Skip to content

Commit

Permalink
Merge pull request #823 from thvdveld/remove-mock-for-tests
Browse files Browse the repository at this point in the history
Remove mock for tests
  • Loading branch information
thvdveld authored Aug 9, 2023
2 parents d3c491f + 7044bd1 commit 784de3f
Show file tree
Hide file tree
Showing 14 changed files with 585 additions and 383 deletions.
1 change: 1 addition & 0 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ FEATURES_TEST=(
"std,medium-ieee802154,proto-rpl,proto-sixlowpan,proto-sixlowpan-fragmentation,socket-udp"
"std,medium-ip,proto-ipv4,proto-ipv6,socket-tcp,socket-udp"
"std,medium-ethernet,medium-ip,medium-ieee802154,proto-ipv4,proto-ipv6,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"std,medium-ieee802154,medium-ip,proto-ipv4,socket-raw"
)

FEATURES_TEST_NIGHTLY=(
Expand Down
112 changes: 14 additions & 98 deletions src/iface/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ use check;
/// a dependency on heap allocation, it instead owns a `BorrowMut<[T]>`, which can be
/// a `&mut [T]`, or `Vec<T>` if a heap is available.
pub struct Interface {
inner: InterfaceInner,
pub(crate) inner: InterfaceInner,
fragments: FragmentsBuffer,
fragmenter: Fragmenter,
}
Expand Down Expand Up @@ -968,97 +968,6 @@ impl InterfaceInner {
None
}

#[cfg(test)]
pub(crate) fn mock() -> Self {
Self {
caps: DeviceCapabilities {
#[cfg(feature = "medium-ethernet")]
medium: crate::phy::Medium::Ethernet,
#[cfg(all(not(feature = "medium-ethernet"), feature = "medium-ip"))]
medium: crate::phy::Medium::Ip,
#[cfg(all(not(feature = "medium-ethernet"), feature = "medium-ieee802154"))]
medium: crate::phy::Medium::Ieee802154,

checksum: crate::phy::ChecksumCapabilities {
#[cfg(feature = "proto-ipv4")]
icmpv4: crate::phy::Checksum::Both,
#[cfg(feature = "proto-ipv6")]
icmpv6: crate::phy::Checksum::Both,
ipv4: crate::phy::Checksum::Both,
tcp: crate::phy::Checksum::Both,
udp: crate::phy::Checksum::Both,
},
max_burst_size: None,
#[cfg(feature = "medium-ethernet")]
max_transmission_unit: 1514,
#[cfg(not(feature = "medium-ethernet"))]
max_transmission_unit: 1500,
},
now: Instant::from_millis_const(0),

ip_addrs: Vec::from_slice(&[
#[cfg(feature = "proto-ipv4")]
IpCidr::Ipv4(Ipv4Cidr::new(Ipv4Address::new(192, 168, 1, 1), 24)),
#[cfg(feature = "proto-ipv6")]
IpCidr::Ipv6(Ipv6Cidr::new(
Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
64,
)),
])
.unwrap(),
rand: Rand::new(1234),
routes: Routes::new(),

#[cfg(feature = "proto-ipv4")]
any_ip: false,

#[cfg(feature = "medium-ieee802154")]
pan_id: Some(crate::wire::Ieee802154Pan(0xabcd)),
#[cfg(feature = "medium-ieee802154")]
sequence_no: 1,

#[cfg(feature = "proto-sixlowpan-fragmentation")]
tag: 1,

#[cfg(feature = "proto-sixlowpan")]
sixlowpan_address_context: Vec::new(),

#[cfg(feature = "proto-ipv4-fragmentation")]
ipv4_id: 1,

#[cfg(all(
feature = "medium-ip",
not(feature = "medium-ethernet"),
not(feature = "medium-ieee802154")
))]
hardware_addr: crate::wire::HardwareAddress::Ip,

#[cfg(feature = "medium-ethernet")]
hardware_addr: crate::wire::HardwareAddress::Ethernet(crate::wire::EthernetAddress([
0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
])),

#[cfg(all(
not(feature = "medium-ip"),
not(feature = "medium-ethernet"),
feature = "medium-ieee802154"
))]
hardware_addr: crate::wire::HardwareAddress::Ieee802154(
crate::wire::Ieee802154Address::Extended([
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x2, 0x2,
]),
),

#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
neighbor_cache: NeighborCache::new(),

#[cfg(feature = "proto-igmp")]
igmp_report_state: IgmpReportState::Inactive,
#[cfg(feature = "proto-igmp")]
ipv4_multicast_groups: LinearMap::new(),
}
}

#[cfg(test)]
#[allow(unused)] // unused depending on which sockets are enabled
pub(crate) fn set_now(&mut self, now: Instant) {
Expand Down Expand Up @@ -1427,16 +1336,21 @@ impl InterfaceInner {
let b = dst_addr.as_bytes();
let hardware_addr = match *dst_addr {
#[cfg(feature = "proto-ipv4")]
IpAddress::Ipv4(_addr) => {
HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[
IpAddress::Ipv4(_addr) => match self.caps.medium {
#[cfg(feature = "medium-ethernet")]
Medium::Ethernet => HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[
0x01,
0x00,
0x5e,
b[1] & 0x7F,
b[2],
b[3],
]))
}
])),
#[cfg(feature = "medium-ieee802154")]
Medium::Ieee802154 => unreachable!(),
#[cfg(feature = "medium-ip")]
Medium::Ip => unreachable!(),
},
#[cfg(feature = "proto-ipv6")]
IpAddress::Ipv6(_addr) => match self.caps.medium {
#[cfg(feature = "medium-ethernet")]
Expand Down Expand Up @@ -1467,8 +1381,10 @@ impl InterfaceInner {
}

match (src_addr, dst_addr) {
#[cfg(feature = "proto-ipv4")]
(&IpAddress::Ipv4(src_addr), IpAddress::Ipv4(dst_addr)) => {
#[cfg(all(feature = "medium-ethernet", feature = "proto-ipv4"))]
(&IpAddress::Ipv4(src_addr), IpAddress::Ipv4(dst_addr))
if matches!(self.caps.medium, Medium::Ethernet) =>
{
net_debug!(
"address {} not in neighbor cache, sending ARP request",
dst_addr
Expand Down
15 changes: 9 additions & 6 deletions src/iface/interface/tests/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ fn test_handle_valid_arp_request(#[case] medium: Medium) {

let local_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x01]);
let remote_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
let local_hw_addr = EthernetAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
let local_hw_addr = EthernetAddress([0x02, 0x02, 0x02, 0x02, 0x02, 0x02]);
let remote_hw_addr = EthernetAddress([0x52, 0x54, 0x00, 0x00, 0x00, 0x00]);

let repr = ArpRepr::EthernetIpv4 {
Expand Down Expand Up @@ -470,7 +470,7 @@ fn test_arp_flush_after_update_ip(#[case] medium: Medium) {

let local_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x01]);
let remote_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
let local_hw_addr = EthernetAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
let local_hw_addr = EthernetAddress([0x02, 0x02, 0x02, 0x02, 0x02, 0x02]);
let remote_hw_addr = EthernetAddress([0x52, 0x54, 0x00, 0x00, 0x00, 0x00]);

let repr = ArpRepr::EthernetIpv4 {
Expand Down Expand Up @@ -615,7 +615,10 @@ fn test_icmpv4_socket(#[case] medium: Medium) {
#[case(Medium::Ethernet)]
#[cfg(all(feature = "proto-igmp", feature = "medium-ethernet"))]
fn test_handle_igmp(#[case] medium: Medium) {
fn recv_igmp(device: &mut Loopback, timestamp: Instant) -> Vec<(Ipv4Repr, IgmpRepr)> {
fn recv_igmp(
device: &mut crate::tests::TestingDevice,
timestamp: Instant,
) -> Vec<(Ipv4Repr, IgmpRepr)> {
let caps = device.capabilities();
let checksum_caps = &caps.checksum;
recv_all(device, timestamp)
Expand Down Expand Up @@ -649,7 +652,7 @@ fn test_handle_igmp(#[case] medium: Medium) {
let (mut iface, mut sockets, mut device) = setup(medium);

// Join multicast groups
let timestamp = Instant::now();
let timestamp = Instant::ZERO;
for group in &groups {
iface
.join_multicast_group(&mut device, *group, timestamp)
Expand All @@ -671,7 +674,7 @@ fn test_handle_igmp(#[case] medium: Medium) {
}

// General query
let timestamp = Instant::now();
let timestamp = Instant::ZERO;
const GENERAL_QUERY_BYTES: &[u8] = &[
0x46, 0xc0, 0x00, 0x24, 0xed, 0xb4, 0x00, 0x00, 0x01, 0x02, 0x47, 0x43, 0xac, 0x16, 0x63,
0x04, 0xe0, 0x00, 0x00, 0x01, 0x94, 0x04, 0x00, 0x00, 0x11, 0x64, 0xec, 0x8f, 0x00, 0x00,
Expand All @@ -692,7 +695,7 @@ fn test_handle_igmp(#[case] medium: Medium) {
iface.socket_ingress(&mut device, &mut sockets);

// Leave multicast groups
let timestamp = Instant::now();
let timestamp = Instant::ZERO;
for group in &groups {
iface
.leave_multicast_group(&mut device, *group, timestamp)
Expand Down
2 changes: 1 addition & 1 deletion src/iface/interface/tests/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ fn test_handle_valid_ndisc_request(#[case] medium: Medium) {

let local_ip_addr = Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 1);
let remote_ip_addr = Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 2);
let local_hw_addr = EthernetAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
let local_hw_addr = EthernetAddress([0x02, 0x02, 0x02, 0x02, 0x02, 0x02]);
let remote_hw_addr = EthernetAddress([0x52, 0x54, 0x00, 0x00, 0x00, 0x00]);

let solicit = Icmpv6Repr::Ndisc(NdiscRepr::NeighborSolicit {
Expand Down
48 changes: 7 additions & 41 deletions src/iface/interface/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ mod sixlowpan;
#[cfg(feature = "proto-igmp")]
use std::vec::Vec;

use crate::tests::setup;

use rstest::*;

use super::*;

use crate::iface::Interface;
use crate::phy::{ChecksumCapabilities, Loopback};
use crate::phy::ChecksumCapabilities;
#[cfg(feature = "alloc")]
use crate::phy::Loopback;
use crate::time::Instant;

#[allow(unused)]
Expand All @@ -23,46 +27,8 @@ fn fill_slice(s: &mut [u8], val: u8) {
}
}

fn setup<'a>(medium: Medium) -> (Interface, SocketSet<'a>, Loopback) {
let mut device = Loopback::new(medium);

let config = Config::new(match medium {
#[cfg(feature = "medium-ethernet")]
Medium::Ethernet => HardwareAddress::Ethernet(Default::default()),
#[cfg(feature = "medium-ip")]
Medium::Ip => HardwareAddress::Ip,
#[cfg(feature = "medium-ieee802154")]
Medium::Ieee802154 => HardwareAddress::Ieee802154(Default::default()),
});

let mut iface = Interface::new(config, &mut device, Instant::ZERO);

#[cfg(feature = "proto-ipv4")]
{
iface.update_ip_addrs(|ip_addrs| {
ip_addrs
.push(IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8))
.unwrap();
});
}

#[cfg(feature = "proto-ipv6")]
{
iface.update_ip_addrs(|ip_addrs| {
ip_addrs
.push(IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 1), 128))
.unwrap();
ip_addrs
.push(IpCidr::new(IpAddress::v6(0xfdbe, 0, 0, 0, 0, 0, 0, 1), 64))
.unwrap();
});
}

(iface, SocketSet::new(vec![]), device)
}

#[cfg(feature = "proto-igmp")]
fn recv_all(device: &mut Loopback, timestamp: Instant) -> Vec<Vec<u8>> {
fn recv_all(device: &mut crate::tests::TestingDevice, timestamp: Instant) -> Vec<Vec<u8>> {
let mut pkts = Vec::new();
while let Some((rx, _tx)) = device.receive(timestamp) {
rx.consume(|pkt| {
Expand All @@ -88,7 +54,7 @@ impl TxToken for MockTxToken {

#[test]
#[should_panic(expected = "The hardware address does not match the medium of the interface.")]
#[cfg(all(feature = "medium-ip", feature = "medium-ethernet"))]
#[cfg(all(feature = "medium-ip", feature = "medium-ethernet", feature = "alloc"))]
fn test_new_panic() {
let mut device = Loopback::new(Medium::Ethernet);
let config = Config::new(HardwareAddress::Ip);
Expand Down
24 changes: 12 additions & 12 deletions src/iface/interface/tests/sixlowpan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ fn test_echo_request_sixlowpan_128_bytes() {
);

assert_eq!(
device.queue[0],
device.queue.pop_front().unwrap(),
&[
0x41, 0xcc, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0xc0, 0xb0, 0x5, 0x4e, 0x7a, 0x11, 0x3a, 0x92, 0xfc, 0x48, 0xc2,
0x41, 0xcc, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x2,
0x2, 0x2, 0x2, 0x2, 0xc0, 0xb0, 0x5, 0x4e, 0x7a, 0x11, 0x3a, 0x92, 0xfc, 0x48, 0xc2,
0xa4, 0x41, 0xfc, 0x76, 0x40, 0x42, 0x42, 0x42, 0x42, 0x42, 0xb, 0x1a, 0x81, 0x0, 0x0,
0x0, 0x0, 0x27, 0x0, 0x2, 0xa2, 0xc2, 0x2d, 0x63, 0x0, 0x0, 0x0, 0x0, 0xd9, 0x5e, 0xc,
0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
Expand All @@ -247,10 +247,10 @@ fn test_echo_request_sixlowpan_128_bytes() {
iface.poll(Instant::now(), &mut device, &mut sockets);

assert_eq!(
device.queue[1],
device.queue.pop_front().unwrap(),
&[
0x41, 0xcc, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0xe0, 0xb0, 0x5, 0x4e, 0xf, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
0x41, 0xcc, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x2,
0x2, 0x2, 0x2, 0x2, 0xe0, 0xb0, 0x5, 0x4e, 0xf, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b,
0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
Expand Down Expand Up @@ -384,10 +384,10 @@ In at rhoncus tortor. Cras blandit tellus diam, varius vestibulum nibh commodo n
iface.poll(Instant::now(), &mut device, &mut sockets);

assert_eq!(
device.queue[0],
device.queue.pop_front().unwrap(),
&[
0x41, 0xcc, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0xc0, 0xb4, 0x5, 0x4e, 0x7e, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x41, 0xcc, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x2,
0x2, 0x2, 0x2, 0x2, 0xc0, 0xb4, 0x5, 0x4e, 0x7e, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf0, 0x4, 0xd2, 0x4, 0xd2, 0xf6,
0x4d, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64,
0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74, 0x2c,
Expand All @@ -399,10 +399,10 @@ In at rhoncus tortor. Cras blandit tellus diam, varius vestibulum nibh commodo n
);

assert_eq!(
device.queue[1],
device.queue.pop_front().unwrap(),
&[
0x41, 0xcc, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0xe0, 0xb4, 0x5, 0x4e, 0xf, 0x6f, 0x72, 0x74, 0x6f, 0x72, 0x2e,
0x41, 0xcc, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x2,
0x2, 0x2, 0x2, 0x2, 0xe0, 0xb4, 0x5, 0x4e, 0xf, 0x6f, 0x72, 0x74, 0x6f, 0x72, 0x2e,
0x20, 0x43, 0x72, 0x61, 0x73, 0x20, 0x62, 0x6c, 0x61, 0x6e, 0x64, 0x69, 0x74, 0x20,
0x74, 0x65, 0x6c, 0x6c, 0x75, 0x73, 0x20, 0x64, 0x69, 0x61, 0x6d, 0x2c, 0x20, 0x76,
0x61, 0x72, 0x69, 0x75, 0x73, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6c,
Expand Down
2 changes: 1 addition & 1 deletion src/iface/neighbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Cache {
}
}

#[cfg(any(feature = "medium-ethernet", feature = "medium-ip"))]
#[cfg(feature = "medium-ethernet")]
#[cfg(test)]
mod test {
use super::*;
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,13 @@ pub mod socket;
pub mod storage;
pub mod time;
pub mod wire;

#[cfg(all(
test,
any(
feature = "medium-ethernet",
feature = "medium-ip",
feature = "medium-ieee802154"
)
))]
mod tests;
Loading

0 comments on commit 784de3f

Please sign in to comment.