Merge branch 'Dr-Emann-crc_zero_sized_read'
This commit is contained in:
commit
3e5a8e6f98
1 changed files with 49 additions and 1 deletions
50
src/crc32.rs
50
src/crc32.rs
|
@ -42,7 +42,7 @@ impl<R: Read> Read for Crc32Reader<R>
|
|||
{
|
||||
let count = match self.inner.read(buf)
|
||||
{
|
||||
Ok(0) if !self.check_matches() => { return Err(io::Error::new(io::ErrorKind::Other, "Invalid checksum")) },
|
||||
Ok(0) if !buf.is_empty() && !self.check_matches() => { return Err(io::Error::new(io::ErrorKind::Other, "Invalid checksum")) },
|
||||
Ok(n) => n,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
|
@ -50,3 +50,51 @@ impl<R: Read> Read for Crc32Reader<R>
|
|||
Ok(count)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test
|
||||
{
|
||||
use super::*;
|
||||
use std::io::Read;
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn test_empty_reader()
|
||||
{
|
||||
let data: &[u8] = b"";
|
||||
let mut buf = [0; 1];
|
||||
|
||||
let mut reader = Crc32Reader::new(data, 0);
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
||||
|
||||
let mut reader = Crc32Reader::new(data, 1);
|
||||
assert!(reader.read(&mut buf).unwrap_err().to_string().contains("Invalid checksum"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_by_byte()
|
||||
{
|
||||
let data: &[u8] = b"1234";
|
||||
let mut buf = [0; 1];
|
||||
|
||||
let mut reader = Crc32Reader::new(data, 0x9be3e0a3);
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), 1);
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), 1);
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), 1);
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), 1);
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
||||
// Can keep reading 0 bytes after the end
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero_read()
|
||||
{
|
||||
let data: &[u8] = b"1234";
|
||||
let mut buf = [0; 5];
|
||||
|
||||
let mut reader = Crc32Reader::new(data, 0x9be3e0a3);
|
||||
assert_eq!(reader.read(&mut buf[..0]).unwrap(), 0);
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), 4);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue