Skip to content

Commit 9779059

Browse files
committed
Add tests for unfiltered raw socket
1 parent a6cd31e commit 9779059

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

src/socket/raw.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,4 +856,94 @@ mod test {
856856
assert!(!socket.accepts(&ipv4_locals::HEADER_REPR));
857857
}
858858
}
859+
860+
fn check_dispatch(socket: &mut Socket<'_>, cx: &mut Context) {
861+
// Check dispatch returns Ok(()) and calls the emit closure
862+
let mut emitted = false;
863+
assert_eq!(
864+
socket.dispatch(cx, |_, _| {
865+
emitted = true;
866+
Ok(())
867+
}),
868+
Ok::<_, ()>(())
869+
);
870+
assert!(emitted);
871+
}
872+
873+
#[rstest]
874+
#[case::ip(Medium::Ip)]
875+
#[case::ethernet(Medium::Ethernet)]
876+
#[cfg(feature = "medium-ethernet")]
877+
#[case::ieee802154(Medium::Ieee802154)]
878+
#[cfg(feature = "medium-ieee802154")]
879+
fn test_unfiltered_sends_all(#[case] medium: Medium) {
880+
// Test a single unfiltered socket can send packets with different IP versions and next
881+
// headers
882+
let mut socket = Socket::new(None, None, buffer(0), buffer(2));
883+
#[cfg(feature = "proto-ipv4")]
884+
{
885+
let (mut iface, _, _) = setup(medium);
886+
let cx = iface.context();
887+
888+
let mut udp_packet = ipv4_locals::PACKET_BYTES;
889+
Ipv4Packet::new_unchecked(&mut udp_packet).set_next_header(IpProtocol::Udp);
890+
891+
assert_eq!(socket.send_slice(&udp_packet), Ok(()));
892+
check_dispatch(&mut socket, cx);
893+
894+
let mut tcp_packet = ipv4_locals::PACKET_BYTES;
895+
Ipv4Packet::new_unchecked(&mut tcp_packet).set_next_header(IpProtocol::Tcp);
896+
897+
assert_eq!(socket.send_slice(&tcp_packet[..]), Ok(()));
898+
check_dispatch(&mut socket, cx);
899+
}
900+
#[cfg(feature = "proto-ipv6")]
901+
{
902+
let (mut iface, _, _) = setup(medium);
903+
let cx = iface.context();
904+
905+
let mut udp_packet = ipv6_locals::PACKET_BYTES;
906+
Ipv6Packet::new_unchecked(&mut udp_packet).set_next_header(IpProtocol::Udp);
907+
908+
assert_eq!(socket.send_slice(&ipv6_locals::PACKET_BYTES), Ok(()));
909+
check_dispatch(&mut socket, cx);
910+
911+
let mut tcp_packet = ipv6_locals::PACKET_BYTES;
912+
Ipv6Packet::new_unchecked(&mut tcp_packet).set_next_header(IpProtocol::Tcp);
913+
914+
assert_eq!(socket.send_slice(&tcp_packet[..]), Ok(()));
915+
check_dispatch(&mut socket, cx);
916+
}
917+
}
918+
919+
#[rstest]
920+
#[case::proto(IpProtocol::Icmp)]
921+
#[case::proto(IpProtocol::Tcp)]
922+
#[case::proto(IpProtocol::Udp)]
923+
fn test_unfiltered_accepts_all(#[case] proto: IpProtocol) {
924+
// Test an unfiltered socket can accept packets with different IP versions and next headers
925+
let socket = Socket::new(None, None, buffer(0), buffer(0));
926+
#[cfg(feature = "proto-ipv4")]
927+
{
928+
let header_repr = IpRepr::Ipv4(Ipv4Repr {
929+
src_addr: Ipv4Address::new(10, 0, 0, 1),
930+
dst_addr: Ipv4Address::new(10, 0, 0, 2),
931+
next_header: proto,
932+
payload_len: 4,
933+
hop_limit: 64,
934+
});
935+
assert!(socket.accepts(&header_repr));
936+
}
937+
#[cfg(feature = "proto-ipv6")]
938+
{
939+
let header_repr = IpRepr::Ipv6(Ipv6Repr {
940+
src_addr: Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1),
941+
dst_addr: Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2),
942+
next_header: proto,
943+
payload_len: 4,
944+
hop_limit: 64,
945+
});
946+
assert!(socket.accepts(&header_repr));
947+
}
948+
}
859949
}

0 commit comments

Comments
 (0)