Skip to content

Commit

Permalink
lol
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Yao <[email protected]>
  • Loading branch information
andr3wy committed May 7, 2024
1 parent 1d906c6 commit 29c0551
Showing 1 changed file with 26 additions and 161 deletions.
187 changes: 26 additions & 161 deletions src/vmm/src/dumbo/pdu/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,170 +402,35 @@ mod tests {
EthIPv4ArpFrame::request_from_bytes(&a[..ETH_IPV4_FRAME_LEN]).unwrap_err(),
ArpError::Operation
);

// TODO: The following test code is way more verbose than it should've been. Make it
// prettier at some point.

// Let's write a valid request.
EthIPv4ArpFrame::write_raw(
&mut a[..ETH_IPV4_FRAME_LEN],
HTYPE_ETHERNET,
ETHERTYPE_IPV4,
MAC_ADDR_LEN,
IPV4_ADDR_LEN,
OPER_REQUEST,
sha,
spa,
tha,
tpa,
)
.unwrap();
EthIPv4ArpFrame::request_from_bytes(&a[..ETH_IPV4_FRAME_LEN]).unwrap();

// Now we start writing invalid requests. We've already tried with an invalid operation.

// Invalid htype.
EthIPv4ArpFrame::write_raw(
&mut a[..ETH_IPV4_FRAME_LEN],
HTYPE_ETHERNET + 1,
ETHERTYPE_IPV4,
MAC_ADDR_LEN,
IPV4_ADDR_LEN,
OPER_REQUEST,
sha,
spa,
tha,
tpa,
)
.unwrap();
assert_eq!(
EthIPv4ArpFrame::request_from_bytes(&a[..ETH_IPV4_FRAME_LEN]).unwrap_err(),
ArpError::HType
);

// Invalid ptype.
EthIPv4ArpFrame::write_raw(
&mut a[..ETH_IPV4_FRAME_LEN],
HTYPE_ETHERNET,
ETHERTYPE_IPV4 + 1,
MAC_ADDR_LEN,
IPV4_ADDR_LEN,
OPER_REQUEST,
sha,
spa,
tha,
tpa,
)
.unwrap();
assert_eq!(
EthIPv4ArpFrame::request_from_bytes(&a[..ETH_IPV4_FRAME_LEN]).unwrap_err(),
ArpError::PType
);

// Invalid hlen.
EthIPv4ArpFrame::write_raw(
&mut a[..ETH_IPV4_FRAME_LEN],
HTYPE_ETHERNET,
ETHERTYPE_IPV4,
MAC_ADDR_LEN + 1,
IPV4_ADDR_LEN,
OPER_REQUEST,
sha,
spa,
tha,
tpa,
)
.unwrap();
assert_eq!(
EthIPv4ArpFrame::request_from_bytes(&a[..ETH_IPV4_FRAME_LEN]).unwrap_err(),
ArpError::HLen
);

// Invalid plen.
EthIPv4ArpFrame::write_raw(
&mut a[..ETH_IPV4_FRAME_LEN],
HTYPE_ETHERNET,
ETHERTYPE_IPV4,
MAC_ADDR_LEN,
IPV4_ADDR_LEN + 1,
OPER_REQUEST,
sha,
spa,
tha,
tpa,
)
.unwrap();
assert_eq!(
EthIPv4ArpFrame::request_from_bytes(&a[..ETH_IPV4_FRAME_LEN]).unwrap_err(),
ArpError::PLen
);
}

#[test]
fn test_speculative() {
let mut a = [0u8; 1000];
let addr = Ipv4Addr::new(1, 2, 3, 4);

assert!(!test_speculative_tpa(a.as_ref(), addr));

{
let mac = MacAddr::from_bytes_unchecked(&[0; 6]);
let mut eth = crate::dumbo::pdu::ethernet::EthernetFrame::write_incomplete(
a.as_mut(),
mac,
mac,
0,

// Various requests
let requests = [
(HTYPE_ETHERNET, ETHERTYPE_IPV4, MAC_ADDR_LEN, IPV4_ADDR_LEN, None), // Valid request
(HTYPE_ETHERNET + 1, ETHERTYPE_IPV4, MAC_ADDR_LEN, IPV4_ADDR_LEN, Some(ArpError::HType)), // Invalid htype
(HTYPE_ETHERNET, ETHERTYPE_IPV4 + 1, MAC_ADDR_LEN, IPV4_ADDR_LEN, Some(ArpError::PType)), // Invalid ptype
(HTYPE_ETHERNET, ETHERTYPE_IPV4, MAC_ADDR_LEN + 1, IPV4_ADDR_LEN, Some(ArpError::HLen)), // Invalid hlen
(HTYPE_ETHERNET, ETHERTYPE_IPV4, MAC_ADDR_LEN, IPV4_ADDR_LEN + 1, Some(ArpError::PLen)), // Invalid plen
];

for (htype, ptype, hlen, plen, err) in requests.iter() {
EthIPv4ArpFrame::write_raw(
&mut a[..ETH_IPV4_FRAME_LEN],
*htype,
*ptype,
*hlen,
*plen,
OPER_REQUEST,
sha,
spa,
tha,
tpa,
)
.unwrap();
let mut arp = EthIPv4ArpFrame::from_bytes_unchecked(eth.inner_mut().payload_mut());
arp.set_tpa(addr);
match err {
None => { EthIPv4ArpFrame::request_from_bytes(&a[..ETH_IPV4_FRAME_LEN]).unwrap(); },
Some(arp_error) => assert_eq!(EthIPv4ArpFrame::request_from_bytes(&a[..ETH_IPV4_FRAME_LEN]).unwrap_err(), *arp_error),
}
}

assert!(test_speculative_tpa(a.as_ref(), addr));

// Let's also test for a very small buffer.
let small = [0u8; 1];
assert!(!test_speculative_tpa(small.as_ref(), addr));
}
} );

// Invalid hlen.
EthIPv4ArpFrame::write_raw(
&mut a[..ETH_IPV4_FRAME_LEN],
HTYPE_ETHERNET,
ETHERTYPE_IPV4,
MAC_ADDR_LEN + 1,
IPV4_ADDR_LEN,
OPER_REQUEST,
sha,
spa,
tha,
tpa,
)
.unwrap();
assert_eq!(
EthIPv4ArpFrame::request_from_bytes(&a[..ETH_IPV4_FRAME_LEN]).unwrap_err(),
ArpError::HLen
);

// Invalid plen.
EthIPv4ArpFrame::write_raw(
&mut a[..ETH_IPV4_FRAME_LEN],
HTYPE_ETHERNET,
ETHERTYPE_IPV4,
MAC_ADDR_LEN,
IPV4_ADDR_LEN + 1,
OPER_REQUEST,
sha,
spa,
tha,
tpa,
)
.unwrap();
assert_eq!(
EthIPv4ArpFrame::request_from_bytes(&a[..ETH_IPV4_FRAME_LEN]).unwrap_err(),
ArpError::PLen
);
}

#[test]
Expand Down

0 comments on commit 29c0551

Please sign in to comment.