mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-09 14:49:25 +00:00
test(property): expand parser invariants (#3534)
* test(property): harden parser invariants for #3522 * test(property): satisfy clippy for #3522
This commit is contained in:
@@ -203,6 +203,7 @@ serial_test = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
aws-config = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
proptest = "1"
|
||||
tokio = { workspace = true, features = ["test-util"] }
|
||||
temp-env = { workspace = true, features = ["async_closure"] }
|
||||
tracing-subscriber = { workspace = true }
|
||||
|
||||
@@ -617,9 +617,17 @@ pub fn parse_copy_source_range(range_str: &str) -> S3Result<HTTPRangeSpec> {
|
||||
.parse::<i64>()
|
||||
.map_err(|_| s3_error!(InvalidArgument, "Invalid range format"))?;
|
||||
|
||||
if length <= 0 {
|
||||
return Err(s3_error!(InvalidArgument, "Invalid range format"));
|
||||
}
|
||||
|
||||
let start = length
|
||||
.checked_neg()
|
||||
.ok_or_else(|| s3_error!(InvalidArgument, "Invalid range format"))?;
|
||||
|
||||
Ok(HTTPRangeSpec {
|
||||
is_suffix_length: true,
|
||||
start: -length,
|
||||
start,
|
||||
end: -1,
|
||||
})
|
||||
} else {
|
||||
@@ -777,6 +785,7 @@ fn get_content_sha256_cksum(headers: &HeaderMap<HeaderValue>, service_type: Serv
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use proptest::prelude::*;
|
||||
use temp_env;
|
||||
|
||||
use super::*;
|
||||
@@ -1560,5 +1569,28 @@ mod tests {
|
||||
assert!(parse_copy_source_range("bytes=").is_err());
|
||||
assert!(parse_copy_source_range("bytes=abc-def").is_err());
|
||||
assert!(parse_copy_source_range("bytes=100-50").is_err()); // start > end
|
||||
assert!(parse_copy_source_range("bytes=-0").is_err());
|
||||
assert!(parse_copy_source_range("bytes=--9223372036854775808").is_err());
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn parse_copy_source_range_never_panics_and_preserves_output_invariants(
|
||||
input in any::<String>(),
|
||||
) {
|
||||
match std::panic::catch_unwind(|| parse_copy_source_range(&input)) {
|
||||
Ok(Ok(spec)) => {
|
||||
if spec.is_suffix_length {
|
||||
prop_assert_eq!(spec.end, -1);
|
||||
prop_assert!(spec.start < 0);
|
||||
} else {
|
||||
prop_assert!(spec.start >= 0);
|
||||
prop_assert!(spec.end == -1 || spec.end >= spec.start);
|
||||
}
|
||||
}
|
||||
Ok(Err(_)) => {}
|
||||
Err(_) => prop_assert!(false, "parse_copy_source_range panicked for input {:?}", input),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user