fuchsia_pkg/
meta_subpackages.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::errors::MetaSubpackagesError;
use fuchsia_merkle::Hash;
use fuchsia_url::RelativePackageUrl;
use serde::ser::Serializer;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
use std::io;

/// A `MetaSubpackages` represents the "meta/fuchsia.pkg/subpackages" file of a Fuchsia
/// archive file of a Fuchsia package. It validates that all subpackage names
/// are valid.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct MetaSubpackages(VersionedMetaSubpackages);

impl MetaSubpackages {
    pub const PATH: &'static str = "meta/fuchsia.pkg/subpackages";

    fn from_v1(meta_subpackages_v1: MetaSubpackagesV1) -> Self {
        Self(VersionedMetaSubpackages::Version1(meta_subpackages_v1))
    }

    /// Get the map from subpackage names to Merkle Tree root hashes.
    pub fn subpackages(&self) -> &HashMap<RelativePackageUrl, Hash> {
        match &self.0 {
            VersionedMetaSubpackages::Version1(meta) => &meta.subpackages,
        }
    }

    /// Take the map from subpackage names to Merkle Tree root hashes.
    pub fn into_subpackages(self) -> HashMap<RelativePackageUrl, Hash> {
        match self.0 {
            VersionedMetaSubpackages::Version1(meta) => meta.subpackages,
        }
    }

    /// Take the Merkle Tree root hashes in an iterator. The returned iterator may include
    /// duplicates.
    pub fn into_hashes_undeduplicated(self) -> impl Iterator<Item = Hash> {
        self.into_subpackages().into_values()
    }

    pub fn deserialize(reader: impl io::BufRead) -> Result<Self, MetaSubpackagesError> {
        Ok(MetaSubpackages::from_v1(serde_json::from_reader(reader)?))
    }

    pub fn serialize(&self, writer: impl io::Write) -> Result<(), MetaSubpackagesError> {
        Ok(serde_json::to_writer(writer, &self)?)
    }
}

impl FromIterator<(RelativePackageUrl, Hash)> for MetaSubpackages {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = (RelativePackageUrl, Hash)>,
    {
        MetaSubpackages(VersionedMetaSubpackages::Version1(MetaSubpackagesV1 {
            subpackages: HashMap::from_iter(iter),
        }))
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(tag = "version", deny_unknown_fields)]
enum VersionedMetaSubpackages {
    #[serde(rename = "1")]
    Version1(MetaSubpackagesV1),
}

impl Default for VersionedMetaSubpackages {
    fn default() -> Self {
        VersionedMetaSubpackages::Version1(MetaSubpackagesV1::default())
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
struct MetaSubpackagesV1 {
    subpackages: HashMap<RelativePackageUrl, Hash>,
}

impl Serialize for MetaSubpackagesV1 {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let MetaSubpackagesV1 { subpackages } = self;

        // Sort the subpackages list to make sure it's in a consistent order.

        #[derive(Serialize)]
        struct Helper<'a> {
            subpackages: BTreeMap<&'a RelativePackageUrl, &'a Hash>,
        }

        Helper { subpackages: subpackages.iter().collect() }.serialize(serializer)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::*;
    use fuchsia_url::test::random_relative_package_url;
    use maplit::hashmap;
    use proptest::prelude::*;
    use serde_json::json;

    fn zeros_hash() -> Hash {
        "0000000000000000000000000000000000000000000000000000000000000000".parse().unwrap()
    }

    fn ones_hash() -> Hash {
        "1111111111111111111111111111111111111111111111111111111111111111".parse().unwrap()
    }

    #[test]
    fn deserialize_known_file() {
        let bytes = r#"{
                "version": "1",
                "subpackages": {
                    "a_0_subpackage": "0000000000000000000000000000000000000000000000000000000000000000",
                    "other-1-subpackage": "1111111111111111111111111111111111111111111111111111111111111111"
                }
            }"#.as_bytes();
        let meta_subpackages = MetaSubpackages::deserialize(bytes).unwrap();
        let expected_subpackages = hashmap! {
            RelativePackageUrl::parse("a_0_subpackage").unwrap() => zeros_hash(),
            RelativePackageUrl::parse("other-1-subpackage").unwrap() => ones_hash(),
        };
        assert_eq!(meta_subpackages.subpackages(), &expected_subpackages);
        assert_eq!(meta_subpackages.into_subpackages(), expected_subpackages);
    }

    proptest! {
        #![proptest_config(ProptestConfig{
            failure_persistence: None,
            ..Default::default()
        })]

        #[test]
        fn serialize(
            ref path0 in random_relative_package_url(),
            ref hex0 in random_hash(),
            ref path1 in random_relative_package_url(),
            ref hex1 in random_hash())
        {
            prop_assume!(path0 != path1);
            let map = hashmap! {
                path0.clone() => *hex0,
                path1.clone() => *hex1,
            };
            let meta_subpackages = MetaSubpackages::from_iter(map);

            prop_assert_eq!(
                serde_json::to_value(meta_subpackages).unwrap(),
                json!(
                    {
                        "version": "1",
                        "subpackages": {
                            path0: hex0,
                            path1: hex1,
                        }
                    }
                )
            );
        }

        #[test]
        fn serialize_deserialize_is_id(
            subpackages in prop::collection::hash_map(
                random_relative_package_url(), random_hash(), 0..4)
        ) {
            let meta_subpackages = MetaSubpackages::from_iter(subpackages);
            let deserialized = MetaSubpackages::deserialize(
                &*serde_json::to_vec(&meta_subpackages).unwrap()
            )
            .unwrap();
            prop_assert_eq!(meta_subpackages, deserialized);
        }
    }
}