Skip to content

Commit

Permalink
Merge pull request #206 from cgwalters/minor-digest-bits
Browse files Browse the repository at this point in the history
digest: Minor self-review fixups
  • Loading branch information
cgwalters authored Sep 3, 2024
2 parents d3ddc0b + 4a61b34 commit 4301862
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/image/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ fn char_is_lowercase_ascii_hex(c: char) -> bool {

/// algorithm-component ::= [a-z0-9]+
fn char_is_algorithm_component(c: char) -> bool {
matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9')
matches!(c, 'a'..='z' | '0'..='9')
}

/// encoded ::= [a-zA-Z0-9=_-]+
fn char_is_encoded(c: char) -> bool {
char_is_algorithm_component(c) || matches!(c, '=' | '_' | '-')
char_is_algorithm_component(c) || matches!(c, 'A'..='Z' | '=' | '_' | '-')
}

/// A parsed pair of algorithm:digest as defined
Expand Down Expand Up @@ -151,8 +151,8 @@ pub struct Sha256Digest {
impl From<Sha256Digest> for Digest {
fn from(value: Sha256Digest) -> Self {
Self {
buf: format!("sha256:{}", value.value).into_boxed_str(),
split: 6,
buf: format!("{ALG_SHA256}:{}", value.value).into_boxed_str(),
split: ALG_SHA256.len(),
}
}
}
Expand Down Expand Up @@ -211,6 +211,7 @@ mod tests {
"_digest:somevalue",
":blah",
"blah:",
"FooBar:123abc",
"^:foo",
"bar^baz:blah",
"sha256:123456*78",
Expand Down Expand Up @@ -244,6 +245,7 @@ mod tests {
let invalid = [
"sha256:123456=78",
"foo:bar",
"sha256:6c3c624b58dbbcd3c0dd82b4z53f04194d1247c6eebdaab7c610cf7d66709b3b", // has a z in the middle
"sha256+blah:6c3c624b58dbbcd3c0dd82b4c53f04194d1247c6eebdaab7c610cf7d66709b3b",
];
for case in invalid {
Expand All @@ -257,8 +259,13 @@ mod tests {

#[test]
fn test_sha256_valid() {
let expected_value = VALID_DIGEST_SHA256.split_once(':').unwrap().1;
let d = Digest::from_str(VALID_DIGEST_SHA256).unwrap();
let d = d.to_sha256().unwrap();
assert_eq!(d.value(), VALID_DIGEST_SHA256.split_once(':').unwrap().1);
assert_eq!(d.value(), expected_value);
let base_digest = Digest::from(d.clone());
assert_eq!(base_digest.value(), expected_value);
let d2 = base_digest.to_sha256().unwrap();
assert_eq!(d, d2);
}
}

0 comments on commit 4301862

Please sign in to comment.