Skip to main content

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 proptest::prelude::*;
98    use serde_json::json;
99
100    fn valid_update_mode_json_value(mode: &str) -> serde_json::Value {
101        json!({
102            "version": "1",
103            "content": {
104                "mode": mode,
105            },
106        })
107    }
108
109    fn valid_update_mode_json_string(mode: &str) -> String {
110        valid_update_mode_json_value(mode).to_string()
111    }
112
113    proptest! {
114        #[test]
115        fn test_json_serialize_roundtrip(s in ".+") {
116            // Generate json and show that it successfully deserializes into the wrapper object.
117            let starting_json_value = valid_update_mode_json_value(&s);
118            let deserialized_object: UpdateModeFile =
119                serde_json::from_value(starting_json_value.clone())
120                    .expect("json to deserialize");
121            assert_eq!(deserialized_object, UpdateModeFile::Version1 { update_mode: s });
122
123            // Serialize back into serde_json::Value object & show we get same json we started with.
124            // Note: even though serialize generally means "convert to string", in this case we're
125            // serializing to a serde_json::Value to ignore ordering when we check equality.
126            let final_json_value =
127                serde_json::to_value(&deserialized_object)
128                    .expect("serialize to value");
129            assert_eq!(final_json_value, starting_json_value);
130        }
131    }
132
133    #[fuchsia::test]
134    async fn parse_update_mode_success_normal() {
135        let p = TestUpdatePackage::new()
136            .add_file("update-mode", &valid_update_mode_json_string("normal"))
137            .await;
138        assert_matches!(p.update_mode().await, Ok(Some(UpdateMode::Normal)));
139    }
140
141    #[fuchsia::test]
142    async fn parse_update_mode_success_force_recovery() {
143        let p = TestUpdatePackage::new()
144            .add_file("update-mode", &valid_update_mode_json_string("force-recovery"))
145            .await;
146        assert_matches!(p.update_mode().await, Ok(Some(UpdateMode::ForceRecovery)));
147    }
148
149    #[fuchsia::test]
150    async fn parse_update_mode_success_missing_update_mode_file() {
151        let p = TestUpdatePackage::new();
152        assert_matches!(p.update_mode().await, Ok(None));
153    }
154
155    #[fuchsia::test]
156    async fn parse_update_mode_fail_unsupported_mode() {
157        let p = TestUpdatePackage::new()
158            .add_file("update-mode", &valid_update_mode_json_string("potato"))
159            .await;
160        assert_matches!(
161            p.update_mode().await,
162            Err(ParseUpdateModeError::UpdateModeNotSupported(mode)) if mode=="potato"
163        );
164    }
165
166    #[fuchsia::test]
167    async fn parse_update_mode_fail_deserialize() {
168        let p = TestUpdatePackage::new().add_file("update-mode", "oh no! this isn't json.").await;
169        assert_matches!(
170            p.update_mode().await,
171            Err(ParseUpdateModeError::Deserialize(s,_)) if s == "oh no! this isn't json."
172        );
173    }
174}