Skip to content

Commit

Permalink
Bring back set_max_frame_length as a function on Codecs
Browse files Browse the repository at this point in the history
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Aug 24, 2018
1 parent e11ea0a commit d6cd03b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 62 deletions.
18 changes: 18 additions & 0 deletions src/length_delimited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ impl Codec {
}
}

/// Returns the current max frame setting
///
/// This is the largest size this codec will accept from the wire. Larger
/// frames will be rejected.
pub fn max_frame_length(&self) -> usize {
self.builder.max_frame_len
}

/// Updates the max frame setting.
///
/// The change takes effect the next time a frame is decoded. In other
/// words, if a frame is currently in process of being decoded with a frame
/// size greater than `val` but less than the max frame length in effect
/// before calling this function, then the frame will be allowed.
pub fn set_max_frame_length(&mut self, val: usize) {
self.builder.max_frame_length(val);
}

fn decode_head(&mut self, src: &mut BytesMut) -> io::Result<Option<usize>> {
let head_len = self.builder.num_head_bytes();
let field_len = self.builder.length_field_len;
Expand Down
124 changes: 62 additions & 62 deletions tests/length_delimited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,34 +204,34 @@ fn read_max_frame_len() {
assert_eq!(io.poll().unwrap_err().kind(), io::ErrorKind::InvalidData);
}

// #[test]
// fn read_update_max_frame_len_at_rest() {
// let mut io = length_delimited::Builder::new()
// .new_read(mock! {
// Ok(b"\x00\x00\x00\x09abcdefghi"[..].into()),
// Ok(b"\x00\x00\x00\x09abcdefghi"[..].into()),
// });

// assert_eq!(io.poll().unwrap(), Ready(Some(b"abcdefghi"[..].into())));
// io.set_max_frame_length(5);
// assert_eq!(io.poll().unwrap_err().kind(), io::ErrorKind::InvalidData);
// }

// #[test]
// fn read_update_max_frame_len_in_flight() {
// let mut io = length_delimited::Builder::new()
// .new_read(mock! {
// Ok(b"\x00\x00\x00\x09abcd"[..].into()),
// Err(would_block()),
// Ok(b"efghi"[..].into()),
// Ok(b"\x00\x00\x00\x09abcdefghi"[..].into()),
// });

// assert_eq!(io.poll().unwrap(), NotReady);
// io.set_max_frame_length(5);
// assert_eq!(io.poll().unwrap(), Ready(Some(b"abcdefghi"[..].into())));
// assert_eq!(io.poll().unwrap_err().kind(), io::ErrorKind::InvalidData);
// }
#[test]
fn read_update_max_frame_len_at_rest() {
let mut io = length_delimited::Builder::new()
.new_read(mock! {
Ok(b"\x00\x00\x00\x09abcdefghi"[..].into()),
Ok(b"\x00\x00\x00\x09abcdefghi"[..].into()),
});

assert_eq!(io.poll().unwrap(), Ready(Some(b"abcdefghi"[..].into())));
io.decoder_mut().set_max_frame_length(5);
assert_eq!(io.poll().unwrap_err().kind(), io::ErrorKind::InvalidData);
}

#[test]
fn read_update_max_frame_len_in_flight() {
let mut io = length_delimited::Builder::new()
.new_read(mock! {
Ok(b"\x00\x00\x00\x09abcd"[..].into()),
Err(would_block()),
Ok(b"efghi"[..].into()),
Ok(b"\x00\x00\x00\x09abcdefghi"[..].into()),
});

assert_eq!(io.poll().unwrap(), NotReady);
io.decoder_mut().set_max_frame_length(5);
assert_eq!(io.poll().unwrap(), Ready(Some(b"abcdefghi"[..].into())));
assert_eq!(io.poll().unwrap_err().kind(), io::ErrorKind::InvalidData);
}

#[test]
fn read_one_byte_length_field() {
Expand Down Expand Up @@ -438,40 +438,40 @@ fn write_max_frame_len() {
assert!(io.get_ref().calls.is_empty());
}

// #[test]
// fn write_update_max_frame_len_at_rest() {
// let mut io = length_delimited::Builder::new()
// .new_write(mock! {
// Ok(b"\x00\x00\x00\x06"[..].into()),
// Ok(b"abcdef"[..].into()),
// Ok(Flush),
// });

// assert!(io.start_send(Bytes::from("abcdef").unwrap().is_ready());
// assert!(io.poll_complete().unwrap().is_ready());
// io.set_max_frame_length(5);
// assert_eq!(io.start_send(Bytes::from("abcdef").unwrap_err().kind(), io::ErrorKind::InvalidInput);
// assert!(io.get_ref().calls.is_empty());
// }

// #[test]
// fn write_update_max_frame_len_in_flight() {
// let mut io = length_delimited::Builder::new()
// .new_write(mock! {
// Ok(b"\x00\x00\x00\x06"[..].into()),
// Ok(b"ab"[..].into()),
// Err(would_block()),
// Ok(b"cdef"[..].into()),
// Ok(Flush),
// });

// assert!(io.start_send(Bytes::from("abcdef").unwrap().is_ready());
// assert!(!io.poll_complete().unwrap().is_ready());
// io.set_max_frame_length(5);
// assert!(io.poll_complete().unwrap().is_ready());
// assert_eq!(io.start_send(Bytes::from("abcdef").unwrap_err().kind(), io::ErrorKind::InvalidInput);
// assert!(io.get_ref().calls.is_empty());
// }
#[test]
fn write_update_max_frame_len_at_rest() {
let mut io = length_delimited::Builder::new()
.new_write(mock! {
Ok(b"\x00\x00\x00\x06"[..].into()),
Ok(b"abcdef"[..].into()),
Ok(Flush),
});

assert!(io.start_send(Bytes::from("abcdef")).unwrap().is_ready());
assert!(io.poll_complete().unwrap().is_ready());
io.encoder_mut().set_max_frame_length(5);
assert_eq!(io.start_send(Bytes::from("abcdef")).unwrap_err().kind(), io::ErrorKind::InvalidInput);
assert!(io.get_ref().calls.is_empty());
}

#[test]
fn write_update_max_frame_len_in_flight() {
let mut io = length_delimited::Builder::new()
.new_write(mock! {
Ok(b"\x00\x00\x00\x06"[..].into()),
Ok(b"ab"[..].into()),
Err(would_block()),
Ok(b"cdef"[..].into()),
Ok(Flush),
});

assert!(io.start_send(Bytes::from("abcdef")).unwrap().is_ready());
assert!(!io.poll_complete().unwrap().is_ready());
io.encoder_mut().set_max_frame_length(5);
assert!(io.poll_complete().unwrap().is_ready());
assert_eq!(io.start_send(Bytes::from("abcdef")).unwrap_err().kind(), io::ErrorKind::InvalidInput);
assert!(io.get_ref().calls.is_empty());
}

// ===== Test utils =====

Expand Down

0 comments on commit d6cd03b

Please sign in to comment.