update_package/
packages.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
// Copyright 2020 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 fidl_fuchsia_io as fio;
use fuchsia_url::PinnedAbsolutePackageUrl;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "version", content = "content", deny_unknown_fields)]
enum Packages {
    #[serde(rename = "1")]
    V1(Vec<PinnedAbsolutePackageUrl>),
}

/// ParsePackageError represents any error which might occur while reading
/// `packages.json` from an update package.
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum ParsePackageError {
    #[error("could not open `packages.json`")]
    FailedToOpen(#[source] fuchsia_fs::node::OpenError),

    #[error("could not parse url from line: {0:?}")]
    URLParseError(String, #[source] fuchsia_url::errors::ParseError),

    #[error("error reading file `packages.json`")]
    ReadError(#[source] fuchsia_fs::file::ReadError),

    #[error("json parsing error while reading `packages.json`")]
    JsonError(#[source] serde_json::error::Error),
}

/// SerializePackageError represents any error which might occur while writing
/// `packages.json` for an update package.
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum SerializePackageError {
    #[error("serialization error while constructing `packages.json`")]
    JsonError(#[source] serde_json::error::Error),
}

/// Returns structured `packages.json` data based on file contents string.
pub fn parse_packages_json(
    contents: &[u8],
) -> Result<Vec<PinnedAbsolutePackageUrl>, ParsePackageError> {
    match serde_json::from_slice(contents).map_err(ParsePackageError::JsonError)? {
        Packages::V1(packages) => Ok(packages),
    }
}

/// Returns serialized `packages.json` contents based package URLs.
pub fn serialize_packages_json(
    pkg_urls: &[PinnedAbsolutePackageUrl],
) -> Result<Vec<u8>, SerializePackageError> {
    serde_json::to_vec(&Packages::V1(pkg_urls.into())).map_err(SerializePackageError::JsonError)
}

/// Returns the list of package urls that go in the universe of this update package.
pub(crate) async fn packages(
    proxy: &fio::DirectoryProxy,
) -> Result<Vec<PinnedAbsolutePackageUrl>, ParsePackageError> {
    let file = fuchsia_fs::directory::open_file(proxy, "packages.json", fio::PERM_READABLE)
        .await
        .map_err(ParsePackageError::FailedToOpen)?;

    let contents = fuchsia_fs::file::read(&file).await.map_err(ParsePackageError::ReadError)?;
    parse_packages_json(&contents)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::TestUpdatePackage;
    use assert_matches::assert_matches;
    use serde_json::json;

    fn pkg_urls<'a>(v: impl IntoIterator<Item = &'a str>) -> Vec<PinnedAbsolutePackageUrl> {
        v.into_iter().map(|s| s.parse().unwrap()).collect()
    }

    #[test]
    fn smoke_test_parse_packages_json() {
        let pkg_urls = pkg_urls([
            "fuchsia-pkg://fuchsia.com/ls/0?hash=71bad1a35b87a073f72f582065f6b6efec7b6a4a129868f37f6131f02107f1ea",
            "fuchsia-pkg://fuchsia.com/pkg-resolver/0?hash=26d43a3fc32eaa65e6981791874b6ab80fae31fbfca1ce8c31ab64275fd4e8c0",
        ]);
        let packages = Packages::V1(pkg_urls.clone());
        let packages_json = serde_json::to_vec(&packages).unwrap();
        assert_eq!(parse_packages_json(&packages_json).unwrap(), pkg_urls);
    }

    #[test]
    fn smoke_test_serialize_packages_json() {
        let input = pkg_urls([
            "fuchsia-pkg://fuchsia.com/ls/0?hash=71bad1a35b87a073f72f582065f6b6efec7b6a4a129868f37f6131f02107f1ea",
            "fuchsia-pkg://fuchsia.com/pkg-resolver/0?hash=26d43a3fc32eaa65e6981791874b6ab80fae31fbfca1ce8c31ab64275fd4e8c0",
        ]);
        let output =
            parse_packages_json(serialize_packages_json(input.as_slice()).unwrap().as_slice())
                .unwrap();
        assert_eq!(input, output);
    }

    #[test]
    fn expect_failure_parse_packages_json() {
        assert_matches!(parse_packages_json(&[]), Err(ParsePackageError::JsonError(_)));
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn smoke_test_packages_json_version_string() {
        let pkg_list = [
            "fuchsia-pkg://fuchsia.com/ls/0?hash=71bad1a35b87a073f72f582065f6b6efec7b6a4a129868f37f6131f02107f1ea",
            "fuchsia-pkg://fuchsia.com/pkg-resolver/0?hash=26d43a3fc32eaa65e6981791874b6ab80fae31fbfca1ce8c31ab64275fd4e8c0",
        ];
        let packages = json!({
            "version": "1",
            "content": pkg_list,
        })
        .to_string();
        let update_pkg = TestUpdatePackage::new().add_file("packages.json", packages).await;
        assert_eq!(update_pkg.packages().await.unwrap(), pkg_urls(pkg_list));
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn expect_failure_json() {
        let update_pkg = TestUpdatePackage::new();
        let packages = "{}";
        let update_pkg = update_pkg.add_file("packages.json", packages).await;
        assert_matches!(update_pkg.packages().await, Err(ParsePackageError::JsonError(_)))
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn expect_failure_version_not_supported() {
        let pkg_list = vec![
            "fuchsia-pkg://fuchsia.com/ls/0?hash=71bad1a35b87a073f72f582065f6b6efec7b6a4a129868f37f6131f02107f1ea",
        ];
        let packages = json!({
            "version": "2",
            "content": pkg_list,
        })
        .to_string();
        let update_pkg = TestUpdatePackage::new().add_file("packages.json", packages).await;
        assert_matches!(
            update_pkg.packages().await,
            Err(ParsePackageError::JsonError(e))
                if e.to_string().contains("unknown variant `2`, expected `1`")
        );
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn expect_failure_no_files() {
        let update_pkg = TestUpdatePackage::new();
        assert_matches!(update_pkg.packages().await, Err(ParsePackageError::FailedToOpen(_)))
    }

    #[test]
    fn reject_unpinned_urls() {
        assert_matches!(
            parse_packages_json(serde_json::json!({
                "version": "1",
                "content": ["fuchsia-pkg://fuchsia.example/unpinned"]
            })
            .to_string().as_bytes()),
            Err(ParsePackageError::JsonError(e)) if e.to_string().contains("missing hash")
        )
    }
}