Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- run: cargo test --features zlib-ng --no-default-features
if: matrix.build != 'mingw'
- run: cargo test --features zlib-rs --no-default-features
if: matrix.build != 'mingw'
- run: cargo test --test zlib-rs-capabilities --features zlib-rs
- run: cargo test --features cloudflare_zlib --no-default-features
if: matrix.build != 'mingw'
- run: |
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ and raw deflate streams.
"""
exclude = [".*"]

[[test]]
name = "zlib-rs-capabilities"
required-features = ["zlib-rs"]

[dependencies]
libz-sys = { version = "1.1.20", optional = true, default-features = false }
libz-ng-sys = { version = "1.1.16", optional = true }
Expand Down
12 changes: 6 additions & 6 deletions src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl Compress {
///
/// If `window_bits` does not fall into the range 9 ..= 15,
/// `new_with_window_bits` will panic.
#[cfg(feature = "any_zlib")]
#[cfg(feature = "any_impl")]
pub fn new_with_window_bits(
level: Compression,
zlib_header: bool,
Expand All @@ -240,7 +240,7 @@ impl Compress {
///
/// If `window_bits` does not fall into the range 9 ..= 15,
/// `new_with_window_bits` will panic.
#[cfg(feature = "any_zlib")]
#[cfg(feature = "any_impl")]
pub fn new_gzip(level: Compression, window_bits: u8) -> Compress {
assert!(
window_bits > 8 && window_bits < 16,
Expand Down Expand Up @@ -399,7 +399,7 @@ impl Decompress {
///
/// If `window_bits` does not fall into the range 9 ..= 15,
/// `new_with_window_bits` will panic.
#[cfg(feature = "any_zlib")]
#[cfg(feature = "any_impl")]
pub fn new_with_window_bits(zlib_header: bool, window_bits: u8) -> Decompress {
assert!(
window_bits > 8 && window_bits < 16,
Expand All @@ -419,7 +419,7 @@ impl Decompress {
///
/// If `window_bits` does not fall into the range 9 ..= 15,
/// `new_with_window_bits` will panic.
#[cfg(feature = "any_zlib")]
#[cfg(feature = "any_impl")]
pub fn new_gzip(window_bits: u8) -> Decompress {
assert!(
window_bits > 8 && window_bits < 16,
Expand Down Expand Up @@ -640,7 +640,7 @@ mod tests {
use crate::write;
use crate::{Compression, Decompress, FlushDecompress};

#[cfg(feature = "any_zlib")]
#[cfg(feature = "any_impl")]
use crate::{Compress, FlushCompress};

#[test]
Expand Down Expand Up @@ -780,7 +780,7 @@ mod tests {
assert_eq!(&decoded[..decoder.total_out() as usize], string);
}

#[cfg(feature = "any_zlib")]
#[cfg(feature = "any_impl")]
#[test]
fn test_gzip_flate() {
let string = "hello, hello!".as_bytes();
Expand Down
87 changes: 87 additions & 0 deletions tests/zlib-rs-capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//! Validate that certain feature-gated functionality is still available.
use flate2::{Compress, Compression, Decompress, FlushCompress, FlushDecompress};

#[test]
fn test_compress_new_with_window_bits() {
let string = "hello world".as_bytes();

// Test with window_bits = 9 (minimum)
let mut encoded = Vec::with_capacity(1024);
let mut encoder = Compress::new_with_window_bits(Compression::default(), true, 9);
encoder
.compress_vec(string, &mut encoded, FlushCompress::Finish)
.unwrap();
assert_ne!(encoded.len(), 0);

let mut decoder = Decompress::new_with_window_bits(true, 9);
let mut decoded = [0; 1024];
decoder
.decompress(&encoded, &mut decoded, FlushDecompress::Finish)
.unwrap();
assert_eq!(&decoded[..string.len()], string);

// Test with window_bits = 15 (maximum)
let mut encoded = Vec::with_capacity(1024);
let mut encoder = Compress::new_with_window_bits(Compression::default(), false, 15);
encoder
.compress_vec(string, &mut encoded, FlushCompress::Finish)
.unwrap();
assert_ne!(encoded.len(), 0);

let mut decoder = Decompress::new_with_window_bits(false, 15);
let mut decoded = [0; 1024];
decoder
.decompress(&encoded, &mut decoded, FlushDecompress::Finish)
.unwrap();
assert_eq!(&decoded[..string.len()], string);
}

#[test]
fn test_decompress_new_gzip_window_bits() {
let string = "hello world".as_bytes();

// Test with different window_bits values
for window_bits in [9, 12, 15] {
let mut encoded = Vec::with_capacity(1024);
let mut encoder = Compress::new_gzip(Compression::default(), window_bits);
encoder
.compress_vec(string, &mut encoded, FlushCompress::Finish)
.unwrap();

let mut decoder = Decompress::new_gzip(window_bits);
let mut decoded = [0; 1024];
decoder
.decompress(&encoded, &mut decoded, FlushDecompress::Finish)
.unwrap();
assert_eq!(
&decoded[..string.len()],
string,
"Failed with window_bits={}",
window_bits
);
}
}

#[test]
#[should_panic(expected = "window_bits must be within 9 ..= 15")]
fn test_compress_new_with_window_bits_invalid_low() {
let _ = Compress::new_with_window_bits(Compression::default(), true, 8);
}

#[test]
#[should_panic(expected = "window_bits must be within 9 ..= 15")]
fn test_compress_new_with_window_bits_invalid_high() {
let _ = Compress::new_with_window_bits(Compression::default(), true, 16);
}

#[test]
#[should_panic(expected = "window_bits must be within 9 ..= 15")]
fn test_compress_new_gzip_invalid_low() {
let _ = Compress::new_gzip(Compression::default(), 8);
}

#[test]
#[should_panic(expected = "window_bits must be within 9 ..= 15")]
fn test_compress_new_gzip_invalid_high() {
let _ = Compress::new_gzip(Compression::default(), 16);
}