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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright 2019 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::MetaContentsError;
use fuchsia_merkle::Hash;
use fuchsia_url::validate_resource_path;
use std::collections::HashMap;
use std::io;
use std::str::FromStr;

/// A `MetaContents` represents the "meta/contents" file of a Fuchsia archive
/// file of a Fuchsia package.
/// It validates that all resource paths are valid and that none of them start
/// with "meta/".
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct MetaContents {
    contents: HashMap<String, Hash>,
}

impl MetaContents {
    pub const PATH: &'static str = "meta/contents";

    /// Creates a `MetaContents` from a `map` from resource paths to Merkle roots.
    /// Validates that:
    ///   1. all resource paths are valid Fuchsia package resource paths,
    ///   2. none of the resource paths start with "meta/",
    ///   3. none of the resource paths are "meta",
    ///   4. none of the resource paths have directories that collide with other full resource
    ///      paths, e.g. path combination ["foo", "foo/bar"] would be rejected because it has
    ///      both a file and a directory at path "foo".
    ///
    /// # Examples
    /// ```
    /// # use fuchsia_merkle::Hash;
    /// # use fuchsia_pkg::MetaContents;
    /// # use maplit::hashmap;
    /// # use std::str::FromStr;
    /// let map = hashmap! {
    ///     "bin/my_prog".to_string() =>
    ///         Hash::from_str(
    ///             "0000000000000000000000000000000000000000000000000000000000000000")
    ///         .unwrap(),
    ///     "lib/mylib.so".to_string() =>
    ///         Hash::from_str(
    ///             "1111111111111111111111111111111111111111111111111111111111111111")
    ///         .unwrap(),
    /// };
    /// let meta_contents = MetaContents::from_map(map).unwrap();
    pub fn from_map(map: HashMap<String, Hash>) -> Result<Self, MetaContentsError> {
        for resource_path in map.keys() {
            validate_resource_path(resource_path).map_err(|e| {
                MetaContentsError::InvalidResourcePath { cause: e, path: resource_path.to_string() }
            })?;
            if resource_path.starts_with("meta/") || resource_path == "meta" {
                return Err(MetaContentsError::ExternalContentInMetaDirectory {
                    path: resource_path.to_string(),
                });
            }
            for (i, _) in resource_path.match_indices('/') {
                if map.contains_key(&resource_path[..i]) {
                    return Err(MetaContentsError::FileDirectoryCollision {
                        path: resource_path[..i].to_string(),
                    });
                }
            }
        }
        Ok(MetaContents { contents: map })
    }

    /// Serializes a "meta/contents" file to `writer`.
    ///
    /// # Examples
    /// ```
    /// # use fuchsia_merkle::Hash;
    /// # use fuchsia_pkg::MetaContents;
    /// # use maplit::hashmap;
    /// # use std::str::FromStr;
    /// let map = hashmap! {
    ///     "bin/my_prog".to_string() =>
    ///         Hash::from_str(
    ///             "0000000000000000000000000000000000000000000000000000000000000000")
    ///         .unwrap(),
    ///     "lib/mylib.so".to_string() =>
    ///         Hash::from_str(
    ///             "1111111111111111111111111111111111111111111111111111111111111111")
    ///         .unwrap(),
    /// };
    /// let meta_contents = MetaContents::from_map(map).unwrap();
    /// let mut bytes = Vec::new();
    /// meta_contents.serialize(&mut bytes).unwrap();
    /// let expected = "bin/my_prog=0000000000000000000000000000000000000000000000000000000000000000\n\
    ///                 lib/mylib.so=1111111111111111111111111111111111111111111111111111111111111111\n";
    /// assert_eq!(bytes.as_slice(), expected.as_bytes());
    /// ```
    pub fn serialize(&self, writer: &mut impl io::Write) -> io::Result<()> {
        let mut entries = self.contents.iter().collect::<Vec<_>>();
        entries.sort();
        for (path, hash) in entries {
            writeln!(writer, "{path}={hash}")?;
        }
        Ok(())
    }

    /// Deserializes a "meta/contents" file from a `reader`.
    ///
    /// # Examples
    /// ```
    /// # use fuchsia_merkle::Hash;
    /// # use fuchsia_pkg::MetaContents;
    /// # use maplit::hashmap;
    /// # use std::str::FromStr;
    /// let bytes = "bin/my_prog=0000000000000000000000000000000000000000000000000000000000000000\n\
    ///              lib/mylib.so=1111111111111111111111111111111111111111111111111111111111111111\n".as_bytes();
    /// let meta_contents = MetaContents::deserialize(bytes).unwrap();
    /// let expected_contents = hashmap! {
    ///     "bin/my_prog".to_string() =>
    ///         Hash::from_str(
    ///             "0000000000000000000000000000000000000000000000000000000000000000")
    ///         .unwrap(),
    ///     "lib/mylib.so".to_string() =>
    ///         Hash::from_str(
    ///             "1111111111111111111111111111111111111111111111111111111111111111")
    ///         .unwrap(),
    /// };
    /// assert_eq!(meta_contents.contents(), &expected_contents);
    /// ```
    pub fn deserialize(mut reader: impl io::BufRead) -> Result<Self, MetaContentsError> {
        let mut contents = HashMap::new();
        let mut buf = String::new();
        while reader.read_line(&mut buf)? > 0 {
            let line = buf.trim_end();
            let i = line.rfind('=').ok_or_else(|| MetaContentsError::EntryHasNoEqualsSign {
                entry: line.to_string(),
            })?;

            let hash = Hash::from_str(&line[i + 1..])?;
            let path = line[..i].to_string();

            use std::collections::hash_map::Entry;
            match contents.entry(path) {
                Entry::Vacant(entry) => {
                    entry.insert(hash);
                }
                Entry::Occupied(entry) => {
                    return Err(MetaContentsError::DuplicateResourcePath {
                        path: entry.key().clone(),
                    });
                }
            }

            buf.clear();
        }
        contents.shrink_to_fit();
        Self::from_map(contents)
    }

