omaha_client/installer/
stub.rs

1// Copyright 2019 The Fuchsia Authors
2//
3// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to
7// those terms.
8
9use super::*;
10use crate::{
11    cup_ecdsa::RequestMetadata, protocol::response::Response, request_builder::RequestParams,
12};
13use futures::future::LocalBoxFuture;
14use futures::prelude::*;
15use thiserror::Error;
16
17/// This is the collection of Errors that can occur during the installation of an update.
18///
19/// This is a placeholder stub implementation.
20///
21#[derive(Debug, Error, Eq, PartialEq)]
22pub enum StubInstallErrors {
23    #[error("Stub Installer Failure")]
24    Failed,
25}
26
27/// A stub implementation of the Install Plan.
28///
29pub struct StubPlan;
30
31impl Plan for StubPlan {
32    fn id(&self) -> String {
33        String::new()
34    }
35}
36
37/// The Installer is responsible for performing (or triggering) the installation of the update
38/// that's referred to by the InstallPlan.
39///
40#[derive(Debug, Default)]
41pub struct StubInstaller {
42    pub should_fail: bool,
43}
44
45impl Installer for StubInstaller {
46    type InstallPlan = StubPlan;
47    type Error = StubInstallErrors;
48    type InstallResult = ();
49
50    /// Perform the installation as given by the install plan (as parsed form the Omaha server
51    /// response).  If given, provide progress via the observer, and a final finished or Error
52    /// indication via the Future.
53    fn perform_install(
54        &mut self,
55        _install_plan: &StubPlan,
56        _observer: Option<&dyn ProgressObserver>,
57    ) -> LocalBoxFuture<'_, (Self::InstallResult, Vec<AppInstallResult<Self::Error>>)> {
58        if self.should_fail {
59            future::ready((
60                (),
61                vec![AppInstallResult::Failed(StubInstallErrors::Failed)],
62            ))
63            .boxed_local()
64        } else {
65            future::ready(((), vec![AppInstallResult::Installed])).boxed_local()
66        }
67    }
68
69    fn perform_reboot(&mut self) -> LocalBoxFuture<'_, Result<(), anyhow::Error>> {
70        future::ready(Ok(())).boxed_local()
71    }
72
73    fn try_create_install_plan<'a>(
74        &'a self,
75        _request_params: &'a RequestParams,
76        _request_metadata: Option<&'a RequestMetadata>,
77        response: &'a Response,
78        _response_bytes: Vec<u8>,
79        _ecdsa_signature: Option<Vec<u8>>,
80    ) -> LocalBoxFuture<'a, Result<Self::InstallPlan, Self::Error>> {
81        if response.protocol_version != "3.0" {
82            future::ready(Err(StubInstallErrors::Failed)).boxed_local()
83        } else {
84            future::ready(Ok(StubPlan)).boxed_local()
85        }
86    }
87}