1#![allow(clippy::let_unit_value)]
6
7mod anchored_packages;
8mod cache_packages;
9mod errors;
10mod path_hash_mapping;
11mod system_image;
12
13pub use crate::anchored_packages::AnchoredPackages;
14pub use crate::cache_packages::CachePackages;
15pub use crate::errors::{
16 AllowListError, CachePackagesInitError, PathHashMappingError, StaticPackagesInitError,
17};
18pub use crate::path_hash_mapping::{Bootfs, PathHashMapping, StaticPackages};
19pub use crate::system_image::{ExecutabilityRestrictions, SystemImage};
20
21static PKGFS_BOOT_ARG_VALUE_PREFIX: &str = "bin/pkgsvr+";
22
23pub fn get_system_image_hash(
24 system_image_hash: &str,
25) -> Result<fuchsia_hash::Hash, SystemImageHashError> {
26 let hash =
29 system_image_hash.strip_prefix(PKGFS_BOOT_ARG_VALUE_PREFIX).unwrap_or(system_image_hash);
30 hash.parse().map_err(SystemImageHashError::BadHash)
31}
32
33#[derive(Debug, thiserror::Error)]
34pub enum SystemImageHashError {
35 #[error("invalid system image hash: {:?}", .0)]
36 BadHash(#[source] fuchsia_hash::ParseHashError),
37}
38
39#[cfg(test)]
40mod test_get_system_image_hash {
41 use super::*;
42 use assert_matches::assert_matches;
43
44 #[test]
45 fn bad_prefix() {
46 assert_matches!(get_system_image_hash("bad-prefix"), Err(SystemImageHashError::BadHash(_)));
47 }
48
49 #[test]
50 fn bad_hash() {
51 assert_matches!(
52 get_system_image_hash("bin/pkgsvr+bad-hash"),
53 Err(SystemImageHashError::BadHash(_))
54 );
55 }
56
57 #[test]
58 fn success() {
59 assert_eq!(
60 get_system_image_hash(
61 "bin/pkgsvr+0000000000000000000000000000000000000000000000000000000000000000"
62 )
63 .unwrap(),
64 fuchsia_hash::Hash::from([0; 32])
65 );
66 }
67
68 #[test]
69 fn success_no_prefix() {
70 assert_eq!(
71 get_system_image_hash(
72 "0000000000000000000000000000000000000000000000000000000000000000"
73 )
74 .unwrap(),
75 fuchsia_hash::Hash::from([0; 32])
76 );
77 }
78}