    /// Get the map from blob resource paths to Merkle Tree root hashes.
    pub fn contents(&self) -> &HashMap<String, Hash> {
        &self.contents
    }

    /// Take the map from blob resource paths to Merkle Tree root hashes.
    pub fn into_contents(self) -> HashMap<String, Hash> {
        self.contents
    }

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::*;
    use assert_matches::assert_matches;
    use fuchsia_url::errors::ResourcePathError;
    use fuchsia_url::test::*;
    use maplit::hashmap;
    use proptest::prelude::*;

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

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

    #[test]
    fn deserialize_empty_file() {
        let empty = Vec::new();
        let meta_contents = MetaContents::deserialize(empty.as_slice()).unwrap();
        assert_eq!(meta_contents.contents(), &HashMap::new());
        assert_eq!(meta_contents.into_contents(), HashMap::new());
    }

    #[test]
    fn deserialize_known_file() {
        let bytes =
            "a-host/path=0000000000000000000000000000000000000000000000000000000000000000\n\
             other/host/path=1111111111111111111111111111111111111111111111111111111111111111\n"
                .as_bytes();
        let meta_contents = MetaContents::deserialize(bytes).unwrap();
        let expected_contents = hashmap! {
            "a-host/path".to_string() => zeros_hash(),
            "other/host/path".to_string() => ones_hash(),
        };
        assert_eq!(meta_contents.contents(), &expected_contents);
        assert_eq!(meta_contents.into_contents(), expected_contents);
    }

    #[test]
    fn from_map_rejects_meta_file() {
        let map = hashmap! {
            "meta".to_string() => zeros_hash(),
        };
        assert_matches!(
            MetaContents::from_map(map),
            Err(MetaContentsError::ExternalContentInMetaDirectory { path }) if path == "meta"
        );
    }

    #[test]
    fn from_map_rejects_file_dir_collisions() {
        for (map, expected_path) in [
            (
                hashmap! {
                    "foo".to_string() => zeros_hash(),
                    "foo/bar".to_string() => zeros_hash(),
                },
                "foo",
            ),
            (
                hashmap! {
                    "foo/bar".to_string() => zeros_hash(),
                    "foo/bar/baz".to_string() => zeros_hash(),
                },
                "foo/bar",
            ),
            (
                hashmap! {
                    "foo".to_string() => zeros_hash(),
                    "foo/bar/baz".to_string() => zeros_hash(),
                },
                "foo",
            ),
        ] {
            assert_matches!(
                MetaContents::from_map(map),
                Err(MetaContentsError::FileDirectoryCollision { path }) if path == expected_path
            );
        }
    }

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

        #[test]
        fn from_map_rejects_invalid_resource_path(
            ref path in random_resource_path(1, 3),
            ref hex in random_merkle_hex())
        {
            prop_assume!(!path.starts_with("meta/"));
            let invalid_path = format!("{path}/");
            let map = hashmap! {
                invalid_path.clone() =>
                    Hash::from_str(hex.as_str()).unwrap(),
            };
            assert_matches!(
                MetaContents::from_map(map),
                Err(MetaContentsError::InvalidResourcePath {
                    cause: ResourcePathError::PathEndsWithSlash,
                    path }) if path == invalid_path
            );
        }

        #[test]
        fn from_map_rejects_file_in_meta(
            ref path in random_resource_path(1, 3),
            ref hex in random_merkle_hex())
        {
            let invalid_path = format!("meta/{path}");
            let map = hashmap! {
                invalid_path.clone() =>
                    Hash::from_str(hex.as_str()).unwrap(),
            };
            assert_matches!(
                MetaContents::from_map(map),
                Err(MetaContentsError::ExternalContentInMetaDirectory { path }) if path == invalid_path
            );
        }

        #[test]
        fn serialize(
            ref path0 in random_external_resource_path(),
            ref hex0 in random_merkle_hex(),
            ref path1 in random_external_resource_path(),
            ref hex1 in random_merkle_hex())
        {
            prop_assume!(path0 != path1);
            let map = hashmap! {
                path0.clone() =>
                    Hash::from_str(hex0.as_str()).unwrap(),
                path1.clone() =>
                    Hash::from_str(hex1.as_str()).unwrap(),
            };
            let meta_contents = MetaContents::from_map(map);
            prop_assume!(meta_contents.is_ok());
            let meta_contents = meta_contents.unwrap();
            let mut bytes = Vec::new();

            meta_contents.serialize(&mut bytes).unwrap();

            let ((first_path, first_hex), (second_path, second_hex)) = if path0 <= path1 {
                ((path0, hex0), (path1, hex1))
            } else {
                ((path1, hex1), (path0, hex0))
            };
            let expected = format!(
                "{}={}\n{}={}\n",
                first_path,
                first_hex.to_ascii_lowercase(),
                second_path,
                second_hex.to_ascii_lowercase());
            prop_assert_eq!(bytes.as_slice(), expected.as_bytes());
        }

        #[test]
        fn serialize_deserialize_is_id(
            contents in prop::collection::hash_map(
                random_external_resource_path(), random_hash(), 0..4)
        ) {
            let meta_contents = MetaContents::from_map(contents);
            prop_assume!(meta_contents.is_ok());
            let meta_contents = meta_contents.unwrap();
            let mut serialized = Vec::new();
            meta_contents.serialize(&mut serialized).unwrap();
            let deserialized = MetaContents::deserialize(serialized.as_slice()).unwrap();
            prop_assert_eq!(meta_contents, deserialized);
        }
    }
}