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

Simplify test suite 1 #132

Merged
merged 8 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
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
556 changes: 555 additions & 1 deletion libz-rs-sys/src/tests/deflate.rs

Large diffs are not rendered by default.

90 changes: 90 additions & 0 deletions libz-rs-sys/src/tests/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use zlib_rs::{deflate::DeflateConfig, DeflateFlush, ReturnCode};

// we use the libz_sys but configure zlib-ng in zlib compat mode
use libz_sys as libz_ng_sys;

use core::ffi::{c_int, c_uint};

pub fn compress_slice_ng<'a>(
output: &'a mut [u8],
input: &[u8],
config: DeflateConfig,
) -> (&'a mut [u8], ReturnCode) {
compress_slice_with_flush_ng(output, input, config, DeflateFlush::Finish)
}

pub fn compress_slice_with_flush_ng<'a>(
output: &'a mut [u8],
input: &[u8],
config: DeflateConfig,
final_flush: DeflateFlush,
) -> (&'a mut [u8], ReturnCode) {
let mut stream = libz_ng_sys::z_stream {
next_in: input.as_ptr() as *mut u8,
avail_in: 0, // for special logic in the first iteration
total_in: 0,
next_out: output.as_mut_ptr(),
avail_out: 0, // for special logic on the first iteration
total_out: 0,
msg: core::ptr::null_mut(),
state: core::ptr::null_mut(),
zalloc: zlib_rs::allocate::zalloc_c,
zfree: zlib_rs::allocate::zfree_c,
opaque: core::ptr::null_mut(),
data_type: 0,
adler: 0,
reserved: 0,
};

let err = unsafe {
libz_ng_sys::deflateInit2_(
&mut stream,
config.level,
config.method as i32,
config.window_bits,
config.mem_level,
config.strategy as i32,
libz_ng_sys::zlibVersion(),
core::mem::size_of::<libz_ng_sys::z_stream>() as c_int,
)
};

if err != libz_ng_sys::Z_OK {
return (&mut [], ReturnCode::from(err));
}

let max = c_uint::MAX as usize;

let mut left = output.len();
let mut source_len = input.len();

loop {
if stream.avail_out == 0 {
stream.avail_out = Ord::min(left, max) as _;
left -= stream.avail_out as usize;
}

if stream.avail_in == 0 {
stream.avail_in = Ord::min(source_len, max) as _;
source_len -= stream.avail_in as usize;
}

let flush = if source_len > 0 {
DeflateFlush::NoFlush
} else {
final_flush
};

let err = unsafe { libz_ng_sys::deflate(&mut stream, flush as i32) };

if err != libz_ng_sys::Z_OK {
break;
}
}

// may DataError if there was insufficient output space
let err = unsafe { libz_ng_sys::deflateEnd(&mut stream) };
let return_code: ReturnCode = ReturnCode::from(err);

(&mut output[..stream.total_out as usize], return_code)
}
3 changes: 3 additions & 0 deletions libz-rs-sys/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod deflate;
mod helpers;
mod inflate;
mod zlib_ng_cve;

Binary file not shown.
Binary file added libz-rs-sys/src/tests/test-data/paper-100k.pdf
Binary file not shown.
49 changes: 49 additions & 0 deletions libz-rs-sys/src/tests/zlib_ng_cve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use zlib_rs::{deflate::DeflateConfig, ReturnCode};

use crate::tests::helpers::compress_slice_ng;

fn cve_test(input: &[u8]) {
let mut output_ng = [0; 1 << 17];
// flush type 4 = Finish is the default
let config = DeflateConfig {
window_bits: 15,
mem_level: 1,
..DeflateConfig::default()
};
let (output_ng, err) = compress_slice_ng(&mut output_ng, input, config);
assert_eq!(err, ReturnCode::Ok);

let mut output_rs = [0; 1 << 17];
let (output_rs, err) = zlib_rs::deflate::compress_slice(&mut output_rs, input, config);
assert_eq!(err, ReturnCode::Ok);

assert_eq!(output_ng, output_rs);

let mut output = vec![0; input.len()];
let config = zlib_rs::inflate::InflateConfig { window_bits: 15 };
let (output, err) = zlib_rs::inflate::uncompress_slice(&mut output, output_rs, config);
assert_eq!(err, ReturnCode::Ok);

assert_eq!(input, output);
}

#[test]
#[cfg_attr(miri, ignore)]
fn zlib_ng_cve_2018_25032_default() {
const DEFAULT: &str = include_str!("test-data/zlib-ng/CVE-2018-25032/default.txt");
cve_test(DEFAULT.as_bytes())
}

#[test]
#[cfg_attr(miri, ignore)]
fn zlib_ng_cve_2018_25032_fixed() {
const FIXED: &str = include_str!("test-data/zlib-ng/CVE-2018-25032/fixed.txt");
cve_test(FIXED.as_bytes())
}

#[test]
#[cfg_attr(miri, ignore)]
fn zlib_ng_gh_382() {
const DEFNEG: &[u8] = include_bytes!("test-data/zlib-ng/GH-382/defneg3.dat");
cve_test(DEFNEG)
}
3 changes: 0 additions & 3 deletions zlib-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,5 @@ libz-sys = { workspace = true, optional = true }
quickcheck = { workspace = true, optional = true }

[dev-dependencies]
libloading = "0.8.1"
libz-ng-sys = "1.1.12"
crc32fast = "1.3.2"
quickcheck.workspace = true
dynamic-libz-sys.workspace = true
7 changes: 3 additions & 4 deletions zlib-rs/src/adler32/avx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,10 @@ mod test {
#[test]
#[cfg_attr(miri, ignore)]
fn large_input() {
const DEFAULT: &str =
include_str!("../deflate/test-data/zlib-ng/CVE-2018-25032/default.txt");
const DEFAULT: &[u8] = include_bytes!("../deflate/test-data/paper-100k.pdf");

let avx2 = adler32_avx2(42, DEFAULT.as_bytes());
let rust = crate::adler32::generic::adler32_rust(42, DEFAULT.as_bytes());
let avx2 = adler32_avx2(42, DEFAULT);
let rust = crate::adler32::generic::adler32_rust(42, DEFAULT);

assert_eq!(avx2, rust);
}
Expand Down
7 changes: 3 additions & 4 deletions zlib-rs/src/adler32/neon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,10 @@ mod tests {

#[test]
fn large_input() {
const DEFAULT: &str =
include_str!("../deflate/test-data/zlib-ng/CVE-2018-25032/default.txt");
const DEFAULT: &[u8] = include_bytes!("../deflate/test-data/paper-100k.pdf");

let neon = adler32_neon(42, &DEFAULT.as_bytes());
let rust = crate::adler32::generic::adler32_rust(42, &DEFAULT.as_bytes());
let neon = adler32_neon(42, &DEFAULT);
let rust = crate::adler32::generic::adler32_rust(42, &DEFAULT);

assert_eq!(neon, rust);
}
Expand Down
2 changes: 1 addition & 1 deletion zlib-rs/src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl gz_header {
// 13 - Acorn RISCOS
// 255 - unknown
#[allow(clippy::if_same_then_else)]
pub(crate) const OS_CODE: u8 = {
pub const OS_CODE: u8 = {
if cfg!(windows) {
10
} else if cfg!(target_os = "macos") {
Expand Down
Loading
Loading