update_package/
update_mode.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Typesafe wrappers around parsing the update-mode file.
6
7use fidl_fuchsia_io as fio;
8use serde::{Deserialize, Serialize};
9use std::str::FromStr;
10use thiserror::Error;
11use zx_status::Status;
12
13/// An error encountered while parsing the update-mode file.
14#[derive(Debug, Error)]
15#[allow(missing_docs)]
16pub enum ParseUpdateModeError {
17    #[error("while opening the file")]
18    OpenFile(#[source] fuchsia_fs::node::OpenError),
19
20    #[error("while reading the file")]
21    ReadFile(#[source] fuchsia_fs::file::ReadError),
22
23    #[error("while deserializing: '{0:?}'")]
24    Deserialize(String, #[source] serde_json::Error),
25
26    #[error("update mode not supported: '{0}'")]
27    UpdateModeNotSupported(String),
28}
29
30/// Wrapper for deserializing the update-mode file.
31#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
32#[serde(tag = "version", content = "content", deny_unknown_fields)]
33enum UpdateModeFile {
34    #[serde(rename = "1")]
35    Version1 {
36        // We make this a String and not an UpdateMode so that we can seperate
37        // the unsupported mode errors from other json deserialization errors.
38        // For example,this would be considered valid json with an unsupported mode:
39        // { version: "1", content: { "mode" : "banana" } }
40        #[serde(rename = "mode")]
41        update_mode: String,
42    },
43}
44
45/// Enum to describe the supported update modes.
46#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
47#[serde(rename_all = "kebab-case")]
48pub enum UpdateMode {
49    /// Follow the normal system update flow.
50    #[default]
51    Normal,
52    /// Instead of the normal flow, write a recovery image and reboot into it.
53    ForceRecovery,
54}
55
56impl FromStr for UpdateMode {
57    type Err = ParseUpdateModeError;
58
59    fn from_str(s: &str) -> Result<Self, Self::Err> {
60        match s {
61            "normal" => Ok(UpdateMode::Normal),
62            "force-recovery" => Ok(UpdateMode::ForceRecovery),
63            other => Err(ParseUpdateModeError::UpdateModeNotSupported(other.to_string())),
64        }
65    }
66}
67
68pub(crate) async fn update_mode(
69    proxy: &fio::DirectoryProxy,
70) -> Result<Option<UpdateMode>, ParseUpdateModeError> {
71    // Open the update-mode file.
72    let fopen_res =
73        fuchsia_fs::directory::open_file(proxy, "update-mode", fio::PERM_READABLE).await;
74    if let Err(fuchsia_fs::node::OpenError::OpenError(Status::NOT_FOUND)) = fopen_res {
75        return Ok(None);
76    }
77
78    // Read the update-mode file.
79    let contents =
80        fuchsia_fs::file::read_to_string(&fopen_res.map_err(ParseUpdateModeError::OpenFile)?)
81            .await
82            .map_err(ParseUpdateModeError::ReadFile)?;
83
84    // Parse the json string to extract UpdateMode.
85    match serde_json::from_str(&contents)
86        .map_err(|e| ParseUpdateModeError::Deserialize(contents, e))?
87    {
88        UpdateModeFile::Version1 { update_mode } => update_mode.parse().map(Some),
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95    use crate::TestUpdatePackage;
96    use assert_matches::assert_matches;
97    use fuchsia_async as fasync;
98    use proptest::prelude::*;
99    use serde_json::json;
100
101    fn valid_update_mode_json_value(mode: &str) -> serde_json::Value {
102        json!({
103            "version": "1",
104            "content": {
105                "mode": mode,
106            },
107        })
108    }
109
110    fn valid_update_mode_json_string(mode: &str) -> String {
111        valid_update_mode_json_value(mode).to_string()
112    }
113
114    proptest! {
115        #[test]
116        fn test_json_serialize_roundtrip(s in ".+") {
117            // Generate json and show that it successfully deserializes into the wrapper object.
118            let starting_json_value = valid_update_mode_json_value(&s);
119            let deserialized_object: UpdateModeFile =
120                serde_json::from_value(starting_json_value.clone())
121                    .expect("json to deserialize");
122            assert_eq!(deserialized_object, UpdateModeFile::Version1 { update_mode: s });
123
124            // Serialize back into serde_json::Value object & show we get same json we started with.
125            // Note: even though serialize generally means "convert to string", in this case we're
126            // serializing to a serde_json::Value to ignore ordering when we check equality.
127            let final_json_value =
128                serde_json::to_value(&deserialized_object)
129                    .expect("serialize to value");
130            assert_eq!(final_json_value, starting_json_value);
131        }
132    }
133
134    #[fasync::run_singlethreaded(test)]
135    async fn parse_update_mode_success_normal() {
136        let p = TestUpdatePackage::new()
137            .add_file("update-mode", &valid_update_mode_json_string("normal"))
138            .await;
139        assert_matches!(p.update_mode().await, Ok(Some(UpdateMode::Normal)));
140    }
141
142    #[fasync::run_singlethreaded(test)]
143    async fn parse_update_mode_success_force_recovery() {
144        let p = TestUpdatePackage::new()
145            .add_file("update-mode", &valid_update_mode_json_string("force-recovery"))
146            .await;
147        assert_matches!(p.update_mode().await, Ok(Some(UpdateMode::ForceRecovery)));
148    }
149
150    #[fasync::run_singlethreaded(test)]
151    async fn parse_update_mode_success_missing_update_mode_file() {
152        let p = TestUpdatePackage::new();
153        assert_matches!(p.update_mode().await, Ok(None));
154    }
155
156    #[fasync::run_singlethreaded(test)]
157    async fn parse_update_mode_fail_unsupported_mode() {
158        let p = TestUpdatePackage::new()
159            .add_file("update-mode", &valid_update_mode_json_string("potato"))
160            .await;
161        assert_matches!(
162            p.update_mode().await,
163            Err(ParseUpdateModeError::UpdateModeNotSupported(mode)) if mode=="potato"
164        );
165    }
166
167    #[fasync::run_singlethreaded(test)]
168    async fn parse_update_mode_fail_deserialize() {
169        let p = TestUpdatePackage::new().add_file("update-mode", "oh no! this isn't json.").await;
170        assert_matches!(
171            p.update_mode().await,
172            Err(ParseUpdateModeError::Deserialize(s,_)) if s == "oh no! this isn't json."
173        );
174    }
175}