Skip to content

Commit e784d57

Browse files
src/fsverity: add tests for trait code
This tests out all of the various conversions we can do. Signed-off-by: Allison Karlitskaya <[email protected]>
1 parent fb1de9f commit e784d57

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/fsverity/hashvalue.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,82 @@ impl FsVerityHashValue for Sha512HashValue {
125125
const EMPTY: Self = Self([0; 64]);
126126
const ID: &str = "sha512";
127127
}
128+
129+
#[cfg(test)]
130+
mod test {
131+
use super::*;
132+
133+
fn test_fsverity_hash<H: FsVerityHashValue>() {
134+
let len = size_of::<H>();
135+
let hexlen = len * 2;
136+
137+
let hex = H::EMPTY.to_hex();
138+
assert_eq!(hex.as_bytes(), [b'0'].repeat(hexlen));
139+
140+
assert_eq!(H::EMPTY.to_id(), format!("{}:{}", H::ID, hex));
141+
assert_eq!(format!("{:?}", H::EMPTY), format!("{}:{}", H::ID, hex));
142+
143+
assert_eq!(H::from_hex(&hex), Ok(H::EMPTY));
144+
145+
assert_eq!(H::from_hex("lol"), Err(FromHexError::OddLength));
146+
assert_eq!(H::from_hex("lolo"), Err(FromHexError::InvalidStringLength));
147+
assert_eq!(
148+
H::from_hex([b'l'].repeat(hexlen)),
149+
Err(FromHexError::InvalidHexCharacter { c: 'l', index: 0 })
150+
);
151+
152+
assert_eq!(H::from_object_dir_and_basename(0, &hex[2..]), Ok(H::EMPTY));
153+
154+
assert_eq!(H::from_object_dir_and_basename(0, &hex[2..]), Ok(H::EMPTY));
155+
156+
assert_eq!(
157+
H::from_object_dir_and_basename(0, "lol"),
158+
Err(FromHexError::InvalidStringLength)
159+
);
160+
161+
assert_eq!(
162+
H::from_object_dir_and_basename(0, [b'l'].repeat(hexlen - 2)),
163+
Err(FromHexError::InvalidHexCharacter { c: 'l', index: 0 })
164+
);
165+
166+
assert_eq!(
167+
H::from_object_pathname(format!("{}/{}", &hex[0..2], &hex[2..])),
168+
Ok(H::EMPTY)
169+
);
170+
171+
assert_eq!(
172+
H::from_object_pathname(format!("../this/is/ignored/{}/{}", &hex[0..2], &hex[2..])),
173+
Ok(H::EMPTY)
174+
);
175+
176+
assert_eq!(
177+
H::from_object_pathname(&hex),
178+
Err(FromHexError::InvalidStringLength)
179+
);
180+
181+
assert_eq!(
182+
H::from_object_pathname("lol"),
183+
Err(FromHexError::InvalidStringLength)
184+
);
185+
186+
assert_eq!(
187+
H::from_object_pathname([b'l'].repeat(hexlen + 1)),
188+
Err(FromHexError::InvalidHexCharacter { c: 'l', index: 0 })
189+
);
190+
191+
assert_eq!(
192+
H::from_object_pathname(format!("{}0{}", &hex[0..2], &hex[2..])),
193+
Err(FromHexError::InvalidHexCharacter { c: '0', index: 2 })
194+
);
195+
}
196+
197+
#[test]
198+
fn test_sha256hashvalue() {
199+
test_fsverity_hash::<Sha256HashValue>();
200+
}
201+
202+
#[test]
203+
fn test_sha512hashvalue() {
204+
test_fsverity_hash::<Sha512HashValue>();
205+
}
206+
}

0 commit comments

Comments
 (0)