Skip to content

Commit 3acd511

Browse files
committed
tests: use two queues for TestingDevice.
Currently TestingDevice is a loopback device, which causes problems if the iface transmits a packet then receives. The packet gets looped back to the interface instead of being handled by the test code. This commit changes TestingDevice to behave more like a "virtual wire" that communicates the interface with the testing code, with one queue for each direction.
1 parent 469dccd commit 3acd511

File tree

4 files changed

+23
-32
lines changed

4 files changed

+23
-32
lines changed

src/iface/interface/tests/ipv4.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -721,20 +721,14 @@ fn test_handle_igmp(#[case] medium: Medium) {
721721
}
722722

723723
// General query
724-
let timestamp = Instant::ZERO;
725724
const GENERAL_QUERY_BYTES: &[u8] = &[
726725
0x46, 0xc0, 0x00, 0x24, 0xed, 0xb4, 0x00, 0x00, 0x01, 0x02, 0x47, 0x43, 0xac, 0x16, 0x63,
727726
0x04, 0xe0, 0x00, 0x00, 0x01, 0x94, 0x04, 0x00, 0x00, 0x11, 0x64, 0xec, 0x8f, 0x00, 0x00,
728727
0x00, 0x00, 0x02, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
729728
0x00,
730729
];
731-
{
732-
// Transmit GENERAL_QUERY_BYTES into loopback
733-
let tx_token = device.transmit(timestamp).unwrap();
734-
tx_token.consume(GENERAL_QUERY_BYTES.len(), |buffer| {
735-
buffer.copy_from_slice(GENERAL_QUERY_BYTES);
736-
});
737-
}
730+
device.rx_queue.push_back(GENERAL_QUERY_BYTES.to_vec());
731+
738732
// Trigger processing until all packets received through the
739733
// loopback have been processed, including responses to
740734
// GENERAL_QUERY_BYTES. Therefore `recv_all()` would return 0

