Skip to content

Commit 784de3f

Browse files
authored
Merge pull request #823 from thvdveld/remove-mock-for-tests
Remove mock for tests
2 parents d3c491f + 7044bd1 commit 784de3f

File tree

14 files changed

+585
-383
lines changed

14 files changed

+585
-383
lines changed

ci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ FEATURES_TEST=(
3030
"std,medium-ieee802154,proto-rpl,proto-sixlowpan,proto-sixlowpan-fragmentation,socket-udp"
3131
"std,medium-ip,proto-ipv4,proto-ipv6,socket-tcp,socket-udp"
3232
"std,medium-ethernet,medium-ip,medium-ieee802154,proto-ipv4,proto-ipv6,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
33+
"std,medium-ieee802154,medium-ip,proto-ipv4,socket-raw"
3334
)
3435

3536
FEATURES_TEST_NIGHTLY=(

src/iface/interface/mod.rs

Lines changed: 14 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ use check;
230230
/// a dependency on heap allocation, it instead owns a `BorrowMut<[T]>`, which can be
231231
/// a `&mut [T]`, or `Vec<T>` if a heap is available.
232232
pub struct Interface {
233-
inner: InterfaceInner,
233+
pub(crate) inner: InterfaceInner,
234234
fragments: FragmentsBuffer,
235235
fragmenter: Fragmenter,
236236
}
@@ -968,97 +968,6 @@ impl InterfaceInner {
968968
None
969969
}
970970

971-
#[cfg(test)]
972-
pub(crate) fn mock() -> Self {
973-
Self {
974-
caps: DeviceCapabilities {
975-
#[cfg(feature = "medium-ethernet")]
976-
medium: crate::phy::Medium::Ethernet,
977-
#[cfg(all(not(feature = "medium-ethernet"), feature = "medium-ip"))]
978-
medium: crate::phy::Medium::Ip,
979-
#[cfg(all(not(feature = "medium-ethernet"), feature = "medium-ieee802154"))]
980-
medium: crate::phy::Medium::Ieee802154,
981-
982-
checksum: crate::phy::ChecksumCapabilities {
983-
#[cfg(feature = "proto-ipv4")]
984-
icmpv4: crate::phy::Checksum::Both,
985-
#[cfg(feature = "proto-ipv6")]
986-
icmpv6: crate::phy::Checksum::Both,
987-
ipv4: crate::phy::Checksum::Both,
988-
tcp: crate::phy::Checksum::Both,
989-
udp: crate::phy::Checksum::Both,
990-
},
991-
max_burst_size: None,
992-
#[cfg(feature = "medium-ethernet")]
993-
max_transmission_unit: 1514,
994-
#[cfg(not(feature = "medium-ethernet"))]
995-
max_transmission_unit: 1500,
996-
},
997-
now: Instant::from_millis_const(0),
998-
999-
ip_addrs: Vec::from_slice(&[
1000-
#[cfg(feature = "proto-ipv4")]
1001-
IpCidr::Ipv4(Ipv4Cidr::new(Ipv4Address::new(192, 168, 1, 1), 24)),
1002-
#[cfg(feature = "proto-ipv6")]
1003-
IpCidr::Ipv6(Ipv6Cidr::new(
1004-
Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
1005-
64,
1006-
)),
1007-
])
1008-
.unwrap(),
1009-
rand: Rand::new(1234),
1010-
routes: Routes::new(),
1011-
1012-
#[cfg(feature = "proto-ipv4")]
1013-
any_ip: false,
1014-
1015-
#[cfg(feature = "medium-ieee802154")]
1016-
pan_id: Some(crate::wire::Ieee802154Pan(0xabcd)),
1017-
#[cfg(feature = "medium-ieee802154")]
1018-
sequence_no: 1,
1019-
1020-
#[cfg(feature = "proto-sixlowpan-fragmentation")]
1021-
tag: 1,
1022-
1023-
#[cfg(feature = "proto-sixlowpan")]
1024-
sixlowpan_address_context: Vec::new(),
1025-
1026-
#[cfg(feature = "proto-ipv4-fragmentation")]
1027-
ipv4_id: 1,
1028-
1029-
#[cfg(all(
1030-
feature = "medium-ip",
1031-
not(feature = "medium-ethernet"),
1032-
not(feature = "medium-ieee802154")
1033-
))]
1034-
hardware_addr: crate::wire::HardwareAddress::Ip,
1035-
1036-
#[cfg(feature = "medium-ethernet")]
1037-
hardware_addr: crate::wire::HardwareAddress::Ethernet(crate::wire::EthernetAddress([
1038-
0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
1039-
])),
1040-
1041-
#[cfg(all(
1042-
not(feature = "medium-ip"),
1043-
not(feature = "medium-ethernet"),
1044-
feature = "medium-ieee802154"
1045-
))]
1046-
hardware_addr: crate::wire::HardwareAddress::Ieee802154(
1047-
crate::wire::Ieee802154Address::Extended([
1048-
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x2, 0x2,
1049-
]),
1050-
),
1051-
1052-
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
1053-
neighbor_cache: NeighborCache::new(),
1054-
1055-
#[cfg(feature = "proto-igmp")]
1056-
igmp_report_state: IgmpReportState::Inactive,
1057-
#[cfg(feature = "proto-igmp")]
1058-
ipv4_multicast_groups: LinearMap::new(),
1059-
}
1060-
}
1061-
1062971
#[cfg(test)]
1063972
#[allow(unused)] // unused depending on which sockets are enabled
1064973
pub(crate) fn set_now(&mut self, now: Instant) {
@@ -1427,16 +1336,21 @@ impl InterfaceInner {
14271336
let b = dst_addr.as_bytes();
14281337
let hardware_addr = match *dst_addr {
14291338
#[cfg(feature = "proto-ipv4")]
1430-
IpAddress::Ipv4(_addr) => {
1431-
HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[
1339+
IpAddress::Ipv4(_addr) => match self.caps.medium {
1340+
#[cfg(feature = "medium-ethernet")]
1341+
Medium::Ethernet => HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[
14321342
0x01,
14331343
0x00,
14341344
0x5e,
14351345
b[1] & 0x7F,
14361346
b[2],
14371347
b[3],
1438-
]))
1439-
}
1348+
])),
1349+
#[cfg(feature = "medium-ieee802154")]
1350+
Medium::Ieee802154 => unreachable!(),
1351+
#[cfg(feature = "medium-ip")]
1352+
Medium::Ip => unreachable!(),
1353+
},
14401354
#[cfg(feature = "proto-ipv6")]
14411355
IpAddress::Ipv6(_addr) => match self.caps.medium {
14421356
#[cfg(feature = "medium-ethernet")]
@@ -1467,8 +1381,10 @@ impl InterfaceInner {
14671381
}
14681382

14691383
match (src_addr, dst_addr) {
1470-
#[cfg(feature = "proto-ipv4")]
1471-
(&IpAddress::Ipv4(src_addr), IpAddress::Ipv4(dst_addr)) => {
1384+
#[cfg(all(feature = "medium-ethernet", feature = "proto-ipv4"))]
1385+
(&IpAddress::Ipv4(src_addr), IpAddress::Ipv4(dst_addr))
1386+
if matches!(self.caps.medium, Medium::Ethernet) =>
1387+
{
14721388
net_debug!(
14731389
"address {} not in neighbor cache, sending ARP request",
14741390
dst_addr

src/iface/interface/tests/ipv4.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ fn test_handle_valid_arp_request(#[case] medium: Medium) {
364364

365365
let local_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x01]);
366366
let remote_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
367-
let local_hw_addr = EthernetAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
367+
let local_hw_addr = EthernetAddress([0x02, 0x02, 0x02, 0x02, 0x02, 0x02]);
368368
let remote_hw_addr = EthernetAddress([0x52, 0x54, 0x00, 0x00, 0x00, 0x00]);
369369

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

471471
let local_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x01]);
472472
let remote_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
473-
let local_hw_addr = EthernetAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
473+
let local_hw_addr = EthernetAddress([0x02, 0x02, 0x02, 0x02, 0x02, 0x02]);
474474
let remote_hw_addr = EthernetAddress([0x52, 0x54, 0x00, 0x00, 0x00, 0x00]);
475475

476476
let repr = ArpRepr::EthernetIpv4 {
@@ -615,7 +615,10 @@ fn test_icmpv4_socket(#[case] medium: Medium) {
615615
#[case(Medium::Ethernet)]
616616
#[cfg(all(feature = "proto-igmp", feature = "medium-ethernet"))]
617617
fn test_handle_igmp(#[case] medium: Medium) {
618-
fn recv_igmp(device: &mut Loopback, timestamp: Instant) -> Vec<(Ipv4Repr, IgmpRepr)> {
618+
fn recv_igmp(
619+
device: &mut crate::tests::TestingDevice,
620+
timestamp: Instant,
621+
) -> Vec<(Ipv4Repr, IgmpRepr)> {
619622
let caps = device.capabilities();
620623
let checksum_caps = &caps.checksum;
621624
recv_all(device, timestamp)
@@ -649,7 +652,7 @@ fn test_handle_igmp(#[case] medium: Medium) {
649652
let (mut iface, mut sockets, mut device) = setup(medium);
650653

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

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

694697
// Leave multicast groups
695-
let timestamp = Instant::now();
698+
let timestamp = Instant::ZERO;
696699
for group in &groups {
697700
iface
698701
.leave_multicast_group(&mut device, *group, timestamp)

src/iface/interface/tests/ipv6.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ fn test_handle_valid_ndisc_request(#[case] medium: Medium) {
554554

555555
let local_ip_addr = Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 1);
556556
let remote_ip_addr = Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 2);
557-
let local_hw_addr = EthernetAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
557+
let local_hw_addr = EthernetAddress([0x02, 0x02, 0x02, 0x02, 0x02, 0x02]);
558558
let remote_hw_addr = EthernetAddress([0x52, 0x54, 0x00, 0x00, 0x00, 0x00]);
559559

560560
let solicit = Icmpv6Repr::Ndisc(NdiscRepr::NeighborSolicit {

src/iface/interface/tests/mod.rs

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ mod sixlowpan;
88
#[cfg(feature = "proto-igmp")]
99
use std::vec::Vec;
1010

11+
use crate::tests::setup;
12+
1113
use rstest::*;
1214

1315
use super::*;
1416

1517
use crate::iface::Interface;
16-
use crate::phy::{ChecksumCapabilities, Loopback};
18+
use crate::phy::ChecksumCapabilities;
19+
#[cfg(feature = "alloc")]
20+
use crate::phy::Loopback;
1721
use crate::time::Instant;
1822

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

26-
fn setup<'a>(medium: Medium) -> (Interface, SocketSet<'a>, Loopback) {
27-
let mut device = Loopback::new(medium);
28-
29-
let config = Config::new(match medium {
30-
#[cfg(feature = "medium-ethernet")]
31-
Medium::Ethernet => HardwareAddress::Ethernet(Default::default()),
32-
#[cfg(feature = "medium-ip")]
33-
Medium::Ip => HardwareAddress::Ip,
34-
#[cfg(feature = "medium-ieee802154")]
35-
Medium::Ieee802154 => HardwareAddress::Ieee802154(Default::default()),
36-
});
37-
38-
let mut iface = Interface::new(config, &mut device, Instant::ZERO);
39-
40-
#[cfg(feature = "proto-ipv4")]
41-
{
42-
iface.update_ip_addrs(|ip_addrs| {
43-
ip_addrs
44-
.push(IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8))
45-
.unwrap();
46-
});
47-
}
48-
49-
#[cfg(feature = "proto-ipv6")]
50-
{
51-
iface.update_ip_addrs(|ip_addrs| {
52-
ip_addrs
53-
.push(IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 1), 128))
54-
.unwrap();
55-
ip_addrs
56-
.push(IpCidr::new(IpAddress::v6(0xfdbe, 0, 0, 0, 0, 0, 0, 1), 64))
57-
.unwrap();
58-
});
59-
}
60-
61-
(iface, SocketSet::new(vec![]), device)
62-
}
63-
6430
#[cfg(feature = "proto-igmp")]
65-
fn recv_all(device: &mut Loopback, timestamp: Instant) -> Vec<Vec<u8>> {
31+
fn recv_all(device: &mut crate::tests::TestingDevice, timestamp: Instant) -> Vec<Vec<u8>> {
6632
let mut pkts = Vec::new();
6733
while let Some((rx, _tx)) = device.receive(timestamp) {
6834
rx.consume(|pkt| {
@@ -88,7 +54,7 @@ impl TxToken for MockTxToken {
8854

8955
#[test]
9056
#[should_panic(expected = "The hardware address does not match the medium of the interface.")]
91-
#[cfg(all(feature = "medium-ip", feature = "medium-ethernet"))]
57+
#[cfg(all(feature = "medium-ip", feature = "medium-ethernet", feature = "alloc"))]
9258
fn test_new_panic() {
9359
let mut device = Loopback::new(Medium::Ethernet);
9460
let config = Config::new(HardwareAddress::Ip);

src/iface/interface/tests/sixlowpan.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ fn test_echo_request_sixlowpan_128_bytes() {
230230
);
231231

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

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

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

401401
assert_eq!(
402-
device.queue[1],
402+
device.queue.pop_front().unwrap(),
403403
&[
404-
0x41, 0xcc, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
405-
0x0, 0x0, 0x0, 0x0, 0xe0, 0xb4, 0x5, 0x4e, 0xf, 0x6f, 0x72, 0x74, 0x6f, 0x72, 0x2e,
404+
0x41, 0xcc, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x2,
405+
0x2, 0x2, 0x2, 0x2, 0xe0, 0xb4, 0x5, 0x4e, 0xf, 0x6f, 0x72, 0x74, 0x6f, 0x72, 0x2e,
406406
0x20, 0x43, 0x72, 0x61, 0x73, 0x20, 0x62, 0x6c, 0x61, 0x6e, 0x64, 0x69, 0x74, 0x20,
407407
0x74, 0x65, 0x6c, 0x6c, 0x75, 0x73, 0x20, 0x64, 0x69, 0x61, 0x6d, 0x2c, 0x20, 0x76,
408408
0x61, 0x72, 0x69, 0x75, 0x73, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6c,

src/iface/neighbor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl Cache {
146146
}
147147
}
148148

149-
#[cfg(any(feature = "medium-ethernet", feature = "medium-ip"))]
149+
#[cfg(feature = "medium-ethernet")]
150150
#[cfg(test)]
151151
mod test {
152152
use super::*;

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,13 @@ pub mod socket;
168168
pub mod storage;
169169
pub mod time;
170170
pub mod wire;
171+
172+
#[cfg(all(
173+
test,
174+
any(
175+
feature = "medium-ethernet",
176+
feature = "medium-ip",
177+
feature = "medium-ieee802154"
178+
)
179+
))]
180+
mod tests;

0 commit comments

Comments
 (0)