Skip to content

Commit

Permalink
test: add a behaviour test for InvalidInput (apache#2644)
Browse files Browse the repository at this point in the history
* test: add a behaviour test for InvalidInput

Signed-off-by: dqhl76 <[email protected]>

* fix: fix the unclear Interrupted error

Signed-off-by: dqhl76 <[email protected]>

* fix: make fmt happy

Signed-off-by: dqhl76 <[email protected]>

* fix: make clippy happy

Signed-off-by: dqhl76 <[email protected]>

---------

Signed-off-by: dqhl76 <[email protected]>
  • Loading branch information
dqhl76 authored Jul 15, 2023
1 parent 40e2a2a commit 70afe5d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
23 changes: 15 additions & 8 deletions core/src/raw/oio/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,25 @@ impl<T: Read + ?Sized> Read for Box<T> {
}
}

fn convert_to_io_error(err: Error) -> io::Error {
let kind = match err.kind() {
ErrorKind::NotFound => io::ErrorKind::NotFound,
ErrorKind::PermissionDenied => io::ErrorKind::PermissionDenied,
ErrorKind::InvalidInput => io::ErrorKind::InvalidInput,
_ => io::ErrorKind::Interrupted,
};

io::Error::new(kind, err)
}

impl futures::AsyncRead for dyn Read {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
let this: &mut dyn Read = &mut *self;
this.poll_read(cx, buf)
.map_err(|err| io::Error::new(io::ErrorKind::Interrupted, err))
this.poll_read(cx, buf).map_err(convert_to_io_error)
}
}

Expand All @@ -168,8 +178,7 @@ impl futures::AsyncSeek for dyn Read {
pos: io::SeekFrom,
) -> Poll<io::Result<u64>> {
let this: &mut dyn Read = &mut *self;
this.poll_seek(cx, pos)
.map_err(|err| io::Error::new(io::ErrorKind::Interrupted, err))
this.poll_seek(cx, pos).map_err(convert_to_io_error)
}
}

Expand Down Expand Up @@ -350,17 +359,15 @@ impl io::Read for dyn BlockingRead {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let this: &mut dyn BlockingRead = &mut *self;
this.read(buf)
.map_err(|err| io::Error::new(io::ErrorKind::Interrupted, err))
this.read(buf).map_err(convert_to_io_error)
}
}

impl io::Seek for dyn BlockingRead {
#[inline]
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
let this: &mut dyn BlockingRead = &mut *self;
this.seek(pos)
.map_err(|err| io::Error::new(io::ErrorKind::Interrupted, err))
this.seek(pos).map_err(convert_to_io_error)
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ impl From<Error> for io::Error {
let kind = match err.kind() {
ErrorKind::NotFound => io::ErrorKind::NotFound,
ErrorKind::PermissionDenied => io::ErrorKind::PermissionDenied,
ErrorKind::InvalidInput => io::ErrorKind::InvalidInput,
_ => io::ErrorKind::Other,
};

Expand Down
28 changes: 27 additions & 1 deletion core/tests/behavior/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ pub fn behavior_write_tests(op: &Operator) -> Vec<Trial> {
test_writer_copy,
test_writer_abort,
test_writer_futures_copy,
test_fuzz_unsized_writer
test_fuzz_unsized_writer,
test_invalid_reader_seek
)
}

Expand Down Expand Up @@ -1233,3 +1234,28 @@ pub async fn test_fuzz_unsized_writer(op: Operator) -> Result<()> {
op.delete(&path).await.expect("delete must succeed");
Ok(())
}

/// seeking a negative position should return a InvalidInput error
pub async fn test_invalid_reader_seek(op: Operator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();
debug!("Generate a random file: {}", &path);
let (content, _) = gen_bytes();

op.write(&path, content.clone())
.await
.expect("write must succeed");

let mut r = op.reader(&path).await?;
let res = r.seek(std::io::SeekFrom::Current(-1024)).await;

assert!(res.is_err());

assert_eq!(
res.unwrap_err().kind(),
std::io::ErrorKind::InvalidInput,
"seeking a negative position should return a InvalidInput error"
);

op.delete(&path).await.expect("delete must succeed");
Ok(())
}

0 comments on commit 70afe5d

Please sign in to comment.