This fixes #23 don't put 0 size read result into buffer. This will prevent it from filling up memory when reading responses

This commit is contained in:
Matt McCoy 2017-06-20 19:14:00 -04:00
parent 08c2b6847b
commit fe398fb531
2 changed files with 35 additions and 5 deletions

View file

@ -443,8 +443,10 @@ impl<T: Read+Write> Client<T> {
let mut line_buffer: Vec<u8> = Vec::new(); let mut line_buffer: Vec<u8> = Vec::new();
while line_buffer.len() < 2 || (line_buffer[line_buffer.len()-1] != LF && line_buffer[line_buffer.len()-2] != CR) { while line_buffer.len() < 2 || (line_buffer[line_buffer.len()-1] != LF && line_buffer[line_buffer.len()-2] != CR) {
let byte_buffer: &mut [u8] = &mut [0]; let byte_buffer: &mut [u8] = &mut [0];
try!(self.stream.read(byte_buffer)); let n = try!(self.stream.read(byte_buffer));
line_buffer.push(byte_buffer[0]); if n > 0 {
line_buffer.push(byte_buffer[0]);
}
} }
if self.debug { if self.debug {
@ -490,6 +492,7 @@ mod tests {
assert!(expected_response == actual_response, "expected response doesn't equal actual"); assert!(expected_response == actual_response, "expected response doesn't equal actual");
} }
#[test] #[test]
fn read_greeting() { fn read_greeting() {
let greeting = "* OK Dovecot ready.\r\n"; let greeting = "* OK Dovecot ready.\r\n";
@ -498,6 +501,16 @@ mod tests {
client.read_greeting().unwrap(); client.read_greeting().unwrap();
} }
#[test]
fn readline_delay_read() {
let greeting = "* OK Dovecot ready.\r\n";
let expected_response: String = greeting.to_string();
let mock_stream = MockStream::new_read_delay(greeting.as_bytes().to_vec());
let mut client = Client::new(mock_stream);
let actual_response = String::from_utf8(client.readline().unwrap()).unwrap();
assert_eq!(expected_response, actual_response);
}
#[test] #[test]
#[should_panic] #[should_panic]
fn readline_err() { fn readline_err() {

View file

@ -5,7 +5,8 @@ pub struct MockStream {
read_buf: Vec<u8>, read_buf: Vec<u8>,
read_pos: usize, read_pos: usize,
pub written_buf: Vec<u8>, pub written_buf: Vec<u8>,
err_on_read: bool err_on_read: bool,
read_delay: usize
} }
impl MockStream { impl MockStream {
@ -14,7 +15,8 @@ impl MockStream {
read_buf: read_buf, read_buf: read_buf,
read_pos: 0, read_pos: 0,
written_buf: Vec::new(), written_buf: Vec::new(),
err_on_read: false err_on_read: false,
read_delay: 0
} }
} }
@ -23,13 +25,28 @@ impl MockStream {
read_buf: Vec::new(), read_buf: Vec::new(),
read_pos: 0, read_pos: 0,
written_buf: Vec::new(), written_buf: Vec::new(),
err_on_read: true err_on_read: true,
read_delay: 0
}
}
pub fn new_read_delay(read_buf: Vec<u8>) -> MockStream {
MockStream{
read_buf: read_buf,
read_pos: 0,
written_buf: Vec::new(),
err_on_read: false,
read_delay: 1,
} }
} }
} }
impl Read for MockStream { impl Read for MockStream {
fn read(&mut self, buf: &mut[u8]) -> Result<usize> { fn read(&mut self, buf: &mut[u8]) -> Result<usize> {
if self.read_delay > 0 {
self.read_delay -= 1;
return Ok(0)
}
if self.err_on_read { if self.err_on_read {
return Err(Error::new(ErrorKind::Other, "MockStream Error")) return Err(Error::new(ErrorKind::Other, "MockStream Error"))
} }