mirror of
https://github.com/smoltcp-rs/smoltcp.git
synced 2025-09-29 22:02:05 +00:00
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.
This commit is contained in:
parent
469dccdbae
commit
3acd511179
@ -721,20 +721,14 @@ fn test_handle_igmp(#[case] medium: Medium) {
|
||||
}
|
||||
|
||||
// General query
|
||||
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,
|
||||
0x00, 0x00, 0x02, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00,
|
||||
];
|
||||
{
|
||||
// Transmit GENERAL_QUERY_BYTES into loopback
|
||||
let tx_token = device.transmit(timestamp).unwrap();
|
||||
tx_token.consume(GENERAL_QUERY_BYTES.len(), |buffer| {
|
||||
buffer.copy_from_slice(GENERAL_QUERY_BYTES);
|
||||
});
|
||||
}
|
||||
device.rx_queue.push_back(GENERAL_QUERY_BYTES.to_vec());
|
||||
|
||||
// Trigger processing until all packets received through the
|
||||
// loopback have been processed, including responses to
|
||||
// GENERAL_QUERY_BYTES. Therefore `recv_all()` would return 0
|
||||
|
@ -30,10 +30,8 @@ fn fill_slice(s: &mut [u8], val: u8) {
|
||||
#[allow(unused)]
|
||||
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| {
|
||||
pkts.push(pkt.to_vec());
|
||||
});
|
||||
while let Some(pkt) = device.tx_queue.pop_front() {
|
||||
pkts.push(pkt)
|
||||
}
|
||||
pkts
|
||||
}
|
||||
|
@ -244,7 +244,7 @@ fn test_echo_request_sixlowpan_128_bytes() {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
device.queue.pop_front().unwrap(),
|
||||
device.tx_queue.pop_front().unwrap(),
|
||||
&[
|
||||
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,
|
||||
@ -261,7 +261,7 @@ fn test_echo_request_sixlowpan_128_bytes() {
|
||||
iface.poll(Instant::now(), &mut device, &mut sockets);
|
||||
|
||||
assert_eq!(
|
||||
device.queue.pop_front().unwrap(),
|
||||
device.tx_queue.pop_front().unwrap(),
|
||||
&[
|
||||
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,
|
||||
@ -415,7 +415,7 @@ 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.pop_front().unwrap(),
|
||||
device.tx_queue.pop_front().unwrap(),
|
||||
&[
|
||||
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,
|
||||
@ -430,7 +430,7 @@ In at rhoncus tortor. Cras blandit tellus diam, varius vestibulum nibh commodo n
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
device.queue.pop_front().unwrap(),
|
||||
device.tx_queue.pop_front().unwrap(),
|
||||
&[
|
||||
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,
|
||||
|
31
src/tests.rs
31
src/tests.rs
@ -1,4 +1,8 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::iface::*;
|
||||
use crate::phy::{self, Device, DeviceCapabilities, Medium};
|
||||
use crate::time::Instant;
|
||||
use crate::wire::*;
|
||||
|
||||
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
|
||||
(iface, SocketSet::new(vec![]), device)
|
||||
}
|
||||
|
||||
use heapless::Deque;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::phy::{self, Device, DeviceCapabilities, Medium};
|
||||
use crate::time::Instant;
|
||||
|
||||
/// A testing device.
|
||||
#[derive(Debug)]
|
||||
pub struct TestingDevice {
|
||||
pub(crate) queue: Deque<Vec<u8, 1514>, 4>,
|
||||
pub(crate) tx_queue: VecDeque<Vec<u8>>,
|
||||
pub(crate) rx_queue: VecDeque<Vec<u8>>,
|
||||
max_transmission_unit: usize,
|
||||
medium: Medium,
|
||||
}
|
||||
@ -71,7 +70,8 @@ impl TestingDevice {
|
||||
/// in FIFO order.
|
||||
pub fn new(medium: Medium) -> Self {
|
||||
TestingDevice {
|
||||
queue: Deque::new(),
|
||||
tx_queue: VecDeque::new(),
|
||||
rx_queue: VecDeque::new(),
|
||||
max_transmission_unit: match medium {
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
Medium::Ethernet => 1514,
|
||||
@ -98,10 +98,10 @@ impl Device for TestingDevice {
|
||||
}
|
||||
|
||||
fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
|
||||
self.queue.pop_front().map(move |buffer| {
|
||||
self.rx_queue.pop_front().map(move |buffer| {
|
||||
let rx = RxToken { buffer };
|
||||
let tx = TxToken {
|
||||
queue: &mut self.queue,
|
||||
queue: &mut self.tx_queue,
|
||||
};
|
||||
(rx, tx)
|
||||
})
|
||||
@ -109,14 +109,14 @@ impl Device for TestingDevice {
|
||||
|
||||
fn transmit(&mut self, _timestamp: Instant) -> Option<Self::TxToken<'_>> {
|
||||
Some(TxToken {
|
||||
queue: &mut self.queue,
|
||||
queue: &mut self.tx_queue,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct RxToken {
|
||||
buffer: Vec<u8, 1514>,
|
||||
buffer: Vec<u8>,
|
||||
}
|
||||
|
||||
impl phy::RxToken for RxToken {
|
||||
@ -131,7 +131,7 @@ impl phy::RxToken for RxToken {
|
||||
#[doc(hidden)]
|
||||
#[derive(Debug)]
|
||||
pub struct TxToken<'a> {
|
||||
queue: &'a mut Deque<Vec<u8, 1514>, 4>,
|
||||
queue: &'a mut VecDeque<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl<'a> phy::TxToken for TxToken<'a> {
|
||||
@ -139,10 +139,9 @@ impl<'a> phy::TxToken for TxToken<'a> {
|
||||
where
|
||||
F: FnOnce(&mut [u8]) -> R,
|
||||
{
|
||||
let mut buffer = Vec::new();
|
||||
buffer.resize(len, 0).unwrap();
|
||||
let mut buffer = vec![0; len];
|
||||
let result = f(&mut buffer);
|
||||
self.queue.push_back(buffer).unwrap();
|
||||
self.queue.push_back(buffer);
|
||||
result
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user