use super::crc::CrcAlgorithm;
use super::{ObjectChecksumMethod, ValidationPlan, WholeObjectPlan};
use crate::error::{self, Error};
use crate::model::{ChecksumAlgorithm, ChecksumType, ObjectMetadata};
fn malformed(algorithm: &ChecksumAlgorithm, problem: &str) -> Error {
error::discovery_failed(format!("stored {} checksum {problem}", algorithm.as_str()))
}
#[derive(Debug)]
pub(in crate::operation::download) struct ExpectedValue {
pub(super) decoded: Vec<u8>,
pub(super) encoded: String,
}
#[derive(Debug)]
pub(in crate::operation::download) enum StoredChecksum {
FullObjectCrc {
algorithm: CrcAlgorithm,
expected: ExpectedValue,
},
Composite,
Ordered,
Unavailable,
}
impl StoredChecksum {
pub(in crate::operation::download) fn from_metadata(
meta: &ObjectMetadata,
) -> Result<Self, Error> {
if matches!(meta.checksum_type, Some(ChecksumType::Composite)) {
return Ok(Self::Composite);
}
let Some((selected_algorithm, value)) = Self::preferred_checksum_header(meta) else {
return Ok(Self::Unavailable);
};
let algorithm = match selected_algorithm {
ChecksumAlgorithm::Crc64Nvme => Some(CrcAlgorithm::Crc64Nvme),
ChecksumAlgorithm::Crc32C => Some(CrcAlgorithm::Crc32c),
ChecksumAlgorithm::Crc32 => Some(CrcAlgorithm::Crc32),
_ => None,
};
if let Some((_, count)) = value.rsplit_once('-') {
return match count.parse::<u32>() {
Ok(count) if count > 0 => Ok(Self::Composite),
_ => Err(malformed(
&selected_algorithm,
"has a part count suffix that isn't a positive number",
)),
};
}
let Some(algorithm) = algorithm else {
return Ok(Self::Ordered);
};
if !matches!(meta.checksum_type, None | Some(ChecksumType::FullObject)) {
return Ok(Self::Unavailable);
}
let decoded = aws_smithy_types::base64::decode(value)
.map_err(|_| malformed(&selected_algorithm, "isn't valid base64"))?;
if decoded.len() != algorithm.width() {
return Err(malformed(
&selected_algorithm,
"has the wrong decoded width for its algorithm",
));
}
Ok(Self::FullObjectCrc {
algorithm,
expected: ExpectedValue {
decoded,
encoded: value.to_owned(),
},
})
}
fn preferred_checksum_header(meta: &ObjectMetadata) -> Option<(ChecksumAlgorithm, &str)> {
[
(
ChecksumAlgorithm::Crc64Nvme,
meta.checksum_crc64_nvme.as_deref(),
),
(ChecksumAlgorithm::Crc32C, meta.checksum_crc32_c.as_deref()),
(ChecksumAlgorithm::Crc32, meta.checksum_crc32.as_deref()),
(ChecksumAlgorithm::Sha1, meta.checksum_sha1.as_deref()),
(ChecksumAlgorithm::Sha256, meta.checksum_sha256.as_deref()),
]
.into_iter()
.find_map(|(algorithm, value)| value.map(|value| (algorithm, value)))
}
pub(super) fn algorithm(meta: &ObjectMetadata) -> Option<ChecksumAlgorithm> {
Self::preferred_checksum_header(meta).map(|(algorithm, _)| algorithm)
}
pub(in crate::operation::download) fn into_plan(self, object_len: u64) -> ValidationPlan {
match self {
Self::FullObjectCrc {
algorithm,
expected,
} => ValidationPlan::WholeObject(WholeObjectPlan {
method: ObjectChecksumMethod::CombineRanges { algorithm },
expected,
object_len,
}),
_ => ValidationPlan::PerResponse,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn supported_crcs_require_their_decoded_width() {
for (algorithm, value) in [
(CrcAlgorithm::Crc32, "AAAAAA=="),
(CrcAlgorithm::Crc32c, "AAAAAA=="),
(CrcAlgorithm::Crc64Nvme, "AAAAAAAAAAA="),
] {
let mut meta = ObjectMetadata::default();
match algorithm {
CrcAlgorithm::Crc32 => meta.checksum_crc32 = Some(value.into()),
CrcAlgorithm::Crc32c => meta.checksum_crc32_c = Some(value.into()),
CrcAlgorithm::Crc64Nvme => meta.checksum_crc64_nvme = Some(value.into()),
}
for checksum_type in [None, Some(ChecksumType::FullObject)] {
meta.checksum_type = checksum_type;
let stored = StoredChecksum::from_metadata(&meta).unwrap();
assert!(matches!(
stored,
StoredChecksum::FullObjectCrc { algorithm: a, .. } if a == algorithm
));
assert!(matches!(
stored.into_plan(7),
ValidationPlan::WholeObject(_)
));
}
}
}
#[test]
fn composite_type_or_suffix_never_selects_whole_object_verification() {
for (value, checksum_type) in [
("AAAAAA==-3", None),
("AAAAAA==-3", Some(ChecksumType::FullObject)),
("AAAAAA==", Some(ChecksumType::Composite)),
] {
let mut meta = ObjectMetadata::default();
meta.checksum_crc32 = Some(value.into());
meta.checksum_type = checksum_type;
let stored = StoredChecksum::from_metadata(&meta).unwrap();
assert!(matches!(stored, StoredChecksum::Composite));
assert!(matches!(stored.into_plan(7), ValidationPlan::PerResponse));
}
}
#[test]
fn explicit_composite_type_classifies_sha_and_absent_headers() {
for algorithm in [
None,
Some(ChecksumAlgorithm::Sha1),
Some(ChecksumAlgorithm::Sha256),
] {
let mut meta = ObjectMetadata::default();
meta.checksum_type = Some(ChecksumType::Composite);
match algorithm {
Some(ChecksumAlgorithm::Sha1) => meta.checksum_sha1 = Some("stored hash".into()),
Some(ChecksumAlgorithm::Sha256) => {
meta.checksum_sha256 = Some("stored hash".into())
}
None => {}
_ => unreachable!("test includes only SHA algorithms"),
}
let stored = StoredChecksum::from_metadata(&meta).unwrap();
assert!(
matches!(stored, StoredChecksum::Composite),
"{algorithm:?}: {stored:?}"
);
assert!(matches!(stored.into_plan(7), ValidationPlan::PerResponse));
}
}
#[test]
fn first_present_supported_header_controls_classification() {
let mut meta = ObjectMetadata::default();
meta.checksum_crc64_nvme = Some("not base64".into());
meta.checksum_crc32_c = Some("AAAAAA==".into());
let error = StoredChecksum::from_metadata(&meta).unwrap_err();
assert_eq!(
error.kind(),
&crate::error::ErrorKind::ObjectNotDiscoverable
);
let message = format!("{error}: {error:?}");
assert!(
message.contains("stored CRC64NVME checksum isn't valid base64"),
"a malformed preferred header is not replaced by a later one: {message}"
);
meta.checksum_crc64_nvme = Some("AAAAAAAAAAA=".into());
assert!(matches!(
StoredChecksum::from_metadata(&meta).unwrap(),
StoredChecksum::FullObjectCrc {
algorithm: CrcAlgorithm::Crc64Nvme,
..
}
));
meta.checksum_crc64_nvme = None;
meta.checksum_crc32 = Some("AAAAAA==".into());
assert!(matches!(
StoredChecksum::from_metadata(&meta).unwrap(),
StoredChecksum::FullObjectCrc {
algorithm: CrcAlgorithm::Crc32c,
..
}
));
}
#[test]
fn malformed_values_fail_discovery() {
let crc32 = |value: &str| {
let mut meta = ObjectMetadata::default();
meta.checksum_crc32 = Some(value.into());
meta
};
let sha256 = |value: &str| {
let mut meta = ObjectMetadata::default();
meta.checksum_sha256 = Some(value.into());
meta
};
for (meta, problem) in [
(crc32(""), "wrong decoded width"),
(crc32("AA=="), "wrong decoded width"),
(crc32("AAAAAAAAAAA="), "wrong decoded width"),
(crc32("!"), "isn't valid base64"),
(crc32("AAAA AA=="), "isn't valid base64"),
(crc32("AAAAAA==-0"), "part count suffix"),
(crc32("AAAAAA==-x"), "part count suffix"),
(crc32("AAAAAA==-"), "part count suffix"),
(sha256("stored-hash"), "part count suffix"),
] {
let error = StoredChecksum::from_metadata(&meta).unwrap_err();
assert_eq!(
error.kind(),
&crate::error::ErrorKind::ObjectNotDiscoverable,
"{meta:?}"
);
let message = format!("{error}: {error:?}");
assert!(message.contains(problem), "{meta:?}: {message}");
}
}
#[test]
fn unrecognized_metadata_is_unavailable() {
let mut meta = ObjectMetadata::default();
meta.checksum_xxhash64 = Some("AAAAAAAAAAA=".into());
assert!(matches!(
StoredChecksum::from_metadata(&meta).unwrap(),
StoredChecksum::Unavailable
));
let mut meta = ObjectMetadata::default();
meta.checksum_crc32 = Some("AAAAAA==".into());
meta.checksum_type = Some(ChecksumType::from("FUTURE_TYPE"));
let stored = StoredChecksum::from_metadata(&meta).unwrap();
assert!(matches!(stored, StoredChecksum::Unavailable));
assert!(matches!(stored.into_plan(7), ValidationPlan::PerResponse));
}
#[test]
fn sha_and_absent_values_remain_response_only() {
let mut meta = ObjectMetadata::default();
assert!(matches!(
StoredChecksum::from_metadata(&meta).unwrap(),
StoredChecksum::Unavailable
));
meta.checksum_sha256 = Some("stored hash".into());
let stored = StoredChecksum::from_metadata(&meta).unwrap();
assert!(matches!(stored, StoredChecksum::Ordered));
assert!(matches!(stored.into_plan(8), ValidationPlan::PerResponse));
}
}