diff --git a/noodles-bam/src/io/reader/header.rs b/noodles-bam/src/io/reader/header.rs index e8dc7470a..3896a2668 100644 --- a/noodles-bam/src/io/reader/header.rs +++ b/noodles-bam/src/io/reader/header.rs @@ -23,9 +23,7 @@ where } fn read_magic_number(&mut self) -> io::Result<[u8; MAGIC_NUMBER.len()]> { - let mut buf = [0; MAGIC_NUMBER.len()]; - self.inner.read_exact(&mut buf)?; - Ok(buf) + read_magic_number(self.inner) } fn raw_sam_header_reader(&mut self) -> io::Result> { @@ -50,7 +48,9 @@ fn read_header_inner(reader: &mut Reader) -> io::Result where R: Read, { - read_magic_number(reader)?; + reader + .read_magic_number() + .and_then(magic_number::validate)?; let mut raw_sam_header_reader = reader.raw_sam_header_reader()?; let mut header = read_sam_header(&mut raw_sam_header_reader)?; diff --git a/noodles-bam/src/io/reader/header/magic_number.rs b/noodles-bam/src/io/reader/header/magic_number.rs index c6f031b42..0b2e1ab8c 100644 --- a/noodles-bam/src/io/reader/header/magic_number.rs +++ b/noodles-bam/src/io/reader/header/magic_number.rs @@ -1,15 +1,20 @@ use std::io::{self, Read}; -use super::Reader; use crate::MAGIC_NUMBER; -pub(super) fn read_magic_number(reader: &mut Reader) -> io::Result<()> +type Buf = [u8; MAGIC_NUMBER.len()]; + +pub(super) fn read_magic_number(reader: &mut R) -> io::Result where R: Read, { - let magic_number = reader.read_magic_number()?; + let mut buf = [0; MAGIC_NUMBER.len()]; + reader.read_exact(&mut buf)?; + Ok(buf) +} - if magic_number == MAGIC_NUMBER { +pub(super) fn validate(buf: Buf) -> io::Result<()> { + if is_valid(buf) { Ok(()) } else { Err(io::Error::new( @@ -19,30 +24,32 @@ where } } +fn is_valid(buf: Buf) -> bool { + buf == MAGIC_NUMBER +} + #[cfg(test)] mod tests { use super::*; #[test] fn test_read_magic_number() -> io::Result<()> { - let mut src = &b"BAM\x01"[..]; - let mut reader = Reader::new(&mut src); - assert!(read_magic_number(&mut reader).is_ok()); + let src = b"BAM\x01"; + let mut reader = &src[..]; + assert_eq!(read_magic_number(&mut reader)?, *b"BAM\x01"); let mut src = &[][..]; - let mut reader = Reader::new(&mut src); - assert!(matches!( - read_magic_number(&mut reader), - Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof - )); - - let mut src = &b"MThd"[..]; - let mut reader = Reader::new(&mut src); assert!(matches!( - read_magic_number(&mut reader), - Err(ref e) if e.kind() == io::ErrorKind::InvalidData + read_magic_number(&mut src), + Err(e) if e.kind() == io::ErrorKind::UnexpectedEof )); Ok(()) } + + #[test] + fn test_is_valid() { + assert!(is_valid(*b"BAM\x01")); + assert!(!is_valid([0x00, 0x00, 0x00, 0x00])); + } }