src/iface/interface/tests/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ fn fill_slice(s: &mut [u8], val: u8) {
3030
#[allow(unused)]
3131
fn recv_all(device: &mut crate::tests::TestingDevice, timestamp: Instant) -> Vec<Vec<u8>> {
3232
let mut pkts = Vec::new();
33-
while let Some((rx, _tx)) = device.receive(timestamp) {
34-
rx.consume(|pkt| {
35-
pkts.push(pkt.to_vec());
36-
});
33+
while let Some(pkt) = device.tx_queue.pop_front() {
34+
pkts.push(pkt)
3735
}
3836
pkts
3937
}

src/iface/interface/tests/sixlowpan.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ fn test_echo_request_sixlowpan_128_bytes() {
244244
);
245245

246246
assert_eq!(
247-
device.queue.pop_front().unwrap(),
247+
device.tx_queue.pop_front().unwrap(),
248248
&[
249249
0x41, 0xcc, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x2,
250250
0x2, 0x2, 0x2, 0x2, 0xc0, 0xb0, 0x5, 0x4e, 0x7a, 0x11, 0x3a, 0x92, 0xfc, 0x48, 0xc2,
@@ -261,7 +261,7 @@ fn test_echo_request_sixlowpan_128_bytes() {
261261
iface.poll(Instant::now(), &mut device, &mut sockets);
262262

263263
assert_eq!(
264-
device.queue.pop_front().unwrap(),
264+
device.tx_queue.pop_front().unwrap(),
265265
&[
266266
0x41, 0xcc, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x2,
267267
0x2, 0x2, 0x2, 0x2, 0xe0, 0xb0, 0x5, 0x4e, 0xf, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
@@ -415,7 +415,7 @@ In at rhoncus tortor. Cras blandit tellus diam, varius vestibulum nibh commodo n
415415
iface.poll(Instant::now(), &mut device, &mut sockets);
416416

417417
assert_eq!(
418-
device.queue.pop_front().unwrap(),
418+
device.tx_queue.pop_front().unwrap(),
419419
&[
420420
0x41, 0xcc, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x2,
421421
0x2, 0x2, 0x2, 0x2, 0xc0, 0xb4, 0x5, 0x4e, 0x7e, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
@@ -430,7 +430,7 @@ In at rhoncus tortor. Cras blandit tellus diam, varius vestibulum nibh commodo n
430430
);
431431

432432
assert_eq!(
433-
device.queue.pop_front().unwrap(),
433+
device.tx_queue.pop_front().unwrap(),
434434
&[
435435
0x41, 0xcc, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x2,
436436
0x2, 0x2, 0x2, 0x2, 0xe0, 0xb4, 0x5, 0x4e, 0xf, 0x6f, 0x72, 0x74, 0x6f, 0x72, 0x2e,

src/tests.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
use std::collections::VecDeque;
2+
13
use crate::iface::*;
4+
use crate::phy::{self, Device, DeviceCapabilities, Medium};
5+
use crate::time::Instant;
26
use crate::wire::*;
37

48
pub(crate) fn setup<'a>(medium: Medium) -> (Interface, SocketSet<'a>, TestingDevice) {
@@ -49,16 +53,11 @@ pub(crate) fn setup<'a>(medium: Medium) -> (Interface, SocketSet<'a>, TestingDev
4953
(iface, SocketSet::new(vec![]), device)
5054
}
5155

52-
use heapless::Deque;
53-
use heapless::Vec;
54-
55-
use crate::phy::{self, Device, DeviceCapabilities, Medium};
56-
use crate::time::Instant;
57-
5856
/// A testing device.
5957
#[derive(Debug)]
6058
pub struct TestingDevice {
61-
pub(crate) queue: Deque<Vec<u8, 1514>, 4>,
59+
pub(crate) tx_queue: VecDeque<Vec<u8>>,
60+
pub(crate) rx_queue: VecDeque<Vec<u8>>,
6261
max_transmission_unit: usize,
6362
medium: Medium,
6463
}
@@ -71,7 +70,8 @@ impl TestingDevice {
7170
/// in FIFO order.
7271
pub fn new(medium: Medium) -> Self {
7372
TestingDevice {
74-
queue: Deque::new(),
73+
tx_queue: VecDeque::new(),
74+
rx_queue: VecDeque::new(),
7575
max_transmission_unit: match medium {
7676
#[cfg(feature = "medium-ethernet")]
7777
Medium::Ethernet => 1514,
@@ -98,25 +98,25 @@ impl Device for TestingDevice {
9898
}
9999

100100
fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
101-
self.queue.pop_front().map(move |buffer| {
101+
self.rx_queue.pop_front().map(move |buffer| {
102102
let rx = RxToken { buffer };
103103
let tx = TxToken {
104-
queue: &mut self.queue,
104+
queue: &mut self.tx_queue,
105105
};
106106
(rx, tx)
107107
})
108108
}
109109

110110
fn transmit(&mut self, _timestamp: Instant) -> Option<Self::TxToken<'_>> {
111111
Some(TxToken {
112-
queue: &mut self.queue,
112+
queue: &mut self.tx_queue,
113113
})
114114
}
115115
}
116116

117117
#[doc(hidden)]
118118
pub struct RxToken {
119-
buffer: Vec<u8, 1514>,
119+
buffer: Vec<u8>,
120120
}
121121

122122
impl phy::RxToken for RxToken {
@@ -131,18 +131,17 @@ impl phy::RxToken for RxToken {
131131
#[doc(hidden)]
132132
#[derive(Debug)]
133133
pub struct TxToken<'a> {
134-
queue: &'a mut Deque<Vec<u8, 1514>, 4>,
134+
queue: &'a mut VecDeque<Vec<u8>>,
135135
}
136136

137137
impl<'a> phy::TxToken for TxToken<'a> {
138138
fn consume<R, F>(self, len: usize, f: F) -> R
139139
where
140140
F: FnOnce(&mut [u8]) -> R,
141141
{
142-
let mut buffer = Vec::new();
143-
buffer.resize(len, 0).unwrap();
142+
let mut buffer = vec![0; len];
144143
let result = f(&mut buffer);
145-
self.queue.push_back(buffer).unwrap();
144+
self.queue.push_back(buffer);
146145
result
147146
}
148147
}

0 commit comments

Comments
 (0)