Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gleb Pomykalov committed Apr 24, 2020
1 parent c3acf02 commit bec8fb4
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ mod recvfrom {

const MSG: &'static [u8] = b"Hello, World!";

fn sendrecv<Fs, Fr>(rsock: RawFd, ssock: RawFd, f_send: Fs, f_recv: Fr) -> Option<SockAddr>
fn sendrecv<Fs, Fr>(rsock: RawFd, ssock: RawFd, f_send: Fs, mut f_recv: Fr) -> Option<SockAddr>
where
Fs: Fn(RawFd, &[u8], MsgFlags) -> Result<usize> + Send + 'static,
Fr: Fn(usize, Option<SockAddr>),
Fr: FnMut(usize, Option<SockAddr>),
{
let mut buf: [u8; 13] = [0u8; 13];
let mut l = 0;
Expand Down Expand Up @@ -240,6 +240,11 @@ mod recvfrom {
pub fn gso() {
require_kernel_version!(udp_offload::gso, ">= 4.18");

// In this test, we send the data and provide a GSO segment size.
// Since we are sending the buffer of size 13, six UDP packets
// with size 2 and two UDP packet with size 1 will be sent.
let segment_size: u16 = 2;

let std_sa = SocketAddr::from_str("127.0.0.1:6791").unwrap();
let inet_addr = InetAddr::from_std(&std_sa);
let sock_addr = SockAddr::new_inet(inet_addr);
Expand All @@ -249,9 +254,8 @@ mod recvfrom {
None
).unwrap();

let segment_size = 2;

setsockopt(rsock, UdpGsoSegment, &segment_size).expect("setsockopt UDP_SEGMENT failed");
setsockopt(rsock, UdpGsoSegment, &(segment_size as _))
.expect("setsockopt UDP_SEGMENT failed");

bind(rsock, &sock_addr).unwrap();
let ssock = socket(
Expand All @@ -261,48 +265,45 @@ mod recvfrom {
None,
).expect("send socket failed");

let from = sendrecv(rsock, ssock, move |s, m, flags| {
let mut num_packets_received: i32 = 0;

sendrecv(rsock, ssock, move |s, m, flags| {
let iov = [IoVec::from_slice(m)];
let cmsg = ControlMessage::UdpGsoSegments(&2);
let cmsg = ControlMessage::UdpGsoSegments(&segment_size);
sendmsg(s, &iov, &[cmsg], flags, Some(&sock_addr))
}, |len, _| {
assert!(len <= segment_size as usize);
}, {
let num_packets_received_ref = &mut num_packets_received;

move |len, _| {
// check that we receive UDP packets with payload size
// less or equal to segment size
assert!(len <= segment_size as usize);
*num_packets_received_ref += 1;
}
});
// UDP sockets should set the from address
assert_eq!(AddressFamily::Inet, from.unwrap().family());

// Buffer size is 13, we will receive six packets of size 2,
// and one packet of size 1.
assert_eq!(7, num_packets_received);
}

#[test]
pub fn gro() {
require_kernel_version!(udp_offload::gro, ">= 5.3");

// It's hard to guarantee receiving GRO packets. Just checking
// that `setsockopt` doesn't fail with error

let std_sa = SocketAddr::from_str("127.0.0.1:6792").unwrap();
let inet_addr = InetAddr::from_std(&std_sa);
let sock_addr = SockAddr::new_inet(inet_addr);
let rsock = socket(AddressFamily::Inet,
SockType::Datagram,
SockFlag::empty(),
None
).unwrap();

setsockopt(rsock, UdpGroSegment, &true).expect("setsockopt UDP_GRO failed");

bind(rsock, &sock_addr).unwrap();
let ssock = socket(
AddressFamily::Inet,
SockType::Datagram,
SockFlag::empty(),
None,
).expect("send socket failed");

let from = sendrecv(rsock, ssock, move |s, m, flags| {
let iov = [IoVec::from_slice(m)];
let cmsg = ControlMessage::UdpGsoSegments(&2);
sendmsg(s, &iov, &[cmsg], flags, Some(&sock_addr))
}, |_, _| {});
).unwrap();I

// UDP sockets should set the from address
assert_eq!(AddressFamily::Inet, from.unwrap().family());
setsockopt(rsock, UdpGroSegment, &true)
.expect("setsockopt UDP_GRO failed");
}
}
}
Expand Down

0 comments on commit bec8fb4

Please sign in to comment.