Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cover some uncovered paths #152

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 135 additions & 1 deletion test-libz-rs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ macro_rules! assert_eq_rs_ng {
};
}

#[cfg(test)]
#[allow(unused)]
macro_rules! extract_error_message {
($strm:expr) => {
if !$strm.msg.is_null() {
core::ffi::CStr::from_ptr($strm.msg).to_str()
} else {
Ok("NULL")
}
};
}

#[cfg(test)]
mod null {
use core::mem::MaybeUninit;
Expand Down Expand Up @@ -569,7 +581,7 @@ mod null {
);
}

const FERRIS_BYTES: [u8; 14] = [120u8, 156, 115, 75, 45, 42, 202, 44, 6, 0, 8, 6, 2, 108];
pub const FERRIS_BYTES: [u8; 14] = [120u8, 156, 115, 75, 45, 42, 202, 44, 6, 0, 8, 6, 2, 108];

#[test]
#[cfg_attr(miri, ignore = "slow")]
Expand Down Expand Up @@ -854,4 +866,126 @@ mod coverage {
(err, dest_len)
});
}

#[test]
fn deflate_invalid_flush() {
let input = b"Ferris";
let mut buf = [0; 64];

assert_eq_rs_ng!({
let mut strm = MaybeUninit::<z_stream>::zeroed();

let err = deflateInit_(
strm.as_mut_ptr(),
6,
zlibVersion(),
core::mem::size_of::<z_stream>() as _,
);
assert_eq!(err, Z_OK);

let strm = strm.assume_init_mut();

strm.next_in = input.as_ptr() as *mut u8;
strm.avail_in = input.len() as _;

strm.next_out = buf.as_mut_ptr();
strm.avail_out = buf.len() as _;

let value = deflate(strm, 1234);

let err = deflateEnd(strm);
assert_eq!(err, Z_OK);

value
});
}

#[test]
fn deflate_insufficient_output_space() {
let input = b"Ferris";
let mut buf = [0; 5];

assert_eq_rs_ng!({
let mut strm = MaybeUninit::<z_stream>::zeroed();

let err = deflateInit_(
strm.as_mut_ptr(),
6,
zlibVersion(),
core::mem::size_of::<z_stream>() as _,
);
assert_eq!(err, Z_OK);

let strm = strm.assume_init_mut();

strm.next_in = input.as_ptr() as *mut u8;
strm.avail_in = input.len() as _;

strm.next_out = buf.as_mut_ptr();
strm.avail_out = buf.len() as _;

// Z_NO_FLUSH keeps the state in `Status::Busy`
let err = deflate(strm, Z_NO_FLUSH);
assert_eq!(err, Z_OK);

let err = deflateEnd(strm);
assert_eq!(err, Z_DATA_ERROR);

err
});
}

#[test]
fn deflate_reset_after_deflate() {
let input = b"Ferris";

assert_eq_rs_ng!({
let mut strm = MaybeUninit::<z_stream>::zeroed();

let err = deflateInit_(
strm.as_mut_ptr(),
6,
zlibVersion(),
core::mem::size_of::<z_stream>() as _,
);
assert_eq!(err, Z_OK);

let strm = strm.assume_init_mut();

{
let mut buf = [0; 64];

strm.next_in = input.as_ptr() as *mut u8;
strm.avail_in = input.len() as _;

strm.next_out = buf.as_mut_ptr();
strm.avail_out = buf.len() as _;

let err = deflate(strm, Z_FINISH);
assert_eq!(err, Z_STREAM_END);

assert_eq!(&buf[..strm.total_out as usize], super::null::FERRIS_BYTES);
}

let err = deflateReset(strm);
assert_eq!(err, Z_OK);

{
let mut buf = [0; 64];

strm.next_in = input.as_ptr() as *mut u8;
strm.avail_in = input.len() as _;

strm.next_out = buf.as_mut_ptr();
strm.avail_out = buf.len() as _;

let err = deflate(strm, Z_FINISH);
assert_eq!(err, Z_STREAM_END,);

assert_eq!(&buf[..strm.total_out as usize], super::null::FERRIS_BYTES);
}

deflateEnd(strm);
});
}
}
Loading