Skip to content

Commit a7707f7

Browse files
committed
test(decompression-plz): added tests for corrupt
1 parent e4d9e36 commit a7707f7

File tree

10 files changed

+241
-19
lines changed

10 files changed

+241
-19
lines changed

decompression-plz/src/state.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ where
7070
Ok(()) => Self::UpdateContentLength(decode_struct),
7171
Err(e) => {
7272
// TODO: maybe remove chunked TE
73-
if e.is_partial() {
74-
Self::UpdateContentLengthAndErr(decode_struct, e)
75-
} else {
76-
return Err(e);
77-
}
73+
Self::UpdateContentLengthAndErr(decode_struct, e)
7874
}
7975
};
8076
Ok(next_state)
@@ -83,16 +79,14 @@ where
8379
mut decode_struct,
8480
mut encoding_infos,
8581
) => {
86-
let next_state = if let Err(e) =
87-
apply_encoding(&mut decode_struct, &mut encoding_infos)
88-
{
89-
if e.is_partial() {
82+
let next_state = match apply_encoding(
83+
&mut decode_struct,
84+
&mut encoding_infos,
85+
) {
86+
Err(e) => {
9087
Self::UpdateContentLengthAndErr(decode_struct, e)
91-
} else {
92-
return Err(e);
9388
}
94-
} else {
95-
Self::UpdateContentLength(decode_struct)
89+
Ok(_) => Self::UpdateContentLength(decode_struct),
9690
};
9791
Ok(next_state)
9892
}

decompression-plz/tests/test_cases/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ fn encoding_state(
2525
}
2626

2727
const VERIFY_SINGLE_HEADER_BODY_ONLY: &str = "Host: example.com\r\n\
28-
Content-Type: text/html; charset=utf-8\r\n\
29-
Content-Length: 11\r\n\r\n\
30-
hello world";
28+
Content-Type: text/html; charset=utf-8\r\n\
29+
Content-Length: 11\r\n\r\n\
30+
hello world";
3131

3232
const VERIFY_SINGLE_HEADER_BODY_AND_EXTRA: &str = "Host: example.com\r\n\
33-
Content-Type: text/html; charset=utf-8\r\n\
34-
Content-Length: 22\r\n\r\n\
35-
hello worldhello world";
33+
Content-Type: text/html; charset=utf-8\r\n\
34+
Content-Length: 22\r\n\r\n\
35+
hello worldhello world";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use super::*;
2+
3+
#[test]
4+
fn test_corrupt_ce_brotli() {
5+
assert_partial_encoding(CONTENT_ENCODING, &ContentEncoding::Brotli, None)
6+
}
7+
8+
#[test]
9+
fn test_corrupt_ce_compress() {
10+
assert_partial_encoding(CONTENT_ENCODING, &ContentEncoding::Compress, None)
11+
}
12+
13+
#[test]
14+
fn test_corrupt_ce_deflate() {
15+
assert_partial_encoding(CONTENT_ENCODING, &ContentEncoding::Deflate, None)
16+
}
17+
18+
#[test]
19+
fn test_corrupt_ce_gzip() {
20+
assert_partial_encoding(CONTENT_ENCODING, &ContentEncoding::Gzip, None)
21+
}
22+
23+
#[test]
24+
fn test_corrupt_ce_zstd() {
25+
assert_partial_encoding(CONTENT_ENCODING, &ContentEncoding::Zstd, None)
26+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use super::*;
2+
3+
#[test]
4+
fn test_corrupt_ce_brotli_extra() {
5+
assert_partial_encoding(
6+
CONTENT_ENCODING,
7+
&ContentEncoding::Brotli,
8+
Some(INPUT.into()),
9+
)
10+
}
11+
12+
#[test]
13+
fn test_corrupt_ce_compress_extra() {
14+
assert_partial_encoding(
15+
CONTENT_ENCODING,
16+
&ContentEncoding::Compress,
17+
Some(INPUT.into()),
18+
)
19+
}
20+
21+
#[test]
22+
fn test_corrupt_ce_deflate_extra() {
23+
assert_partial_encoding(
24+
CONTENT_ENCODING,
25+
&ContentEncoding::Deflate,
26+
Some(INPUT.into()),
27+
)
28+
}
29+
30+
#[test]
31+
fn test_corrupt_ce_gzip_extra() {
32+
assert_partial_encoding(
33+
CONTENT_ENCODING,
34+
&ContentEncoding::Gzip,
35+
Some(INPUT.into()),
36+
)
37+
}
38+
39+
#[test]
40+
fn test_corrupt_ce_zstd_extra() {
41+
assert_partial_encoding(
42+
CONTENT_ENCODING,
43+
&ContentEncoding::Zstd,
44+
Some(INPUT.into()),
45+
)
46+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use super::*;
2+
mod body_only;
3+
mod extra;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use super::*;
2+
mod ce;
3+
mod te;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use super::*;
2+
3+
#[test]
4+
fn test_corrupt_te_brotli() {
5+
assert_partial_encoding(TRANSFER_ENCODING, &ContentEncoding::Brotli, None)
6+
}
7+
8+
#[test]
9+
fn test_corrupt_te_compress() {
10+
assert_partial_encoding(
11+
TRANSFER_ENCODING,
12+
&ContentEncoding::Compress,
13+
None,
14+
)
15+
}
16+
17+
#[test]
18+
fn test_corrupt_te_deflate() {
19+
assert_partial_encoding(TRANSFER_ENCODING, &ContentEncoding::Deflate, None)
20+
}
21+
22+
#[test]
23+
fn test_corrupt_te_gzip() {
24+
assert_partial_encoding(TRANSFER_ENCODING, &ContentEncoding::Gzip, None)
25+
}
26+
27+
#[test]
28+
fn test_corrupt_te_zstd() {
29+
assert_partial_encoding(TRANSFER_ENCODING, &ContentEncoding::Zstd, None)
30+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use super::*;
2+
3+
#[test]
4+
fn test_corrupt_te_brotli_extra() {
5+
assert_partial_encoding(
6+
TRANSFER_ENCODING,
7+
&ContentEncoding::Brotli,
8+
Some(INPUT.into()),
9+
)
10+
}
11+
12+
#[test]
13+
fn test_corrupt_te_compress_extra() {
14+
assert_partial_encoding(
15+
TRANSFER_ENCODING,
16+
&ContentEncoding::Compress,
17+
Some(INPUT.into()),
18+
)
19+
}
20+
21+
#[test]
22+
fn test_corrupt_te_deflate_extra() {
23+
assert_partial_encoding(
24+
TRANSFER_ENCODING,
25+
&ContentEncoding::Deflate,
26+
Some(INPUT.into()),
27+
)
28+
}
29+
30+
#[test]
31+
fn test_corrupt_te_gzip_extra() {
32+
assert_partial_encoding(
33+
TRANSFER_ENCODING,
34+
&ContentEncoding::Gzip,
35+
Some(INPUT.into()),
36+
)
37+
}
38+
39+
#[test]
40+
fn test_corrupt_te_zstd_extra() {
41+
assert_partial_encoding(
42+
TRANSFER_ENCODING,
43+
&ContentEncoding::Zstd,
44+
Some(INPUT.into()),
45+
)
46+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use super::*;
2+
mod body_only;
3+
mod extra;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,72 @@
1+
use super::*;
2+
use decompression_plz::MultiDecompressErrorReason;
3+
use header_plz::body_headers::content_encoding::ContentEncoding;
4+
use tests_utils::INPUT;
5+
mod corrupt;
16

7+
fn assert_partial_encoding(
8+
encoding: &str,
9+
compression: &ContentEncoding,
10+
extra: Option<BytesMut>,
11+
) {
12+
let headers = format!(
13+
"Host: example.com\r\n\
14+
Content-Type: text/html; charset=utf-8\r\n\
15+
{}: {}\r\n\
16+
Content-Length: 11\r\n\r\n",
17+
encoding,
18+
compression.as_ref()
19+
);
20+
21+
let verify = format!(
22+
"Host: example.com\r\n\
23+
Content-Type: text/html; charset=utf-8\r\n\
24+
{}: {}\r\n\
25+
Content-Length: {}\r\n\r\n\
26+
hello world{}",
27+
encoding,
28+
compression.as_ref(),
29+
11 + extra.as_ref().map_or(0, |b| b.len()),
30+
extra
31+
.as_ref()
32+
.map_or(String::new(), |b| String::from_utf8_lossy(b).to_string())
33+
);
34+
35+
let expected_state = encoding_state(encoding);
36+
37+
let mut tm = TestMessage::build(
38+
headers.as_bytes().into(),
39+
Body::Raw(INPUT.into()),
40+
extra,
41+
);
42+
43+
let mut buf = BytesMut::new();
44+
let mut state = DecodeState::init(&mut tm, &mut buf);
45+
state = state.try_next().unwrap();
46+
assert!((expected_state)(&state));
47+
48+
state = state.try_next().unwrap();
49+
assert!(matches!(state, DecodeState::UpdateContentLengthAndErr(..)));
50+
51+
if let Err(e) = state.try_next() {
52+
assert!(matches!(e, MultiDecompressErrorReason::Corrupt));
53+
let result = tm.into_bytes();
54+
assert_eq!(result, verify);
55+
} else {
56+
panic!()
57+
}
58+
}
59+
60+
#[test]
61+
fn test_corrupt_te_gzip_extra() {
62+
assert_partial_encoding(
63+
TRANSFER_ENCODING,
64+
&ContentEncoding::Gzip,
65+
Some(INPUT.into()),
66+
);
67+
}
68+
69+
#[test]
70+
fn test_corrupt_ce_gzip() {
71+
assert_partial_encoding(CONTENT_ENCODING, &ContentEncoding::Gzip, None);
72+
}

0 commit comments

Comments
 (0)