Skip to content

Commit

Permalink
Add IPv4 VLAN test and disable other tests without VLAN
Browse files Browse the repository at this point in the history
  • Loading branch information
DerFetzer committed Feb 6, 2024
1 parent f15988a commit b8315a2
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 9 deletions.
1 change: 1 addition & 0 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ FEATURES_TEST=(
"std,medium-ieee802154,proto-sixlowpan,proto-sixlowpan-fragmentation,socket-udp"
"std,medium-ieee802154,proto-rpl,proto-sixlowpan,proto-sixlowpan-fragmentation,socket-udp"
"std,medium-ip,proto-ipv4,proto-ipv6,socket-tcp,socket-udp"
"std,medium-ethernet,medium-ip,medium-ieee802154,proto-ipv4,proto-ipv6,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"std,medium-ethernet,medium-ip,medium-ieee802154,proto-ipv4,proto-ipv6,proto-vlan,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"std,medium-ieee802154,medium-ip,proto-ipv4,socket-raw"
"std,medium-ethernet,proto-ipv4,proto-ipsec,socket-raw"
Expand Down
91 changes: 88 additions & 3 deletions src/iface/interface/tests/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

#[rstest]
#[case(Medium::Ethernet)]
#[cfg(feature = "medium-ethernet")]
#[cfg(all(feature = "medium-ethernet", not(feature = "proto-vlan")))]
fn test_any_ip_accept_arp(#[case] medium: Medium) {
let mut buffer = [0u8; 64];
#[allow(non_snake_case)]
Expand Down Expand Up @@ -58,6 +58,91 @@ fn test_any_ip_accept_arp(#[case] medium: Medium) {
.is_some());
}

#[rstest]
#[case(Medium::Ethernet)]
#[cfg(feature = "proto-vlan")]
fn test_any_ip_accept_arp_vlan(#[case] medium: Medium) {
let mut buffer = [0u8; 64];
#[allow(non_snake_case)]
fn ETHERNET_FRAME_ARP(buffer: &mut [u8]) -> &[u8] {
let ethernet_repr = EthernetRepr {
src_addr: EthernetAddress::from_bytes(&[0x02, 0x02, 0x02, 0x02, 0x02, 0x03]),
dst_addr: EthernetAddress::from_bytes(&[0x02, 0x02, 0x02, 0x02, 0x02, 0x02]),
ethertype: EthernetProtocol::VlanOuter,
};
let outer_vlan_repr = VlanRepr {
vlan_identifier: 200,
drop_eligible_indicator: false,
priority_code_point: Pcp::Be,
ethertype: EthernetProtocol::VlanInner,
};
let inner_vlan_repr = VlanRepr {
vlan_identifier: 100,
drop_eligible_indicator: false,
priority_code_point: Pcp::Be,
ethertype: EthernetProtocol::Arp,
};
let frame_repr = ArpRepr::EthernetIpv4 {
operation: ArpOperation::Request,
source_hardware_addr: EthernetAddress::from_bytes(&[
0x02, 0x02, 0x02, 0x02, 0x02, 0x03,
]),
source_protocol_addr: Ipv4Address::from_bytes(&[192, 168, 1, 2]),
target_hardware_addr: EthernetAddress::from_bytes(&[
0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
]),
target_protocol_addr: Ipv4Address::from_bytes(&[192, 168, 1, 3]),
};
let mut frame = EthernetFrame::new_unchecked(&mut buffer[..]);
ethernet_repr.emit(&mut frame);

let mut frame = VlanPacket::new_unchecked(&mut buffer[ethernet_repr.buffer_len()..]);
outer_vlan_repr.emit(&mut frame);

let mut frame = VlanPacket::new_unchecked(
&mut buffer[ethernet_repr.buffer_len() + outer_vlan_repr.buffer_len()..],
);
inner_vlan_repr.emit(&mut frame);

let mut frame = ArpPacket::new_unchecked(
&mut buffer[ethernet_repr.buffer_len()
+ outer_vlan_repr.buffer_len()
+ inner_vlan_repr.buffer_len()..],
);
frame_repr.emit(&mut frame);

&buffer[..ethernet_repr.buffer_len()
+ frame_repr.buffer_len()
+ outer_vlan_repr.buffer_len()
+ inner_vlan_repr.buffer_len()]
}

let (mut iface, mut sockets, _) = setup(medium);

assert!(iface
.inner
.process_ethernet(
&mut sockets,
PacketMeta::default(),
ETHERNET_FRAME_ARP(buffer.as_mut()),
&mut iface.fragments,
)
.is_none());

// Accept any IP address
iface.set_any_ip(true);

assert!(iface
.inner
.process_ethernet(
&mut sockets,
PacketMeta::default(),
ETHERNET_FRAME_ARP(buffer.as_mut()),
&mut iface.fragments,
)
.is_some());
}

#[rstest]
#[case(Medium::Ip)]
#[cfg(feature = "medium-ip")]
Expand Down Expand Up @@ -406,7 +491,7 @@ fn test_handle_ipv4_broadcast(#[case] medium: Medium) {

#[rstest]
#[case(Medium::Ethernet)]
#[cfg(feature = "medium-ethernet")]
#[cfg(all(feature = "medium-ethernet", not(feature = "proto-vlan")))]
fn test_handle_valid_arp_request(#[case] medium: Medium) {
let (mut iface, mut sockets, _device) = setup(medium);

Expand Down Expand Up @@ -512,7 +597,7 @@ fn test_handle_other_arp_request(#[case] medium: Medium) {

#[rstest]
#[case(Medium::Ethernet)]
#[cfg(feature = "medium-ethernet")]
#[cfg(all(feature = "medium-ethernet", not(feature = "proto-vlan")))]
fn test_arp_flush_after_update_ip(#[case] medium: Medium) {
let (mut iface, mut sockets, _device) = setup(medium);

Expand Down
2 changes: 1 addition & 1 deletion src/iface/interface/tests/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ fn ndsic_neighbor_advertisement_ieee802154(#[case] medium: Medium) {

#[rstest]
#[case(Medium::Ethernet)]
#[cfg(feature = "medium-ethernet")]
#[cfg(all(feature = "medium-ethernet", not(feature = "proto-vlan")))]
fn test_handle_valid_ndisc_request(#[case] medium: Medium) {
let (mut iface, mut sockets, _device) = setup(medium);

Expand Down
2 changes: 1 addition & 1 deletion src/wire/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub use self::dhcpv4::{
};

#[cfg(feature = "proto-vlan")]
pub use self::vlan::{Packet as VlanPacket, VlanConfig};
pub use self::vlan::{Packet as VlanPacket, Pcp, Repr as VlanRepr, VlanConfig};

#[cfg(feature = "proto-dns")]
pub use self::dns::{
Expand Down
8 changes: 4 additions & 4 deletions src/wire/vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ impl<T: AsRef<[u8]>> AsRef<[u8]> for Packet<T> {
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Repr {
vlan_identifier: u16,
drop_eligible_indicator: bool,
priority_code_point: Pcp,
ethertype: EtherType,
pub vlan_identifier: u16,
pub drop_eligible_indicator: bool,
pub priority_code_point: Pcp,
pub ethertype: EtherType,
}

impl Repr {
Expand Down

0 comments on commit b8315a2

Please sign in to comment.