Skip to content

Commit

Permalink
cram/io/reader/header/magic_number: Split read and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Jan 5, 2025
1 parent 123198f commit 8fef33a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 51 deletions.
6 changes: 5 additions & 1 deletion noodles-cram/src/async/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ where
/// # }
/// ```
pub async fn read_file_definition(&mut self) -> io::Result<FileDefinition> {
header::read_magic_number(&mut self.inner).await?;
use crate::io::reader::header::magic_number;

header::read_magic_number(&mut self.inner)
.await
.and_then(magic_number::validate)?;

let format = read_format(&mut self.inner).await?;
let file_id = read_file_id(&mut self.inner).await?;
Expand Down
42 changes: 14 additions & 28 deletions noodles-cram/src/async/io/reader/header/magic_number.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
use tokio::io::{self, AsyncRead, AsyncReadExt};

pub(crate) async fn read_magic_number<R>(reader: &mut R) -> io::Result<()>
use crate::MAGIC_NUMBER;

pub(crate) async fn read_magic_number<R>(reader: &mut R) -> io::Result<[u8; MAGIC_NUMBER.len()]>
where
R: AsyncRead + Unpin,
{
use crate::MAGIC_NUMBER;

let mut magic = [0; 4];
reader.read_exact(&mut magic).await?;

if magic == MAGIC_NUMBER {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"invalid CRAM header",
))
}
let mut buf = [0; MAGIC_NUMBER.len()];
reader.read_exact(&mut buf).await?;
Ok(buf)
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_read_magic_number() {
let data = b"CRAM";
let mut reader = &data[..];
assert!(read_magic_number(&mut reader).await.is_ok());
async fn test_read_magic_number() -> io::Result<()> {
let src = b"CRAM";
let mut reader = &src[..];
assert_eq!(read_magic_number(&mut reader).await?, *b"CRAM");

let data = [];
let mut reader = &data[..];
let mut src = &[][..];
assert!(matches!(
read_magic_number(&mut reader).await,
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof
read_magic_number(&mut src).await,
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof
));

let data = b"BAM\x01";
let mut reader = &data[..];
assert!(matches!(
read_magic_number(&mut reader).await,
Err(ref e) if e.kind() == io::ErrorKind::InvalidData
));
Ok(())
}
}
4 changes: 2 additions & 2 deletions noodles-cram/src/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod builder;
pub(crate) mod container;
pub(crate) mod data_container;
mod header;
pub(crate) mod header;
pub(crate) mod header_container;
pub(crate) mod num;
mod query;
Expand Down Expand Up @@ -128,7 +128,7 @@ where
/// # Ok::<(), io::Error>(())
/// ```
pub fn read_file_definition(&mut self) -> io::Result<FileDefinition> {
header::read_magic_number(&mut self.inner)?;
header::read_magic_number(&mut self.inner).and_then(header::magic_number::validate)?;

let format = read_format(&mut self.inner)?;
let file_id = read_file_id(&mut self.inner)?;
Expand Down
2 changes: 1 addition & 1 deletion noodles-cram/src/io/reader/header.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod magic_number;
pub(crate) mod magic_number;

pub(super) use self::magic_number::read_magic_number;
45 changes: 26 additions & 19 deletions noodles-cram/src/io/reader/header/magic_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ use std::io::{self, Read};

use crate::MAGIC_NUMBER;

pub(crate) fn read_magic_number<R>(reader: &mut R) -> io::Result<()>
type Buf = [u8; MAGIC_NUMBER.len()];

pub(crate) fn read_magic_number<R>(reader: &mut R) -> io::Result<Buf>
where
R: Read,
{
let mut buf = [0; 4];
let mut buf = [0; MAGIC_NUMBER.len()];
reader.read_exact(&mut buf)?;
Ok(buf)
}

if buf == MAGIC_NUMBER {
pub(crate) fn validate(buf: Buf) -> io::Result<()> {
if is_valid(buf) {
Ok(())
} else {
Err(io::Error::new(
Expand All @@ -19,31 +24,33 @@ where
}
}

fn is_valid(buf: Buf) -> bool {
buf == MAGIC_NUMBER
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_read_magic_number() {
let data = b"CRAM";
let mut reader = &data[..];
assert!(read_magic_number(&mut reader).is_ok());
}
fn test_read_magic_number() -> io::Result<()> {
let src = b"CRAM";
let mut reader = &src[..];
assert_eq!(read_magic_number(&mut reader)?, *b"CRAM");

#[test]
fn test_read_magic_number_with_invalid_input() {
let data = [];
let mut reader = &data[..];
let src = [];
let mut reader = &src[..];
assert!(matches!(
read_magic_number(&mut reader),
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof,
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof
));

let data = b"BAM\x01";
let mut reader = &data[..];
assert!(matches!(
read_magic_number(&mut reader),
Err(ref e) if e.kind() == io::ErrorKind::InvalidData,
));
Ok(())
}

#[test]
fn test_is_valid() {
assert!(is_valid(*b"CRAM"));
assert!(!is_valid([0x00, 0x00, 0x00, 0x00]));
}
}

0 comments on commit 8fef33a

Please sign in to comment.