Skip to content

Commit

Permalink
Add unit tests for tcp socket peek_slice
Browse files Browse the repository at this point in the history
  • Loading branch information
hunhoffe committed Aug 23, 2023
1 parent 86d4cf1 commit 953ee32
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,10 @@ impl<'a> Socket<'a> {
let assembler_was_empty = self.assembler.is_empty();

// Try adding payload octets to the assembler.
let Ok(contig_len) = self.assembler.add_then_remove_front(payload_offset, payload_len) else {
let Ok(contig_len) = self
.assembler
.add_then_remove_front(payload_offset, payload_len)
else {
net_debug!(
"assembler: too many holes to add {} octets at offset {}",
payload_len,
Expand Down Expand Up @@ -3664,6 +3667,58 @@ mod test {
assert_eq!(s.rx_buffer.dequeue_many(6), &b"abcdef"[..]);
}

#[test]
fn test_peek_slice() {
const BUF_SIZE: usize = 10;

let send_buf = b"0123456";

let mut s = socket_established_with_buffer_sizes(BUF_SIZE, BUF_SIZE);

// Populate the recv buffer
send!(
s,
TcpRepr {
seq_number: REMOTE_SEQ + 1,
ack_number: Some(LOCAL_SEQ + 1),
payload: &send_buf[..],
..SEND_TEMPL
}
);

// Peek into the recv buffer
let mut peeked_buf = [0u8; BUF_SIZE];
let actually_peeked = s.peek_slice(&mut peeked_buf[..]).unwrap();
let mut recv_buf = [0u8; BUF_SIZE];
let actually_recvd = s.recv_slice(&mut recv_buf[..]).unwrap();
assert_eq!(
&mut peeked_buf[..actually_peeked],
&mut recv_buf[..actually_recvd]
);
}

#[test]
fn test_peek_slice_buffer_wrap() {
const BUF_SIZE: usize = 10;

let send_buf = b"0123456789";

let mut s = socket_established_with_buffer_sizes(BUF_SIZE, BUF_SIZE);

let _ = s.rx_buffer.enqueue_slice(&send_buf[..8]);
let _ = s.rx_buffer.dequeue_many(6);
let _ = s.rx_buffer.enqueue_slice(&send_buf[..5]);

let mut peeked_buf = [0u8; BUF_SIZE];
let actually_peeked = s.peek_slice(&mut peeked_buf[..]).unwrap();
let mut recv_buf = [0u8; BUF_SIZE];
let actually_recvd = s.recv_slice(&mut recv_buf[..]).unwrap();
assert_eq!(
&mut peeked_buf[..actually_peeked],
&mut recv_buf[..actually_recvd]
);
}

fn setup_rfc2018_cases() -> (TestSocket, Vec<u8>) {
// This is a utility function used by the tests for RFC 2018 cases. It configures a socket
// in a particular way suitable for those cases.
Expand Down

0 comments on commit 953ee32

Please sign in to comment.