Skip to content

Commit

Permalink
tests: use two queues for TestingDevice.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dirbaio committed Sep 16, 2024
1 parent 75d816e commit 86ec8f9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
10 changes: 2 additions & 8 deletions src/iface/interface/tests/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/iface/interface/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions src/iface/interface/tests/sixlowpan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 86ec8f9

Please sign in to comment.